-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.py
More file actions
319 lines (255 loc) · 9.42 KB
/
Copy pathuser.py
File metadata and controls
319 lines (255 loc) · 9.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
"""
User, Authentication and Session management.
"""
import datetime
from ipaddress import IPv4Address, IPv6Address, ip_address
import json
import os
import random
from typing import Any, Dict, Optional, Union
import uuid
from fastapi import Depends, FastAPI, HTTPException, Request
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from jose import JWTError, jwt
from passlib.context import CryptContext
import psycopg2.extensions
import psycopg2.extras
from pydantic import BaseModel
from sqlalchemy import Column, DateTime, JSON
from sqlalchemy.exc import NoResultFound
from sqlalchemy.sql import func, text
from sqlmodel import SQLModel, Field, select, Session
from sqlmodel.sql.expression import Select
from . import db
SECRET_KEY = os.environ["SECRET_KEY"]
ALGORITHM = "HS256"
DEFAULT_EXPIRY = datetime.timedelta(days=14)
OTP_VALIDITY_PERIOD = datetime.timedelta(minutes=5)
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
class AuthenticationError(HTTPException):
DEFAULT_STATUS_CODE = 401 # Unauthorized
DEFAULT_DETAIL = "Incorrect email or password"
"""The response seen by the user, NO SENSITIVE DATA!"""
def __init__(self, ctx: str = "", **kwargs):
self.ctx = ctx
super().__init__(
status_code=kwargs.pop("status_code", self.DEFAULT_STATUS_CODE),
detail=kwargs.pop("detail", self.DEFAULT_DETAIL),
**kwargs,
)
class InactiveUser(AuthenticationError):
DEFAULT_STATUS_CODE = 400
DEFAULT_DETAIL = "User is disabled or inactive. Contact admin."
class UnknownUser(AuthenticationError, KeyError):
"""The requested user was not found."""
class ExpiredToken(AuthenticationError):
"""The JWT presented is expired or the signature cannot be verified."""
class InvalidToken(AuthenticationError):
"""The token is otherwise valid but malformed -- maybe from a previous version."""
class ExpiredOTP(AuthenticationError):
"""The OTP for this user/ip combination is not valid or nonexistant."""
class IncorrectOTP(AuthenticationError):
"""Valid OTP for this user/ip was found, but the provided value does not match."""
class Token(BaseModel):
access_token: str
token_type: str
class TokenData(BaseModel):
user_id: int
session_id: int
def to_jwt(self, expires: Optional[datetime.timedelta] = None) -> str:
"""
Encode data as JSON Web Token
:param expires: expiration date for the token (DEFAULT_EXPIRY)
:return: Signed and encoded payload.
"""
to_encode = self.dict()
now = datetime.datetime.now(tz=datetime.timezone.utc)
to_encode.update(
dict(
exp=now + (expires or DEFAULT_EXPIRY),
sub="{}:{}".format(self.user_id, self.session_id),
),
)
return jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
@classmethod
def from_jwt(cls, token: str) -> "TokenData":
"""
Decode payload and validate token signature.
:param token: Signed and encoded payload
:return: TokenData instance
"""
try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
sub: str = payload["sub"]
return cls(user_id=payload["user_id"], session_id=payload["session_id"])
except JWTError:
raise ExpiredToken("Expired Token or signature mismatch")
except KeyError:
raise InvalidToken("Invalid Token format: {}".format(payload))
class User(SQLModel, table=True):
"""
A database-backed user account
"""
id: Optional[int] = Field(default=None, primary_key=True)
created: datetime.datetime = Field(
default=None,
sa_column=Column(
"created",
DateTime(timezone=True),
server_default=func.now(),
),
)
created_ip: str
email: str
enabled: bool = True
admin: bool = False
name: Optional[str] = None
data: dict = Field(
default_factory=dict,
sa_column=Column("data", JSON),
)
@classmethod
def from_query(cls, query: Select, session: Optional[Session] = None) -> "User":
with db.get_session(session) as s:
result = s.exec(query)
return result.one()
@classmethod
def from_email(cls, email: str, session: Optional[Session] = None) -> "User":
try:
return cls.from_query(
select(cls).where(cls.email == email), session=session
)
except NoResultFound:
raise UnknownUser(email)
@classmethod
def from_id(cls, id: int, session: Optional[Session] = None) -> "User":
try:
return cls.from_query(select(cls).where(cls.id == id), session=session)
except NoResultFound:
raise UnknownUser(id)
@classmethod
def from_token(cls, token: TokenData, session: Optional[Session] = None) -> "User":
return cls.from_id(token.user_id, session=session)
def _find_token(self, ip: str, session: Optional[Session] = None) -> "Otp":
query = (
select(Otp)
.where(Otp.user_id == self.id)
.where(Otp.ip == ip)
.where(Otp.expires > func.now())
)
with db.get_session(session) as s:
result = s.exec(query)
try:
return result.one()
except NoResultFound:
raise ExpiredOTP("Expired token for {}".format(ip))
def login(self, ip: str, session: Optional[Session] = None) -> str:
"""
Request login for the given user.
A given user can only have one active OTP at any given time.
:param ip: IP address of the request (auth must match!)
:return: otp used to authenticate the session
:raise: IncorrectOTP if the given (user, ip) pair already has
an active token (must wait before granting another).
"""
with db.get_session(session) as s:
try:
old_token_data = self._find_token(ip, session=s)
if old_token_data:
raise IncorrectOTP(detail="Previous token still active")
except ExpiredOTP:
old_token_data = None
new_otp = "{:06}".format(random.randint(0, 999999))
hashed_otp = pwd_context.hash(new_otp)
s.add(Otp(user_id=self.id, ip=ip, otp=hashed_otp))
s.commit()
return new_otp
def authenticate(
self, ip: str, otp: str, session: Optional[Session] = None
) -> TokenData:
"""
Validate OTP and generate a session token.
:param ip: IP requesting authentication must match IP passed to login()
:param otp: One time password
:return: TokenData
:raise: ExpiredOTP if the OTP doesn't exist or is expired
:raise: IncorrectOTP if OTP is found, but doesn't match
"""
with db.get_session(session) as s:
token = self._find_token(ip=ip, session=s)
if not pwd_context.verify(otp, token.otp):
raise IncorrectOTP("Bad token")
token.expires = func.now()
s.add(token)
s.commit()
return TokenData(user_id=self.id, session_id=token.id)
class Otp(SQLModel, table=True):
"""
One-time password for email / magic login.
"""
id: Optional[int] = Field(default=None, primary_key=True)
user_id: int = Field(foreign_key="user.id")
ts: datetime.datetime = Field(
default=None,
sa_column=Column(
"ts",
DateTime(timezone=True),
server_default=func.now(),
),
)
ip: str
expires: datetime.datetime = Field(
default=None,
sa_column=Column(
"expires",
DateTime(timezone=True),
server_default=text("(now() + '5 minutes'::interval)"),
),
)
otp: str
class EditableUser(BaseModel):
"""Components of the User that the User can edit"""
email: Optional[str] = None
name: Optional[str] = None
data: Dict[str, Any] = None
class AdminEditableUser(EditableUser):
"""Components of the User that an admin can edit"""
enabled: Optional[bool] = True
async def get_token(jwt_raw: str = Depends(oauth2_scheme)) -> TokenData:
"""
Depends returns a decoded TokenData (or raises Exception).
:param jwt_raw: auth token from oauth2 bearer
:return: TokenData instance
"""
return TokenData.from_jwt(jwt_raw)
def get_current(token_data: TokenData = Depends(get_token)) -> User:
"""
Fetch the User from the oauth session token.
:param token_data: TokenData from `get_token`
:return: User
"""
return User.from_token(token_data)
def get_current_active(current: User = Depends(get_current)) -> User:
"""
Provide an enabled User from the oauth session token (or raise HTTP 400).
:param current: User from `get_current`
:return: User
"""
if not current.enabled:
raise InactiveUser()
return current
def local_otp_delivery():
"""
:return: callable accepting a User and otp string, arranging for it to be sent to the user
"""
def deliver(user: User, otp: str):
# XXX: send s.otp via email!
print(
"{} magic token is: {}".format(
user.name or user.email,
otp,
),
)
return {"detail": "new OTP sent"}
return deliver