Skip to content
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

Add a new guard clause to the oauth2 client credentials flow. #1050

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/pulp-glue/1050.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed sending no scope instead an empty scope when using the `OAuth2ClientCredentialsAuth` authentication class.
6 changes: 4 additions & 2 deletions pulp-glue/pulp_glue/common/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def __init__(
client_id: str,
client_secret: str,
token_url: str,
scopes: t.List[str],
scopes: t.Optional[t.List[str]] = None,
):
self.client_id = client_id
self.client_secret = client_secret
Expand Down Expand Up @@ -78,10 +78,12 @@ def retrieve_token(self) -> None:
data = {
"client_id": self.client_id,
"client_secret": self.client_secret,
"scope": " ".join(self.scopes),
"grant_type": "client_credentials",
}

if self.scopes:
data["scope"] = " ".join(self.scopes)

response: requests.Response = requests.post(self.token_url, data=data)

response.raise_for_status()
Expand Down
29 changes: 29 additions & 0 deletions pulp-glue/tests/test_authentication.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import typing as t

import pytest

from pulp_glue.common.authentication import OAuth2ClientCredentialsAuth

pytestmark = pytest.mark.glue


def test_sending_no_scope_when_empty(monkeypatch: pytest.MonkeyPatch) -> None:

class OAuth2MockResponse:
def raise_for_status(self):
return None

def json(self):
return {"expires_in": 1, "access_token": "aaa"}

def _requests_post_mocked(url: str, data: t.Dict[str, t.Any]):
assert "scope" not in data
return OAuth2MockResponse()

monkeypatch.setattr("requests.post", _requests_post_mocked)

OAuth2ClientCredentialsAuth(token_url="", client_id="", client_secret="").retrieve_token()

OAuth2ClientCredentialsAuth(
token_url="", client_id="", client_secret="", scopes=[]
).retrieve_token()
Loading