Skip to content

Commit f4b00c8

Browse files
committed
enable user defined base64 decoding function
1 parent 9c9280f commit f4b00c8

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
@@ -3133,13 +3133,28 @@ namespace jwt {
31333133
const details::map_of_claims<json_traits> jwk_claims;
31343134

31353135
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
31363149
JWT_CLAIM_EXPLICIT jwk(const typename json_traits::string_type& str)
31373150
: jwk(details::map_of_claims<json_traits>::parse_claims(str)) {}
31383151

31393152
JWT_CLAIM_EXPLICIT jwk(const typename json_traits::value_type& json) : jwk(json_traits::as_object(json)) {}
31403153

31413154
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+
}) {
31433158
// https://datatracker.ietf.org/doc/html/rfc7518#section-6.1
31443159
// * indicate required params
31453160
// "kty"* : "EC", "RSA", "oct"
@@ -3156,6 +3171,7 @@ namespace jwt {
31563171
// if "oct", then "k"*
31573172
// if "oct", then SHOULD contain "alg"
31583173
}
3174+
#endif
31593175

31603176
/**
31613177
* Get key type claim
@@ -3345,12 +3361,12 @@ namespace jwt {
33453361
}
33463362

33473363
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) {
33493367
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()));
33543370
#ifdef JWT_OPENSSL_3_0
33553371
// https://www.openssl.org/docs/manmaster/man7/EVP_PKEY-RSA.html
33563372
// see https://www.openssl.org/docs/man3.0/man3/EVP_PKEY_fromdata.html
@@ -3383,7 +3399,8 @@ namespace jwt {
33833399
#endif
33843400
}
33853401

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) {
33873404
if (!claims.has_claim("kty")) {
33883405
// TODO: custom exception or error code
33893406
throw std::runtime_error("missing required claim \"kty\"");
@@ -3395,12 +3412,12 @@ namespace jwt {
33953412
}
33963413

33973414
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));
33993416
} else if (claims.get_claim("kty").as_string() == "EC") {
34003417
// TODO: build EC key
34013418
throw std::runtime_error("not implemented");
34023419
} 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()));
34043421
} else {
34053422
// TODO: do not build error messages like this
34063423
throw std::runtime_error("unknown key type (\"kty\"):" + claims.get_claim("kty").as_string());

0 commit comments

Comments
 (0)