Skip to content

Commit e2e00ce

Browse files
committed
Add exchange access key function
1 parent 50a8495 commit e2e00ce

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

descope/auth.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,22 @@ def refresh_token(self, refresh_token: str) -> dict:
222222
resp = response.json()
223223
return self._generate_auth_info(resp, refresh_token)
224224

225+
def exchange_access_key(self, access_key: str) -> dict:
226+
uri = Auth._compose_exchange_access_key_url()
227+
server_response = self.do_get(uri, None, None, access_key)
228+
229+
json = server_response.json()
230+
response = {
231+
"keyId": json.get("keyId", ""),
232+
"exp": json.get("expiration", 0),
233+
}
234+
235+
jwt = json.get("sessionJwt", "")
236+
if jwt:
237+
response[SESSION_TOKEN_NAME] = self._validate_token(jwt)
238+
239+
return response
240+
225241
@staticmethod
226242
def _compose_exchange_params(code: str) -> dict:
227243
return {"code": code}
@@ -460,3 +476,7 @@ def _validate_and_load_tokens(self, session_token: str, refresh_token: str) -> d
460476
@staticmethod
461477
def _compose_refresh_token_url() -> str:
462478
return EndpointsV1.refreshTokenPath
479+
480+
@staticmethod
481+
def _compose_exchange_access_key_url() -> str:
482+
return EndpointsV1.exchangeAuthAccessKeyPath

descope/common.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ class EndpointsV1:
2020
logoutPath = "/v1/auth/logoutall"
2121
mePath = "/v1/auth/me"
2222

23+
# accesskey
24+
exchangeAuthAccessKeyPath = "/v1/auth/accesskey/exchange"
25+
2326
# otp
2427
signUpAuthOTPPath = "/v1/auth/otp/signup"
2528
signInAuthOTPPath = "/v1/auth/otp/signin"

descope/descope_client.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,18 @@ def refresh_token(self, refresh_token: str) -> dict:
145145
AuthException: Exception is raised if session is not authorized or another error occurs
146146
"""
147147
return self._auth.refresh_token(refresh_token)
148+
149+
def exchange_access_key(self, access_key: str) -> dict:
150+
"""
151+
Return a new session token for the given access key
152+
153+
Args:
154+
access_key (str): The access key
155+
156+
Return value (dict): returns the session token from the server together with the expiry and key id
157+
(sessionToken:dict, keyId:str, expiration:int)
158+
159+
Raise:
160+
AuthException: Exception is raised if access key is not valid or another error occurs
161+
"""
162+
return self._auth.exchange_access_key(access_key)

tests/test_auth.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,19 @@ def test_refresh_token(self):
239239
dummy_refresh_token,
240240
)
241241

242+
def test_exchange_access_key(self):
243+
dummy_access_key = "dummy access key"
244+
auth = Auth(self.dummy_project_id, self.public_key_dict)
245+
246+
# Test fail flow
247+
with patch("requests.get") as mock_request:
248+
mock_request.return_value.ok = False
249+
self.assertRaises(
250+
AuthException,
251+
auth.exchange_access_key,
252+
dummy_access_key,
253+
)
254+
242255

243256
if __name__ == "__main__":
244257
unittest.main()

0 commit comments

Comments
 (0)