Skip to content

Commit b527015

Browse files
Delegate validate challenge logic to backend
1 parent 28e60a0 commit b527015

File tree

3 files changed

+25
-24
lines changed

3 files changed

+25
-24
lines changed

authsignal/client.py

+23-22
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
from authsignal.version import VERSION
44

55
import humps
6+
from typing import Dict, Any, Optional
67
import json
78
import requests
9+
810
_UNICODE_STRING = str
911

1012
API_BASE_URL = 'https://signal.authsignal.com'
13+
API_CHALLENGE_URL = 'https://api.authsignal.com/v1'
1114

1215
BLOCK = "BLOCK"
1316
ALLOW = "ALLOW"
@@ -175,31 +178,29 @@ def enroll_verified_authenticator(self, user_id, authenticator_payload, path=No
175178
except requests.exceptions.RequestException as e:
176179
raise ApiException(str(e), path) from e
177180

178-
def validate_challenge(self, token, user_id=None):
179-
try:
180-
decoded_token = jwt.decode(token, self.api_key, algorithms=["HS256"], options={'verify_aud': False})
181-
182-
except jwt.DecodeError as e:
183-
print(e)
184-
return
185-
186-
decoded_user_id = decoded_token["other"]["userId"]
187-
action = decoded_token["other"]["actionCode"]
188-
idempotency_key = decoded_token["other"]["idempotencyKey"]
181+
def validate_challenge(self, token: str, user_id: Optional[str] = None) -> Dict[str, Any]:
182+
path = f"{API_CHALLENGE_URL}/validate"
183+
headers = {
184+
'Content-Type': 'application/json',
185+
'Accept': 'application/json'
186+
}
189187

190-
if user_id and user_id != decoded_user_id:
191-
return {"user_id": decoded_user_id, "success": False, "state": None}
192-
193-
if action and idempotency_key:
194-
action_result = self.get_action(user_id=decoded_user_id, action=action, idempotency_key=idempotency_key)
195-
196-
if action_result:
197-
state = action_result["state"]
198-
success = state == "CHALLENGE_SUCCEEDED"
188+
try:
189+
response = self.session.post(
190+
path,
191+
auth=requests.auth.HTTPBasicAuth(self.api_key, ''),
192+
data=json.dumps({'token': token, 'userId': user_id}),
193+
headers=headers,
194+
timeout=self.timeout
195+
)
196+
197+
response_data = humps.decamelize(response.json())
199198

200-
return {"user_id": decoded_user_id, "success": success, "state": state, "action": action}
199+
action = response_data.pop('action_code', None)
201200

202-
return {"userId": decoded_user_id, "success": False, "state": None}
201+
return {'action': action, **response_data}
202+
except requests.exceptions.RequestException as e:
203+
raise ApiException(str(e), path) from e
203204

204205
def _default_headers(self):
205206
return {'Content-type': 'application/json',

authsignal/version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
VERSION = '1.0.4'
1+
VERSION = '2.0.0'

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "authsignal"
3-
version = "1.0.4"
3+
version = "2.0.0"
44
description = "Authsignal Python SDK for Passwordless Step Up Authentication"
55
authors = ["justinsoong <[email protected]>"]
66
license = "MIT"

0 commit comments

Comments
 (0)