-
Notifications
You must be signed in to change notification settings - Fork 205
acquire_token_by_refresh_token() for RT migration #193
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
""" | ||
The configuration file would look like this: | ||
|
||
{ | ||
"authority": "https://login.microsoftonline.com/organizations", | ||
"client_id": "your_client_id", | ||
"scope": ["User.ReadBasic.All"], | ||
// You can find the other permission names from this document | ||
// https://docs.microsoft.com/en-us/graph/permissions-reference | ||
} | ||
|
||
You can then run this sample with a JSON configuration file: | ||
|
||
python sample.py parameters.json | ||
""" | ||
|
||
import sys # For simplicity, we'll read config file from 1st CLI param sys.argv[1] | ||
import json | ||
import logging | ||
|
||
import msal | ||
|
||
|
||
# Optional logging | ||
# logging.basicConfig(level=logging.DEBUG) # Enable DEBUG log for entire script | ||
# logging.getLogger("msal").setLevel(logging.INFO) # Optionally disable MSAL DEBUG logs | ||
|
||
def get_preexisting_rt_and_their_scopes_from_elsewhere(): | ||
# Maybe you have an ADAL-powered app like this | ||
# https://github.com/AzureAD/azure-activedirectory-library-for-python/blob/1.2.3/sample/device_code_sample.py#L72 | ||
# which uses a resource rather than a scope, | ||
# you need to convert your v1 resource into v2 scopes | ||
# See https://docs.microsoft.com/azure/active-directory/develop/azure-ad-endpoint-comparison#scopes-not-resources | ||
# You may be able to append "/.default" to your v1 resource to form a scope | ||
# See https://docs.microsoft.com/azure/active-directory/develop/v2-permissions-and-consent#the-default-scope | ||
|
||
# Or maybe you have an app already talking to Microsoft identity platform v2, | ||
# powered by some 3rd-party auth library, and persist its tokens somehow. | ||
|
||
# Either way, you need to extract RTs from there, and return them like this. | ||
return [ | ||
("old_rt_1", ["scope1", "scope2"]), | ||
("old_rt_2", ["scope3", "scope4"]), | ||
] | ||
|
||
|
||
# We will migrate all the old RTs into a new app powered by MSAL | ||
config = json.load(open(sys.argv[1])) | ||
app = msal.PublicClientApplication( | ||
config["client_id"], authority=config["authority"], | ||
# token_cache=... # Default cache is in memory only. | ||
# You can learn how to use SerializableTokenCache from | ||
# https://msal-python.rtfd.io/en/latest/#msal.SerializableTokenCache | ||
) | ||
|
||
# We choose a migration strategy of migrating all RTs in one loop | ||
for old_rt, scopes in get_preexisting_rt_and_their_scopes_from_elsewhere(): | ||
result = app.acquire_token_by_refresh_token(old_rt, scopes) | ||
if "error" in result: | ||
print("Discarding unsuccessful RT. Error: ", json.dumps(result, indent=2)) | ||
|
||
print("Migration completed") | ||
|
||
# From now on, those successfully-migrated RTs are saved inside MSAL's cache, | ||
# and becomes available in normal MSAL coding pattern, which is NOT part of migration. | ||
# You can refer to: | ||
# https://github.com/AzureAD/microsoft-authentication-library-for-python/blob/1.2.0/sample/device_flow_sample.py#L42-L60 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.