Skip to content

Commit e516be9

Browse files
committed
extract key class from jwk
1 parent d278b39 commit e516be9

File tree

1 file changed

+38
-38
lines changed

1 file changed

+38
-38
lines changed

include/jwt-cpp/jwt.h

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,40 @@ namespace jwt {
783783
}
784784
} // namespace helper
785785

786+
class key {
787+
public:
788+
static key symmetric(const std::string& bytes) { return key(bytes); }
789+
790+
static key asymmetric(std::shared_ptr<EVP_PKEY> pkey) { return key(pkey); }
791+
792+
std::string get_symmetric_key() const {
793+
if (!is_symmetric) { throw std::logic_error("not a symmetric key"); }
794+
795+
return oct_key;
796+
}
797+
798+
std::shared_ptr<EVP_PKEY> get_asymmetric_key() const {
799+
if (is_symmetric) { throw std::logic_error("not an asymmetric key"); }
800+
801+
return pkey;
802+
}
803+
804+
private:
805+
key(const std::string& key) {
806+
is_symmetric = true;
807+
oct_key = key;
808+
}
809+
810+
key(std::shared_ptr<EVP_PKEY> key) {
811+
is_symmetric = false;
812+
pkey = key;
813+
}
814+
815+
bool is_symmetric;
816+
std::shared_ptr<EVP_PKEY> pkey;
817+
std::string oct_key;
818+
};
819+
786820
/**
787821
* \brief Various cryptographic algorithms when working with JWT
788822
*
@@ -3105,7 +3139,7 @@ namespace jwt {
31053139
JWT_CLAIM_EXPLICIT jwk(const typename json_traits::value_type& json) : jwk(json_traits::as_object(json)) {}
31063140

31073141
JWT_CLAIM_EXPLICIT jwk(const typename json_traits::object_type& json)
3108-
: jwk_claims(json), key(build_key(jwk_claims)) {
3142+
: jwk_claims(json), k(build_key(jwk_claims)) {
31093143
// https://datatracker.ietf.org/doc/html/rfc7518#section-6.1
31103144
// * indicate required params
31113145
// "kty"* : "EC", "RSA", "oct"
@@ -3301,50 +3335,16 @@ namespace jwt {
33013335

33023336
bool empty() const noexcept { return jwk_claims.empty(); }
33033337

3304-
std::shared_ptr<EVP_PKEY> get_pkey() const { return key.get_asymmetric_key(); }
3338+
std::shared_ptr<EVP_PKEY> get_pkey() const { return k.get_asymmetric_key(); }
33053339

3306-
std::string get_oct_key() const { return key.get_symmetric_key(); }
3340+
std::string get_oct_key() const { return k.get_symmetric_key(); }
33073341

33083342
bool supports(const std::string& alg_name) const {
33093343
const alg_list& x = supported_alg.find(get_key_type())->second;
33103344
return std::find(x.begin(), x.end(), alg_name) != x.end();
33113345
}
33123346

33133347
private:
3314-
class key {
3315-
public:
3316-
static key symmetric(const std::string& bytes) { return key(bytes); }
3317-
3318-
static key asymmetric(std::shared_ptr<EVP_PKEY> pkey) { return key(pkey); }
3319-
3320-
std::string get_symmetric_key() const {
3321-
if (!is_symmetric) { throw std::logic_error("not a symmetric key"); }
3322-
3323-
return oct_key;
3324-
}
3325-
3326-
std::shared_ptr<EVP_PKEY> get_asymmetric_key() const {
3327-
if (is_symmetric) { throw std::logic_error("not an asymmetric key"); }
3328-
3329-
return pkey;
3330-
}
3331-
3332-
private:
3333-
key(const std::string& key) {
3334-
is_symmetric = true;
3335-
oct_key = key;
3336-
}
3337-
3338-
key(std::shared_ptr<EVP_PKEY> key) {
3339-
is_symmetric = false;
3340-
pkey = key;
3341-
}
3342-
3343-
bool is_symmetric;
3344-
std::shared_ptr<EVP_PKEY> pkey;
3345-
std::string oct_key;
3346-
};
3347-
33483348
static std::shared_ptr<EVP_PKEY> build_rsa_key(const details::map_of_claims<json_traits>& claims) {
33493349
EVP_PKEY* evp_key = nullptr;
33503350
auto n = jwt::helper::raw2bn(
@@ -3407,7 +3407,7 @@ namespace jwt {
34073407
}
34083408
}
34093409

3410-
key key;
3410+
key k;
34113411
};
34123412

34133413
/**

0 commit comments

Comments
 (0)