authentication on flask code example
Example 1: flask authentication user without database
class User(UserMixin):
def __init__(self, username, hash):
self.name = username
self.hash = hash
@property
def id(self):
return self.name
@self.server.route('/api/login', methods=['GET', 'POST'])
def login():
user = load_user(request.values.get('username'))
if user and user.hash == request.values.get('hash'):
login_user(user)
return jsonify(status='ok', username=user.username)
else:
return jsonify(status='error', message='wrong username or hash')
Example 2: jwt authentication python flask
GET /protected HTTP/1.1
Authorization: JWT eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZGVudGl0eSI6MSwiaWF0IjoxNDQ0OTE3NjQwLCJuYmYiOjE0NDQ5MTc2NDAsImV4cCI6MTQ0NDkxNzk0MH0.KPmI6WSjRjlpzecPvs3q_T3cJQvAgJvaQAPtk1abC_E