Skip to content

Commit 5da48c2

Browse files
committed
Added Token authentication method
1 parent 60a472e commit 5da48c2

File tree

6 files changed

+27
-11
lines changed

6 files changed

+27
-11
lines changed

adafruit_httpserver/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
from .authentication import (
2727
Basic,
28+
Token,
2829
Bearer,
2930
check_authentication,
3031
require_authentication,

adafruit_httpserver/authentication.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,27 @@ def __str__(self) -> str:
2828
return f"Basic {self._value}"
2929

3030

31-
class Bearer:
32-
"""Represents HTTP Bearer Token Authentication."""
31+
class Token:
32+
"""Represents HTTP Token Authentication."""
33+
34+
prefix = "Token"
3335

3436
def __init__(self, token: str) -> None:
3537
self._value = token
3638

3739
def __str__(self) -> str:
38-
return f"Bearer {self._value}"
40+
return f"{self.prefix} {self._value}"
41+
42+
43+
class Bearer(Token): # pylint: disable=too-few-public-methods
44+
"""Represents HTTP Bearer Token Authentication."""
45+
46+
prefix = "Bearer"
3947

4048

41-
def check_authentication(request: Request, auths: List[Union[Basic, Bearer]]) -> bool:
49+
def check_authentication(
50+
request: Request, auths: List[Union[Basic, Token, Bearer]]
51+
) -> bool:
4252
"""
4353
Returns ``True`` if request is authorized by any of the authentications, ``False`` otherwise.
4454
@@ -47,15 +57,17 @@ def check_authentication(request: Request, auths: List[Union[Basic, Bearer]]) ->
4757
check_authentication(request, [Basic("username", "password")])
4858
"""
4959

50-
auth_header = request.headers.get("Authorization")
60+
auth_header = request.headers.get_directive("Authorization")
5161

5262
if auth_header is None:
5363
return False
5464

5565
return any(auth_header == str(auth) for auth in auths)
5666

5767

58-
def require_authentication(request: Request, auths: List[Union[Basic, Bearer]]) -> None:
68+
def require_authentication(
69+
request: Request, auths: List[Union[Basic, Token, Bearer]]
70+
) -> None:
5971
"""
6072
Checks if the request is authorized and raises ``AuthenticationError`` if not.
6173

adafruit_httpserver/server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from errno import EAGAIN, ECONNRESET, ETIMEDOUT
1818
from traceback import print_exception
1919

20-
from .authentication import Basic, Bearer, require_authentication
20+
from .authentication import Basic, Token, Bearer, require_authentication
2121
from .exceptions import (
2222
ServerStoppedError,
2323
AuthenticationError,
@@ -398,7 +398,7 @@ def poll(self) -> str:
398398
conn.close()
399399
raise error # Raise the exception again to be handled by the user.
400400

401-
def require_authentication(self, auths: List[Union[Basic, Bearer]]) -> None:
401+
def require_authentication(self, auths: List[Union[Basic, Token, Bearer]]) -> None:
402402
"""
403403
Requires authentication for all routes and files in ``root_path``.
404404
Any non-authenticated request will be rejected with a 401 status code.

docs/examples.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,15 +243,15 @@ If you want to apply authentication to the whole server, you need to call ``.req
243243

244244
.. literalinclude:: ../examples/httpserver_authentication_server.py
245245
:caption: examples/httpserver_authentication_server.py
246-
:emphasize-lines: 8,11-15,19
246+
:emphasize-lines: 8,11-16,20
247247
:linenos:
248248

249249
On the other hand, if you want to apply authentication to a set of routes, you need to call ``require_authentication`` function.
250250
In both cases you can check if ``request`` is authenticated by calling ``check_authentication`` on it.
251251

252252
.. literalinclude:: ../examples/httpserver_authentication_handlers.py
253253
:caption: examples/httpserver_authentication_handlers.py
254-
:emphasize-lines: 9-15,21-25,33,47,59
254+
:emphasize-lines: 9-16,22-27,35,49,61
255255
:linenos:
256256

257257
Redirects

examples/httpserver_authentication_handlers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from adafruit_httpserver.authentication import (
1010
AuthenticationError,
1111
Basic,
12+
Token,
1213
Bearer,
1314
check_authentication,
1415
require_authentication,
@@ -21,6 +22,7 @@
2122
# Create a list of available authentication methods.
2223
auths = [
2324
Basic("user", "password"),
25+
Token("2db53340-4f9c-4f70-9037-d25bee77eca6"),
2426
Bearer("642ec696-2a79-4d60-be3a-7c9a3164d766"),
2527
]
2628

examples/httpserver_authentication_server.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
import socketpool
66
import wifi
77

8-
from adafruit_httpserver import Server, Request, Response, Basic, Bearer
8+
from adafruit_httpserver import Server, Request, Response, Basic, Token, Bearer
99

1010

1111
# Create a list of available authentication methods.
1212
auths = [
1313
Basic("user", "password"),
14+
Token("2db53340-4f9c-4f70-9037-d25bee77eca6"),
1415
Bearer("642ec696-2a79-4d60-be3a-7c9a3164d766"),
1516
]
1617

0 commit comments

Comments
 (0)