Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c528dc4

Browse files
authored
Add files via upload
1 parent 6247df3 commit c528dc4

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed

python-flask-rest-api-jwt-auth/app.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from flask import Flask
2+
3+
app = Flask(__name__)
4+
app.config['SECRET_KEY'] = 'secret-key'
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from app import app
2+
from flaskext.mysql import MySQL
3+
4+
mysql = MySQL()
5+
6+
# MySQL configurations
7+
app.config['MYSQL_DATABASE_USER'] = 'root'
8+
app.config['MYSQL_DATABASE_PASSWORD'] = 'root'
9+
app.config['MYSQL_DATABASE_DB'] = 'roytuts'
10+
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
11+
mysql.init_app(app)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
You can go through the tutorial https://www.roytuts.com/jwt-authentication-using-python-flask/
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import pymysql
2+
from app import app
3+
from db_conf import mysql
4+
from flask import jsonify
5+
from flask_jwt import JWT, jwt_required, current_identity
6+
from werkzeug import generate_password_hash, check_password_hash
7+
8+
class User(object):
9+
def __init__(self, id, username):
10+
self.id = id
11+
self.username = username
12+
13+
def __str__(self):
14+
return "User(id='%s')" % self.id
15+
16+
@app.route('/rest-auth')
17+
@jwt_required()
18+
def get_response():
19+
return jsonify('You are an authenticate person to see this message')
20+
21+
def authenticate(username, password):
22+
if username and password:
23+
conn = None;
24+
cursor = None;
25+
try:
26+
conn = mysql.connect()
27+
cursor = conn.cursor(pymysql.cursors.DictCursor)
28+
cursor.execute("SELECT id, username, password FROM user WHERE username=%s", username)
29+
row = cursor.fetchone()
30+
31+
if row:
32+
if check_password_hash(row['password'], password):
33+
return User(row['id'], row['username'])
34+
else:
35+
return None
36+
except Exception as e:
37+
print(e)
38+
finally:
39+
cursor.close()
40+
conn.close()
41+
return None
42+
43+
def identity(payload):
44+
if payload['identity']:
45+
conn = None;
46+
cursor = None;
47+
try:
48+
conn = mysql.connect()
49+
cursor = conn.cursor(pymysql.cursors.DictCursor)
50+
cursor.execute("SELECT id, username, password FROM user WHERE id=%s", payload['identity'])
51+
row = cursor.fetchone()
52+
53+
if row:
54+
return (row['id'], row['username'])
55+
else:
56+
return None
57+
except Exception as e:
58+
print(e)
59+
finally:
60+
cursor.close()
61+
conn.close()
62+
else:
63+
return None
64+
65+
jwt = JWT(app, authenticate, identity)
66+
67+
if __name__ == "__main__":
68+
app.run()

0 commit comments

Comments
 (0)