-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcredentials.py
204 lines (177 loc) · 5.65 KB
/
credentials.py
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
"""
Python SDK for OpenFGA
API version: 1.x
Website: https://openfga.dev
Documentation: https://openfga.dev/docs
Support: https://openfga.dev/community
License: [Apache-2.0](https://github.com/openfga/python-sdk/blob/main/LICENSE)
NOTE: This file was auto generated by OpenAPI Generator (https://openapi-generator.tech). DO NOT EDIT.
"""
from urllib.parse import urlparse
from openfga_sdk.exceptions import ApiValueError
def none_or_empty(value):
"""
Return true if value is either none or empty string
"""
return value is None or value == ""
class CredentialConfiguration:
"""
Configuration for SDK credential
:param client_id: Client ID which will be matched with client_secret
:param client_secret: Client secret which will be matched with client_id
:param api_token: Bearer token to be sent for authentication
:param api_audience: API audience used for OAuth2
:param api_issuer: API issuer used for OAuth2
"""
def __init__(
self,
client_id: str | None = None,
client_secret: str | None = None,
api_audience: str | None = None,
api_issuer: str | None = None,
api_token: str | None = None,
):
self._client_id = client_id
self._client_secret = client_secret
self._api_audience = api_audience
self._api_issuer = api_issuer
self._api_token = api_token
@property
def client_id(self):
"""
Return the client id configured
"""
return self._client_id
@client_id.setter
def client_id(self, value):
"""
Update the client id
"""
self._client_id = value
@property
def client_secret(self):
"""
Return the client secret configured
"""
return self._client_secret
@client_secret.setter
def client_secret(self, value):
"""
Update the client secret
"""
self._client_secret = value
@property
def api_audience(self):
"""
Return the api audience configured
"""
return self._api_audience
@api_audience.setter
def api_audience(self, value):
"""
Update the api audience
"""
self._api_audience = value
@property
def api_issuer(self):
"""
Return the api issuer configured
"""
return self._api_issuer
@api_issuer.setter
def api_issuer(self, value):
"""
Update the api issuer
"""
self._api_issuer = value
@property
def api_token(self):
"""
Return the api token configured
"""
return self._api_token
@api_token.setter
def api_token(self, value):
"""
Update the api token
"""
self._api_token = value
class Credentials:
"""
Manage the credential for the API Client
:param method: Type of authentication. Possible value is 'none', 'api_token' and 'client_credentials'. Default as 'none'.
:param configuration: Credential configuration of type CredentialConfiguration. Default as None.
"""
def __init__(
self,
method: str | None = "none",
configuration: CredentialConfiguration | None = None,
):
self._method = method
self._configuration = configuration
@property
def method(self):
"""
Return the method configured
"""
return self._method
@method.setter
def method(self, value):
"""
Update the method
"""
self._method = value
@property
def configuration(self):
"""
Return the configuration
"""
return self._configuration
@configuration.setter
def configuration(self, value):
"""
Update the configuration
"""
self._configuration = value
def validate_credentials_config(self):
"""
Check whether credentials configuration is valid
"""
if (
self.method != "none"
and self.method != "api_token"
and self.method != "client_credentials"
):
raise ApiValueError(
f"method `{self.method}` must be either `none`, `api_token` or `client_credentials`"
)
if self.method == "api_token" and (
self.configuration is None or none_or_empty(self.configuration.api_token)
):
raise ApiValueError(
f"configuration `{self.configuration}` api_token must be defined and non empty when method is api_token"
)
if self.method == "client_credentials":
if (
self.configuration is None
or none_or_empty(self.configuration.client_id)
or none_or_empty(self.configuration.client_secret)
or none_or_empty(self.configuration.api_audience)
or none_or_empty(self.configuration.api_issuer)
):
raise ApiValueError(
"configuration `{}` requires client_id, client_secret, api_audience and api_issuer defined for client_credentials method."
)
# validate token issuer
combined_url = "https://" + self.configuration.api_issuer
parsed_url = None
try:
parsed_url = urlparse(combined_url)
except ValueError:
raise ApiValueError(
f"api_issuer `{self.configuration.api_issuer}` is invalid"
)
if parsed_url.netloc == "":
raise ApiValueError(
f"api_issuer `{self.configuration.api_issuer}` is invalid"
)