สำหรับนักพัฒนาที่กำลังหาวิธีการใช้ authenticate แบบใช้ Token หรือรวมไปถึงการขอ Token เพื่อที่จะนำไปใช้ สำหรับภาษา Python ก็มี Package ตัวหนึ่งมาแนะนำครับ flask_jwt ตัวนี้จะใช้งานควบคู่ไปกับตัว Flask ธรรมดาด้วยให้ทำการดาวน์โหลดตัว Package มาก่อนโดยการใช้คำสั่ง
โค้ด: เลือกทั้งหมด
pip install Flask-JWT
โค้ด: เลือกทั้งหมด
from flask import Flask
from flask_jwt import JWT, jwt_required, current_identity
from werkzeug.security import safe_str_cmp
โค้ด: เลือกทั้งหมด
class User(object):
def __init__(self, id, username, password):
self.id = id
self.username = username
self.password = password
def __str__(self):
return "User(id='%s')" % self.id
โค้ด: เลือกทั้งหมด
users = [
User(1, 'test01', 'test1234'),
User(2, 'test02', 'test6789'),
]
username_table = {u.username: u for u in users}
userid_table = {u.id: u for u in users}
โค้ด: เลือกทั้งหมด
def authenticate(username, password):
user = username_table.get(username, None)
if user and safe_str_cmp(user.password.encode('utf-8'), password.encode('utf-8')):
return user
def identity(payload):
user_id = payload['identity']
return userid_table.get(user_id, None)
โค้ด: เลือกทั้งหมด
app = Flask(__name__)
app.debug = True
app.config['SECRET_KEY'] = 'super-secret'
jwt = JWT(app, authenticate, identity)
@app.route('/helloworld')
@jwt_required()
def helloworld():
return 'HelloWorld %s' % current_identity
if __name__ == '__main__':
app.run()
โค้ด: เลือกทั้งหมด
from flask import Flask
from flask_jwt import JWT, jwt_required, current_identity
from werkzeug.security import safe_str_cmp
class User(object):
def __init__(self, id, username, password):
self.id = id
self.username = username
self.password = password
def __str__(self):
return "User(id='%s')" % self.id
users = [
User(1, 'test01', 'test1234'),
User(2, 'test02', 'test6789'),
]
username_table = {u.username: u for u in users}
userid_table = {u.id: u for u in users}
def authenticate(username, password):
user = username_table.get(username, None)
if user and safe_str_cmp(user.password.encode('utf-8'), password.encode('utf-8')):
return user
def identity(payload):
user_id = payload['identity']
return userid_table.get(user_id, None)
app = Flask(__name__)
app.debug = True
app.config['SECRET_KEY'] = 'super-secret'
jwt = JWT(app, authenticate, identity)
@app.route('/helloworld')
@jwt_required()
def helloworld():
return 'HelloWorld %s' % current_identity
if __name__ == '__main__':
app.run()
โค้ด: เลือกทั้งหมด
curl -d '{"username": "<ใส่ username>","password": "<ใส่ password>" }' -H "Content-Type: application/json" -X POST http://127.0.0.1:5000/auth
โค้ด: เลือกทั้งหมด
{
"access_token": "<Token ที่ขอไป>"
}
โค้ด: เลือกทั้งหมด
$ curl -d '{"username": "test01","password": "test1234" }' -H "Content-Type: application/json" -X POST http://127.0.0.1:5000/auth
โค้ด: เลือกทั้งหมด
{
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZGVudGl0eSI6MSwiaWF0IjoxNTg4MjQ3NzE0LCJuYmYiOjE1ODgyNDc3MTQsImV4cCI6MTU4ODI0ODAxNH0.EEHYLQFLso7MUMV5FW7IwAVzTXGoduW3Obchvkql46g"
}
โค้ด: เลือกทั้งหมด
curl -H "Authorization: JWT <Token ที่ได้มา>" http://< hostname>:<port>/helloworld
โค้ด: เลือกทั้งหมด
curl -H "Authorization: JWT eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZGVudGl0eSI6MSwiaWF0IjoxNTg4MjQ3NzE0LCJuYmYiOjE1ODgyNDc3MTQsImV4cCI6MTU4ODI0ODAxNH0.EEHYLQFLso7MUMV5FW7IwAVzTXGoduW3Obchvkql46g" http://127.0.0.1:5000/helloworld
โค้ด: เลือกทั้งหมด
HelloWorld User(id='1')
อ้างอิง
https://pythonhosted.org/Flask-JWT/
https://flask-jwt-extended.readthedocs.io/en/stable/
https://medium.com/trabe/user-authentication-with-flask-jwt-be0e6f457dff