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

Fix #377: Allow basic auth-scheme #384

Merged
merged 5 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 6 additions & 1 deletion src/kinto_http/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

__all__ = (
"BrowserOAuth",
"TokenAuth"
"BearerTokenAuth",
"Endpoints",
"Session",
Expand All @@ -31,11 +32,15 @@
)


class BearerTokenAuth(requests.auth.AuthBase):
class TokenAuth(requests.auth.AuthBase):
def __init__(self, token, type=None):
self.token = token
self.type = type or "Bearer"

def __call__(self, r):
# Sets auth-scheme to either Bearer or Basic
r.headers["Authorization"] = "{} {}".format(self.type, self.token)
return r

class BearerTokenAuth(TokenAuth):
pass
7 changes: 5 additions & 2 deletions src/kinto_http/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,14 @@ def create_session(server_url=None, auth=None, session=None, **kwargs):
elif "bearer" in auth.lower():
# eg, "Bearer ghruhgrwyhg"
_type, token = auth.split(" ", 1)
auth = kinto_http.BearerTokenAuth(token, type=_type)
auth = kinto_http.TokenAuth(token, type=_type)
elif "basic" in auth.lower():
_type, token = auth.split(" ", 1)
auth = kinto_http.TokenAuth(token, type=_type)
elif auth: # not empty
raise ValueError(
"Unsupported `auth` parameter value. Must be a tuple() or string "
"in the form of `user:pass` or `Bearer xyz`"
"in the form of `user:pass` or `Bearer xyz` or `Basic xyz`"
)

if session is None:
Expand Down
9 changes: 8 additions & 1 deletion tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,15 @@ def test_auth_can_be_passed_as_colon_separate(session_setup: Tuple[MagicMock, Se


def test_auth_can_be_passed_as_basic_header(session_setup: Tuple[MagicMock, Session]):
session = create_session(auth="Basic asdfghjkl;")
assert isinstance(session.auth, kinto_http.TokenAuth)
assert session.auth.type == "Basic"
assert session.auth.token == "asdfghjkl;"


def test_auth_can_be_passed_as_bearer(session_setup: Tuple[MagicMock, Session]):
session = create_session(auth="Bearer+OIDC abcdef")
assert isinstance(session.auth, kinto_http.BearerTokenAuth)
assert isinstance(session.auth, kinto_http.TokenAuth)
assert session.auth.type == "Bearer+OIDC"
assert session.auth.token == "abcdef"

Expand Down