-
-
Notifications
You must be signed in to change notification settings - Fork 6
Extend OIDC discovery parsing in preparation for SSO #1121
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| #include <sourcemeta/one/authentication_discovery.h> | ||
|
|
||
| #include <sourcemeta/core/json.h> | ||
|
|
||
| #include <optional> // std::optional, std::nullopt | ||
| #include <string> // std::string | ||
| #include <string_view> // std::string_view | ||
|
|
||
| namespace { | ||
|
|
||
| auto read_endpoint(const sourcemeta::core::JSON &document, | ||
| const std::string_view property) | ||
| -> std::optional<std::string> { | ||
| const auto *endpoint{document.try_at(property)}; | ||
| if (endpoint == nullptr || !endpoint->is_string()) { | ||
| return std::nullopt; | ||
| } | ||
|
|
||
| return endpoint->to_string(); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| namespace sourcemeta::one { | ||
|
|
||
| auto discovery_url(const std::string_view issuer) -> std::string { | ||
| std::string result{issuer}; | ||
| if (!result.empty() && result.back() == '/') { | ||
| result.pop_back(); | ||
| } | ||
|
|
||
| result += "/.well-known/openid-configuration"; | ||
| return result; | ||
| } | ||
|
|
||
| auto discovery_parse(const std::string_view body) | ||
| -> std::optional<DiscoveryDocument> { | ||
| const auto document{sourcemeta::core::try_parse_json(body)}; | ||
| if (!document.has_value() || !document.value().is_object()) { | ||
| return std::nullopt; | ||
| } | ||
|
|
||
| return DiscoveryDocument{ | ||
| .jwks_uri = read_endpoint(document.value(), "jwks_uri"), | ||
| .authorization_endpoint = | ||
| read_endpoint(document.value(), "authorization_endpoint"), | ||
| .token_endpoint = read_endpoint(document.value(), "token_endpoint"), | ||
| .userinfo_endpoint = | ||
| read_endpoint(document.value(), "userinfo_endpoint")}; | ||
| } | ||
|
|
||
| } // namespace sourcemeta::one |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| #include <sourcemeta/one/authentication.h> | ||
|
|
||
| #include <sourcemeta/core/test.h> | ||
|
|
||
| TEST(discovery_url_appends_the_well_known_path) { | ||
| EXPECT_EQ(sourcemeta::one::discovery_url("https://acme.test"), | ||
| "https://acme.test/.well-known/openid-configuration"); | ||
| } | ||
|
|
||
| TEST(discovery_url_strips_a_trailing_slash) { | ||
| EXPECT_EQ(sourcemeta::one::discovery_url("https://acme.test/"), | ||
| "https://acme.test/.well-known/openid-configuration"); | ||
| } | ||
|
|
||
| TEST(discovery_url_of_an_empty_issuer) { | ||
| EXPECT_EQ(sourcemeta::one::discovery_url(""), | ||
| "/.well-known/openid-configuration"); | ||
| } | ||
|
|
||
| TEST(discovery_parse_reads_every_endpoint) { | ||
| const auto document{sourcemeta::one::discovery_parse(R"JSON({ | ||
| "jwks_uri": "https://acme.test/keys", | ||
| "authorization_endpoint": "https://acme.test/authorize", | ||
| "token_endpoint": "https://acme.test/token", | ||
| "userinfo_endpoint": "https://acme.test/userinfo" | ||
| })JSON")}; | ||
| EXPECT_TRUE(document.has_value()); | ||
| EXPECT_TRUE(document.value().jwks_uri.has_value()); | ||
| EXPECT_EQ(document.value().jwks_uri.value(), "https://acme.test/keys"); | ||
| EXPECT_TRUE(document.value().authorization_endpoint.has_value()); | ||
| EXPECT_EQ(document.value().authorization_endpoint.value(), | ||
| "https://acme.test/authorize"); | ||
| EXPECT_TRUE(document.value().token_endpoint.has_value()); | ||
| EXPECT_EQ(document.value().token_endpoint.value(), "https://acme.test/token"); | ||
| EXPECT_TRUE(document.value().userinfo_endpoint.has_value()); | ||
| EXPECT_EQ(document.value().userinfo_endpoint.value(), | ||
| "https://acme.test/userinfo"); | ||
| } | ||
|
|
||
| TEST(discovery_parse_reads_a_document_with_only_a_key_set_location) { | ||
| const auto document{sourcemeta::one::discovery_parse(R"JSON({ | ||
| "jwks_uri": "https://acme.test/keys" | ||
| })JSON")}; | ||
| EXPECT_TRUE(document.has_value()); | ||
| EXPECT_TRUE(document.value().jwks_uri.has_value()); | ||
| EXPECT_EQ(document.value().jwks_uri.value(), "https://acme.test/keys"); | ||
| EXPECT_FALSE(document.value().authorization_endpoint.has_value()); | ||
| EXPECT_FALSE(document.value().token_endpoint.has_value()); | ||
| EXPECT_FALSE(document.value().userinfo_endpoint.has_value()); | ||
| } | ||
|
|
||
| TEST(discovery_parse_reads_an_empty_document) { | ||
| const auto document{sourcemeta::one::discovery_parse("{}")}; | ||
| EXPECT_TRUE(document.has_value()); | ||
| EXPECT_FALSE(document.value().jwks_uri.has_value()); | ||
| EXPECT_FALSE(document.value().authorization_endpoint.has_value()); | ||
| EXPECT_FALSE(document.value().token_endpoint.has_value()); | ||
| EXPECT_FALSE(document.value().userinfo_endpoint.has_value()); | ||
| } | ||
|
|
||
| TEST(discovery_parse_ignores_a_non_string_endpoint) { | ||
| const auto document{sourcemeta::one::discovery_parse(R"JSON({ | ||
| "jwks_uri": 1, | ||
| "authorization_endpoint": [ "https://acme.test/authorize" ], | ||
| "token_endpoint": null, | ||
| "userinfo_endpoint": "https://acme.test/userinfo" | ||
| })JSON")}; | ||
| EXPECT_TRUE(document.has_value()); | ||
| EXPECT_FALSE(document.value().jwks_uri.has_value()); | ||
| EXPECT_FALSE(document.value().authorization_endpoint.has_value()); | ||
| EXPECT_FALSE(document.value().token_endpoint.has_value()); | ||
| EXPECT_TRUE(document.value().userinfo_endpoint.has_value()); | ||
| EXPECT_EQ(document.value().userinfo_endpoint.value(), | ||
| "https://acme.test/userinfo"); | ||
| } | ||
|
|
||
| TEST(discovery_parse_denies_a_document_that_is_not_an_object) { | ||
| EXPECT_FALSE(sourcemeta::one::discovery_parse("[]").has_value()); | ||
| EXPECT_FALSE( | ||
| sourcemeta::one::discovery_parse("\"https://acme.test\"").has_value()); | ||
| EXPECT_FALSE(sourcemeta::one::discovery_parse("null").has_value()); | ||
| } | ||
|
|
||
| TEST(discovery_parse_denies_a_document_that_is_not_json) { | ||
| EXPECT_FALSE(sourcemeta::one::discovery_parse("").has_value()); | ||
| EXPECT_FALSE(sourcemeta::one::discovery_parse("<html></html>").has_value()); | ||
| EXPECT_FALSE(sourcemeta::one::discovery_parse("{").has_value()); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| #include <sourcemeta/one/authentication_discovery.h> | ||
|
|
||
| #include <optional> // std::optional, std::nullopt | ||
| #include <string> // std::string | ||
| #include <string_view> // std::string_view | ||
|
|
||
| namespace sourcemeta::one { | ||
|
|
||
| // Provider metadata only arises from token-based and interactive | ||
| // authentication, which are enterprise features, so this edition never | ||
| // locates a metadata document and never reads one | ||
| auto discovery_url(const std::string_view) -> std::string { return {}; } | ||
|
|
||
| auto discovery_parse(const std::string_view) | ||
| -> std::optional<DiscoveryDocument> { | ||
| return std::nullopt; | ||
| } | ||
|
|
||
| } // namespace sourcemeta::one |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/authentication/include/sourcemeta/one/authentication_discovery.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| #ifndef SOURCEMETA_ONE_AUTHENTICATION_DISCOVERY_H | ||
| #define SOURCEMETA_ONE_AUTHENTICATION_DISCOVERY_H | ||
|
|
||
| #ifndef SOURCEMETA_ONE_AUTHENTICATION_EXPORT | ||
| #include <sourcemeta/one/authentication_export.h> | ||
| #endif | ||
|
|
||
| #include <optional> // std::optional | ||
| #include <string> // std::string | ||
| #include <string_view> // std::string_view | ||
|
|
||
| namespace sourcemeta::one { | ||
|
|
||
| // The provider endpoints that a metadata document advertises, each present | ||
| // only when the document declares it as a string | ||
| struct DiscoveryDocument { | ||
| std::optional<std::string> jwks_uri; | ||
| std::optional<std::string> authorization_endpoint; | ||
| std::optional<std::string> token_endpoint; | ||
| std::optional<std::string> userinfo_endpoint; | ||
| }; | ||
|
|
||
| // The well-known location of an issuer's metadata document | ||
| auto SOURCEMETA_ONE_AUTHENTICATION_EXPORT discovery_url(std::string_view issuer) | ||
| -> std::string; | ||
|
|
||
| // Read the provider endpoints out of a metadata document, returning nothing | ||
| // for a body that is not a JSON object | ||
| auto SOURCEMETA_ONE_AUTHENTICATION_EXPORT discovery_parse(std::string_view body) | ||
| -> std::optional<DiscoveryDocument>; | ||
|
|
||
| } // namespace sourcemeta::one | ||
|
|
||
| #endif |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: The two new includes (
authentication_discovery.handauthentication_session.h) are unused by theAuthenticationclass interface. They add unnecessary compilation dependencies to this public header — any change to either file triggers a rebuild of every translation unit that includesauthentication.h. Add them only when the class declaration actually references types or functions from these headers.Prompt for AI agents