Skip to content

Commit

Permalink
Merge pull request #59 from akatsoulas/refresh-token
Browse files Browse the repository at this point in the history
Add a method to refresh the id_token.
  • Loading branch information
akatsoulas committed Nov 22, 2016
2 parents 28cab17 + 852025a commit f12c1f3
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 0 deletions.
Empty file.
Empty file.
18 changes: 18 additions & 0 deletions mozilla_django_oidc/contrib/auth0/utils.py
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')
1 change: 1 addition & 0 deletions mozilla_django_oidc/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ class OIDCLogoutView(View):

@property
def redirect_url(self):
"""Return the logout url defined in settings."""
return import_from_settings('LOGOUT_REDIRECT_URL', '/')

def get(self, request):
Expand Down
Empty file added tests/auth0_tests/__init__.py
Empty file.
34 changes: 34 additions & 0 deletions tests/auth0_tests/test_utils.py
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)

0 comments on commit f12c1f3

Please sign in to comment.