-
Notifications
You must be signed in to change notification settings - Fork 1
III-6844 match apiKeys #2151
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
III-6844 match apiKeys #2151
Changes from 10 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
0763b3e
Add ApiKeysMatchedToClientIds
JonasVHG 7b5e27e
Add ClientIdResolver
JonasVHG d9aa849
Update constructor of RequestAuthenticatorMiddleware
JonasVHG 45ddff5
add exception
JonasVHG dbf5d66
implement logic match_api_keys_to_client_ids
JonasVHG c0ae9e7
bugfix
JonasVHG 0aeb28c
use new name
JonasVHG b39ecdb
use config prefix
JonasVHG 7af50cf
Add acceptance test
JonasVHG a2b604c
Merge branch 'master' into III-6844-match-apikeys
JonasVHG ee3e3f1
linting
JonasVHG 4429354
Merge branch 'III-6844-match-apikeys' of github.com:cultuurnet/udb3-b…
JonasVHG ab1d9ac
move files to concrete folder
JonasVHG e944495
Merge branch 'master' into III-6844-match-apikeys
JonasVHG c18ad8e
Add unit test
JonasVHG 7323d6e
Add unit test
JonasVHG 7065284
linting
JonasVHG 5285c95
Improve scenario name
JonasVHG d7ef864
Merge branch 'master' into III-6844-match-apikeys
JonasVHG 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
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,30 @@ | ||
| Feature: Test authentication with apiKeys matched to clientIds | ||
JonasVHG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Background: | ||
| Given I am using the UDB3 base URL | ||
| And I am not using an UiTID v1 API key | ||
| And I send and accept "application/json" | ||
|
|
||
| Scenario: I can create offers with a clientId that has the entry scope | ||
| Given I am using an UiTID v1 API key of consumer "apiKeyMatchedToClientIdWithEntryScope" | ||
| And I am authorized as JWT provider user "invoerder_1" | ||
| And I create a minimal place and save the "url" as "placeUrl" | ||
| And the response status should be "201" | ||
| When I create a minimal permanent event and save the "url" as "eventUrl" | ||
| Then the response status should be "201" | ||
|
|
||
| Scenario: I cannot create offers with a clientId that only has the search scope | ||
| Given I am using an UiTID v1 API key of consumer "apiKeyMatchedToClientIdWithSearchScope" | ||
| And I am authorized as JWT provider user "invoerder_1" | ||
| And I set the JSON request payload from "places/place-with-required-fields.json" | ||
| When I send a POST request to "/places/" | ||
| And the response status should be "403" | ||
| And the JSON response should be: | ||
| """ | ||
| { | ||
| "type": "https:\/\/api.publiq.be\/probs\/auth\/forbidden", | ||
| "title": "Forbidden", | ||
| "status": 403, | ||
| "detail": "Given API key is not authorized to use Entry API." | ||
| } | ||
| """ | ||
JonasVHG marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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,10 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace CultuurNet\UDB3\Temp; | ||
|
|
||
| interface ApiKeysMatchedToClientIds | ||
| { | ||
| public function getClientId(string $apiKey): string; | ||
| } |
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,20 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace CultuurNet\UDB3\Temp; | ||
|
|
||
| final class InMemoryApiKeysMatchedToClientIds implements ApiKeysMatchedToClientIds | ||
| { | ||
| public function __construct(readonly array $apiKeysMatchedToClientIds) | ||
| { | ||
| } | ||
|
|
||
| public function getClientId(string $apiKey): string | ||
| { | ||
| if (!array_key_exists($apiKey, $this->apiKeysMatchedToClientIds)) { | ||
| throw new UnmatchedApiKey($apiKey); | ||
| } | ||
| return $this->apiKeysMatchedToClientIds[$apiKey]; | ||
| } | ||
| } |
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,15 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace CultuurNet\UDB3\Temp; | ||
|
|
||
| use Exception; | ||
|
|
||
| final class UnmatchedApiKey extends Exception | ||
| { | ||
| public function __construct(readonly string $apiKey) | ||
| { | ||
| parent::__construct($this->apiKey . ' could not be matched to a clientId.'); | ||
| } | ||
| } |
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,10 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace CultuurNet\UDB3\User; | ||
|
|
||
| interface ClientIdResolver | ||
| { | ||
| public function hasEntryAccess(string $clientId): bool; | ||
| } |
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,64 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace CultuurNet\UDB3\User\Keycloak; | ||
|
|
||
| use CultuurNet\UDB3\Json; | ||
| use CultuurNet\UDB3\User\ClientIdResolver; | ||
| use GuzzleHttp\Exception\ConnectException; | ||
| use GuzzleHttp\Psr7\Request; | ||
| use GuzzleHttp\Psr7\Uri; | ||
| use Psr\Http\Client\ClientInterface; | ||
|
|
||
| final class KeycloakClientIdResolver implements ClientIdResolver | ||
| { | ||
| public function __construct( | ||
| readonly ClientInterface $client, | ||
| readonly string $domain, | ||
| readonly string $realm, | ||
| readonly string $token | ||
| ) { | ||
| } | ||
|
|
||
| public function hasEntryAccess(string $clientId): bool | ||
JonasVHG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| $request = new Request( | ||
| 'GET', | ||
| (new Uri($this->domain)) | ||
| ->withPath('/admin/realms/' . $this->realm . '/clients/') | ||
| ->withQuery(http_build_query([ | ||
| 'clientId' => $clientId, | ||
| ])), | ||
| [ | ||
| 'Authorization' => 'Bearer ' . $this->token, | ||
| ] | ||
| ); | ||
|
|
||
| $response = $this->client->sendRequest($request); | ||
|
|
||
| if ($response->getStatusCode() !== 200) { | ||
JonasVHG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $message = 'Keycloak error when getting metadata: ' . $response->getStatusCode(); | ||
|
|
||
| if ($response->getStatusCode() >= 500) { | ||
| throw new ConnectException( | ||
| $message, | ||
| new Request('GET', '/admin/realms/' . $this->realm . '/clients/') | ||
| ); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| $contents = Json::decodeAssociatively($response->getBody()->getContents()); | ||
JonasVHG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if (count($contents) !== 1) { | ||
| return false; | ||
| } | ||
|
|
||
| if (!isset($contents[0]['defaultClientScopes'])) { | ||
| return false; | ||
| } | ||
|
|
||
| return in_array('publiq-api-entry-scope', $contents[0]['defaultClientScopes']); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Code Quality: Magic string The scope name
This would make testing easier and the code more maintainable if the scope name changes. |
||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.