|
1 | 1 | import decimal
|
| 2 | +import authsignal |
2 | 3 | from authsignal.version import VERSION
|
3 | 4 |
|
4 | 5 | import humps
|
|
9 | 10 |
|
10 | 11 | _UNICODE_STRING = str
|
11 | 12 |
|
12 |
| -API_BASE_URL = 'https://signal.authsignal.com' |
13 |
| -API_CHALLENGE_URL = 'https://api.authsignal.com/v1' |
| 13 | +API_BASE_URL = 'https://api.authsignal.com/v1' |
14 | 14 |
|
15 | 15 | BLOCK = "BLOCK"
|
16 | 16 | ALLOW = "ALLOW"
|
@@ -52,8 +52,8 @@ def __init__(
|
52 | 52 | self.url = api_url
|
53 | 53 | self.timeout = timeout
|
54 | 54 | self.version = version
|
| 55 | + self.api_version = 'v1' |
55 | 56 |
|
56 |
| - |
57 | 57 | def track(self, user_id, action, payload=None, path=None):
|
58 | 58 | """Tracks an action to authsignal, scoped to the user_id and action
|
59 | 59 | Returns the status of the action so that you can determine to whether to continue
|
@@ -236,7 +236,7 @@ def enroll_verified_authenticator(self, user_id, authenticator_payload, path=No
|
236 | 236 | raise ApiException(str(e), path) from e
|
237 | 237 |
|
238 | 238 | def validate_challenge(self, token: str, user_id: Optional[str] = None, action: Optional[str] = None) -> Dict[str, Any]:
|
239 |
| - path = f"{API_CHALLENGE_URL}/validate" |
| 239 | + path = self._validate_challenge_url() |
240 | 240 | headers = {
|
241 | 241 | 'Content-Type': 'application/json',
|
242 | 242 | 'Accept': 'application/json'
|
@@ -267,20 +267,34 @@ def _default_headers(self):
|
267 | 267 | return {'Content-type': 'application/json',
|
268 | 268 | 'Accept': '*/*',
|
269 | 269 | 'User-Agent': self._user_agent()}
|
| 270 | + |
270 | 271 | def _user_agent(self):
|
271 | 272 | return f'Authsignal Python v{self.version}'
|
272 | 273 |
|
273 | 274 | def _track_url(self, user_id, action):
|
274 |
| - return f'{self.url}/v1/users/{user_id}/actions/{action}' |
275 |
| - |
| 275 | + path = self._ensure_versioned_path(f'/users/{user_id}/actions/{action}') |
| 276 | + return f'{self.url}{path}' |
| 277 | + |
276 | 278 | def _get_action_url(self, user_id, action, idempotency_key):
|
277 |
| - return f'{self.url}/v1/users/{user_id}/actions/{action}/{idempotency_key}' |
278 |
| - |
| 279 | + path = self._ensure_versioned_path(f'/users/{user_id}/actions/{action}/{idempotency_key}') |
| 280 | + return f'{self.url}{path}' |
| 281 | + |
279 | 282 | def _get_user_url(self, user_id):
|
280 |
| - return f'{self.url}/v1/users/{user_id}' |
| 283 | + path = self._ensure_versioned_path(f'/users/{user_id}') |
| 284 | + return f'{self.url}{path}' |
281 | 285 |
|
282 | 286 | def _post_enrollment_url(self, user_id):
|
283 |
| - return f'{self.url}/v1/users/{user_id}/authenticators' |
| 287 | + path = self._ensure_versioned_path(f'/users/{user_id}/authenticators') |
| 288 | + return f'{self.url}{path}' |
| 289 | + |
| 290 | + def _validate_challenge_url(self): |
| 291 | + path = self._ensure_versioned_path(f'/validate') |
| 292 | + return f'{self.url}{path}' |
| 293 | + |
| 294 | + def _ensure_versioned_path(self, path): |
| 295 | + if not self.url.endswith(f'/{self.api_version}'): |
| 296 | + return f'/{self.api_version}{path}' |
| 297 | + return path |
284 | 298 |
|
285 | 299 | class ApiException(Exception):
|
286 | 300 | def __init__(self, message, url, http_status_code=None, body=None, api_status=None,
|
@@ -311,4 +325,3 @@ def _assert_non_empty_dict(val, name):
|
311 | 325 | raise TypeError('{0} must be a non-empty dict'.format(name))
|
312 | 326 | elif not val:
|
313 | 327 | raise ValueError('{0} must be a non-empty dict'.format(name))
|
314 |
| - |
|
0 commit comments