This repository was archived by the owner on Sep 21, 2025. It is now read-only.
forked from Colin-b/httpx_auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_authentication.py
More file actions
114 lines (90 loc) · 4.01 KB
/
_authentication.py
File metadata and controls
114 lines (90 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import typing
from typing import Generator
import httpx
from httpx import Request, Response
class _MultiAuth(httpx.Auth):
"""Authentication using multiple authentication methods."""
def __init__(self, *authentication_modes):
self.authentication_modes = authentication_modes
def sync_auth_flow(
self, request: Request
) -> typing.Generator[Request, Response, None]:
for authentication_mode in self.authentication_modes:
# auth_flow may yield one or more requests, the last of which is the user request with added auth headers
flow = authentication_mode.sync_auth_flow(request)
req = next(flow)
while True:
if req is request:
break
resp = yield req
req = flow.send(resp)
yield request
async def async_auth_flow(
self, request: Request
) -> typing.AsyncGenerator[Request, Response]:
for authentication_mode in self.authentication_modes:
# auth_flow may yield one or more requests, the last of which is the user request with added auth headers
flow = authentication_mode.async_auth_flow(request)
req = await flow.__anext__()
while True:
if req is request:
break
resp = yield req
req = await flow.asend(resp)
yield request
def __add__(self, other) -> "_MultiAuth":
if isinstance(other, _MultiAuth):
return _MultiAuth(*self.authentication_modes, *other.authentication_modes)
return _MultiAuth(*self.authentication_modes, other)
def __and__(self, other) -> "_MultiAuth":
if isinstance(other, _MultiAuth):
return _MultiAuth(*self.authentication_modes, *other.authentication_modes)
return _MultiAuth(*self.authentication_modes, other)
class SupportMultiAuth:
"""Inherit from this class to be able to use your class with httpx_auth provided authentication classes."""
def __add__(self, other) -> _MultiAuth:
if isinstance(other, _MultiAuth):
return _MultiAuth(self, *other.authentication_modes)
return _MultiAuth(self, other)
def __and__(self, other) -> _MultiAuth:
if isinstance(other, _MultiAuth):
return _MultiAuth(self, *other.authentication_modes)
return _MultiAuth(self, other)
class HeaderApiKey(httpx.Auth, SupportMultiAuth):
"""Describes an API Key requests authentication."""
def __init__(self, api_key: str, header_name: str = None):
"""
:param api_key: The API key that will be sent.
:param header_name: Name of the header field. "X-API-Key" by default.
"""
self.api_key = api_key
if not api_key:
raise Exception("API Key is mandatory.")
self.header_name = header_name or "X-API-Key"
def auth_flow(
self, request: httpx.Request
) -> Generator[httpx.Request, httpx.Response, None]:
request.headers[self.header_name] = self.api_key
yield request
class QueryApiKey(httpx.Auth, SupportMultiAuth):
"""Describes an API Key requests authentication."""
def __init__(self, api_key: str, query_parameter_name: str = None):
"""
:param api_key: The API key that will be sent.
:param query_parameter_name: Name of the query parameter. "api_key" by default.
"""
self.api_key = api_key
if not api_key:
raise Exception("API Key is mandatory.")
self.query_parameter_name = query_parameter_name or "api_key"
def auth_flow(
self, request: httpx.Request
) -> Generator[httpx.Request, httpx.Response, None]:
request.url = request.url.copy_merge_params(
{self.query_parameter_name: self.api_key}
)
yield request
class Basic(httpx.BasicAuth, SupportMultiAuth):
"""Describes a basic requests authentication."""
def __init__(self, username: str, password: str):
httpx.BasicAuth.__init__(self, username, password)