Skip to content

Commit

Permalink
esta un poco oxidado pero sirve
Browse files Browse the repository at this point in the history
  • Loading branch information
afpinzon10 authored Apr 23, 2018
1 parent b0f4a73 commit 8fdfa03
Show file tree
Hide file tree
Showing 11 changed files with 357 additions and 0 deletions.
12 changes: 12 additions & 0 deletions EntidadVirtual/01-login/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM python:3

WORKDIR /home/app

#If we add the requirements and install dependencies first, docker can use cache if requirements don't change
ADD requirements.txt /home/app
RUN pip install --no-cache-dir -r requirements.txt

ADD . /home/app
CMD python server.py

EXPOSE 3000
1 change: 1 addition & 0 deletions EntidadVirtual/01-login/Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: python server.py
61 changes: 61 additions & 0 deletions EntidadVirtual/01-login/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Auth0 Python Web App Sample

This sample demonstrates how to add authentication to a Python web app using Auth0.

# Running the App

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

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.

Register `http://localhost:3000/callback` as `Allowed Callback URLs` and `http://localhost:3000`
as `Allowed Logout URLs` in your client settings.

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/).

# Running the App with Docker

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

To run the sample with [Docker](https://www.docker.com/), make sure you have `docker` installed.

Rename the .env.example file to .env, change the environment variables, and register the URLs as explained [previously](#running-the-app).

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.

## What is Auth0?

Auth0 helps you to:

* Add authentication with [multiple authentication sources](https://auth0.com/docs/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**.
* Add authentication through more traditional **[username/password databases](https://docs.auth0.com/mysql-connection-tutorial)**.
* Add support for **[linking different user accounts](https://auth0.com/docs/link-accounts)** with the same user.
* Support for generating signed [JSON Web Tokens](https://auth0.com/docs/jwt) to call your APIs and
**flow the user identity** securely.
* Analytics of how, when and where users are logging in.
* Pull data from other sources and add it to the user profile, through [JavaScript rules](https://auth0.com/docs/rules).

## Create a free account in Auth0

1. Go to [Auth0](https://auth0.com) and click Sign Up.
2. Use Google, GitHub or Microsoft Account to login.

## Issue Reporting

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.

## Author

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

## License

This project is licensed under the MIT license. See the [LICENSE](LICENCE) file for more info.
11 changes: 11 additions & 0 deletions EntidadVirtual/01-login/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
""" Constants file for Auth0's seed project
"""
AUTH0_CLIENT_ID = 'AUTH0_CLIENT_ID'
AUTH0_CLIENT_SECRET = 'AUTH0_CLIENT_SECRET'
AUTH0_CALLBACK_URL = 'AUTH0_CALLBACK_URL'
AUTH0_DOMAIN = 'AUTH0_DOMAIN'
AUTH0_AUDIENCE = 'AUTH0_AUDIENCE'
PROFILE_KEY = 'profile'
SECRET_KEY = 'ThisIsTheSecretKey'
JWT_PAYLOAD = 'jwt_payload'

2 changes: 2 additions & 0 deletions EntidadVirtual/01-login/exec.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
docker build -t auth0-python-web-01-login .
docker run --env-file .env -p 3000:3000 -it auth0-python-web-01-login
2 changes: 2 additions & 0 deletions EntidadVirtual/01-login/exec.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
docker build -t auth0-python-web-01-login .
docker run --env-file .env -p 3000:3000 -it auth0-python-web-01-login
98 changes: 98 additions & 0 deletions EntidadVirtual/01-login/public/app.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
body {
font-family: "proxima-nova", sans-serif;
text-align: center;
font-size: 300%;
font-weight: 100;
}
pre {
text-align: left;
}
input[type=checkbox],
input[type=radio] {
position: absolute;
opacity: 0;
}
input[type=checkbox] + label,
input[type=radio] + label {
display: inline-block;
}
input[type=checkbox] + label:before,
input[type=radio] + label:before {
content: "";
display: inline-block;
vertical-align: -0.2em;
width: 1em;
height: 1em;
border: 0.15em solid #0074d9;
border-radius: 0.2em;
margin-right: 0.3em;
background-color: white;
}
input[type=radio] + label:before {
border-radius: 50%;
}
input[type=radio]:checked + label:before,
input[type=checkbox]:checked + label:before {
background-color: #0074d9;
box-shadow: inset 0 0 0 0.15em white;
}
input[type=radio]:focus + label:before,
input[type=checkbox]:focus + label:before {
outline: 0;
}
.btn {
font-size: 140%;
text-transform: uppercase;
letter-spacing: 1px;
border: 0;
background-color: #16214D;
color: white;
}
.btn:hover {
background-color: #44C7F4;
}
.btn:focus {
outline: none !important;
}
.btn.btn-lg {
padding: 20px 30px;
}
.btn:disabled {
background-color: #333;
color: #666;
}
h1,
h2,
h3 {
font-weight: 100;
}
#logo img {
width: 300px;
margin-bottom: 60px;
}
.home-description {
font-weight: 100;
margin: 100px 0;
}
h2 {
margin-top: 30px;
margin-bottom: 40px;
font-size: 200%;
}
label {
font-size: 100%;
font-weight: 300;
}
.btn-next {
margin-top: 30px;
}
.answer {
width: 70%;
margin: auto;
text-align: left;
padding-left: 10%;
margin-bottom: 20px;
}
.login-page .login-box {
padding: 100px 0;
}
5 changes: 5 additions & 0 deletions EntidadVirtual/01-login/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
flask
python-dotenv
requests
authlib
six
118 changes: 118 additions & 0 deletions EntidadVirtual/01-login/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
"""Python Flask WebApp Auth0 integration example
"""
from functools import wraps
import json
from os import environ as env
from werkzeug.exceptions import HTTPException

from dotenv import load_dotenv, find_dotenv
from flask import Flask
from flask import jsonify
from flask import redirect
from flask import render_template
from flask import session
from flask import url_for
from authlib.flask.client import OAuth
from six.moves.urllib.parse import urlencode
import requests

import constants

ENV_FILE = find_dotenv()
if ENV_FILE:
load_dotenv(ENV_FILE)

AUTH0_CALLBACK_URL = env.get(constants.AUTH0_CALLBACK_URL)
AUTH0_CLIENT_ID = env.get(constants.AUTH0_CLIENT_ID)
AUTH0_CLIENT_SECRET = env.get(constants.AUTH0_CLIENT_SECRET)
AUTH0_DOMAIN = env.get(constants.AUTH0_DOMAIN)
AUTH0_BASE_URL = 'https://' + AUTH0_DOMAIN
AUTH0_AUDIENCE = env.get(constants.AUTH0_AUDIENCE)
if AUTH0_AUDIENCE is '':
AUTH0_AUDIENCE = AUTH0_BASE_URL + '/userinfo'

app = Flask(__name__, static_url_path='/public', static_folder='./public')
app.secret_key = constants.SECRET_KEY
app.debug = True


@app.errorhandler(Exception)
def handle_auth_error(ex):
response = jsonify(message=str(ex))
response.status_code = (ex.code if isinstance(ex, HTTPException) else 500)
return response


oauth = OAuth(app)

auth0 = oauth.register(
'auth0',
client_id='KbPC5q-VuaUwBrkEP9_DY2-gWj-5t-Kt',
client_secret='hM_dr9PBWS8fQ9Ijyd3veM-8lmJ43SF8BKFkFwuWu9ArKpnlle8197m4ajopCtY6',
api_base_url='https://isis2503-softsecurity.auth0.com',
access_token_url='https://isis2503-softsecurity.auth0.com/oauth/token',
authorize_url='https://isis2503-softsecurity.auth0.com/authorize',
client_kwargs={
'scope': 'openid profile',
},
)


def requires_auth(f):
@wraps(f)
def decorated(*args, **kwargs):
if constants.PROFILE_KEY not in session:
return redirect('/login')
return f(*args, **kwargs)

return decorated


# Controllers API
@app.route('/')
def home():
return render_template('home.html')


@app.route('/callback')
def callback_handling():
resp = auth0.authorize_access_token()

url = 'https://isis2503-softsecurity.auth0.com' + '/userinfo'
headers = {'authorization': 'Bearer ' + resp['access_token']}
resp = requests.get(url, headers=headers)
userinfo = resp.json()

session[constants.JWT_PAYLOAD] = userinfo

session[constants.PROFILE_KEY] = {
'user_id': userinfo['sub'],
'name': userinfo['name'],
'picture': userinfo['picture']
}

return redirect('/dashboard')


@app.route('/login')
def login():
return auth0.authorize_redirect(audience='https://isis2503-softsecurity.auth0.com/userinfo')


@app.route('/logout')
def logout():
session.clear()
params = {'returnTo': url_for('home', _external=True), 'client_id': 'KbPC5q-VuaUwBrkEP9_DY2-gWj-5t-Kt'}
return redirect(auth0.api_base_url + '/v2/logout?' + urlencode(params))


@app.route('/dashboard')
@requires_auth
def dashboard():
return render_template('dashboard.html',
userinfo=session[constants.PROFILE_KEY],
userinfo_pretty=json.dumps(session[constants.JWT_PAYLOAD], indent=4))


if __name__ == "__main__":
app.run(host='0.0.0.0', port=env.get('PORT', 3000))
23 changes: 23 additions & 0 deletions EntidadVirtual/01-login/templates/dashboard.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">

<!-- font awesome from BootstrapCDN -->
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet">
<link href="/public/app.css" rel="stylesheet">
</head>
<body class="home">
<div class="container">
<div class="login-page clearfix">
<div class="logged-in-box auth0-box logged-in">
<h1 id="logo"><img src="//cdn.auth0.com/samples/auth0_logo_final_blue_RGB.png" /></h1>
<img class="avatar" src="{{userinfo['picture']}}"/>
<h2>Welcome {{userinfo['name']}}</h2>
<pre>{{userinfo_pretty}}</pre>
<a class="btn btn-primary btn-lg btn-logout btn-block" href="/logout">Logout</a>
</div>
</div>
</div>
</body>
</html>
24 changes: 24 additions & 0 deletions EntidadVirtual/01-login/templates/home.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<html>
<head>

<meta name="viewport" content="width=device-width, initial-scale=1">

<!-- font awesome from BootstrapCDN -->
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">

<link href="/public/app.css" rel="stylesheet">
</head>
<body class="home">
<div class="container">
<div class="login-page clearfix">
<div class="login-box auth0-box before">
<img src="https://i.cloudup.com/StzWWrY34s.png" />
<h3>Auth0 Example</h3>
<p>Zero friction identity infrastructure, built for developers</p>
<a class="btn btn-primary btn-lg btn-login btn-block" href="/login">Log In</a>
</div>
</div>
</div>
</body>
</html>

0 comments on commit 8fdfa03

Please sign in to comment.