Skip to content

Commit 0f60183

Browse files
itamartalItamar Talampcode-comaviadl
authored
feat: add selectedTenant to AccessKeyLoginOptions for access key exchange (#768)
Amp-Thread-ID: https://ampcode.com/threads/T-019c7af6-3c5a-709c-886e-e51681bc3d8a Co-authored-by: Itamar Tal <itamar.tal4@hotmail.com> Co-authored-by: Amp <amp@ampcode.com> Co-authored-by: Aviad Lichtenstadt <aviad@descope.com>
1 parent 51e286e commit 0f60183

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

descope/auth.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,11 @@ def exchange_access_key(
251251
) -> dict:
252252
uri = EndpointsV1.exchange_auth_access_key_path
253253
body = {
254-
"loginOptions": login_options.__dict__ if login_options else {},
254+
"loginOptions": {
255+
k: v for k, v in login_options.__dict__.items() if v is not None
256+
}
257+
if login_options
258+
else {},
255259
}
256260
server_response = self._http.post(uri, body=body, pswd=access_key)
257261
json_body = server_response.json()

descope/common.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,10 @@ class AccessKeyLoginOptions:
135135
def __init__(
136136
self,
137137
custom_claims: Optional[dict] = None,
138+
selected_tenant: Optional[str] = None,
138139
):
139140
self.customClaims = custom_claims
141+
self.selectedTenant = selected_tenant
140142

141143

142144
def validate_refresh_token_provided(

tests/test_auth.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,40 @@ def test_exchange_access_key(self):
451451
timeout=DEFAULT_TIMEOUT_SECONDS,
452452
)
453453

454+
# Test success flow with selected tenant
455+
with patch("requests.post") as mock_post:
456+
my_mock_response = mock.Mock()
457+
my_mock_response.ok = True
458+
data = {"sessionJwt": valid_jwt_token}
459+
my_mock_response.json.return_value = data
460+
mock_post.return_value = my_mock_response
461+
jwt_response = auth.exchange_access_key(
462+
access_key=dummy_access_key,
463+
login_options=AccessKeyLoginOptions(
464+
custom_claims={"k1": "v1"}, selected_tenant="t1"
465+
),
466+
)
467+
self.assertEqual(jwt_response["keyId"], "U2Cu0j0WPw3YOiPISJb52L0wUVMg")
468+
469+
mock_post.assert_called_with(
470+
f"{common.DEFAULT_BASE_URL}{EndpointsV1.exchange_auth_access_key_path}",
471+
headers={
472+
**common.default_headers,
473+
"Authorization": f"Bearer {self.dummy_project_id}:dummy access key",
474+
"x-descope-project-id": self.dummy_project_id,
475+
},
476+
params=None,
477+
json={
478+
"loginOptions": {
479+
"customClaims": {"k1": "v1"},
480+
"selectedTenant": "t1",
481+
}
482+
},
483+
allow_redirects=False,
484+
verify=True,
485+
timeout=DEFAULT_TIMEOUT_SECONDS,
486+
)
487+
454488
def test_exchange_token_success_and_empty_code(self):
455489
auth = Auth(
456490
self.dummy_project_id,

0 commit comments

Comments
 (0)