-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add OAuth2 ClientCredentials grant flow support.
Closes #926
- Loading branch information
Showing
6 changed files
with
177 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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,2 @@ | ||
Added support to OAuth2 ClientCredentials grant flow as authentication method. | ||
This is tech preview and may change without previous warning. |
This file contains 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
* [Using the CLI](using_the_cli.md) | ||
* [Supported Workflows](supported_workflows.md) | ||
* [Authentication Methods](authentication.md) |
This file contains 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 @@ | ||
# Supported Authentication Methods | ||
|
||
Pulp-CLI support some authentication methods to authenticate against a Pulp instance. | ||
Some very simple and common, like HTTP Basic Auth, and some more complex like OAuth2. | ||
|
||
## OAuth2 ClientCredentials grant | ||
|
||
!!! warning | ||
This is an experimental feature. The support of it could change without any major warning. | ||
|
||
More on https://datatracker.ietf.org/doc/html/rfc6749#section-4.4 | ||
|
||
Using this method the pulp-cli can request a token from an Identity Provider using a pair of | ||
credentials (client_id/client_secret). The token is ten sent through using the `Authorization` header. | ||
The issuer URL and the scope of token must be specified by the Pulp server through the OpenAPI scheme definition. |
This file contains 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,91 @@ | ||
import typing as t | ||
from datetime import datetime, timedelta | ||
|
||
import requests | ||
|
||
|
||
class OAuth2ClientCredentialsAuth(requests.auth.AuthBase): | ||
""" | ||
This implements the OAuth2 ClientCredentials Grant authentication flow. | ||
https://datatracker.ietf.org/doc/html/rfc6749#section-4.4 | ||
""" | ||
|
||
def __init__( | ||
self, | ||
client_id: str, | ||
client_secret: str, | ||
token_url: str, | ||
scopes: t.List[str], | ||
): | ||
self.client_id = client_id | ||
self.client_secret = client_secret | ||
self.token_url = token_url | ||
self.scopes = scopes | ||
|
||
self.access_token: t.Optional[str] = None | ||
self.expire_at: t.Optional[datetime] = None | ||
|
||
def __call__(self, request: requests.PreparedRequest) -> requests.PreparedRequest: | ||
if self.expire_at is None or self.expire_at < datetime.now(): | ||
self.retrieve_token() | ||
|
||
assert self.access_token is not None | ||
|
||
request.headers["Authorization"] = f"Bearer {self.access_token}" | ||
|
||
# Call to untyped function "register_hook" in typed context | ||
request.register_hook("response", self.handle401) # type: ignore[no-untyped-call] | ||
|
||
return request | ||
|
||
def handle401( | ||
self, | ||
response: requests.Response, | ||
**kwargs: t.Any, | ||
) -> requests.Response: | ||
if response.status_code != 401: | ||
return response | ||
|
||
# If we get this far, probably the token is not valid anymore. | ||
|
||
# Try to reach for a new token once. | ||
self.retrieve_token() | ||
|
||
assert self.access_token is not None | ||
|
||
# Consume content and release the original connection | ||
# to allow our new request to reuse the same one. | ||
response.content | ||
response.close() | ||
prepared_new_request = response.request.copy() | ||
|
||
prepared_new_request.headers["Authorization"] = f"Bearer {self.access_token}" | ||
|
||
# Avoid to enter into an infinity loop. | ||
# Call to untyped function "deregister_hook" in typed context | ||
prepared_new_request.deregister_hook( # type: ignore[no-untyped-call] | ||
"response", self.handle401 | ||
) | ||
|
||
# "Response" has no attribute "connection" | ||
new_response: requests.Response = response.connection.send(prepared_new_request, **kwargs) | ||
new_response.history.append(response) | ||
new_response.request = prepared_new_request | ||
|
||
return new_response | ||
|
||
def retrieve_token(self) -> None: | ||
data = { | ||
"client_id": self.client_id, | ||
"client_secret": self.client_secret, | ||
"scope": " ".join(self.scopes), | ||
"grant_type": "client_credentials", | ||
} | ||
|
||
response: requests.Response = requests.post(self.token_url, data=data) | ||
|
||
response.raise_for_status() | ||
|
||
token = response.json() | ||
self.expire_at = datetime.now() + timedelta(seconds=token["expires_in"]) | ||
self.access_token = token["access_token"] |
This file contains 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 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