-
Notifications
You must be signed in to change notification settings - Fork 0
PTHMINT-116: Add scoped credential resolver and client support #55
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
+512
−15
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,106 @@ | ||
| # Copyright (c) MultiSafepay, Inc. All rights reserved. | ||
|
|
||
| # This file is licensed under the Open Software License (OSL) version 3.0. | ||
| # For a copy of the license, see the LICENSE.txt file in the project root. | ||
|
|
||
| # See the DISCLAIMER.md file for disclaimer details. | ||
|
|
||
| """Credential resolver contracts and default scoped resolver.""" | ||
|
|
||
| from dataclasses import dataclass | ||
| from typing import Optional, Protocol | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class AuthScope: | ||
| """Auth scope selection payload for credential resolution.""" | ||
|
|
||
| scope: str | ||
| group_id: Optional[str] = None | ||
|
|
||
|
|
||
| class CredentialResolver(Protocol): | ||
| """Protocol for resolving API keys by auth scope and context.""" | ||
|
|
||
| def resolve( | ||
| self: "CredentialResolver", | ||
| auth_scope: str, | ||
| group_id: Optional[str] = None, | ||
| ) -> str: | ||
| """Resolve the API key to use for a given scope and context.""" | ||
|
|
||
|
|
||
| class ScopedCredentialResolver: | ||
| """Default resolver implementation for account, partner and group scopes.""" | ||
|
|
||
| AUTH_SCOPE_DEFAULT = "default_account" | ||
| AUTH_SCOPE_PARTNER_AFFILIATE = "partner_affiliate" | ||
| AUTH_SCOPE_TERMINAL_GROUP = "terminal_group" | ||
|
|
||
| def __init__( | ||
| self: "ScopedCredentialResolver", | ||
| default_api_key: str, | ||
| partner_affiliate_api_key: Optional[str] = None, | ||
| terminal_group_api_keys: Optional[dict[str, str]] = None, | ||
| ) -> None: | ||
| """ | ||
| Initialize a scoped credential resolver. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| default_api_key (str): Fallback/default account API key. | ||
| partner_affiliate_api_key (Optional[str]): Partner/affiliate API key. | ||
| terminal_group_api_keys (Optional[dict[str, str]]): Mapping of | ||
| terminal_group_id to API key. | ||
|
|
||
| """ | ||
| self.default_api_key = (default_api_key or "").strip() | ||
| self.partner_affiliate_api_key = ( | ||
| partner_affiliate_api_key or "" | ||
| ).strip() or None | ||
| self.terminal_group_api_keys = { | ||
| group_id: api_key.strip() | ||
| for group_id, api_key in (terminal_group_api_keys or {}).items() | ||
| if api_key and api_key.strip() | ||
| } | ||
|
|
||
| if ( | ||
| not self.default_api_key | ||
| and self.partner_affiliate_api_key is None | ||
| and not self.terminal_group_api_keys | ||
| ): | ||
| raise ValueError( | ||
| "ScopedCredentialResolver requires at least one API key.", | ||
| ) | ||
|
|
||
| def resolve( | ||
| self: "ScopedCredentialResolver", | ||
| auth_scope: str, | ||
| group_id: Optional[str] = None, | ||
| ) -> str: | ||
| """Resolve API key for the given scope and auth context.""" | ||
| if auth_scope == self.AUTH_SCOPE_TERMINAL_GROUP: | ||
| if not group_id: | ||
| raise ValueError( | ||
| "Missing terminal_group_id in auth scope.", | ||
| ) | ||
| api_key = self.terminal_group_api_keys.get(group_id) | ||
| if not api_key: | ||
| raise ValueError( | ||
| "No API key configured for terminal_group_id " | ||
| f"'{group_id}'.", | ||
| ) | ||
| return api_key | ||
|
|
||
| if auth_scope == self.AUTH_SCOPE_PARTNER_AFFILIATE: | ||
| api_key = self.partner_affiliate_api_key or self.default_api_key | ||
| if not api_key: | ||
| raise ValueError( | ||
| "No API key configured for partner_affiliate scope.", | ||
| ) | ||
| return api_key | ||
|
|
||
| if not self.default_api_key: | ||
| raise ValueError("No API key configured for default scope.") | ||
|
|
||
| return self.default_api_key |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.