From 5d7a96ca5f1cb98ef0d6dd0dd7095a6e46d06cf7 Mon Sep 17 00:00:00 2001 From: Alexis Luque Date: Wed, 15 Aug 2018 15:28:31 -0300 Subject: [PATCH] Migrate from auth0.js --- complete_with_auth0/gallery_demo/app.py | 31 ++++++++++--------- complete_with_auth0/gallery_demo/constants.py | 1 + .../gallery_demo/requirements.txt | 2 +- .../gallery_demo/templates/index.html | 13 +------- .../gallery_demo/requirements.txt | 2 +- 5 files changed, 21 insertions(+), 28 deletions(-) diff --git a/complete_with_auth0/gallery_demo/app.py b/complete_with_auth0/gallery_demo/app.py index d20b481..2b759da 100644 --- a/complete_with_auth0/gallery_demo/app.py +++ b/complete_with_auth0/gallery_demo/app.py @@ -2,22 +2,19 @@ from flask_bootstrap import Bootstrap from PIL import Image from werkzeug.utils import secure_filename -from dotenv import Dotenv +from dotenv import load_dotenv, find_dotenv from functools import wraps import os import constants import requests # Load Env variables -env = None - -try: - env = Dotenv('./.env') -except IOError: - env = os.environ +ENV_FILE = find_dotenv() +if ENV_FILE: + load_dotenv(ENV_FILE) app = Flask(__name__) -app.secret_key = env['SECRET_KEY'] +app.secret_key = os.environ.get('SECRET_KEY') Bootstrap(app) APP_ROOT = os.path.dirname(os.path.abspath(__file__)) @@ -42,7 +39,12 @@ def is_logged_in(): @app.route('/') def index(): - return render_template('index.html', env=env, logged_in=is_logged_in()) + authorize_url = 'https://{auth0_domain}/authorize?client_id={client_id}&response_type=code&redirect_uri={redirect_uri}'\ + .format( + auth0_domain=os.environ.get(constants.AUTH0_DOMAIN), + client_id=os.environ.get(constants.AUTH0_CLIENT_ID), + redirect_uri=os.environ.get(constants.AUTH0_CALLBACK_URL)) + return render_template('index.html', env=os.environ, logged_in=is_logged_in(), authorize_url=authorize_url) @app.route('/gallery') def gallery(): @@ -90,7 +92,7 @@ def upload(): def logout(): session.clear() return redirect('https://{auth0_domain}/v2/logout?client_id={auth0_client_id}&returnTo={app_url}'\ - .format(auth0_domain=env[constants.AUTH0_DOMAIN], auth0_client_id=env[constants.AUTH0_CLIENT_ID], + .format(auth0_domain=os.environ.get(constants.AUTH0_DOMAIN), auth0_client_id=os.environ.get(constants.AUTH0_CLIENT_ID), app_url='http://localhost:3000')) @app.route('/callback') @@ -98,10 +100,11 @@ def callback_handling(): code = request.args.get(constants.CODE_KEY) json_header = {constants.CONTENT_TYPE_KEY: constants.APP_JSON_KEY} token_url = 'https://{auth0_domain}/oauth/token'.format( - auth0_domain=env[constants.AUTH0_DOMAIN]) + auth0_domain=os.environ.get(constants.AUTH0_DOMAIN)) token_payload = { - constants.CLIENT_ID_KEY: env[constants.AUTH0_CLIENT_ID], - constants.REDIRECT_URI_KEY: env[constants.AUTH0_CALLBACK_URL], + constants.CLIENT_ID_KEY: os.environ.get(constants.AUTH0_CLIENT_ID), + constants.CLIENT_SECRET_KEY: os.environ.get(constants.AUTH0_CLIENT_SECRET), + constants.REDIRECT_URI_KEY: os.environ.get(constants.AUTH0_CALLBACK_URL), constants.CODE_KEY: code, constants.GRANT_TYPE_KEY: constants.AUTHORIZATION_CODE_KEY } @@ -110,7 +113,7 @@ def callback_handling(): headers=json_header).json() user_url = 'https://{auth0_domain}/userinfo?access_token={access_token}'\ - .format(auth0_domain=env[constants.AUTH0_DOMAIN], + .format(auth0_domain=os.environ.get(constants.AUTH0_DOMAIN), access_token=token_info[constants.ACCESS_TOKEN_KEY]) user_info = requests.get(user_url).json() diff --git a/complete_with_auth0/gallery_demo/constants.py b/complete_with_auth0/gallery_demo/constants.py index e2d7998..999f6c0 100644 --- a/complete_with_auth0/gallery_demo/constants.py +++ b/complete_with_auth0/gallery_demo/constants.py @@ -6,6 +6,7 @@ AUTH0_DOMAIN = 'AUTH0_DOMAIN' AUTHORIZATION_CODE_KEY = 'authorization_code' CLIENT_ID_KEY = 'client_id' +CLIENT_SECRET_KEY = 'client_secret' CODE_KEY = 'code' CONTENT_TYPE_KEY = 'content-type' GRANT_TYPE_KEY = 'grant_type' diff --git a/complete_with_auth0/gallery_demo/requirements.txt b/complete_with_auth0/gallery_demo/requirements.txt index 8fd043b..f22099d 100644 --- a/complete_with_auth0/gallery_demo/requirements.txt +++ b/complete_with_auth0/gallery_demo/requirements.txt @@ -1,4 +1,4 @@ -dotenv==0.0.5 +python-dotenv==0.9.1 Flask==0.12.1 Flask-Bootstrap==3.3.7.1 Pillow==4.1.0 diff --git a/complete_with_auth0/gallery_demo/templates/index.html b/complete_with_auth0/gallery_demo/templates/index.html index dad385a..596036c 100644 --- a/complete_with_auth0/gallery_demo/templates/index.html +++ b/complete_with_auth0/gallery_demo/templates/index.html @@ -9,20 +9,9 @@

Hi There!!!

You can upload images or head over to the gallery

Logout

{% else %} -

to upload images or head over to the gallery

+

to upload images or head over to the gallery

{% endif %} {% endblock %} - -{% block scripts %} -{{super()}} - - - -{% endblock %} diff --git a/complete_without_auth0/gallery_demo/requirements.txt b/complete_without_auth0/gallery_demo/requirements.txt index 8fd043b..f22099d 100644 --- a/complete_without_auth0/gallery_demo/requirements.txt +++ b/complete_without_auth0/gallery_demo/requirements.txt @@ -1,4 +1,4 @@ -dotenv==0.0.5 +python-dotenv==0.9.1 Flask==0.12.1 Flask-Bootstrap==3.3.7.1 Pillow==4.1.0