Skip to content

Commit ab45902

Browse files
Add support for federated connection token exchange
1 parent 8262ce4 commit ab45902

File tree

2 files changed

+82
-3
lines changed

2 files changed

+82
-3
lines changed

auth0/authentication/get_token.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,6 @@ def refresh_token(
195195
196196
Args:
197197
refresh_token (str): The refresh token returned from the initial token request.
198-
199198
scope (str): Use this to limit the scopes of the new access token.
200199
Multiple scopes are separated with whitespace.
201200
@@ -236,7 +235,6 @@ def passwordless_login(
236235
Multiple scopes are separated with whitespace.
237236
238237
audience (str): The unique identifier of the target API you want to access.
239-
240238
Returns:
241239
access_token, id_token
242240
"""
@@ -277,3 +275,39 @@ def backchannel_login(
277275
"grant_type": grant_type,
278276
},
279277
)
278+
279+
def federated_connection_token(
280+
self,
281+
refresh_token: str,
282+
connection: str,
283+
login_hint: str | None = None,
284+
) -> Any:
285+
"""Calls oauth/token endpoint with token-exchange:federated-connection-access-token grant type
286+
287+
Args:
288+
refresh_token (str): The refresh token returned from the initial token request.
289+
290+
connection (str): The name of the connection to use.
291+
292+
login_hint (str, optional): The login hint to use.
293+
294+
Returns:
295+
access_token, expires_at, scope
296+
"""
297+
298+
data = {
299+
"client_id": self.client_id,
300+
"grant_type": "urn:auth0:params:oauth:grant-type:token-exchange:federated-connection-access-token",
301+
"connection": connection,
302+
"subject_token": refresh_token,
303+
"subject_token_type": "urn:ietf:oauth:token-type:refresh_token",
304+
"requested_token_type": "http://auth0.com/oauth/token-type/federated-connection-access-token",
305+
}
306+
307+
if login_hint:
308+
data["login_hint"] = login_hint
309+
310+
return self.authenticated_post(
311+
f"{self.protocol}://{self.domain}/oauth/token",
312+
data=data,
313+
)

auth0/test/authentication/test_get_token.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,4 +334,49 @@ def test_backchannel_login(self, mock_post):
334334
"auth_req_id": "reqid",
335335
"grant_type": "urn:openid:params:grant-type:ciba",
336336
},
337-
)
337+
)
338+
339+
@mock.patch("auth0.rest.RestClient.post")
340+
def test_federated_connection_token(self, mock_post):
341+
342+
343+
g = GetToken("my.domain.com", "<client_id>", client_secret="<client_secret>")
344+
345+
g.federated_connection_token(refresh_token='<refresh_token>', connection='<connection_name>')
346+
347+
args, kwargs = mock_post.call_args
348+
349+
self.assertEqual(args[0], "https://my.domain.com/oauth/token")
350+
self.assertEqual(
351+
kwargs["data"],
352+
{
353+
"client_id": "<client_id>",
354+
"client_secret": "<client_secret>",
355+
"grant_type": "urn:auth0:params:oauth:grant-type:token-exchange:federated-connection-access-token",
356+
"connection": "<connection_name>",
357+
"subject_token": "<refresh_token>",
358+
"subject_token_type": "urn:ietf:oauth:token-type:refresh_token",
359+
"requested_token_type": "http://auth0.com/oauth/token-type/federated-connection-access-token",
360+
}
361+
)
362+
363+
364+
# Get a new federated connection access token with a login hint
365+
g.federated_connection_token(refresh_token='<refresh_token>', connection='<connection_name>', login_hint='<login_hint>')
366+
367+
args, kwargs = mock_post.call_args
368+
369+
self.assertEqual(args[0], "https://my.domain.com/oauth/token")
370+
self.assertEqual(
371+
kwargs["data"],
372+
{
373+
"client_id": "<client_id>",
374+
"client_secret": "<client_secret>",
375+
"grant_type": "urn:auth0:params:oauth:grant-type:token-exchange:federated-connection-access-token",
376+
"connection": "<connection_name>",
377+
"subject_token": "<refresh_token>",
378+
"subject_token_type": "urn:ietf:oauth:token-type:refresh_token",
379+
"requested_token_type": "http://auth0.com/oauth/token-type/federated-connection-access-token",
380+
'login_hint': '<login_hint>',
381+
}
382+
)

0 commit comments

Comments
 (0)