Skip to content

Commit

Permalink
Merge pull request #280 from jeff-sternberg/username_claim
Browse files Browse the repository at this point in the history
add configurable username claim
  • Loading branch information
minrk committed Aug 8, 2019
2 parents c41e63e + 2898b74 commit 158eb20
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
16 changes: 7 additions & 9 deletions oauthenticator/azuread.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,16 @@
import json
import jwt
import os
import re
import string
import urllib
import sys

from tornado.auth import OAuth2Mixin
from tornado.log import app_log
from tornado import web

from tornado.httputil import url_concat
from tornado.httpclient import HTTPRequest, AsyncHTTPClient

from jupyterhub.auth import LocalAuthenticator

from traitlets import List, Set, Unicode
from traitlets import Unicode, default

from .common import next_page_from_links
from .oauth2 import OAuthLoginHandler, OAuthenticator


Expand Down Expand Up @@ -51,6 +44,7 @@ class AzureAdOAuthenticator(OAuthenticator):
login_handler = AzureAdLoginHandler

tenant_id = Unicode(config=True)
username_claim = Unicode(config=True)

def get_tenant(self):
if hasattr(self, 'tenant_id') and self.tenant_id:
Expand All @@ -64,6 +58,10 @@ def get_tenant(self):
app_log.info('ID4: {0}'.format(tenant_id))
return tenant_id

@default('username_claim')
def _username_claim_default(self):
return 'name'

async def authenticate(self, handler, data=None):
code = handler.get_argument("code")
http_client = AsyncHTTPClient()
Expand Down Expand Up @@ -101,7 +99,7 @@ async def authenticate(self, handler, data=None):
id_token = resp_json['id_token']
decoded = jwt.decode(id_token, verify=False)

userdict = {"name": decoded['name']}
userdict = {"name": decoded[self.username_claim]}
userdict["auth_state"] = auth_state = {}
auth_state['access_token'] = access_token
# results in a decoded JWT for the user data
Expand Down
2 changes: 1 addition & 1 deletion oauthenticator/tests/test_azuread.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ def test_gettenant_with_tenant_id():

def test_gettenant_from_env():
t_id = AzureAdOAuthenticator.get_tenant(object)
assert t_id.default_value == "some_random_id"
assert t_id.default_value == "some_random_id"

0 comments on commit 158eb20

Please sign in to comment.