-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
198 additions
and
57 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
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
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
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 |
---|---|---|
@@ -1,24 +1,24 @@ | ||
from authlib.integrations.django_client import OAuth | ||
from authlib.oidc.discovery import get_well_known_url | ||
from django.conf import settings | ||
from nens_auth_client.cognito import CognitoOAuthClient | ||
from django.utils.module_loading import import_string | ||
|
||
# Create the global OAuth registry | ||
oauth_registry = OAuth() | ||
|
||
|
||
def get_oauth_client(): | ||
client = oauth_registry.create_client("cognito") | ||
client = oauth_registry.create_client("oauth") | ||
if client is not None: | ||
return client | ||
|
||
url = get_well_known_url(settings.NENS_AUTH_ISSUER, external=True) | ||
oauth_registry.register( | ||
name="cognito", | ||
name="oauth", | ||
client_id=settings.NENS_AUTH_CLIENT_ID, | ||
client_secret=settings.NENS_AUTH_CLIENT_SECRET, | ||
server_metadata_url=url, | ||
client_kwargs={"scope": " ".join(settings.NENS_AUTH_SCOPE)}, | ||
client_cls=CognitoOAuthClient, | ||
client_cls=import_string(settings.NENS_AUTH_OAUTH_BACKEND), | ||
) | ||
return oauth_registry.create_client("cognito") | ||
return oauth_registry.create_client("oauth") |
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
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from nens_auth_client.cognito import CognitoOAuthClient | ||
from nens_auth_client.cognito import preprocess_access_token | ||
|
||
import pytest | ||
|
@@ -20,3 +21,34 @@ def test_preprocess_access_token(claims, expected, settings): | |
settings.NENS_AUTH_RESOURCE_SERVER_ID = "api/" | ||
preprocess_access_token(claims) | ||
assert claims == expected | ||
|
||
|
||
def test_extract_provider_name_present(): | ||
# Extract provider name when it is present. | ||
claims = {"identities": [{"providerName": "Google"}]} | ||
assert CognitoOAuthClient.extract_provider_name(claims) == "Google" | ||
|
||
|
||
def test_extract_provider_name_absent(): | ||
# Return None when a provider name cannot be found. | ||
claims = {"some": "claim"} | ||
assert CognitoOAuthClient.extract_provider_name(claims) is None | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"claims,expected", | ||
[ | ||
({"cognito:username": "foo", "email": "[email protected]"}, "foo"), | ||
({"cognito:username": "foo", "email": "[email protected]", "identities": []}, "foo"), | ||
( | ||
{ | ||
"cognito:username": "abc123", | ||
"email": "[email protected]", | ||
"identities": ["something"], | ||
}, | ||
"[email protected]", | ||
), | ||
], | ||
) | ||
def test_extract_username(claims, expected): | ||
assert CognitoOAuthClient.extract_username(claims) == expected |
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
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,27 @@ | ||
from nens_auth_client.wso2 import WSO2AuthClient | ||
|
||
import pytest | ||
|
||
|
||
def test_extract_provider_name(): | ||
# Extract provider name when it is present. | ||
claims = {"sub": "abc123"} | ||
assert WSO2AuthClient.extract_provider_name(claims) is None | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"claims,expected", | ||
[ | ||
({"email": "[email protected]"}, "[email protected]"), | ||
], | ||
) | ||
def test_extract_username(claims, expected): | ||
assert WSO2AuthClient.extract_username(claims) == expected | ||
|
||
|
||
def test_parse_access_token_includes_claims(access_token_generator): | ||
with pytest.raises(NotImplementedError) as e: | ||
WSO2AuthClient.parse_access_token(None, access_token_generator()) | ||
|
||
# error is raised with claims as arg | ||
assert e.value.args[0]["client_id"] == "1234" |
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
Oops, something went wrong.