The ADAL for python library makes it easy for python applications to authenticate to AAD in order to access AAD protected web resources.
To support 'service principal' with certificate, ADAL depends on the 'cryptography' package. For smooth installation, some suggestions:
- For Windows and OSX
Upgrade to the latest pip (8.1.2 as of June 2016) and just do pip install adal.
- For Linux
Upgrade to the latest pip (8.1.2 as of June 2016).
You'll need a C compiler, libffi + its development headers, and openssl + its development headers. Refer to cryptography installation
- To install from source:
Upgrade to the latest pip (8.1.2 as of June 2016).
Before run python setup.py install, to avoid dealing with compilation errors from cryptography, run pip install cryptography first to use statically-linked wheels.
If you still like build from source, refer to cryptography installation.
For more context, starts with this stackoverflow thread.
The convinient methods in 0.1.0 have been removed, and now your application should provide parameter values to client_id and resource.
2 Reasons:
-
Each adal client should have a unique id representing an valid application registered in a tenant. The old methods borrowed the client-id of azure-cli, which is never right. It is simple to register your application and get a client id. Many walkthroughs exist. You can follow one of those. Though that involves C# client, but the flow, and particularly the wizard snapshots are the same with adal-python. Do check out if you are new to AAD.
-
The old method defaults the
resourceargument to 'https://management.core.windows.net/', now you can just supply this value explictly. Please note, there are lots of different azure resources you can acquire tokens through adal though, for example, the samples in the repository acquire for the 'graph' resource. Because it is not an appropriate assumption to be made at the library level, we removed the old defaults.
In order to use this token acquisition method, you need to configure a service principal. Please follow this walkthrough.
See the sample.
import adal
context = adal.AuthenticationContext('https://login.microsoftonline.com/ABCDEFGH-1234-1234-1234-ABCDEFGHIJKL')
RESOURCE = '00000002-0000-0000-c000-000000000000' #AAD graph resource
token = context.acquire_token_with_client_credentials(
RESOURCE,
"http://PythonSDK",
"Key-Configured-In-Portal")A service principal is also required. See the sample.
import adal
context = adal.AuthenticationContext('https://login.microsoftonline.com/ABCDEFGH-1234-1234-1234-ABCDEFGHIJKL')
RESOURCE = '00000002-0000-0000-c000-000000000000' #AAD graph resource
token = context.acquire_token_with_client_certificate(
RESOURCE,
"http://PythonSDK",
'yourPrivateKeyFileContent',
'thumbprintOfPrivateKey')See the sample.
import adal
context = adal.AuthenticationContext('https://login.microsoftonline.com/ABCDEFGH-1234-1234-1234-ABCDEFGHIJKL')
RESOURCE = '00000002-0000-0000-c000-000000000000' #AAD graph resource
token = context.acquire_token_with_username_password(
RESOURCE,
'yourName',
'yourPassword',
'yourClientIdHere')
refresh_token = token['refreshToken']
token = context.acquire_token_with_refresh_token(
refresh_token,
'yourClientIdHere',
RESOURCE)See the sample.
context = adal.AuthenticationContext('https://login.microsoftonline.com/ABCDEFGH-1234-1234-1234-ABCDEFGHIJKL')
RESOURCE = '00000002-0000-0000-c000-000000000000' #AAD graph resource
code = context.acquire_user_code(RESOURCE, 'yourClientIdHere')
print(code['message'])
token = context.acquire_token_with_device_code(RESOURCE, code, 'yourClientIdHere')See the sample for a complete bare bones web site that makes use of the code below.
context = adal.AuthenticationContext('https://login.microsoftonline.com/ABCDEFGH-1234-1234-1234-ABCDEFGHIJKL')
RESOURCE = '00000002-0000-0000-c000-000000000000' #AAD graph resource
return auth_context.acquire_token_with_authorization_code(
'yourCodeFromQueryString',
'yourWebRedirectUri',
RESOURCE,
'yourClientId',
'yourClientSecret')We provide a full suite of sample applications and documentation on GitHub to help you get started with learning the Azure Identity system. This includes tutorials for native clients such as Windows, Windows Phone, iOS, OSX, Android, and Linux. We also provide full walkthroughs for authentication flows such as OAuth2, OpenID Connect, Graph API, and other awesome features.
We leverage Stack Overflow to work with the community on supporting Azure Active Directory and its SDKs, including this one! We highly recommend you ask your questions on Stack Overflow (we're all on there!) Also browser existing issues to see if someone has had your question before.
We recommend you use the "adal" tag so we can see it! Here is the latest Q&A on Stack Overflow for ADAL: http://stackoverflow.com/questions/tagged/adal
If you find a security issue with our libraries or services please report it to secure@microsoft.com with as much detail as possible. Your submission may be eligible for a bounty through the Microsoft Bounty program. Please do not post security issues to GitHub Issues or any other public site. We will contact you shortly upon receiving the information. We encourage you to get notifications of when security incidents occur by visiting this page and subscribing to Security Advisory Alerts.
All code is licensed under the MIT license and we triage actively on GitHub. We enthusiastically welcome contributions and feedback. You can clone the repo and start contributing now.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
$ pip install adal
If need to bypass self-signed certificates, turn on the environment variable of ADAL_PYTHON_SSL_NO_VERIFY