@@ -3206,13 +3206,28 @@ namespace jwt {
3206
3206
const details::map_of_claims<json_traits> jwk_claims;
3207
3207
3208
3208
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
3209
3222
JWT_CLAIM_EXPLICIT jwk (const typename json_traits::string_type& str)
3210
3223
: jwk(details::map_of_claims<json_traits>::parse_claims(str)) {}
3211
3224
3212
3225
JWT_CLAIM_EXPLICIT jwk (const typename json_traits::value_type& json) : jwk(json_traits::as_object(json)) {}
3213
3226
3214
3227
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
+ }) {
3216
3231
// https://datatracker.ietf.org/doc/html/rfc7518#section-6.1
3217
3232
// * indicate required params
3218
3233
// "kty"* : "EC", "RSA", "oct"
@@ -3229,6 +3244,7 @@ namespace jwt {
3229
3244
// if "oct", then "k"*
3230
3245
// if "oct", then SHOULD contain "alg"
3231
3246
}
3247
+ #endif
3232
3248
3233
3249
/* *
3234
3250
* Get key type claim
@@ -3418,12 +3434,12 @@ namespace jwt {
3418
3434
}
3419
3435
3420
3436
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) {
3422
3440
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 ()));
3427
3443
#ifdef JWT_OPENSSL_3_0
3428
3444
// https://www.openssl.org/docs/manmaster/man7/EVP_PKEY-RSA.html
3429
3445
// see https://www.openssl.org/docs/man3.0/man3/EVP_PKEY_fromdata.html
@@ -3456,7 +3472,8 @@ namespace jwt {
3456
3472
#endif
3457
3473
}
3458
3474
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) {
3460
3477
if (!claims.has_claim (" kty" )) {
3461
3478
// TODO: custom exception or error code
3462
3479
throw std::runtime_error (" missing required claim \" kty\" " );
@@ -3468,12 +3485,12 @@ namespace jwt {
3468
3485
}
3469
3486
3470
3487
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 ));
3472
3489
} else if (claims.get_claim (" kty" ).as_string () == " EC" ) {
3473
3490
// TODO: build EC key
3474
3491
throw std::runtime_error (" not implemented" );
3475
3492
} 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 ()));
3477
3494
} else {
3478
3495
// TODO: do not build error messages like this
3479
3496
throw std::runtime_error (" unknown key type (\" kty\" ):" + claims.get_claim (" kty" ).as_string ());
0 commit comments