Skip to content

Commit d9e8607

Browse files
committed
enable user defined base64 decoding function
1 parent 60892cf commit d9e8607

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

include/jwt-cpp/jwt.h

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3206,13 +3206,28 @@ namespace jwt {
32063206
const details::map_of_claims<json_traits> jwk_claims;
32073207

32083208
public:
3209+
template<typename Decode>
3210+
jwk(const typename json_traits::string_type& str, Decode&& decode)
3211+
: jwk(details::map_of_claims<json_traits>::parse_claims(str), decode) {}
3212+
3213+
template<typename Decode>
3214+
jwk(const typename json_traits::value_type& json, Decode&& decode)
3215+
: jwk(json_traits::as_object(json), decode) {}
3216+
3217+
template<typename Decode>
3218+
jwk(const typename json_traits::object_type& json, Decode&& decode)
3219+
: jwk_claims(json), k(build_key(jwk_claims, decode)) {}
3220+
3221+
#ifndef JWT_DISABLE_BASE64
32093222
JWT_CLAIM_EXPLICIT jwk(const typename json_traits::string_type& str)
32103223
: jwk(details::map_of_claims<json_traits>::parse_claims(str)) {}
32113224

32123225
JWT_CLAIM_EXPLICIT jwk(const typename json_traits::value_type& json) : jwk(json_traits::as_object(json)) {}
32133226

32143227
JWT_CLAIM_EXPLICIT jwk(const typename json_traits::object_type& json)
3215-
: jwk_claims(json), k(build_key(jwk_claims)) {
3228+
: jwk(json, [](const typename json_traits::string_type& str) {
3229+
return base::decode<alphabet::base64url>(base::pad<alphabet::base64url>(str));
3230+
}) {
32163231
// https://datatracker.ietf.org/doc/html/rfc7518#section-6.1
32173232
// * indicate required params
32183233
// "kty"* : "EC", "RSA", "oct"
@@ -3229,6 +3244,7 @@ namespace jwt {
32293244
// if "oct", then "k"*
32303245
// if "oct", then SHOULD contain "alg"
32313246
}
3247+
#endif
32323248

32333249
/**
32343250
* Get key type claim
@@ -3452,12 +3468,12 @@ namespace jwt {
34523468
std::string oct_key;
34533469
};
34543470

3455-
static helper::evp_pkey_handle build_rsa_key(const details::map_of_claims<json_traits>& claims) {
3471+
template<typename Decode>
3472+
static helper::evp_pkey_handle build_rsa_key(const details::map_of_claims<json_traits>& claims,
3473+
Decode&& decode) {
34563474
EVP_PKEY* evp_key = nullptr;
3457-
auto n = jwt::helper::raw2bn(
3458-
base::decode<alphabet::base64url>(base::pad<alphabet::base64url>(claims.get_claim("n").as_string())));
3459-
auto e = jwt::helper::raw2bn(
3460-
base::decode<alphabet::base64url>(base::pad<alphabet::base64url>(claims.get_claim("e").as_string())));
3475+
auto n = jwt::helper::raw2bn(decode(claims.get_claim("n").as_string()));
3476+
auto e = jwt::helper::raw2bn(decode(claims.get_claim("e").as_string()));
34613477
#ifdef JWT_OPENSSL_3_0
34623478
// https://www.openssl.org/docs/manmaster/man7/EVP_PKEY-RSA.html
34633479
// see https://www.openssl.org/docs/man3.0/man3/EVP_PKEY_fromdata.html
@@ -3490,7 +3506,8 @@ namespace jwt {
34903506
#endif
34913507
}
34923508

3493-
static key build_key(const details::map_of_claims<json_traits>& claims) {
3509+
template<typename Decode>
3510+
static key build_key(const details::map_of_claims<json_traits>& claims, Decode&& decode) {
34943511
if (!claims.has_claim("kty")) {
34953512
// TODO: custom exception or error code
34963513
throw std::runtime_error("missing required claim \"kty\"");
@@ -3502,12 +3519,12 @@ namespace jwt {
35023519
}
35033520

35043521
if (claims.get_claim("kty").as_string() == "RSA") {
3505-
return key::asymmetric(build_rsa_key(claims));
3522+
return key::asymmetric(build_rsa_key(claims, decode));
35063523
} else if (claims.get_claim("kty").as_string() == "EC") {
35073524
// TODO: build EC key
35083525
throw std::runtime_error("not implemented");
35093526
} else if (claims.get_claim("kty").as_string() == "oct") {
3510-
return key::symmetric(base::decode<alphabet::base64url>(claims.get_claim("k").as_string()));
3527+
return key::symmetric(decode(claims.get_claim("k").as_string()));
35113528
} else {
35123529
// TODO: do not build error messages like this
35133530
throw std::runtime_error("unknown key type (\"kty\"):" + claims.get_claim("kty").as_string());

0 commit comments

Comments
 (0)