auths.py 3.39 KB
Newer Older
1
2
3
4
from pydantic import BaseModel
from typing import List, Union, Optional
import time
import uuid
Timothy J. Baek's avatar
Timothy J. Baek committed
5
from peewee import *
6
7

from apps.web.models.users import UserModel, Users
Timothy J. Baek's avatar
Timothy J. Baek committed
8
from utils.utils import (
9
10
11
12
13
14
    verify_password,
    get_password_hash,
    bearer_scheme,
    create_token,
)

Timothy J. Baek's avatar
Timothy J. Baek committed
15
from apps.web.internal.db import DB
16
17
18
19
20
21

####################
# DB MODEL
####################


Timothy J. Baek's avatar
Timothy J. Baek committed
22
23
24
25
26
27
28
29
30
31
class Auth(Model):
    id = CharField(unique=True)
    email = CharField()
    password = CharField()
    active = BooleanField()

    class Meta:
        database = DB


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class AuthModel(BaseModel):
    id: str
    email: str
    password: str
    active: bool = True


####################
# Forms
####################


class Token(BaseModel):
    token: str
    token_type: str


class UserResponse(BaseModel):
    id: str
    email: str
    name: str
    role: str
Timothy J. Baek's avatar
Timothy J. Baek committed
54
    profile_image_url: str
55
56
57
58
59
60
61
62
63
64
65


class SigninResponse(Token, UserResponse):
    pass


class SigninForm(BaseModel):
    email: str
    password: str


66
67
68
69
class ProfileImageUrlForm(BaseModel):
    profile_image_url: str


70
71
72
73
74
class UpdateProfileForm(BaseModel):
    profile_image_url: str
    name: str


75
76
77
78
79
class UpdatePasswordForm(BaseModel):
    password: str
    new_password: str


80
81
82
83
84
85
86
87
88
class SignupForm(BaseModel):
    name: str
    email: str
    password: str


class AuthsTable:
    def __init__(self, db):
        self.db = db
Timothy J. Baek's avatar
Timothy J. Baek committed
89
        self.db.create_tables([Auth])
90

Timothy J. Baek's avatar
Timothy J. Baek committed
91
92
93
    def insert_new_auth(
        self, email: str, password: str, name: str, role: str = "pending"
    ) -> Optional[UserModel]:
94
95
96
97
        print("insert_new_auth")

        id = str(uuid.uuid4())

Timothy J. Baek's avatar
Timothy J. Baek committed
98
99
100
        auth = AuthModel(
            **{"id": id, "email": email, "password": password, "active": True}
        )
Timothy J. Baek's avatar
Timothy J. Baek committed
101
102
        result = Auth.create(**auth.model_dump())

103
104
105
106
107
108
109
        user = Users.insert_new_user(id, name, email, role)

        if result and user:
            return user
        else:
            return None

Timothy J. Baek's avatar
Timothy J. Baek committed
110
    def authenticate_user(self, email: str, password: str) -> Optional[UserModel]:
Timothy J. Baek's avatar
Timothy J. Baek committed
111
        print("authenticate_user", email)
Timothy J. Baek's avatar
Timothy J. Baek committed
112
113
114
115
116
117
118
119
        try:
            auth = Auth.get(Auth.email == email, Auth.active == True)
            if auth:
                if verify_password(password, auth.password):
                    user = Users.get_user_by_id(auth.id)
                    return user
                else:
                    return None
120
121
            else:
                return None
Timothy J. Baek's avatar
Timothy J. Baek committed
122
        except:
123
124
            return None

Timothy J. Baek's avatar
Timothy J. Baek committed
125
    def update_user_password_by_id(self, id: str, new_password: str) -> bool:
126
        try:
Timothy J. Baek's avatar
Timothy J. Baek committed
127
128
            query = Auth.update(password=new_password).where(Auth.id == id)
            result = query.execute()
Timothy J. Baek's avatar
Timothy J. Baek committed
129
130

            return True if result == 1 else False
131
132
133
        except:
            return False

Timothy J. Baek's avatar
Timothy J. Baek committed
134
135
136
137
138
139
140
141
142
    def update_email_by_id(self, id: str, email: str) -> bool:
        try:
            query = Auth.update(email=email).where(Auth.id == id)
            result = query.execute()

            return True if result == 1 else False
        except:
            return False

143
    def delete_auth_by_id(self, id: str) -> bool:
Timothy J. Baek's avatar
Timothy J. Baek committed
144
145
146
147
148
149
150
        try:
            # Delete User
            result = Users.delete_user_by_id(id)

            if result:
                # Delete Auth
                query = Auth.delete().where(Auth.id == id)
Timothy J. Baek's avatar
Timothy J. Baek committed
151
                query.execute()  # Remove the rows, return number of rows removed.
Timothy J. Baek's avatar
Timothy J. Baek committed
152
153
154
155
156
157
158

                return True
            else:
                return False
        except:
            return False

159
160

Auths = AuthsTable(DB)