Giter Club home page Giter Club logo

flask-jwt-login's Introduction

Flask-JWT-Login

Build Status Coverage Status PyPI

Flask extension that helps authentication using JWT(Json Web Token)

Guide

How to initiate

from flask import Flask
from flask_jwt_login import JWT

app = Flask(__name__)	# create app object
jwt = JWT(app)			# initialize flask_jwt_login

Configuration

app.py

from flask import Flask

app = Flask(__name__)
app.config.from_object('config.Config')

config.py

class Config(object):
    SECRET_KEY = 'random secret key for development'
    HASH_ALGORITHM = 'HS512' 
    # hash algorithm to use at encode and decode token
    JWT_COOKIE_NAME = 'token'
    # token name to be used
    
    # if you don't specify HASH_ALGORITHM or JWT_COOKIE_NAME,
    # they will have default value. (HS512 and token)

Which hash algorithm do I have to use?

Refer this link (PyJWT Documentation - Digital Signature Algorithms)

authentication

authentication handler

...
...

# initialize
jwt = JWT(app)

# user data class
# I want to recommend you to add hashed passsword data into token
class User():
	def __init__(self, id, pw, name):
		self.id = id
		self.pw = pw
		self.name = name

	def __repr__(self):
		return "User(id=%s, password=%s, name=%s)" % (self.id, self.pw, self.name)
		
# You have to write a function that check users' ids and passwords.
@jwt.authentication_handler
def authentication_handler(id, pw):
	for row in user_table:
		if row['id'] == id and row['pw'] == pw:
			return User(row['id'], row['pw'], row['name'])

	# if there is no matching user, returns None
	return None

process login

from flask_jwt_login import process_login

@app.route('/some_url')
def some_function():
	token = process_login(request.form["id"], request.form["pw"])
	# this token will be the value returned from authentication handler
	
	response = make_response("sign in")
	response.set_cookie(TOKEN_NAME, token)
	return response

unauthorized handler

@jwt.unauthorized_handler
def unauthorized_handler():
	# if authentication failed, this handler will be performed
	return 'Unauthorized Access', 501

Protected Page & User Information

login required & get current user

# this url is only accessed by users who have a valid token.
@app.route("/protected")
@login_required
def protected():
	return "Protected Page. name :" + get_current_user()["name"]

flask-jwt-login's People

Contributors

jeongukjae avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.