Skip to content

Commit 70c9a0c

Browse files
committed
enable user defined base64 decoding function
1 parent 58fe6bc commit 70c9a0c

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
@@ -3418,12 +3434,12 @@ namespace jwt {
34183434
}
34193435

34203436
private:
3421-
static helper::evp_pkey_handle build_rsa_key(const details::map_of_claims<json_traits>& claims) {
3437+
template<typename Decode>
3438+
static helper::evp_pkey_handle build_rsa_key(const details::map_of_claims<json_traits>& claims,
3439+
Decode&& decode) {
34223440
EVP_PKEY* evp_key = nullptr;
3423-
auto n = jwt::helper::raw2bn(
3424-
base::decode<alphabet::base64url>(base::pad<alphabet::base64url>(claims.get_claim("n").as_string())));
3425-
auto e = jwt::helper::raw2bn(
3426-
base::decode<alphabet::base64url>(base::pad<alphabet::base64url>(claims.get_claim("e").as_string())));
3441+
auto n = jwt::helper::raw2bn(decode(claims.get_claim("n").as_string()));
3442+
auto e = jwt::helper::raw2bn(decode(claims.get_claim("e").as_string()));
34273443
#ifdef JWT_OPENSSL_3_0
34283444
// https://www.openssl.org/docs/manmaster/man7/EVP_PKEY-RSA.html
34293445
// see https://www.openssl.org/docs/man3.0/man3/EVP_PKEY_fromdata.html
@@ -3456,7 +3472,8 @@ namespace jwt {
34563472
#endif
34573473
}
34583474

3459-
static key build_key(const details::map_of_claims<json_traits>& claims) {
3475+
template<typename Decode>
3476+
static key build_key(const details::map_of_claims<json_traits>& claims, Decode&& decode) {
34603477
if (!claims.has_claim("kty")) {
34613478
// TODO: custom exception or error code
34623479
throw std::runtime_error("missing required claim \"kty\"");
@@ -3468,12 +3485,12 @@ namespace jwt {
34683485
}
34693486

34703487
if (claims.get_claim("kty").as_string() == "RSA") {
3471-
return key::asymmetric(build_rsa_key(claims));
3488+
return key::asymmetric(build_rsa_key(claims, decode));
34723489
} else if (claims.get_claim("kty").as_string() == "EC") {
34733490
// TODO: build EC key
34743491
throw std::runtime_error("not implemented");
34753492
} else if (claims.get_claim("kty").as_string() == "oct") {
3476-
return key::symmetric(base::decode<alphabet::base64url>(claims.get_claim("k").as_string()));
3493+
return key::symmetric(decode(claims.get_claim("k").as_string()));
34773494
} else {
34783495
// TODO: do not build error messages like this
34793496
throw std::runtime_error("unknown key type (\"kty\"):" + claims.get_claim("kty").as_string());

0 commit comments

Comments
 (0)