-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathauth_manager.py
More file actions
68 lines (50 loc) · 1.7 KB
/
auth_manager.py
File metadata and controls
68 lines (50 loc) · 1.7 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
from abc import ABC
from typing import Optional
from .token_extractor import NoAuthTokenExtractor, TokenExtractor
from .token_parser import NoAuthTokenParser, TokenParser
class AuthManager(ABC):
"""
The authorization manager offers services to manage authorization tokens from client requests
to extract user details before injecting them in the security context.
"""
_token_parser: TokenParser
_token_extractor: TokenExtractor
def __init__(self, token_parser: TokenParser, token_extractor: TokenExtractor):
self._token_parser = token_parser
self._token_extractor = token_extractor
@property
def token_parser(self) -> TokenParser:
return self._token_parser
@property
def token_extractor(self) -> TokenExtractor:
return self._token_extractor
"""
The possibly empty global instance of `AuthManager`.
"""
_auth_manager: Optional[AuthManager] = None
def get_auth_manager() -> AuthManager:
"""
Return the global instance of `AuthManager`.
Raises:
RuntimeError if the clobal instance is not set.
"""
global _auth_manager
if _auth_manager is None:
raise RuntimeError(
"AuthManager is not initialized. Call 'set_auth_manager' first."
)
return _auth_manager
def set_auth_manager(auth_manager: AuthManager):
"""
Initialize the global instance of `AuthManager`.
"""
global _auth_manager
_auth_manager = auth_manager
class AllowAll(AuthManager):
"""
An AuthManager not extracting nor parsing the authorization token.
"""
def __init__(self):
super().__init__(
token_extractor=NoAuthTokenExtractor(), token_parser=NoAuthTokenParser()
)