Skip to content

Commit 0fe517f

Browse files
committed
enable user defined base64 decoding function
1 parent 8c09bd5 commit 0fe517f

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

include/jwt-cpp/jwt.h

+14-6
Original file line numberDiff line numberDiff line change
@@ -858,15 +858,15 @@ namespace jwt {
858858
public:
859859
static key symmetric(const std::string& bytes) { return key(bytes); }
860860

861-
static key asymmetric(std::shared_ptr<EVP_PKEY> pkey) { return key(pkey); }
861+
static key asymmetric(helper::evp_pkey_handle pkey) { return key(pkey); }
862862

863863
std::string get_symmetric_key() const {
864864
if (!is_symmetric) { throw std::logic_error("not a symmetric key"); }
865865

866866
return oct_key;
867867
}
868868

869-
std::shared_ptr<EVP_PKEY> get_asymmetric_key() const {
869+
helper::evp_pkey_handle get_asymmetric_key() const {
870870
if (is_symmetric) { throw std::logic_error("not an asymmetric key"); }
871871

872872
return pkey;
@@ -878,13 +878,13 @@ namespace jwt {
878878
oct_key = key;
879879
}
880880

881-
key(std::shared_ptr<EVP_PKEY> key) {
881+
key(helper::evp_pkey_handle key) {
882882
is_symmetric = false;
883883
pkey = key;
884884
}
885885

886886
bool is_symmetric;
887-
std::shared_ptr<EVP_PKEY> pkey;
887+
helper::evp_pkey_handle pkey;
888888
std::string oct_key;
889889
};
890890

@@ -3896,7 +3896,8 @@ namespace jwt {
38963896
using iterator = typename jwt_vector_t::iterator;
38973897
using const_iterator = typename jwt_vector_t::const_iterator;
38983898

3899-
JWT_CLAIM_EXPLICIT jwks(const typename json_traits::string_type& str) {
3899+
template<typename Decode>
3900+
jwks(const typename json_traits::string_type& str, Decode decode) {
39003901
typename json_traits::value_type parsed_val;
39013902
if (!json_traits::parse(parsed_val, str)) throw error::invalid_json_exception();
39023903

@@ -3905,9 +3906,16 @@ namespace jwt {
39053906

39063907
auto jwk_list = jwks_json.get_claim("keys").as_array();
39073908
std::transform(jwk_list.begin(), jwk_list.end(), std::back_inserter(jwk_claims),
3908-
[](const typename json_traits::value_type& val) { return jwk_t{val}; });
3909+
[&](const typename json_traits::value_type& val) { return jwk_t(val, decode); });
39093910
}
39103911

3912+
#ifndef JWT_DISABLE_BASE64
3913+
JWT_CLAIM_EXPLICIT jwks(const typename json_traits::string_type& str)
3914+
: jwks(str, [](const typename json_traits::string_type& str) {
3915+
return base::decode<alphabet::base64url>(base::pad<alphabet::base64url>(str));
3916+
}) {}
3917+
#endif
3918+
39113919
iterator begin() { return jwk_claims.begin(); }
39123920
iterator end() { return jwk_claims.end(); }
39133921
const_iterator cbegin() const { return jwk_claims.begin(); }

include/jwt-cpp/traits/kazuho-picojson/defaults.h

+14
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ namespace jwt {
5454
return decoded_jwt<traits::kazuho_picojson>(token, decode);
5555
}
5656

57+
#ifndef JWT_DISABLE_BASE64
5758
/**
5859
* Parse a jwk
5960
* \param token JWK Token to parse
@@ -63,7 +64,14 @@ namespace jwt {
6364
inline jwk<traits::kazuho_picojson> parse_jwk(const traits::kazuho_picojson::string_type& token) {
6465
return jwk<traits::kazuho_picojson>(token);
6566
}
67+
#endif
68+
69+
template<typename Decode>
70+
jwk<traits::kazuho_picojson> parse_jwk(const traits::kazuho_picojson::string_type& token, Decode decode) {
71+
return jwk<traits::kazuho_picojson>(token, decode);
72+
}
6673

74+
#ifndef JWT_DISABLE_BASE64
6775
/**
6876
* Parse a jwks
6977
* \param token JWKs Token to parse
@@ -73,6 +81,12 @@ namespace jwt {
7381
inline jwks<traits::kazuho_picojson> parse_jwks(const traits::kazuho_picojson::string_type& token) {
7482
return jwks<traits::kazuho_picojson>(token);
7583
}
84+
#endif
85+
86+
template<typename Decode>
87+
jwks<traits::kazuho_picojson> parse_jwks(const traits::kazuho_picojson::string_type& token, Decode decode) {
88+
return jwks<traits::kazuho_picojson>(token, decode);
89+
}
7690

7791
/**
7892
* This type is the specialization of the \ref verify_ops::verify_context class which

0 commit comments

Comments
 (0)