Skip to content

Commit 472930b

Browse files
authored
Merge pull request #116270 from rayluo/patch-2
Update migration sample based on latest MSAL 1.3.0
2 parents 907cbd0 + f2dc466 commit 472930b

File tree

1 file changed

+40
-21
lines changed

1 file changed

+40
-21
lines changed

articles/active-directory/develop/migrate-python-adal-msal.md

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -75,31 +75,50 @@ The Microsoft authentication library (MSAL) abstracts the concept of refresh tok
7575

7676
The following code will help you migrate your refresh tokens managed by another OAuth2 library (including but not limited to ADAL Python) to be managed by MSAL for Python. One reason for migrating those refresh tokens is to prevent existing users from needing to sign in again when you migrate your app to MSAL for Python.
7777

78-
The method for migrating a refresh token is to use MSAL for Python to acquire a new access token using the previous refresh token. When the new refresh token is returned, MSAL for Python will store it in the cache. Here is an example of how to do it:
78+
The method for migrating a refresh token is to use MSAL for Python to acquire a new access token using the previous refresh token. When the new refresh token is returned, MSAL for Python will store it in the cache.
79+
Since MSAL Python 1.3.0, we provide an API inside MSAL for this purpose.
80+
Please refer to the following code snippet, quoted from
81+
[a completed sample of migrating refresh tokens with MSAL Python](https://github.com/AzureAD/microsoft-authentication-library-for-python/blob/1.3.0/sample/migrate_rt.py#L28-L67)
7982

8083
```python
81-
from msal import PublicClientApplication
82-
83-
def get_preexisting_rt_and_their_scopes_from_elsewhere(...):
84-
raise NotImplementedError("You will need to implement this by yourself")
85-
86-
app = PublicClientApplication(..., token_cache=...)
87-
88-
for old_rt, old_scope in get_preexisting_rt_and_their_scopes_from_elsewhere(...):
89-
# Assuming the old scope could be a space-delimited string.
90-
# MSAL expects a list, like ["scope1", "scope2"].
91-
scopes = old_scope.split()
92-
# If your old refresh token came from ADAL for Python, which uses a resource rather than a scope,
93-
# you need to convert your v1 resource into v2 scopes
94-
# See https://docs.microsoft.com/azure/active-directory/develop/azure-ad-endpoint-comparison#scopes-not-resources
95-
# You may be able to append "/.default" to your v1 resource to form a scope
96-
# See https://docs.microsoft.com/azure/active-directory/develop/v2-permissions-and-consent#the-default-scope
97-
98-
result = app.client.obtain_token_by_refresh_token(old_rt, scope=scopes)
99-
# When this call returns the new token(s), a new refresh token is issued by the Microsoft identity platform and MSAL for Python
100-
# stores it in the token cache.
84+
import msal
85+
def get_preexisting_rt_and_their_scopes_from_elsewhere():
86+
# Maybe you have an ADAL-powered app like this
87+
# https://github.com/AzureAD/azure-activedirectory-library-for-python/blob/1.2.3/sample/device_code_sample.py#L72
88+
# which uses a resource rather than a scope,
89+
# you need to convert your v1 resource into v2 scopes
90+
# See https://docs.microsoft.com/azure/active-directory/develop/azure-ad-endpoint-comparison#scopes-not-resources
91+
# You may be able to append "/.default" to your v1 resource to form a scope
92+
# See https://docs.microsoft.com/azure/active-directory/develop/v2-permissions-and-consent#the-default-scope
93+
94+
# Or maybe you have an app already talking to Microsoft identity platform v2,
95+
# powered by some 3rd-party auth library, and persist its tokens somehow.
96+
97+
# Either way, you need to extract RTs from there, and return them like this.
98+
return [
99+
("old_rt_1", ["scope1", "scope2"]),
100+
("old_rt_2", ["scope3", "scope4"]),
101+
]
102+
103+
104+
# We will migrate all the old RTs into a new app powered by MSAL
105+
app = msal.PublicClientApplication(
106+
"client_id", authority="...",
107+
# token_cache=... # Default cache is in memory only.
108+
# You can learn how to use SerializableTokenCache from
109+
# https://msal-python.rtfd.io/en/latest/#msal.SerializableTokenCache
110+
)
111+
112+
# We choose a migration strategy of migrating all RTs in one loop
113+
for old_rt, scopes in get_preexisting_rt_and_their_scopes_from_elsewhere():
114+
result = app.acquire_token_by_refresh_token(old_rt, scopes)
115+
if "error" in result:
116+
print("Discarding unsuccessful RT. Error: ", json.dumps(result, indent=2))
117+
118+
print("Migration completed")
101119
```
102120

121+
103122
## Next steps
104123

105124
For more information, refer to [v1.0 and v2.0 comparison](active-directory-v2-compare.md).

0 commit comments

Comments
 (0)