Skip to content

Commit abc26ab

Browse files
committed
Add user information, logout and session managment
1 parent 9dac3d7 commit abc26ab

9 files changed

Lines changed: 98 additions & 23 deletions

File tree

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
.DS_Store
1+
.DS_Store
2+
*.iml
3+
.idea
4+
.env
5+
__pycache__

01-Login/.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.env
12
.env.example
23
.gitignore
34
.git

01-Login/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM python:3-alpine
1+
FROM python:3
22

33
WORKDIR /home/app
44

01-Login/README.md

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,41 @@ This sample demonstrates how to add authentication to a Python web app using Aut
66

77
To run the sample, make sure you have `python`, `pip` installed.
88

9-
Rename `.env.example` to `.env` and populate it with the client ID, domain, secret, callback URL and audience for your Auth0 app. If you are not implementing any API you can use `https://YOUR_DOMAIN.auth0.com/userinfo` as the audience. Also, add the callback URL to the settings section of your Auth0 client.
9+
Rename `.env.example` to `.env` and populate it with the client ID, domain, secret, callback URL and audience for your
10+
Auth0 app. If you are not implementing any API you can use `https://YOUR_DOMAIN.auth0.com/userinfo` as the audience.
11+
Also, add the callback URL to the settings section of your Auth0 client.
1012

11-
Register `http://localhost:3000/callback` as `Allowed Callback URLs` and `http://localhost:3000` as `Allowed Logout URLs` in your app settings.
13+
Register `http://localhost:3000/callback` as `Allowed Callback URLs` and `http://localhost:3000`
14+
as `Allowed Logout URLs` in your app settings.
1215

13-
Run `pip install -r requirements.txt` to install the dependencies and run `python server.py`. The app will be served at [http://localhost:3000/](http://localhost:3000/).
16+
Run `pip install -r requirements.txt` to install the dependencies and run `python server.py`.
17+
The app will be served at [http://localhost:3000/](http://localhost:3000/).
1418

1519
# Running the App with Docker
1620

1721
To run the sample, make sure you have `docker` installed.
1822

19-
Rename `.env.example` to `.env` and populate it with the client ID, domain, secret, callback URL and audience for your Auth0 app. If you are not implementing any API you can use `https://YOUR_DOMAIN.auth0.com/userinfo` as the audience. Also, add the callback URL to the settings section of your Auth0 client.
23+
Rename `.env.example` to `.env` and populate it with the client ID, domain, secret, callback URL and audience for your
24+
Auth0 app. If you are not implementing any API you can use `https://YOUR_DOMAIN.auth0.com/userinfo` as the audience.
25+
Also, add the callback URL to the settings section of your Auth0 client.
2026

21-
Register `http://localhost:3000/callback` as `Allowed Callback URLs` and `http://localhost:3000` as `Allowed Logout URLs` in your app settings.
27+
Register `http://localhost:3000/callback` as `Allowed Callback URLs` and `http://localhost:3000`
28+
as `Allowed Logout URLs` in your app settings.
2229

23-
Run `sh exec.sh` to build and run the docker image in Linux or run `.\exec.ps1` to build and run the docker image on Windows.
30+
Run `sh exec.sh` to build and run the docker image in Linux or run `.\exec.ps1` to build
31+
and run the docker image on Windows.
2432

2533
## What is Auth0?
2634

2735
Auth0 helps you to:
2836

29-
* Add authentication with [multiple authentication sources](https://docs.auth0.com/identityproviders), either social like **Google, Facebook, Microsoft Account, LinkedIn, GitHub, Twitter, Box, Salesforce, among others**, or enterprise identity systems like **Windows Azure AD, Google Apps, Active Directory, ADFS or any SAML Identity Provider**.
37+
* Add authentication with [multiple authentication sources](https://docs.auth0.com/identityproviders),
38+
either social like **Google, Facebook, Microsoft Account, LinkedIn, GitHub, Twitter, Box, Salesforce, among others**,or
39+
enterprise identity systems like **Windows Azure AD, Google Apps, Active Directory, ADFS or any SAML Identity Provider**.
3040
* Add authentication through more traditional **[username/password databases](https://docs.auth0.com/mysql-connection-tutorial)**.
3141
* Add support for **[linking different user accounts](https://docs.auth0.com/link-accounts)** with the same user.
32-
* Support for generating signed [JSON Web Tokens](https://docs.auth0.com/jwt) to call your APIs and **flow the user identity** securely.
42+
* Support for generating signed [JSON Web Tokens](https://docs.auth0.com/jwt) to call your APIs and
43+
**flow the user identity** securely.
3344
* Analytics of how, when and where users are logging in.
3445
* Pull data from other sources and add it to the user profile, through [JavaScript rules](https://docs.auth0.com/rules).
3546

@@ -40,12 +51,14 @@ Auth0 helps you to:
4051

4152
## Issue Reporting
4253

43-
If you have found a bug or if you have a feature request, please report them at this repository issues section. Please do not report security vulnerabilities on the public GitHub issue tracker. The [Responsible Disclosure Program](https://auth0.com/whitehat) details the procedure for disclosing security issues.
54+
If you have found a bug or if you have a feature request, please report them at this repository issues section.
55+
Please do not report security vulnerabilities on the public GitHub issue tracker.
56+
The [Responsible Disclosure Program](https://auth0.com/whitehat) details the procedure for disclosing security issues.
4457

4558
## Author
4659

4760
[Auth0](https://auth0.com)
4861

4962
## License
5063

51-
This project is licensed under the MIT license. See the [LICENSE](https://opensource.org/licenses/MIT) file for more info.
64+
This project is licensed under the MIT license. See the [LICENSE](LICENCE) file for more info.

01-Login/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@
1717
PROFILE_KEY = 'profile'
1818
REDIRECT_URI_KEY = 'redirect_uri'
1919
SECRET_KEY = 'ThisIsTheSecretKey'
20+
JWT_PAYLOAD = 'jwt_payload'
2021

01-Login/public/app.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ body {
44
font-size: 300%;
55
font-weight: 100;
66
}
7+
pre {
8+
text-align: left;
9+
}
710
input[type=checkbox],
811
input[type=radio] {
912
position: absolute;

01-Login/requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
flask
22
python-dotenv
33
requests
4-
flask-oauthlib
4+
flask-oauthlib
5+
python-jose

01-Login/server.py

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
11
"""Python Flask WebApp Auth0 integration example
22
"""
33
from os import environ as env
4+
from jose import jwt
45
from dotenv import load_dotenv, find_dotenv
6+
from six.moves.urllib.request import urlopen
57
from flask import Flask
68
from flask import render_template
79
from flask import request
10+
from flask import session
11+
from flask import redirect
812
from flask_oauthlib.client import OAuth
13+
from functools import wraps
14+
from six.moves.urllib.parse import urlencode
15+
from flask import url_for
16+
import json
917

1018
import constants
1119

12-
load_dotenv(find_dotenv())
13-
AUTH0_CALLBACK_URL = env[constants.AUTH0_CALLBACK_URL]
14-
AUTH0_CLIENT_ID = env[constants.AUTH0_CLIENT_ID]
15-
AUTH0_CLIENT_SECRET = env[constants.AUTH0_CLIENT_SECRET]
16-
AUTH0_DOMAIN = env[constants.AUTH0_DOMAIN]
20+
ENV_FILE = find_dotenv()
21+
if ENV_FILE:
22+
load_dotenv(ENV_FILE)
23+
24+
AUTH0_CALLBACK_URL = env.get(constants.AUTH0_CALLBACK_URL)
25+
AUTH0_CLIENT_ID = env.get(constants.AUTH0_CLIENT_ID)
26+
AUTH0_CLIENT_SECRET = env.get(constants.AUTH0_CLIENT_SECRET)
27+
AUTH0_DOMAIN = env.get(constants.AUTH0_DOMAIN)
1728
AUTH0_AUDIENCE = env.get(constants.AUTH0_AUDIENCE)
1829

1930
APP = Flask(__name__, static_url_path='/public', static_folder='./public')
@@ -37,6 +48,15 @@
3748
)
3849

3950

51+
def requires_auth(f):
52+
@wraps(f)
53+
def decorated(*args, **kwargs):
54+
if constants.PROFILE_KEY not in session:
55+
return redirect('/login')
56+
return f(*args, **kwargs)
57+
return decorated
58+
59+
4060
# Controllers API
4161
@APP.route('/')
4262
def home():
@@ -52,13 +72,42 @@ def callback_handling():
5272
request.args['error_description']
5373
))
5474

55-
return render_template('dashboard.html')
75+
# Obtain JWT and the keys to validate the signature
76+
idToken = resp['id_token']
77+
jwks = urlopen("https://"+AUTH0_DOMAIN+"/.well-known/jwks.json")
78+
79+
payload = jwt.decode(idToken, jwks.read(), algorithms=['RS256'], audience=AUTH0_CLIENT_ID, issuer="https://"+AUTH0_DOMAIN+"/")
80+
81+
session[constants.JWT_PAYLOAD] = payload
82+
83+
session[constants.PROFILE_KEY] = {
84+
'user_id': payload['user_id'],
85+
'email': payload['email'],
86+
'picture': payload['picture']
87+
}
88+
89+
return redirect('/dashboard')
5690

5791

5892
@APP.route('/login')
5993
def login():
6094
return auth0.authorize(callback=AUTH0_CALLBACK_URL)
6195

6296

97+
@APP.route('/logout')
98+
def logout():
99+
session.clear()
100+
params = {'returnTo': url_for('home', _external=True), 'client_id': AUTH0_CLIENT_ID}
101+
return redirect(auth0.base_url + '/v2/logout?' + urlencode(params))
102+
103+
104+
@APP.route('/dashboard')
105+
@requires_auth
106+
def dashboard():
107+
return render_template('dashboard.html',
108+
userinfo=session[constants.PROFILE_KEY],
109+
userinfo_pretty=json.dumps(session[constants.JWT_PAYLOAD], indent=4))
110+
111+
63112
if __name__ == "__main__":
64113
APP.run(host='0.0.0.0', port=env.get('PORT', 3000))

01-Login/templates/dashboard.html

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@
1010
<body class="home">
1111
<div class="container">
1212
<div class="login-page clearfix">
13-
<div class="logged-in-box auth0-box logged-in">
14-
<h1 id="logo"><img src="//cdn.auth0.com/samples/auth0_logo_final_blue_RGB.png" /></h1>
15-
<h2>Welcome, you are logged!</h2>
16-
</div>
13+
<div class="logged-in-box auth0-box logged-in">
14+
<h1 id="logo"><img src="//cdn.auth0.com/samples/auth0_logo_final_blue_RGB.png" /></h1>
15+
<img class="avatar" src="{{userinfo['picture']}}"/>
16+
<h2>Welcome {{userinfo['email']}}</h2>
17+
<pre>{{userinfo_pretty}}</pre>
18+
<a class="btn btn-primary btn-lg btn-logout btn-block" href="/logout">Logout</a>
19+
</div>
1720
</div>
1821
</div>
1922
</body>

0 commit comments

Comments
 (0)