@@ -3133,13 +3133,28 @@ namespace jwt {
3133
3133
const details::map_of_claims<json_traits> jwk_claims;
3134
3134
3135
3135
public:
3136
+ template <typename Decode>
3137
+ jwk (const typename json_traits::string_type& str, Decode&& decode)
3138
+ : jwk(details::map_of_claims<json_traits>::parse_claims(str), decode) {}
3139
+
3140
+ template <typename Decode>
3141
+ jwk (const typename json_traits::value_type& json, Decode&& decode)
3142
+ : jwk(json_traits::as_object(json), decode) {}
3143
+
3144
+ template <typename Decode>
3145
+ jwk (const typename json_traits::object_type& json, Decode&& decode)
3146
+ : jwk_claims(json), k(build_key(jwk_claims, decode)) {}
3147
+
3148
+ #ifndef JWT_DISABLE_BASE64
3136
3149
JWT_CLAIM_EXPLICIT jwk (const typename json_traits::string_type& str)
3137
3150
: jwk(details::map_of_claims<json_traits>::parse_claims(str)) {}
3138
3151
3139
3152
JWT_CLAIM_EXPLICIT jwk (const typename json_traits::value_type& json) : jwk(json_traits::as_object(json)) {}
3140
3153
3141
3154
JWT_CLAIM_EXPLICIT jwk (const typename json_traits::object_type& json)
3142
- : jwk_claims(json), k(build_key(jwk_claims)) {
3155
+ : jwk(json, [](const typename json_traits::string_type& str) {
3156
+ return base::decode<alphabet::base64url>(base::pad<alphabet::base64url>(str));
3157
+ }) {
3143
3158
// https://datatracker.ietf.org/doc/html/rfc7518#section-6.1
3144
3159
// * indicate required params
3145
3160
// "kty"* : "EC", "RSA", "oct"
@@ -3156,6 +3171,7 @@ namespace jwt {
3156
3171
// if "oct", then "k"*
3157
3172
// if "oct", then SHOULD contain "alg"
3158
3173
}
3174
+ #endif
3159
3175
3160
3176
/* *
3161
3177
* Get key type claim
@@ -3345,12 +3361,12 @@ namespace jwt {
3345
3361
}
3346
3362
3347
3363
private:
3348
- static std::shared_ptr<EVP_PKEY> build_rsa_key (const details::map_of_claims<json_traits>& claims) {
3364
+ template <typename Decode>
3365
+ static std::shared_ptr<EVP_PKEY> build_rsa_key (const details::map_of_claims<json_traits>& claims,
3366
+ Decode&& decode) {
3349
3367
EVP_PKEY* evp_key = nullptr ;
3350
- auto n = jwt::helper::raw2bn (
3351
- base::decode<alphabet::base64url>(base::pad<alphabet::base64url>(claims.get_claim (" n" ).as_string ())));
3352
- auto e = jwt::helper::raw2bn (
3353
- base::decode<alphabet::base64url>(base::pad<alphabet::base64url>(claims.get_claim (" e" ).as_string ())));
3368
+ auto n = jwt::helper::raw2bn (decode (claims.get_claim (" n" ).as_string ()));
3369
+ auto e = jwt::helper::raw2bn (decode (claims.get_claim (" e" ).as_string ()));
3354
3370
#ifdef JWT_OPENSSL_3_0
3355
3371
// https://www.openssl.org/docs/manmaster/man7/EVP_PKEY-RSA.html
3356
3372
// see https://www.openssl.org/docs/man3.0/man3/EVP_PKEY_fromdata.html
@@ -3383,7 +3399,8 @@ namespace jwt {
3383
3399
#endif
3384
3400
}
3385
3401
3386
- static key build_key (const details::map_of_claims<json_traits>& claims) {
3402
+ template <typename Decode>
3403
+ static key build_key (const details::map_of_claims<json_traits>& claims, Decode&& decode) {
3387
3404
if (!claims.has_claim (" kty" )) {
3388
3405
// TODO: custom exception or error code
3389
3406
throw std::runtime_error (" missing required claim \" kty\" " );
@@ -3395,12 +3412,12 @@ namespace jwt {
3395
3412
}
3396
3413
3397
3414
if (claims.get_claim (" kty" ).as_string () == " RSA" ) {
3398
- return key::asymmetric (build_rsa_key (claims));
3415
+ return key::asymmetric (build_rsa_key (claims, decode ));
3399
3416
} else if (claims.get_claim (" kty" ).as_string () == " EC" ) {
3400
3417
// TODO: build EC key
3401
3418
throw std::runtime_error (" not implemented" );
3402
3419
} else if (claims.get_claim (" kty" ).as_string () == " oct" ) {
3403
- return key::symmetric (base:: decode<alphabet::base64url> (claims.get_claim (" k" ).as_string ()));
3420
+ return key::symmetric (decode (claims.get_claim (" k" ).as_string ()));
3404
3421
} else {
3405
3422
// TODO: do not build error messages like this
3406
3423
throw std::runtime_error (" unknown key type (\" kty\" ):" + claims.get_claim (" kty" ).as_string ());
0 commit comments