-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from akatsoulas/refresh-token
Add a method to refresh the id_token.
- Loading branch information
Showing
6 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
Empty file.
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,18 @@ | ||
import requests | ||
|
||
from mozilla_django_oidc.utils import import_from_settings | ||
|
||
|
||
def refresh_id_token(id_token): | ||
"""Renews the id_token from the delegation endpoint in Auth0.""" | ||
delegation_url = 'https://{0}/delegation'.format(import_from_settings('OIDC_OP_DOMAIN')) | ||
data = { | ||
'client_id': import_from_settings('OIDC_RP_CLIENT_ID'), | ||
'grant_type': 'urn:ietf:params:oauth:grant-type:jwt-bearer', | ||
'id_token': id_token, | ||
'api_type': 'app' | ||
} | ||
|
||
response = requests.post(delegation_url, data=data) | ||
response.raise_for_status() | ||
return response.json().get('id_token') |
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
Empty file.
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,34 @@ | ||
import requests | ||
from mock import Mock, patch | ||
|
||
from django.test import TestCase, override_settings | ||
|
||
from mozilla_django_oidc.contrib.auth0.utils import refresh_id_token | ||
|
||
|
||
class Auth0UtilsTestCase(TestCase): | ||
"""Tests for the Auth0 utils.""" | ||
|
||
@override_settings(OIDC_RP_CLIENT_ID='client_id') | ||
@override_settings(OIDC_OP_DOMAIN='op_domain') | ||
@patch('mozilla_django_oidc.contrib.auth0.utils.requests.post') | ||
def test_successful_refresh_token(self, mock_post): | ||
"""Test a successful attempt for a refresh id_token.""" | ||
mock_response = Mock() | ||
mock_response.json.return_value = { | ||
'id_token': 'foobar' | ||
} | ||
mock_post.return_value = mock_response | ||
self.assertEqual(refresh_id_token('token'), 'foobar') | ||
|
||
@override_settings(OIDC_RP_CLIENT_ID='client_id') | ||
@override_settings(OIDC_OP_DOMAIN='op_domain') | ||
@patch('mozilla_django_oidc.contrib.auth0.utils.requests.post') | ||
def test_unsuccessful_attempt(self, mock_post): | ||
"""Test an attempt to get a refresh token that raises an error.""" | ||
mock_response = Mock() | ||
http_error = requests.exceptions.HTTPError() | ||
mock_response.raise_for_status.side_effect = http_error | ||
mock_post.return_value = mock_response | ||
with self.assertRaises(Exception): | ||
self.assertEqual(refresh_id_token('token'), None) |