2020-05-09 18:00:07 +00:00
|
|
|
from flask_sqlalchemy import SQLAlchemy, orm
|
2020-05-27 15:56:10 +00:00
|
|
|
from datetime import datetime
|
2020-05-21 11:20:27 +00:00
|
|
|
import uuid
|
2020-05-27 15:56:10 +00:00
|
|
|
import pyotp
|
2020-05-09 18:00:07 +00:00
|
|
|
|
|
|
|
db = SQLAlchemy() # type: SQLAlchemy
|
|
|
|
|
|
|
|
|
2020-05-21 11:20:27 +00:00
|
|
|
def generate_uuid():
|
|
|
|
return str(uuid.uuid4())
|
|
|
|
|
|
|
|
|
|
|
|
class User(db.Model):
|
2020-05-27 15:56:10 +00:00
|
|
|
id = db.Column(
|
|
|
|
db.String(length=36), primary_key=True, default=generate_uuid)
|
|
|
|
username = db.Column(
|
|
|
|
db.String, unique=True)
|
2020-05-09 18:00:07 +00:00
|
|
|
|
2020-05-27 15:56:10 +00:00
|
|
|
totps = db.relationship('Totp', back_populates='user')
|
2020-05-09 18:00:07 +00:00
|
|
|
|
|
|
|
|
2020-05-27 15:56:10 +00:00
|
|
|
class Totp(object):
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
secret = db.Column(db.String, nullable=False)
|
|
|
|
name = db.Column(db.String, nullable=False)
|
|
|
|
created_at = db.Column(db.DateTime, default=datetime.now, nullable=False)
|
2020-05-09 18:00:07 +00:00
|
|
|
|
2020-05-27 15:56:10 +00:00
|
|
|
user_id = db.Column(
|
|
|
|
db.Integer,
|
|
|
|
db.ForeignKey(User.id), nullable=False)
|
|
|
|
user = db.relationship(User)
|
2020-05-09 18:00:07 +00:00
|
|
|
|
2020-05-27 15:56:10 +00:00
|
|
|
def verify(self, token: str):
|
|
|
|
totp = pyotp.TOTP(self._secret)
|
|
|
|
return totp.verify(token)
|