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 ...@@ -2,6 +2,7 @@ from canteeneo import app, db, auth
from flask import jsonify, request, g from flask import jsonify, request, g
from models import Dish, Stall, Location, User from models import Dish, Stall, Location, User
from datetime import datetime from datetime import datetime
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer, BadSignature, SignatureExpired
@app.route('/api/all') @app.route('/api/all')
def all(): def all():
...@@ -63,19 +64,31 @@ def new_user(): ...@@ -63,19 +64,31 @@ def new_user():
@auth.verify_password @auth.verify_password
def verify_password(username, password): def verify_password(username, password):
user = User.verify_token(username) user = verify_token(username)
print(user)
if user is None: if user is None:
user = User.query.filter_by(username=username).first() user = User.query.filter_by(username=username).first()
print(user)
if user is None or not user.check_password(password): if user is None or not user.check_password(password):
return False return False
print(user)
g.user = user g.user = user
return True 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') @app.route('/api/token')
@auth.login_required @auth.login_required
def get_auth_token(): def get_auth_token():
token = g.user.generate_token() token = generate_token(g.user)
return jsonify({'token': token.decode('ascii'), 'duration': 600}) 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 werkzeug.security import generate_password_hash, check_password_hash
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer, BadSignature, SignatureExpired
dish_favorites = db.Table('dish_favorites', dish_favorites = db.Table('dish_favorites',
db.Column('dish_id', db.Integer, db.ForeignKey('dish.id')), db.Column('dish_id', db.Integer, db.ForeignKey('dish.id')),
...@@ -106,22 +105,6 @@ class User(db.Model): ...@@ -106,22 +105,6 @@ class User(db.Model):
def check_password(self, password): def check_password(self, password):
return check_password_hash(self.password, 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): class DishReview(db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(80)) 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