-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding Support For Back Channel Login (#643)
Adding Support For CIBA
- Loading branch information
1 parent
9a32645
commit 3d5df93
Showing
4 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from typing import Any | ||
|
||
from .base import AuthenticationBase | ||
|
||
|
||
class BackChannelLogin(AuthenticationBase): | ||
"""Back-Channel Login endpoint""" | ||
|
||
def back_channel_login( | ||
self, binding_message: str, login_hint: str, scope: str, **kwargs | ||
) -> Any: | ||
"""Send a Back-Channel Login. | ||
Args: | ||
binding_message (str): Human-readable string displayed on both the device calling /bc-authorize and the user’s | ||
authentication device to ensure the user is approves the correct request. | ||
login_hint (str): String containing information about the user to contact for authentication. | ||
scope(str): "openid" is a required scope.Multiple scopes are separated | ||
with whitespace. | ||
**kwargs: Other fields to send along with the PAR. | ||
Returns: | ||
auth_req_id, expires_in, interval | ||
""" | ||
return self.authenticated_post( | ||
f"{self.protocol}://{self.domain}/bc-authorize", | ||
data={ | ||
"client_id": self.client_id, | ||
"binding_message": binding_message, | ||
"login_hint": login_hint, | ||
"scope": scope, | ||
**kwargs, | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
|
||
import unittest | ||
from unittest import mock | ||
|
||
import requests | ||
from ...exceptions import Auth0Error, RateLimitError | ||
|
||
from ...authentication.back_channel_login import BackChannelLogin | ||
|
||
class TestBackChannelLogin(unittest.TestCase): | ||
@mock.patch("auth0.rest.RestClient.post") | ||
def test_ciba(self, mock_post): | ||
g = BackChannelLogin("my.domain.com", "cid", client_secret="clsec") | ||
|
||
g.back_channel_login( | ||
binding_message="This is a binding message", | ||
login_hint="{ \"format\": \"iss_sub\", \"iss\": \"https://my.domain.auth0.com/\", \"sub\": \"auth0|[USER ID]\" }", | ||
scope="openid", | ||
) | ||
|
||
args, kwargs = mock_post.call_args | ||
|
||
self.assertEqual(args[0], "https://my.domain.com/bc-authorize") | ||
self.assertEqual( | ||
kwargs["data"], | ||
{ | ||
"client_id": "cid", | ||
"client_secret": "clsec", | ||
"binding_message": "This is a binding message", | ||
"login_hint": "{ \"format\": \"iss_sub\", \"iss\": \"https://my.domain.auth0.com/\", \"sub\": \"auth0|[USER ID]\" }", | ||
"scope": "openid", | ||
}, | ||
) | ||
|
||
@mock.patch("auth0.rest.RestClient.post") | ||
def test_should_require_binding_message(self, mock_post): | ||
g = BackChannelLogin("my.domain.com", "cid", client_secret="clsec") | ||
|
||
# Expecting an exception to be raised when binding_message is missing | ||
with self.assertRaises(Exception) as context: | ||
g.back_channel_login( | ||
login_hint='{ "format": "iss_sub", "iss": "https://my.domain.auth0.com/", "sub": "auth0|USER_ID" }', | ||
scope="openid", | ||
) | ||
|
||
# Assert the error message is correct | ||
self.assertIn("missing 1 required positional argument: \'binding_message\'", str(context.exception)) | ||
|
||
@mock.patch("auth0.rest.RestClient.post") | ||
def test_should_require_login_hint(self, mock_post): | ||
g = BackChannelLogin("my.domain.com", "cid", client_secret="clsec") | ||
|
||
# Expecting an exception to be raised when login_hint is missing | ||
with self.assertRaises(Exception) as context: | ||
g.back_channel_login( | ||
binding_message="This is a binding message.", | ||
scope="openid", | ||
) | ||
|
||
# Assert the error message is correct | ||
self.assertIn("missing 1 required positional argument: \'login_hint\'", str(context.exception)) | ||
|
||
@mock.patch("auth0.rest.RestClient.post") | ||
def test_should_require_scope(self, mock_post): | ||
g = BackChannelLogin("my.domain.com", "cid", client_secret="clsec") | ||
|
||
# Expecting an exception to be raised when scope is missing | ||
with self.assertRaises(Exception) as context: | ||
g.back_channel_login( | ||
binding_message="This is a binding message.", | ||
login_hint='{ "format": "iss_sub", "iss": "https://my.domain.auth0.com/", "sub": "auth0|USER_ID" }', | ||
) | ||
|
||
# Assert the error message is correct | ||
self.assertIn("missing 1 required positional argument: \'scope\'", str(context.exception)) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters