Debian Codegraph @

flask_security/models/fsqla_v3.py

raw
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_v2 import FsModels as FsModelsV2
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
42 class FsModels(FsModelsV2):
43 fs_model_version = 3
44
45
46 class FsRoleMixin(FsRoleMixinV2):
47 pass
48
49
50 class FsUserMixin(FsUserMixinV2):
51 """User information"""
52
53 try:
54 import webauthn as webauthn_pkg
55
56 # List of WebAuthn registrations
57 @declared_attr
58 def webauthn(cls):
59 return FsModels.db.relationship(
60 "WebAuthn", backref="users", cascade="all, delete"
61 )
62
63 except ImportError:
64 pass
65
66 # The user handle as required during registration.
67 # Note max length 64 as specified in spec.
68 fs_webauthn_user_handle = Column(String(64), unique=True, nullable=True)
69
70 # MFA - one time recovery codes - comma separated.
71 mf_recovery_codes = Column(MutableList.as_mutable(AsaList()), nullable=True)
72
73 # Change password to nullable so we can tell after registration whether
74 # a user has a password or not.
75 password = Column(String(255), nullable=True)
76
77 # since phone can be used to authenticate - must be unique.
78 us_phone_number = Column(String(128), nullable=True, unique=True)
79
80 # This is repeated since I couldn't figure out how to have it reference the
81 # new version of FsModels.
82 @declared_attr
83 def roles(cls):
84 return FsModels.db.relationship(
85 "Role",
86 secondary=FsModels.roles_users,
87 backref=FsModels.db.backref(
88 "users", lazy="dynamic", cascade_backrefs=False
89 ),
90 )
91
92
93 class FsWebAuthnMixin(WebAuthnMixin):
94 """WebAuthn"""
95
96 id = Column(Integer, primary_key=True)
97 credential_id = Column(LargeBinary(1024), index=True, unique=True, nullable=False)
98 public_key = Column(LargeBinary, nullable=False)
99 sign_count = Column(Integer, default=0)
100 transports = Column(MutableList.as_mutable(AsaList()), nullable=True)
101 backup_state = Column(Boolean, nullable=False) # Upcoming post V3 spec
102 device_type = Column(String(64), nullable=False)
103
104 # a JSON string as returned from registration
105 extensions = Column(String(255), nullable=True)
106 create_datetime = Column(type_=DateTime, nullable=False, server_default=func.now())
107 lastuse_datetime = Column(type_=DateTime, nullable=False)
108 # name is provided by user - we make sure is unique per user
109 name = Column(String(64), nullable=False)
110
111 # Usage - a credential can EITHER be for first factor or secondary factor
112 usage = Column(String(64), nullable=False)
113
114 @declared_attr
115 def user_id(cls):
116 return Column(
117 Integer,
118 ForeignKey(f"{FsModels.user_table_name}.id", ondelete="CASCADE"),
119 nullable=False,
120 )