Last active
February 17, 2020 12:12
-
-
Save gouthambs/0a509faf231cff3cdec7 to your computer and use it in GitHub Desktop.
Flask token based authentication
This file contains 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
# Author: Gouthaman Balaraman | |
# http://gouthamanbalaraman.com/minimal-flask-login-example.html | |
from flask import Flask, Response | |
from flask.ext.login import LoginManager, UserMixin, login_required | |
app = Flask(__name__) | |
login_manager = LoginManager() | |
login_manager.init_app(app) | |
class User(UserMixin): | |
# proxy for a database of users | |
user_database = {"JohnDoe": ("JohnDoe", "John"), | |
"JaneDoe": ("JaneDoe", "Jane")} | |
def __init__(self, username, password): | |
self.id = username | |
self.password = password | |
@classmethod | |
def get(cls,id): | |
return cls.user_database.get(id) | |
@login_manager.request_loader | |
def load_user(request): | |
token = request.headers.get('Authorization') | |
if token is None: | |
token = request.args.get('token') | |
if token is not None: | |
username,password = token.split(":") # naive token | |
user_entry = User.get(username) | |
if (user_entry is not None): | |
user = User(user_entry[0],user_entry[1]) | |
if (user.password == password): | |
return user | |
return None | |
@app.route("/",methods=["GET"]) | |
def index(): | |
return Response(response="Hello World!",status=200) | |
@app.route("/protected/",methods=["GET"]) | |
@login_required | |
def protected(): | |
return Response(response="Hello Protected World!", status=200) | |
if __name__ == '__main__': | |
app.config["SECRET_KEY"] = "ITSASECRET" | |
app.run(port=5000,debug=True) |
This file contains 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
The MIT License | |
Further resources on the MIT License | |
Copyright 2014 Gouthaman Balaraman | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
if token is none, how to send user to login page?
`if token is None:
return redirect(url_for('login'))
else:
`
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
if token is none, how to send user to login page?