-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathclient_credentials.py
More file actions
101 lines (82 loc) · 2.77 KB
/
Copy pathclient_credentials.py
File metadata and controls
101 lines (82 loc) · 2.77 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
from firebolt.client.auth.base import AuthRequest, FireboltAuthVersion
from firebolt.client.auth.request_auth_base import _RequestBasedAuth
from firebolt.utils.urls import AUTH_SERVICE_ACCOUNT_URL
class ClientCredentials(_RequestBasedAuth):
"""Service Account authentication class for Firebolt Database.
Gets authentication token using
provided credentials and updates it when it expires.
Args:
client_id (str): Client ID
client_secret (str): Client secret
use_token_cache (bool): True if token should be cached in filesystem;
False otherwise
Attributes:
client_id (str): Client ID
client_secret (str): Client secret
"""
__slots__ = (
"client_id",
"client_secret",
"_token",
"_expires",
"_use_token_cache",
"_user_agent",
)
def __init__(
self,
client_id: str,
client_secret: str,
use_token_cache: bool = True,
):
self.client_id = client_id
self.client_secret = client_secret
super().__init__(use_token_cache)
def copy(self) -> "ClientCredentials":
"""Make another auth object with same credentials.
Returns:
ClientCredentials: Auth object
"""
return ClientCredentials(
self.client_id, self.client_secret, self._use_token_cache
)
@property
def principal(self) -> str:
"""Get the principal (client id) associated with this auth.
Returns:
str: Principal client id
"""
return self.client_id
@property
def secret(self) -> str:
"""Get the secret (secret key) associated with this auth.
Returns:
str: Secret
"""
return self.client_secret
def get_firebolt_version(self) -> FireboltAuthVersion:
"""Get Firebolt version from auth.
Returns:
FireboltAuthVersion: V2 for Client Credentials authentication
"""
return FireboltAuthVersion.V2
def _make_auth_request(self) -> AuthRequest:
"""Get new token using username and password.
Yields:
Request: An http request to get token. Expects Response to be sent back
Raises:
AuthenticationError: Error while authenticating with provided credentials
"""
response = self.request_class(
"POST",
AUTH_SERVICE_ACCOUNT_URL,
headers={
"User-Agent": self._user_agent,
},
data={
"client_id": self.client_id,
"client_secret": self.client_secret,
"grant_type": "client_credentials",
"audience": "https://api.firebolt.io",
},
)
return response