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
Show file tree
Hide file tree
Changes from 5 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
39 changes: 37 additions & 2 deletions msrestazure/azure_active_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

ConnectionError
)
import requests_oauthlib as oauth

from msrest.authentication import OAuthTokenAuthentication
from msrest.authentication import (
OAuthTokenAuthentication,
Authentication
)
from msrest.exceptions import TokenExpiredError as Expired
from msrest.exceptions import (
AuthenticationError,
Expand Down Expand Up @@ -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 #.

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 __init__(self, adal_method, *args, **kwargs):
self._adal_method = adal_method
self._args = args
self._kwargs = kwargs

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.


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.

except adal.AdalError as err:
#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.")

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.

raise AuthenticationError(err)
except ConnectionError as err:
raise AuthenticationError('Please ensure you have network connection. Error detail: ' + str(err))

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.

1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
msrest>=0.4.4,<0.5.0
adal~=0.4.0
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,7 @@
'License :: OSI Approved :: MIT License',
'Topic :: Software Development'],
install_requires=[
"msrest>=0.4.4"],
"msrest~=0.4.4",
"adal~=0.4.0"
],
)
33 changes: 32 additions & 1 deletion test/unittest_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,22 @@

from requests_oauthlib import OAuth2Session
import oauthlib
import adal

from msrestazure import AzureConfiguration
from msrestazure import azure_active_directory
from msrestazure.azure_active_directory import (
AADMixin,
InteractiveCredentials,
ServicePrincipalCredentials,
UserPassCredentials
UserPassCredentials,
AdalAuthentication
)
from msrest.exceptions import (
TokenExpiredError,
AuthenticationError,
)
from requests import ConnectionError


class TestInteractiveCredentials(unittest.TestCase):
Expand Down Expand Up @@ -356,6 +359,34 @@ def test_user_pass_credentials(self):
client_id="client_id", username='my_username',
password='my_password', resource='https://management.core.chinacloudapi.cn/', verify=False)

def test_adal_authentication(self):
def success_auth():
return {
'tokenType': 'https',
'accessToken': 'cryptictoken'
}

credentials = AdalAuthentication(success_auth)
session = credentials.signed_session()
self.assertEquals(session.headers['Authorization'], 'https cryptictoken')

def error():
raise adal.AdalError("You hacker", {})
credentials = AdalAuthentication(error)
with self.assertRaises(AuthenticationError) as cm:
session = credentials.signed_session()

def expired():
raise adal.AdalError("Too late", {'error_description': "AADSTS70008: Expired"})
credentials = AdalAuthentication(expired)
with self.assertRaises(TokenExpiredError) as cm:
session = credentials.signed_session()

def connection_error():
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