Skip to content

Commit 842268d

Browse files
authored
chore: optimize the Python sample (#1269)
Signed-off-by: Ray Luo <[email protected]> Signed-off-by: Monis Khan <[email protected]>
1 parent 302da23 commit 842268d

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

examples/msal-python/__init__.py

Whitespace-only changes.
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
azure-keyvault-secrets==4.7.0
2-
msal==1.22.0
2+
# MSAL's acquire_token_for_client() comes with built-in cache since 1.23
3+
# And it is safe to upgrade to any new versions in 1.x series
4+
msal>=1.23,<2

examples/msal-python/token_credential.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,21 @@
88
class MyClientAssertionCredential(object):
99

1010
def __init__(self, azure_client_id, azure_tenant_id, azure_authority_host, azure_federated_token_file):
11-
# read the projected service account token file
12-
f = open(azure_federated_token_file, 'rb')
11+
self.azure_federated_token_file = azure_federated_token_file
1312
# create a confidential client application
1413
self.app = ConfidentialClientApplication(
1514
azure_client_id,
1615
client_credential={
17-
'client_assertion': f.read().decode("utf-8")
16+
'client_assertion': self.read_federation_token, # A callable will be lazily called, whenever a new token is needed
1817
},
1918
authority="{}{}".format(azure_authority_host, azure_tenant_id)
2019
)
2120

21+
def read_federation_token(self):
22+
# read the projected service account token file
23+
with open(self.azure_federated_token_file, 'rb') as f:
24+
return f.read().decode("utf-8")
25+
2226
def get_token(self, *scopes, **kwargs):
2327
# get the token using the application
2428
token = self.app.acquire_token_for_client(list(scopes))

0 commit comments

Comments
 (0)