Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use traitlet.default for Azure AD tenant_id #282

Merged
merged 1 commit into from
Nov 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions oauthenticator/azuread.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,9 @@ class AzureAdOAuthenticator(OAuthenticator):
tenant_id = Unicode(config=True)
username_claim = Unicode(config=True)

def get_tenant(self):
if hasattr(self, 'tenant_id') and self.tenant_id:
app_log.info('ID3: {0}'.format(self.tenant_id))
return self.tenant_id
else:
tenant_id = Unicode(
os.environ.get('AAD_TENANT_ID', ''),
config=True,
help="Tenant")
app_log.info('ID4: {0}'.format(tenant_id))
return tenant_id
@default('tenant_id')
def _tenant_id_default(self):
return os.environ.get('AAD_TENANT_ID', '')

@default('username_claim')
def _username_claim_default(self):
Expand All @@ -77,7 +69,7 @@ async def authenticate(self, handler, data=None):
data = urllib.parse.urlencode(
params, doseq=True, encoding='utf-8', safe='=')

url = azure_token_url_for(self.get_tenant())
url = azure_token_url_for(self.tenant_id)

headers = {
'Content-Type':
Expand Down
18 changes: 3 additions & 15 deletions oauthenticator/tests/test_azuread.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
from pytest import mark
from ..azuread import AzureAdOAuthenticator

_t_id = 'XXX-XXX-XXXX'


class Config(object):
tenant_id = _t_id


def test_gettenant_with_tenant_id():
t_id = AzureAdOAuthenticator.get_tenant(Config())
assert t_id == _t_id


import os
os.environ["AAD_TENANT_ID"] = "some_random_id"


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