@@ -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
@@ -3452,12 +3468,12 @@ namespace jwt {
3452
3468
std::string oct_key;
3453
3469
};
3454
3470
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) {
3456
3474
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 ()));
3461
3477
#ifdef JWT_OPENSSL_3_0
3462
3478
// https://www.openssl.org/docs/manmaster/man7/EVP_PKEY-RSA.html
3463
3479
// see https://www.openssl.org/docs/man3.0/man3/EVP_PKEY_fromdata.html
@@ -3490,7 +3506,8 @@ namespace jwt {
3490
3506
#endif
3491
3507
}
3492
3508
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) {
3494
3511
if (!claims.has_claim (" kty" )) {
3495
3512
// TODO: custom exception or error code
3496
3513
throw std::runtime_error (" missing required claim \" kty\" " );
@@ -3502,12 +3519,12 @@ namespace jwt {
3502
3519
}
3503
3520
3504
3521
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 ));
3506
3523
} else if (claims.get_claim (" kty" ).as_string () == " EC" ) {
3507
3524
// TODO: build EC key
3508
3525
throw std::runtime_error (" not implemented" );
3509
3526
} 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 ()));
3511
3528
} else {
3512
3529
// TODO: do not build error messages like this
3513
3530
throw std::runtime_error (" unknown key type (\" kty\" ):" + claims.get_claim (" kty" ).as_string ());
0 commit comments