|
1 | | -""" |
2 | | -The configuration file would look like this: |
3 | | -
|
4 | | -{ |
5 | | - "authority": "https://login.microsoftonline.com/organizations", |
6 | | - "client_id": "your_client_id", |
7 | | - "scope": ["https://graph.microsoft.com/.default"], |
8 | | - "redirect_uri": "http://localhost:5000/getAToken", |
9 | | - // Configure this redirect uri for this sample |
10 | | - // redirect_uri should match what you've configured in here |
11 | | - // https://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-configure-app-access-web-apis#add-redirect-uris-to-your-application |
12 | | - "client_secret": "yoursecret" |
13 | | -} |
14 | | -
|
15 | | -You can then run this sample with a JSON configuration file: |
16 | | -
|
17 | | - python sample.py parameters.json your_flask_session_secret_here |
18 | | -
|
19 | | -And the on the browser open http://localhost:5000/ |
20 | | -
|
21 | | -""" |
22 | | - |
23 | | -import sys # For simplicity, we'll read config file from 1st CLI param sys.argv[1] |
24 | | -import json |
25 | | -import logging |
26 | | -import uuid |
27 | | -import os |
28 | | - |
29 | | -import flask |
30 | | - |
31 | | -import msal |
32 | | - |
33 | | -app = flask.Flask(__name__) |
34 | | -app.debug = True |
35 | | -app.secret_key = os.environ.get("FLASK_SECRET") |
36 | | -assert app.secret_key, "This sample requires a FLASK_SECRET env var to enable session" |
37 | | - |
38 | | - |
39 | | -# Optional logging |
40 | | -# logging.basicConfig(level=logging.DEBUG) |
41 | | - |
42 | | -config = json.load(open(sys.argv[1])) |
43 | | - |
44 | | -application = msal.ConfidentialClientApplication( |
45 | | - config["client_id"], authority=config["authority"], |
46 | | - client_credential=config["client_secret"], |
47 | | - # token_cache=... # Default cache is in memory only. |
48 | | - # You can learn how to use SerializableTokenCache from |
49 | | - # https://msal-python.rtfd.io/en/latest/#msal.SerializableTokenCache |
50 | | - ) |
51 | | - |
52 | | - |
53 | | -@app.route("/") |
54 | | -def main(): |
55 | | - resp = flask.Response(status=307) |
56 | | - resp.headers['location'] = '/login' |
57 | | - return resp |
58 | | - |
59 | | - |
60 | | -@app.route("/login") |
61 | | -def login(): |
62 | | - auth_state = str(uuid.uuid4()) |
63 | | - flask.session['state'] = auth_state |
64 | | - authorization_url = application.get_authorization_request_url(config['scope'], state=auth_state, |
65 | | - redirect_uri=config['redirect_uri']) |
66 | | - resp = flask.Response(status=307) |
67 | | - resp.headers['location'] = authorization_url |
68 | | - return resp |
69 | | - |
70 | | - |
71 | | -@app.route("/getAToken") |
72 | | -def main_logic(): |
73 | | - code = flask.request.args['code'] |
74 | | - state = flask.request.args['state'] |
75 | | - if state != flask.session['state']: |
76 | | - raise ValueError("State does not match") |
77 | | - |
78 | | - result = application.acquire_token_by_authorization_code(code, scopes=config["scope"], |
79 | | - redirect_uri=config['redirect_uri']) |
80 | | - return flask.render_template('display.html', auth_result=result) |
81 | | - |
82 | | - |
83 | | -if __name__ == "__main__": |
84 | | - app.run() |
| 1 | +# We have moved! |
| 2 | +# |
| 3 | +# Please visit https://github.com/Azure-Samples/ms-identity-python-webapp |
0 commit comments