Commit 4d08379b by Matt Drayer

Merge pull request #12451 from edx/mattdrayer/microste-jwt-auth

mattdrayer/microsite-jwt-auth: Access setting through theming/microsites
parents adb9200e 55cf0fb2
...@@ -45,7 +45,7 @@ class EdxRestApiClientTest(TestCase): ...@@ -45,7 +45,7 @@ class EdxRestApiClientTest(TestCase):
@httpretty.activate @httpretty.activate
@freeze_time('2015-7-2') @freeze_time('2015-7-2')
@override_settings(JWT_ISSUER='http://example.com/oauth', JWT_EXPIRATION=30) @override_settings(JWT_AUTH={'JWT_ISSUER': 'http://example.com/oauth', 'JWT_EXPIRATION': 30})
def test_tracking_context(self): def test_tracking_context(self):
""" """
Ensure the tracking context is set up in the api client correctly and Ensure the tracking context is set up in the api client correctly and
...@@ -71,8 +71,8 @@ class EdxRestApiClientTest(TestCase): ...@@ -71,8 +71,8 @@ class EdxRestApiClientTest(TestCase):
'username': self.user.username, 'username': self.user.username,
'full_name': self.user.profile.name, 'full_name': self.user.profile.name,
'email': self.user.email, 'email': self.user.email,
'iss': settings.JWT_ISSUER, 'iss': settings.JWT_AUTH['JWT_ISSUER'],
'exp': datetime.datetime.utcnow() + datetime.timedelta(seconds=settings.JWT_EXPIRATION), 'exp': datetime.datetime.utcnow() + datetime.timedelta(seconds=settings.JWT_AUTH['JWT_EXPIRATION']),
'tracking_context': { 'tracking_context': {
'lms_user_id': self.user.id, # pylint: disable=no-member 'lms_user_id': self.user.id, # pylint: disable=no-member
'lms_client_id': self.TEST_CLIENT_ID, 'lms_client_id': self.TEST_CLIENT_ID,
......
...@@ -16,6 +16,8 @@ from django.views.generic import View ...@@ -16,6 +16,8 @@ from django.views.generic import View
from edx_oauth2_provider import views as dop_views # django-oauth2-provider views from edx_oauth2_provider import views as dop_views # django-oauth2-provider views
from oauth2_provider import models as dot_models, views as dot_views # django-oauth-toolkit from oauth2_provider import models as dot_models, views as dot_views # django-oauth-toolkit
from openedx.core.djangoapps.theming import helpers
from . import adapters from . import adapters
...@@ -121,10 +123,10 @@ class AccessTokenView(_DispatchingView): ...@@ -121,10 +123,10 @@ class AccessTokenView(_DispatchingView):
def _generate_jwt(self, user, scopes, expires_in): def _generate_jwt(self, user, scopes, expires_in):
""" Returns a JWT access token. """ """ Returns a JWT access token. """
now = int(time()) now = int(time())
jwt_auth = helpers.get_value("JWT_AUTH", settings.JWT_AUTH)
payload = { payload = {
'iss': settings.JWT_AUTH['JWT_ISSUER'], 'iss': jwt_auth['JWT_ISSUER'],
'aud': settings.JWT_AUTH['JWT_AUDIENCE'], 'aud': jwt_auth['JWT_AUDIENCE'],
'exp': now + expires_in, 'exp': now + expires_in,
'iat': now, 'iat': now,
'preferred_username': user.username, 'preferred_username': user.username,
...@@ -136,8 +138,8 @@ class AccessTokenView(_DispatchingView): ...@@ -136,8 +138,8 @@ class AccessTokenView(_DispatchingView):
if handler: if handler:
handler(payload, user) handler(payload, user)
secret = settings.JWT_AUTH['JWT_SECRET_KEY'] secret = jwt_auth['JWT_SECRET_KEY']
token = jwt.encode(payload, secret, algorithm=settings.JWT_AUTH['JWT_ALGORITHM']) token = jwt.encode(payload, secret, algorithm=jwt_auth['JWT_ALGORITHM'])
return token return token
......
...@@ -766,8 +766,6 @@ LTI_AGGREGATE_SCORE_PASSBACK_DELAY = ENV_TOKENS.get( ...@@ -766,8 +766,6 @@ LTI_AGGREGATE_SCORE_PASSBACK_DELAY = ENV_TOKENS.get(
CREDIT_HELP_LINK_URL = ENV_TOKENS.get('CREDIT_HELP_LINK_URL', CREDIT_HELP_LINK_URL) CREDIT_HELP_LINK_URL = ENV_TOKENS.get('CREDIT_HELP_LINK_URL', CREDIT_HELP_LINK_URL)
#### JWT configuration #### #### JWT configuration ####
JWT_ISSUER = ENV_TOKENS.get('JWT_ISSUER', JWT_ISSUER)
JWT_EXPIRATION = ENV_TOKENS.get('JWT_EXPIRATION', JWT_EXPIRATION)
JWT_AUTH.update(ENV_TOKENS.get('JWT_AUTH', {})) JWT_AUTH.update(ENV_TOKENS.get('JWT_AUTH', {}))
PUBLIC_RSA_KEY = ENV_TOKENS.get('PUBLIC_RSA_KEY', PUBLIC_RSA_KEY) PUBLIC_RSA_KEY = ENV_TOKENS.get('PUBLIC_RSA_KEY', PUBLIC_RSA_KEY)
PRIVATE_RSA_KEY = ENV_TOKENS.get('PRIVATE_RSA_KEY', PRIVATE_RSA_KEY) PRIVATE_RSA_KEY = ENV_TOKENS.get('PRIVATE_RSA_KEY', PRIVATE_RSA_KEY)
......
...@@ -2141,6 +2141,8 @@ JWT_AUTH = { ...@@ -2141,6 +2141,8 @@ JWT_AUTH = {
'JWT_PAYLOAD_GET_USERNAME_HANDLER': lambda d: d.get('username'), 'JWT_PAYLOAD_GET_USERNAME_HANDLER': lambda d: d.get('username'),
'JWT_LEEWAY': 1, 'JWT_LEEWAY': 1,
'JWT_DECODE_HANDLER': 'edx_rest_framework_extensions.utils.jwt_decode_handler', 'JWT_DECODE_HANDLER': 'edx_rest_framework_extensions.utils.jwt_decode_handler',
# Number of seconds before JWT tokens expire
'JWT_EXPIRATION': 30,
} }
# The footer URLs dictionary maps social footer names # The footer URLs dictionary maps social footer names
...@@ -2793,9 +2795,6 @@ LTI_USER_EMAIL_DOMAIN = 'lti.example.com' ...@@ -2793,9 +2795,6 @@ LTI_USER_EMAIL_DOMAIN = 'lti.example.com'
# The time value is in seconds. # The time value is in seconds.
LTI_AGGREGATE_SCORE_PASSBACK_DELAY = 15 * 60 LTI_AGGREGATE_SCORE_PASSBACK_DELAY = 15 * 60
# Number of seconds before JWT tokens expire
JWT_EXPIRATION = 30
JWT_ISSUER = None
# For help generating a key pair import and run `openedx.core.lib.rsa_key_utils.generate_rsa_key_pair()` # For help generating a key pair import and run `openedx.core.lib.rsa_key_utils.generate_rsa_key_pair()`
PUBLIC_RSA_KEY = None PUBLIC_RSA_KEY = None
......
...@@ -32,6 +32,7 @@ def is_commerce_service_configured(): ...@@ -32,6 +32,7 @@ def is_commerce_service_configured():
def ecommerce_api_client(user): def ecommerce_api_client(user):
""" Returns an E-Commerce API client setup with authentication for the specified user. """ """ Returns an E-Commerce API client setup with authentication for the specified user. """
jwt_auth = helpers.get_value("JWT_AUTH", settings.JWT_AUTH)
return EdxRestApiClient( return EdxRestApiClient(
helpers.get_value("ECOMMERCE_API_URL", settings.ECOMMERCE_API_URL), helpers.get_value("ECOMMERCE_API_URL", settings.ECOMMERCE_API_URL),
helpers.get_value("ECOMMERCE_API_SIGNING_KEY", settings.ECOMMERCE_API_SIGNING_KEY), helpers.get_value("ECOMMERCE_API_SIGNING_KEY", settings.ECOMMERCE_API_SIGNING_KEY),
...@@ -39,6 +40,6 @@ def ecommerce_api_client(user): ...@@ -39,6 +40,6 @@ def ecommerce_api_client(user):
user.profile.name if hasattr(user, 'profile') else None, user.profile.name if hasattr(user, 'profile') else None,
user.email, user.email,
tracking_context=create_tracking_context(user), tracking_context=create_tracking_context(user),
issuer=settings.JWT_ISSUER, issuer=jwt_auth['JWT_ISSUER'],
expires_in=settings.JWT_EXPIRATION expires_in=jwt_auth['JWT_EXPIRATION']
) )
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment