Skip to content
This repository has been archived by the owner on Apr 10, 2024. It is now read-only.

Adding ADAL wrapper to msrestazure #8

Merged
merged 9 commits into from
Nov 30, 2016
Merged

Adding ADAL wrapper to msrestazure #8

merged 9 commits into from
Nov 30, 2016

Conversation

lmazuel
Copy link
Member

@lmazuel lmazuel commented Nov 29, 2016

After discussion with @begoldsm, this allows people other than us (Python team or CLI team) to connect using ADAL, without forcing on ADAL features.

Samples:

from msrestazure.azure_active_directory import AdalAuthentication
import adal

context = adal.AuthenticationContext('https://login.microsoftonline.com/common')
token_retriver = lambda: context.acquire_token_with_username_password(
    'https://management.core.windows.net/',
    'unittest@myaccountoutlook.onmicrosoft.com',
    'PASSWORD!!!',
    '04b07795-8ddb-461a-bbee-02f9e1bf7b46' # Xplat CLI or yours
)
return AdalAuthentication(token_retriver)

This is a 99% copy/paste from CLI code, I almost didn't code anything here, just backport the code from @yugangw-msft. I didn't include ADAL in install_requires for now on purpose, I think it's more interesting to do it in extra_requires. And be clear in the documentation. Also the main advantage here is that we don't own any ADAL features, so any new stuff can be used directly without changing msrestazure.

@annatisch @brettcannon what do you think?

@lmazuel
Copy link
Member Author

lmazuel commented Nov 29, 2016

@brettcannon also suggests:

from msrestazure.azure_active_directory import AdalAuthentication
import adal

context = adal.AuthenticationContext('https://login.microsoftonline.com/common')
return AdalAuthentication(
    context.acquire_token_with_username_password,
    'https://management.core.windows.net/',
    'unittest@myaccountoutlook.onmicrosoft.com',
    'PASSWORD!!!',
    '04b07795-8ddb-461a-bbee-02f9e1bf7b46' # Xplat CLI or yours
)

IOW, use args/kwargs to avoid the lambda syntax

@lmazuel
Copy link
Member Author

lmazuel commented Nov 29, 2016

Adding support for @brettcannon syntax. Now both syntax (lambda + args/kwargs) are supported the same, depending of the coders habits.

#pylint: disable=no-member
if (hasattr(err, 'error_response') and ('error_description' in err.error_response)
and ('AADSTS70008:' in err.error_response['error_description'])):
raise Expired("Credentials have expired due to inactivity. Please run 'az login'")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider changing this error message, since this is not the CLI and they can't run az login.

@begoldsm
Copy link

:shipit:

@@ -39,10 +39,16 @@
MismatchingStateError,
OAuth2Error,
TokenExpiredError)
from requests import RequestException
from requests import (
RequestException,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need to make this span multiple lines. Same goes for other imports.

@@ -525,3 +531,32 @@ def set_token(self, response_url):
raise_with_traceback(AuthenticationError, "", err)
else:
self.token = token

class AdalAuthentication(Authentication):#pylint: disable=too-few-public-methods
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two spaces between : and #.

@@ -525,3 +531,32 @@ def set_token(self, response_url):
raise_with_traceback(AuthenticationError, "", err)
else:
self.token = token

class AdalAuthentication(Authentication):#pylint: disable=too-few-public-methods
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably a docstring that explains what the class is about. Same goes for methods.

def signed_session(self):
session = super(AdalAuthentication, self).signed_session()

import adal # Adal is not mandatory
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you make adal mandatory then this should move to the top of the file.

if (hasattr(err, 'error_response') and ('error_description' in err.error_response)
and ('AADSTS70008:' in err.error_response['error_description'])):
raise Expired("Credentials have expired due to inactivity.")

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace the blank line with an else: clause.


header = "{} {}".format(scheme, token)
session.headers['Authorization'] = header
return session
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing a newline at the end of the file.


try:
raw_token = self._adal_method(*self._args, **self._kwargs)
scheme, token = raw_token['tokenType'], raw_token['accessToken']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be moved outside of the try block as it won't trigger an AdalError.

@lmazuel
Copy link
Member Author

lmazuel commented Nov 30, 2016

@brettcannon I think I made the changes
@annatisch nothing to say?

@annatisch
Copy link
Member

@lmazuel - not really, it's nice and simple :).
Unless you want to hook in the auto-token-refreshing?

raise ConnectionError("Plug the network")
credentials = AdalAuthentication(connection_error)
with self.assertRaises(AuthenticationError) as cm:
session = credentials.signed_session()

if __name__ == '__main__':
unittest.main()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing a newline at the end of the file.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done this one



class AdalAuthentication(Authentication): # pylint: disable=too-few-public-methods
"""A wrapper to use ADAL for Python easily to authenticate on Azure.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put the closing """ on the same line and have a newline between it and __init__().

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done too

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

General question: should I PEP8 or/and flake8 or/and PyLint or/and mypy?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants