Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default constructor for jwks object #346

Merged
merged 9 commits into from
Apr 24, 2024
2 changes: 1 addition & 1 deletion docs/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ The minimum is `jwt.h` but you will need to add the defines:
- [`JWT_DISABLE_BASE64`](https://github.com/Thalhammer/jwt-cpp/blob/c9a511f436eaa13857336ebeb44dbc5b7860fe01/include/jwt-cpp/jwt.h#L11)
- [`JWT_DISABLE_PICOJSON`](https://github.com/Thalhammer/jwt-cpp/blob/c9a511f436eaa13857336ebeb44dbc5b7860fe01/include/jwt-cpp/jwt.h#L4)

In addition to providing your own JSON traits implementation, see [traits.md](traits.ms) for more information.
In addition to providing your own JSON traits implementation, see [traits.md](traits.md) for more information.

### CMake

Expand Down
5 changes: 5 additions & 0 deletions include/jwt-cpp/jwt.h
Original file line number Diff line number Diff line change
Expand Up @@ -3798,6 +3798,11 @@ namespace jwt {
using iterator = typename jwks_vector_t::iterator;
using const_iterator = typename jwks_vector_t::const_iterator;

/**
* Default constructor producing an empty object without any keys
*/
jwks() = default;

/**
* Parses a string buffer to extract the JWKS.
* \param str buffer containing JSON object representing a JWKS
Expand Down
11 changes: 10 additions & 1 deletion tests/JwksTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ TEST(JwksTest, Missingx5c) {
ASSERT_EQ(jwk3.get_x5c_key_value(), "1");
}

TEST(JwksTest, DefaultConstructor) {
using Jwks = decltype(jwt::parse_jwks(std::declval<std::string>()));

Jwks keys{};
ASSERT_EQ(keys.begin(), keys.end());
ASSERT_FALSE(keys.has_jwk(""));
ASSERT_FALSE(keys.has_jwk("random-jwt"));
}

TEST(JwksTest, CachingBasedOnKid) {
std::string public_key = R"({
"keys": [{
Expand Down Expand Up @@ -149,4 +158,4 @@ TEST(JwksTest, CachingBasedOnKid) {
}

ASSERT_EQ(xs.size(), 2);
}
}
Loading