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 ability to create HttpxOidcClientAuth from User #153

Merged
merged 2 commits into from
Jan 2, 2023
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
15 changes: 13 additions & 2 deletions testsuite/httpx/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,33 @@

from httpx import Auth, Request, URL, Response

from testsuite.oidc.rhsso import User
from testsuite.openshift.objects.api_key import APIKey
from testsuite.oidc import Token

TokenType = Union[Token, Callable[[], Token]]


class HttpxOidcClientAuth(Auth):
"""Auth class for Httpx client for product secured by oidc"""

def __init__(self, token: Union[Token, Callable[[], Token]], location="authorization") -> None:
def __init__(self, token: TokenType, location="authorization",
username: str = None, password: str = None) -> None:
self.location = location
self._token = token
self.username = username
self.password = password

@classmethod
def from_user(cls, token: TokenType, user: User, location="authorization"):
"""Creates Auth from RHSSO User object"""
return cls(token, location, user.username, user.password)

@cached_property
def token(self):
"""Lazily retrieves token from OIDC provider"""
if callable(self._token):
return self._token()
return self._token(self.username, self.password)
return self._token

def _add_credentials(self, request: Request, token):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ def authorization(authorization, realm_role, blame):

def test_user_with_role(client, user_with_role, rhsso):
"""Test request when user does have required role using new user with assigned role"""
auth = HttpxOidcClientAuth(rhsso.get_token(user_with_role.username, user_with_role.password),
"authorization")
auth = HttpxOidcClientAuth.from_user(rhsso.get_token, user_with_role, "authorization")
response = client.get("/get", auth=auth)
assert response.status_code == 200

Expand Down
6 changes: 2 additions & 4 deletions testsuite/tests/kuadrant/authorino/metadata/test_uma.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@ def client_secret(create_client_secret, rhsso):
@pytest.fixture(scope="module")
def auth2(rhsso):
"""Creates new RHSSO User and returns his authentication object for HTTPX"""
new_username = "newTestUser"
new_password = "p"
rhsso.realm.create_user(new_username, new_password)
return HttpxOidcClientAuth(rhsso.get_token(new_username, new_password))
user = rhsso.realm.create_user("newTestUser", "p")
return HttpxOidcClientAuth.from_user(rhsso.get_token, user=user)


@pytest.fixture(scope="module")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ def test_correct_auth(client, auth):

def test_incorrect_auth(client, rhsso, user2):
"""Updates RHSSO user email address and tests incorrect auth"""
auth = HttpxOidcClientAuth(rhsso.get_token(user2.username, user2.password), "authorization")
auth = HttpxOidcClientAuth.from_user(rhsso.get_token, user2, "authorization")
jsmolar marked this conversation as resolved.
Show resolved Hide resolved
response = client.get("get", auth=auth)
assert response.status_code == 403