| 1 |
""" |
| 2 |
Copyright 2021-2022 by J. Christopher Wagner (jwag). All rights reserved. |
| 3 |
:license: MIT, see LICENSE for more details. |
| 4 |
|
| 5 |
|
| 6 |
Complete models for all features when using Flask-SqlAlchemy |
| 7 |
|
| 8 |
BE AWARE: Once any version of this is shipped no changes can be made - instead |
| 9 |
a new version needs to be created. |
| 10 |
|
| 11 |
This is Version 3: |
| 12 |
- Add support for webauthn. |
| 13 |
- Add support for 2FA recovery codes. |
| 14 |
- password can be null. |
| 15 |
- us_phone_number must be unique. |
| 16 |
- Add support for list types. |
| 17 |
""" |
| 18 |
|
| 19 |
# mypy: disable-error-code="assignment" |
| 20 |
# pyright: reportAssignmentType = false, reportIncompatibleVariableOverride=false |
| 21 |
|
| 22 |
from sqlalchemy import ( |
| 23 |
Boolean, |
| 24 |
Column, |
| 25 |
DateTime, |
| 26 |
ForeignKey, |
| 27 |
Integer, |
| 28 |
LargeBinary, |
| 29 |
String, |
| 30 |
) |
| 31 |
from sqlalchemy.ext.declarative import declared_attr |
| 32 |
from sqlalchemy.ext.mutable import MutableList |
| 33 |
from sqlalchemy.sql import func |
| 34 |
|
| 35 |
|
| 36 |
from .fsqla import FsModels |
| 37 |
from .fsqla_v2 import FsUserMixin as FsUserMixinV2 |
| 38 |
from .fsqla_v2 import FsRoleMixin as FsRoleMixinV2 |
| 39 |
from flask_security import AsaList, WebAuthnMixin |
| 40 |
|
| 41 |
FsModels.fs_model_version = 3 |
| 42 |
|
| 43 |
|
| 44 |
class FsRoleMixin(FsRoleMixinV2): |
| 45 |
pass |
| 46 |
|
| 47 |
|
| 48 |
class FsUserMixin(FsUserMixinV2): |
| 49 |
"""User information""" |
| 50 |
|
| 51 |
try: |
| 52 |
import webauthn as webauthn_pkg |
| 53 |
|
| 54 |
# List of WebAuthn registrations |
| 55 |
@declared_attr |
| 56 |
def webauthn(cls): |
| 57 |
return FsModels.db.relationship( |
| 58 |
"WebAuthn", backref="users", cascade="all, delete" |
| 59 |
) |
| 60 |
|
| 61 |
except ImportError: |
| 62 |
pass |
| 63 |
|
| 64 |
# The user handle as required during registration. |
| 65 |
# Note max length 64 as specified in spec. |
| 66 |
fs_webauthn_user_handle = Column(String(64), unique=True, nullable=True) |
| 67 |
|
| 68 |
# MFA - one time recovery codes - comma separated. |
| 69 |
mf_recovery_codes = Column(MutableList.as_mutable(AsaList()), nullable=True) |
| 70 |
|
| 71 |
# Change password to nullable so we can tell after registration whether |
| 72 |
# a user has a password or not. |
| 73 |
password = Column(String(255), nullable=True) |
| 74 |
|
| 75 |
# since phone can be used to authenticate - must be unique. |
| 76 |
us_phone_number = Column(String(128), nullable=True, unique=True) |
| 77 |
|
| 78 |
|
| 79 |
class FsWebAuthnMixin(WebAuthnMixin): |
| 80 |
"""WebAuthn""" |
| 81 |
|
| 82 |
id = Column(Integer, primary_key=True) |
| 83 |
credential_id = Column(LargeBinary(1024), index=True, unique=True, nullable=False) |
| 84 |
public_key = Column(LargeBinary, nullable=False) |
| 85 |
sign_count = Column(Integer, default=0) |
| 86 |
transports = Column(MutableList.as_mutable(AsaList()), nullable=True) |
| 87 |
backup_state = Column(Boolean, nullable=False) # Upcoming post V3 spec |
| 88 |
device_type = Column(String(64), nullable=False) |
| 89 |
|
| 90 |
# a JSON string as returned from registration |
| 91 |
extensions = Column(String(255), nullable=True) |
| 92 |
create_datetime = Column(type_=DateTime, nullable=False, server_default=func.now()) |
| 93 |
lastuse_datetime = Column(type_=DateTime, nullable=False) |
| 94 |
# name is provided by user - we make sure is unique per user |
| 95 |
name = Column(String(64), nullable=False) |
| 96 |
|
| 97 |
# Usage - a credential can EITHER be for first factor or secondary factor |
| 98 |
usage = Column(String(64), nullable=False) |
| 99 |
|
| 100 |
@declared_attr |
| 101 |
def user_id(cls): |
| 102 |
return Column( |
| 103 |
Integer, |
| 104 |
ForeignKey(f"{FsModels.user_table_name}.id", ondelete="CASCADE"), |
| 105 |
nullable=False, |
| 106 |
) |