-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
58 lines (42 loc) · 1.79 KB
/
app.py
File metadata and controls
58 lines (42 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from flask import Flask
from msal import PublicClientApplication
import os
class PythonMSAL:
def __init__(self, client_id, authority, username, password, scopes):
self.client_id = client_id
self.authority = authority
self.username = username
self.password = password
self.scopes = scopes
def msal_connect(self):
app = PublicClientApplication(self.client_id, authority=self.authority)
result = None
accounts = app.get_accounts()
if accounts:
# If so, you could then somehow display these accounts and let end user choose
print("Pick the account you want to use to proceed:")
for a in accounts:
print(a["username"])
# Assuming the end user chose this one
chosen = accounts[0]
# Now let's try to find a token in cache for this account
result = app.acquire_token_silent(self.scopes, account=chosen)
if not result:
# So no suitable token exists in cache. Let's get a new one from AAD.
result = app.acquire_token_by_username_password(self.username, self.password, scopes=self.scopes)
return result
flapp = Flask(__name__)
@flapp.route("/")
def test_connection():
msal_obj = PythonMSAL(os.environ['CLIENT_ID'],
os.environ['AUTHORITY'],
os.environ['USERNAME'],
os.environ['PASSWORD'],
["user.read"])
result = msal_obj.msal_connect()
if "access_token" in result:
return result["access_token"]
else:
return result.get("error") + " " + \
result.get("error_description") + " " + \
result.get("correlation_id")