Skip to content
Open
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
7 changes: 6 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,13 @@ Before creating an issue please make sure that it was not already reported.
#### Code

1) Create a new branch based on `develop` branch.
* Optional: create and enable virtualenv:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used a virtualenv here as I didn't want to pollute my local machine with extra packages. Thought it might be useful for others, too.

```
python3 -m venv myenv
source myenv/bin/activate
```
2) Fetch all dev dependencies.
* Install required python modules using `pip`: **python -m pip install .[testing]**
* Install required python modules using `pip`: **python -m pip install ".[testing]"**
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my shell (zsh) didn't like the [ and ] without quoting them.

3) Ensure tests are ok by running them using [`pytest`](http://doc.pytest.org/en/latest/index.html).
4) Add your changes.
5) Follow [Black](https://black.readthedocs.io/en/stable/) code formatting.
Expand Down
2 changes: 2 additions & 0 deletions httpx_auth/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from httpx_auth._authentication import (
Basic,
BearerToken,
HeaderApiKey,
QueryApiKey,
SupportMultiAuth,
Expand Down Expand Up @@ -46,6 +47,7 @@

__all__ = [
"Basic",
"BearerToken",
"HeaderApiKey",
"QueryApiKey",
"OAuth2",
Expand Down
12 changes: 12 additions & 0 deletions httpx_auth/_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ def auth_flow(
yield request


class BearerToken(HeaderApiKey):
"""Describes a Bearer Token requests authentication."""

def __init__(self, token: str):
"""
:param token: The Bearer token that will be sent.
"""
if not token:
raise Exception("Token is mandatory.")
super().__init__(f"Bearer {token}", "Authorization")


class QueryApiKey(httpx.Auth, SupportMultiAuth):
"""Describes an API Key requests authentication."""

Expand Down
8 changes: 6 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ issues = "https://github.com/Colin-b/httpx_auth/issues"

[project.optional-dependencies]
testing = [
"pytest==8.3.4",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure why this was missing here, I assume it comes with pytest-cov in whatever version is deemed suitable there.

# Used to generate test tokens
"pyjwt==2.*",
# Used to mock httpx
Expand All @@ -55,12 +56,15 @@ testing = [
"pytest-asyncio==0.25.*",
]

[tool.setuptools]
py-modules = [
"httpx_auth"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is needed for:

Processing /Users/joscha/dev/httpx_auth
  Installing build dependencies ... done
  Getting requirements to build wheel ... error
  error: subprocess-exited-with-error
  
  × Getting requirements to build wheel did not run successfully.
  │ exit code: 1
  ╰─> [93 lines of output]
      No `packages` or `py_modules` configuration, performing automatic discovery.
      `flat-layout` detected -- analysing .
      discovered packages -- ['httpx_auth', 'myenv', 'httpx_auth._oauth2', 'myenv.bin', 'myenv.include', 'myenv.lib']

possibly related to the virtualenv?

]

[tool.setuptools.dynamic]
version = {attr = "httpx_auth.version.__version__"}

[tool.pytest.ini_options]
filterwarnings = [
"error",
]
# Silence deprecation warnings about option "asyncio_default_fixture_loop_scope"
asyncio_default_fixture_loop_scope = "function"
3 changes: 3 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[pytest]
asyncio_mode = auto
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this, I get:

INTERNALERROR> pytest.PytestConfigWarning: Unknown config option: asyncio_default_fixture_loop_scope

asyncio_default_fixture_loop_scope = "function"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and without this I get:
Screenshot 2025-02-27 at 1 18 34 pm

Empty file added tests/bearer/__init__.py
Empty file.
23 changes: 23 additions & 0 deletions tests/bearer/test_bearer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from pytest_httpx import HTTPXMock
import httpx
import pytest

import httpx_auth


def test_bearer_requires_atoken():
with pytest.raises(Exception) as exception_info:
httpx_auth.BearerToken(None)
assert str(exception_info.value) == "Token is mandatory."

def test_bearer_token_is_sent(httpx_mock: HTTPXMock):
auth = httpx_auth.BearerToken("my_token")

httpx_mock.add_response(
url="https://authorized_only",
method="GET",
match_headers={"Authorization": "Bearer my_token"},
)

with httpx.Client() as client:
client.get("https://authorized_only", auth=auth)