Skip to content

Commit cba92ab

Browse files
🔭 Allow generic access to JWT header and payload (#251)
* boost print header claims * type * update other example * lint * update example * update example * update examples * tidy * renaming to clarity * delete old check in traits * return underlying type
1 parent 0628011 commit cba92ab

File tree

10 files changed

+23
-47
lines changed

10 files changed

+23
-47
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ int main() {
9696
std::string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXUyJ9.eyJpc3MiOiJhdXRoMCJ9.AbIJTDMFc7yUa5MhvcP03nJPyCPzZtQcGEp-zWfOkEE";
9797
auto decoded = jwt::decode(token);
9898
99-
for(auto& e : decoded.get_payload_claims())
99+
for(auto& e : decoded.get_payload_json())
100100
std::cout << e.first << " = " << e.second << std::endl;
101101
}
102102
```

example/conan/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ int main() {
1212

1313
auto decoded = jwt::decode(token);
1414

15-
for (auto& e : decoded.get_payload_claims())
15+
for (auto& e : decoded.get_payload_json())
1616
std::cout << e.first << " = " << e.second << std::endl;
1717
}

example/es256k.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ K9EDZi0mZ7VUeeNKq476CU5X940yusahgneePQrDMF2nWFEtBCOiXQ==
3333

3434
verify.verify(decoded);
3535

36-
for (auto& e : decoded.get_header_claims())
37-
std::cout << e.first << " = " << e.second.to_json() << std::endl;
38-
for (auto& e : decoded.get_payload_claims())
39-
std::cout << e.first << " = " << e.second.to_json() << std::endl;
36+
for (auto& e : decoded.get_header_json())
37+
std::cout << e.first << " = " << e.second << std::endl;
38+
for (auto& e : decoded.get_payload_json())
39+
std::cout << e.first << " = " << e.second << std::endl;
4040
}

example/partial-claim-verifier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ YwIDAQAB
5757

5858
auto decoded = jwt::decode(token);
5959

60-
for (const auto& e : decoded.get_payload_claims())
60+
for (const auto& e : decoded.get_payload_json())
6161
std::cout << e.first << " = " << e.second << std::endl;
6262

6363
std::cout << std::endl;

example/print-claims.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ int main() {
66
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXUyJ9.eyJpc3MiOiJhdXRoMCJ9.AbIJTDMFc7yUa5MhvcP03nJPyCPzZtQcGEp-zWfOkEE";
77
auto decoded = jwt::decode(token);
88

9-
for (auto& e : decoded.get_payload_claims())
9+
for (auto& e : decoded.get_payload_json())
1010
std::cout << e.first << " = " << e.second << std::endl;
1111
}

example/private-claims.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ int main() {
3232

3333
const auto decoded = jwt::decode(token);
3434

35-
const auto api_array = decoded.get_payload_claims()["object"].to_json().get("api").get("array");
35+
const auto api_array = decoded.get_payload_claim("object").to_json().get("api").get("array");
3636
std::cout << "api array = " << api_array << std::endl;
3737

3838
jwt::verify()

example/rsa-verify.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ YwIDAQAB
2727

2828
verify.verify(decoded);
2929

30-
for (auto& e : decoded.get_header_claims())
31-
std::cout << e.first << " = " << e.second.to_json() << std::endl;
32-
for (auto& e : decoded.get_payload_claims())
33-
std::cout << e.first << " = " << e.second.to_json() << std::endl;
30+
for (auto& e : decoded.get_header_json())
31+
std::cout << e.first << " = " << e.second << std::endl;
32+
for (auto& e : decoded.get_payload_json())
33+
std::cout << e.first << " = " << e.second << std::endl;
3434
}

example/traits/boost-json.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ int main() {
3434
.sign(jwt::algorithm::none{});
3535
const auto decoded = jwt::decode<traits>(token);
3636

37+
for (auto& e : decoded.get_header_json())
38+
std::cout << e.key() << " = " << e.value() << std::endl;
39+
3740
const auto array =
3841
traits::as_array(decoded.get_payload_claim("object").to_json().as_object()["api"].as_object()["array"]);
3942
std::cout << "payload /object/api/array = " << array << std::endl;

example/traits/kazuho-picojson.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ int main() {
3333
.sign(jwt::algorithm::none{});
3434
const auto decoded = jwt::decode<traits>(token);
3535

36-
const auto api_array = decoded.get_payload_claims()["object"].to_json().get("api").get("array");
36+
const auto api_array = decoded.get_payload_claim("object").to_json().get("api").get("array");
3737
std::cout << "api array = " << api_array << std::endl;
3838

3939
jwt::verify<traits>()

include/jwt-cpp/jwt.h

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2102,10 +2102,6 @@ namespace jwt {
21022102
is_count_signature<object_type, string_type>::value &&
21032103
is_subcription_operator_signature<object_type, value_type, string_type>::value &&
21042104
is_at_const_signature<object_type, value_type, string_type>::value;
2105-
2106-
static constexpr auto supports_claims_transform =
2107-
value && is_detected<has_value_type, object_type>::value &&
2108-
std::is_same<typename object_type::value_type, std::pair<const string_type, value_type>>::value;
21092105
};
21102106

21112107
template<typename value_type, typename array_type>
@@ -2329,10 +2325,8 @@ namespace jwt {
23292325

23302326
namespace details {
23312327
template<typename json_traits>
2332-
class map_of_claims {
2328+
struct map_of_claims {
23332329
typename json_traits::object_type claims;
2334-
2335-
public:
23362330
using basic_claim_t = basic_claim<json_traits>;
23372331
using iterator = typename json_traits::object_type::iterator;
23382332
using const_iterator = typename json_traits::object_type::const_iterator;
@@ -2386,21 +2380,6 @@ namespace jwt {
23862380
if (!has_claim(name)) throw error::claim_not_present_exception();
23872381
return basic_claim_t{claims.at(name)};
23882382
}
2389-
2390-
std::unordered_map<typename json_traits::string_type, basic_claim_t> get_claims() const {
2391-
static_assert(
2392-
details::is_valid_json_object<typename json_traits::value_type, typename json_traits::string_type,
2393-
typename json_traits::object_type>::supports_claims_transform,
2394-
"currently there is a limitation on the internal implemantation of the `object_type` to have an "
2395-
"`std::pair` like `value_type`");
2396-
2397-
std::unordered_map<typename json_traits::string_type, basic_claim_t> res;
2398-
std::transform(claims.begin(), claims.end(), std::inserter(res, res.end()),
2399-
[](const typename json_traits::object_type::value_type& val) {
2400-
return std::make_pair(val.first, basic_claim_t{val.second});
2401-
});
2402-
return res;
2403-
}
24042383
};
24052384
} // namespace details
24062385

@@ -2701,19 +2680,15 @@ namespace jwt {
27012680
*/
27022681
const typename json_traits::string_type& get_signature_base64() const noexcept { return signature_base64; }
27032682
/**
2704-
* Get all payload claims
2683+
* Get all payload as JSON object
27052684
* \return map of claims
27062685
*/
2707-
std::unordered_map<typename json_traits::string_type, basic_claim_t> get_payload_claims() const {
2708-
return this->payload_claims.get_claims();
2709-
}
2686+
typename json_traits::object_type get_payload_json() const { return this->payload_claims.claims; }
27102687
/**
2711-
* Get all header claims
2688+
* Get all header as JSON object
27122689
* \return map of claims
27132690
*/
2714-
std::unordered_map<typename json_traits::string_type, basic_claim_t> get_header_claims() const {
2715-
return this->header_claims.get_claims();
2716-
}
2691+
typename json_traits::object_type get_header_json() const { return this->header_claims.claims; }
27172692
/**
27182693
* Get a payload claim by name
27192694
*
@@ -3588,9 +3563,7 @@ namespace jwt {
35883563
* Get all jwk claims
35893564
* \return Map of claims
35903565
*/
3591-
std::unordered_map<typename json_traits::string_type, basic_claim_t> get_claims() const {
3592-
return this->jwk_claims.get_claims();
3593-
}
3566+
typename json_traits::object_type get_claims() const { return this->jwk_claims.claims; }
35943567
};
35953568

35963569
/**

0 commit comments

Comments
 (0)