|
| 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 | + python sample.py parameters.json |
| 17 | + On the browser open http://localhost:5000/ |
| 18 | +
|
| 19 | +""" |
| 20 | + |
| 21 | +import sys # For simplicity, we'll read config file from 1st CLI param sys.argv[1] |
| 22 | +import json |
| 23 | +import logging |
| 24 | +import uuid |
| 25 | + |
| 26 | +import flask |
| 27 | + |
| 28 | +import msal |
| 29 | + |
| 30 | +app = flask.Flask(__name__) |
| 31 | +app.debug = True |
| 32 | +app.secret_key = 'development' |
| 33 | + |
| 34 | + |
| 35 | +# Optional logging |
| 36 | +# logging.basicConfig(level=logging.DEBUG) |
| 37 | + |
| 38 | +config = json.load(open(sys.argv[1])) |
| 39 | + |
| 40 | +application = msal.ConfidentialClientApplication( |
| 41 | + config["client_id"], authority=config["authority"], |
| 42 | + client_credential=config["client_secret"], |
| 43 | + # token_cache=... # Default cache is in memory only. |
| 44 | + # You can learn how to use SerializableTokenCache from |
| 45 | + # https://msal-python.rtfd.io/en/latest/#msal.SerializableTokenCache |
| 46 | + ) |
| 47 | + |
| 48 | + |
| 49 | +@app.route("/") |
| 50 | +def main(): |
| 51 | + resp = flask.Response(status=307) |
| 52 | + resp.headers['location'] = '/login' |
| 53 | + return resp |
| 54 | + |
| 55 | + |
| 56 | +@app.route("/login") |
| 57 | +def login(): |
| 58 | + auth_state = str(uuid.uuid4()) |
| 59 | + flask.session['state'] = auth_state |
| 60 | + authorization_url = application.get_authorization_request_url(config['scope'], state=auth_state, |
| 61 | + redirect_uri=config['redirect_uri']) |
| 62 | + resp = flask.Response(status=307) |
| 63 | + resp.headers['location'] = authorization_url |
| 64 | + return resp |
| 65 | + |
| 66 | + |
| 67 | +@app.route("/getAToken") |
| 68 | +def main_logic(): |
| 69 | + code = flask.request.args['code'] |
| 70 | + state = flask.request.args['state'] |
| 71 | + if state != flask.session['state']: |
| 72 | + raise ValueError("State does not match") |
| 73 | + |
| 74 | + result = application.acquire_token_by_authorization_code(code, scopes=config["scope"], |
| 75 | + redirect_uri=config['redirect_uri']) |
| 76 | + return flask.render_template('display.html', auth_result=result) |
| 77 | + |
| 78 | + |
| 79 | +if __name__ == "__main__": |
| 80 | + app.run() |
0 commit comments