Repository navigation
feat(keycardai-oauth): client authentication on credentials #430
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
Show all changes
2 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| """Client authentication, an optional capability of a credential. | ||
|
|
||
| A credential that implements ClientAuthenticator returns the headers and | ||
| form fields that authenticate the client on one request. The capability is | ||
| separate from ApplicationCredential, so a credential implemented outside the | ||
| SDK does not need it. | ||
| """ | ||
|
|
||
| from dataclasses import dataclass, field | ||
| from typing import Protocol, runtime_checkable | ||
|
|
||
| from .exceptions import ConfigError | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class ClientAuthentication: | ||
| """Headers and form fields that authenticate the client on one request. | ||
|
|
||
| Holds only client authentication. It never includes a grant parameter | ||
| such as ``grant_type`` or ``subject_token_type``. | ||
|
|
||
| Both fields hold secrets, such as a Basic header or a client assertion, | ||
| so ``repr()`` leaves them out. | ||
| """ | ||
|
|
||
| headers: dict[str, str] = field(default_factory=dict, repr=False) | ||
| form_fields: dict[str, str] = field(default_factory=dict, repr=False) | ||
|
|
||
|
|
||
| @runtime_checkable | ||
| class ClientAuthenticator(Protocol): | ||
| """A credential that can authenticate the client on any endpoint. | ||
|
|
||
| A credential may also define ``check_client_authentication()``, which | ||
| raises ConfigError when the credential cannot authenticate the client. | ||
| require_client_authenticator calls it. | ||
| """ | ||
|
|
||
| async def client_authentication( | ||
| self, *, issuer: str, endpoint: str | ||
| ) -> ClientAuthentication: | ||
| """Return the client authentication for one request. | ||
|
|
||
| Call it once per request. An assertion-based credential signs or | ||
| fetches a new assertion on every call. | ||
|
|
||
| Args: | ||
| issuer: The zone's issuer. A multi-zone credential selects its | ||
| zone by it and fails for an unknown zone. | ||
| endpoint: The URL the request goes to. An assertion-based | ||
| credential signs it as the assertion's ``aud``. | ||
| """ | ||
| ... | ||
|
|
||
|
|
||
| def require_client_authenticator(credential: object) -> ClientAuthenticator: | ||
| """Return ``credential`` as a ClientAuthenticator. | ||
|
|
||
| Call it before sending any request. | ||
|
|
||
| Raises: | ||
| ConfigError: The credential does not provide client authentication, | ||
| or its ``check_client_authentication()`` raised. | ||
| """ | ||
| if not isinstance(credential, ClientAuthenticator): | ||
| raise ConfigError( | ||
| f"{type(credential).__name__} does not provide client authentication" | ||
| ) | ||
| check = getattr(credential, "check_client_authentication", None) | ||
| if callable(check): | ||
| check() | ||
| return credential |
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
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.
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.
Every other error in this file derives from OAuthServerError, so
except OAuthServerErrormisses this one. Worth adding it as a base too?