Commit 158feeaa authored by Willard's avatar Willard

Move token methods outside of model

parent cfd246f1
......@@ -2,6 +2,7 @@ from canteeneo import app, db, auth
from flask import jsonify, request, g
from models import Dish, Stall, Location, User
from datetime import datetime
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer, BadSignature, SignatureExpired
@app.route('/api/all')
def all():
......@@ -63,19 +64,31 @@ def new_user():
@auth.verify_password
def verify_password(username, password):
user = User.verify_token(username)
print(user)
user = verify_token(username)
if user is None:
user = User.query.filter_by(username=username).first()
print(user)
if user is None or not user.check_password(password):
return False
print(user)
g.user = user
return True
def generate_token(user):
s = Serializer(app.config['SECRET_KEY'], expires_in=600)
return s.dumps({'id': user.id})
def verify_token(token):
s = Serializer(app.config['SECRET_KEY'])
try:
data = s.loads(token)
except SignatureExpired:
return None
except BadSignature:
return None
user = User.query.get(data['id'])
return user
@app.route('/api/token')
@auth.login_required
def get_auth_token():
token = g.user.generate_token()
token = generate_token(g.user)
return jsonify({'token': token.decode('ascii'), 'duration': 600})
from canteeneo import app, db
from canteeneo import db
from werkzeug.security import generate_password_hash, check_password_hash
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer, BadSignature, SignatureExpired
dish_favorites = db.Table('dish_favorites',
db.Column('dish_id', db.Integer, db.ForeignKey('dish.id')),
......@@ -106,22 +105,6 @@ class User(db.Model):
def check_password(self, password):
return check_password_hash(self.password, password)
def generate_token(self):
s = Serializer(app.config['SECRET_KEY'], expires_in=600)
return s.dumps({'id': self.id})
@staticmethod
def verify_token(token):
s = Serializer(app.config['SECRET_KEY'])
try:
data = s.loads(token)
except SignatureExpired:
return None
except BadSignature:
return None
user = User.query.get(data['id'])
return user
class DishReview(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(80))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment