-
Notifications
You must be signed in to change notification settings - Fork 207
Authorization code flow sample #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d09bcfb
Adding sample for certificate credential flow
abhidnya13 22ff09f
Merge branch 'dev' of https://github.com/AzureAD/microsoft-authentica…
abhidnya13 3dab324
Adding links to wiki page and minor changes
abhidnya13 0469f01
Adding authorization code flow sample
abhidnya13 99be8c8
Merge branch 'dev' of https://github.com/AzureAD/microsoft-authentica…
abhidnya13 a3d0eed
Changes in variable names and deleting extra files
abhidnya13 62752cf
Adding comments
abhidnya13 c7de03f
Addressing PR comments
abhidnya13 4ab2d2b
Addressing PR comments
abhidnya13 9cb6562
Adding links to documentation
abhidnya13 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Adding authorization code flow sample
- Loading branch information
commit 0469f012aa2fd74c1019e14c1d2cb1009bf5db0d
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
sample/authorization-code-flow-sample/authorization_code_flow_sample.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| """ | ||
| The configuration file would look like this: | ||
|
|
||
| { | ||
| "authority": "https://login.microsoftonline.com/organizations", | ||
| "client_id": "your_client_id", | ||
| "scope": ["https://graph.microsoft.com/.default"] | ||
| "client_secret": "yoursecret" | ||
| } | ||
|
|
||
| You can then run this sample with a JSON configuration file: | ||
|
|
||
| python sample.py parameters.json | ||
| """ | ||
|
|
||
| import sys # For simplicity, we'll read config file from 1st CLI param sys.argv[1] | ||
| import json | ||
| import logging | ||
| import uuid | ||
|
|
||
| import flask as flask | ||
|
|
||
| import msal | ||
|
|
||
| app = flask.Flask(__name__) | ||
| app.debug = True | ||
| app.secret_key = 'development' | ||
|
|
||
|
|
||
| # Optional logging | ||
| # logging.basicConfig(level=logging.DEBUG) | ||
|
|
||
| config = json.load(open(sys.argv[1])) | ||
|
|
||
| application = msal.ConfidentialClientApplication( | ||
| config["client_id"], authority=config["authority"], | ||
| client_credential=config["client_secret"], | ||
| # token_cache=... # Default cache is in memory only. | ||
| # You can learn how to use SerializableTokenCache from | ||
| # https://msal-python.rtfd.io/en/latest/#msal.SerializableTokenCache | ||
| ) | ||
|
|
||
|
|
||
| @app.route("/") | ||
| def main(): | ||
| login_url = 'http://localhost:5000/login' | ||
rayluo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| resp = flask.Response(status=307) | ||
| resp.headers['location'] = login_url | ||
| return resp | ||
|
|
||
|
|
||
| @app.route("/login") | ||
| def login(): | ||
| auth_state = str(uuid.uuid4()) | ||
| flask.session['state'] = auth_state | ||
| authorization_url = application.get_authorization_request_url(config['scope'], state=auth_state, | ||
| redirect_uri=config['redirect_uri']) | ||
| resp = flask.Response(status=307) | ||
| resp.headers['location'] = authorization_url | ||
| return resp | ||
|
|
||
|
|
||
| @app.route("/getAToken") | ||
| def main_logic(): | ||
| code = flask.request.args['code'] | ||
| state = flask.request.args['state'] | ||
| if state != flask.session['state']: | ||
| raise ValueError("State does not match") | ||
|
|
||
| result = application.acquire_token_silent(config["scope"], account=None) | ||
|
|
||
| if not result: | ||
| logging.info("No suitable token exists in cache. Let's get a new one from AAD.") | ||
| result = application.acquire_token_by_authorization_code(code, scopes=config["scope"], | ||
| redirect_uri=config['redirect_uri']) | ||
| return flask.render_template('display.html', graph_data=result) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| app.run() | ||
19 changes: 19 additions & 0 deletions
19
sample/authorization-code-flow-sample/templates/display.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <title>Here is the data from Microsoft Graph !!</title> | ||
| </head> | ||
| <body> | ||
| <p1><b>User Data:</b> </p1> | ||
| <table> | ||
| {% for key, value in graph_data.items() %} | ||
| <tr> | ||
| <th> {{ key }} </th> | ||
| <td> {{ value }} </td> | ||
| </tr> | ||
| {% endfor %} | ||
| </table> | ||
|
|
||
| </body> | ||
| </html> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.