|
| 1 | +import type { AuthPin, AuthToken, Role, User } from '@prisma/client' |
| 2 | +import type { Cookies } from '@sveltejs/kit' |
| 3 | +import { CookiesManager } from './cookies_manager' |
| 4 | +import { Database, db } from './database' |
| 5 | + |
| 6 | +export class Auth { |
| 7 | + public static async getSessionLifetimeSec(): Promise<number> { |
| 8 | + return await Database.getAppSettingInt('session_lifetime_sec') |
| 9 | + } |
| 10 | + |
| 11 | + public static async getPinCodeLifetimeSec(): Promise<number> { |
| 12 | + return await Database.getAppSettingInt('pin_code_lifetime_sec') |
| 13 | + } |
| 14 | + |
| 15 | + public static getLimit(lifetime_sec: number): Date { |
| 16 | + const limit = new Date() |
| 17 | + |
| 18 | + if (lifetime_sec == 0) console.warn('lifetime_sec is 0') |
| 19 | + |
| 20 | + limit.setSeconds(limit.getSeconds() - lifetime_sec) |
| 21 | + |
| 22 | + return limit |
| 23 | + } |
| 24 | + |
| 25 | + public static async createAuthToken( |
| 26 | + user_id: number, |
| 27 | + session_lifetime_sec: number |
| 28 | + ): Promise<AuthToken> { |
| 29 | + const session_limit = await Auth.getLimit(session_lifetime_sec) |
| 30 | + |
| 31 | + const [auth_token] = await db.$transaction([ |
| 32 | + db.authToken.create({ |
| 33 | + data: { user_id, token: crypto.randomUUID() }, |
| 34 | + }), |
| 35 | + db.authToken.deleteMany({ |
| 36 | + where: { updated_at: { lt: session_limit } }, |
| 37 | + }), |
| 38 | + ]) |
| 39 | + |
| 40 | + return auth_token |
| 41 | + } |
| 42 | + |
| 43 | + public static async updateAuthToken(auth_token_id: number): Promise<AuthToken> { |
| 44 | + const auth_token = await db.authToken.update({ |
| 45 | + where: { id: auth_token_id }, |
| 46 | + data: { updated_at: new Date() }, |
| 47 | + }) |
| 48 | + |
| 49 | + return auth_token |
| 50 | + } |
| 51 | + |
| 52 | + public static async findAuthPin(email: string, pin_code: string): Promise<AuthPin | null> { |
| 53 | + const pin_lifetime_sec = await Auth.getPinCodeLifetimeSec() |
| 54 | + const pin_limit = Auth.getLimit(pin_lifetime_sec) |
| 55 | + |
| 56 | + const auth_pin = await db.authPin.findFirst({ |
| 57 | + where: { |
| 58 | + updated_at: { gte: pin_limit }, |
| 59 | + pin_code, |
| 60 | + user: { |
| 61 | + email, |
| 62 | + }, |
| 63 | + }, |
| 64 | + }) |
| 65 | + |
| 66 | + return auth_pin |
| 67 | + } |
| 68 | + |
| 69 | + public static async signIn( |
| 70 | + user_id: number, |
| 71 | + cookies: Cookies, |
| 72 | + pin_code_id?: number |
| 73 | + ): Promise<void> { |
| 74 | + const session_lifetime_sec = await Auth.getSessionLifetimeSec() |
| 75 | + |
| 76 | + const auth_token = await Auth.createAuthToken(user_id, session_lifetime_sec) |
| 77 | + |
| 78 | + new CookiesManager(cookies).setSessionId(auth_token.token, session_lifetime_sec) |
| 79 | + |
| 80 | + if (pin_code_id) { |
| 81 | + await db.authPin.delete({ where: { id: pin_code_id } }) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + public static async signOut(cookies: Cookies): Promise<void> { |
| 86 | + const cookiesManager = new CookiesManager(cookies) |
| 87 | + |
| 88 | + await db.authToken.delete({ where: { token: cookiesManager.session_id } }) |
| 89 | + cookiesManager.deleteSessionId() |
| 90 | + } |
| 91 | + |
| 92 | + public static async accessValid(auth_token_id: number, cookies: Cookies): Promise<void> { |
| 93 | + const auth_token = await Auth.updateAuthToken(auth_token_id) |
| 94 | + const session_lifetime_sec = await Auth.getSessionLifetimeSec() |
| 95 | + |
| 96 | + new CookiesManager(cookies).setSessionId(auth_token.token, session_lifetime_sec) |
| 97 | + } |
| 98 | + |
| 99 | + public static async findAuthToken(session_id: string): Promise< |
| 100 | + | (AuthToken & { |
| 101 | + user: User & { |
| 102 | + role: Role |
| 103 | + } |
| 104 | + }) |
| 105 | + | null |
| 106 | + > { |
| 107 | + const session_lifetime_sec = await Auth.getSessionLifetimeSec() |
| 108 | + const session_limit = Auth.getLimit(session_lifetime_sec) |
| 109 | + |
| 110 | + const auth_token = await db.authToken.findFirst({ |
| 111 | + where: { |
| 112 | + updated_at: { gte: session_limit }, |
| 113 | + token: session_id, |
| 114 | + }, |
| 115 | + include: { |
| 116 | + user: { |
| 117 | + include: { |
| 118 | + role: true, |
| 119 | + }, |
| 120 | + }, |
| 121 | + }, |
| 122 | + }) |
| 123 | + |
| 124 | + return auth_token |
| 125 | + } |
| 126 | +} |
0 commit comments