forked from stackitcloud/stackit-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_auth.py
More file actions
27 lines (19 loc) · 782 Bytes
/
custom_auth.py
File metadata and controls
27 lines (19 loc) · 782 Bytes
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
import jwt
from requests import Request
from requests.auth import AuthBase
from stackit.core.configuration import Configuration
"""
You can create your own authorization class to implement your own authorization logic.
You need to derive from the class 'AuthBase' to implement your own class.
"""
class MyAuth(AuthBase):
__access_token: str
def __init__(self, key: str):
self.__access_token = jwt.encode({"some": "payload"}, key, algorithm="HS256")
def __call__(self, request: Request) -> Request:
request.headers["Authorization"] = f"Bearer {self.__access_token}"
return request
# Initialize custom auth method
auth_method = MyAuth(key="my_super_secret_key")
# Set it in the configuration
config = Configuration(custom_auth=auth_method)