Skip to content

Commit 852025a

Browse files
committed
Add a method to refresh the id_token.
1 parent dc68193 commit 852025a

File tree

6 files changed

+53
-0
lines changed

6 files changed

+53
-0
lines changed

mozilla_django_oidc/contrib/__init__.py

Whitespace-only changes.

mozilla_django_oidc/contrib/auth0/__init__.py

Whitespace-only changes.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import requests
2+
3+
from mozilla_django_oidc.utils import import_from_settings
4+
5+
6+
def refresh_id_token(id_token):
7+
"""Renews the id_token from the delegation endpoint in Auth0."""
8+
delegation_url = 'https://{0}/delegation'.format(import_from_settings('OIDC_OP_DOMAIN'))
9+
data = {
10+
'client_id': import_from_settings('OIDC_RP_CLIENT_ID'),
11+
'grant_type': 'urn:ietf:params:oauth:grant-type:jwt-bearer',
12+
'id_token': id_token,
13+
'api_type': 'app'
14+
}
15+
16+
response = requests.post(delegation_url, data=data)
17+
response.raise_for_status()
18+
return response.json().get('id_token')

mozilla_django_oidc/views.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ class OIDCLogoutView(View):
109109

110110
@property
111111
def redirect_url(self):
112+
"""Return the logout url defined in settings."""
112113
return import_from_settings('LOGOUT_REDIRECT_URL', '/')
113114

114115
def get(self, request):

tests/auth0_tests/__init__.py

Whitespace-only changes.

tests/auth0_tests/test_utils.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import requests
2+
from mock import Mock, patch
3+
4+
from django.test import TestCase, override_settings
5+
6+
from mozilla_django_oidc.contrib.auth0.utils import refresh_id_token
7+
8+
9+
class Auth0UtilsTestCase(TestCase):
10+
"""Tests for the Auth0 utils."""
11+
12+
@override_settings(OIDC_RP_CLIENT_ID='client_id')
13+
@override_settings(OIDC_OP_DOMAIN='op_domain')
14+
@patch('mozilla_django_oidc.contrib.auth0.utils.requests.post')
15+
def test_successful_refresh_token(self, mock_post):
16+
"""Test a successful attempt for a refresh id_token."""
17+
mock_response = Mock()
18+
mock_response.json.return_value = {
19+
'id_token': 'foobar'
20+
}
21+
mock_post.return_value = mock_response
22+
self.assertEqual(refresh_id_token('token'), 'foobar')
23+
24+
@override_settings(OIDC_RP_CLIENT_ID='client_id')
25+
@override_settings(OIDC_OP_DOMAIN='op_domain')
26+
@patch('mozilla_django_oidc.contrib.auth0.utils.requests.post')
27+
def test_unsuccessful_attempt(self, mock_post):
28+
"""Test an attempt to get a refresh token that raises an error."""
29+
mock_response = Mock()
30+
http_error = requests.exceptions.HTTPError()
31+
mock_response.raise_for_status.side_effect = http_error
32+
mock_post.return_value = mock_response
33+
with self.assertRaises(Exception):
34+
self.assertEqual(refresh_id_token('token'), None)

0 commit comments

Comments
 (0)