1+ import { Static , t } from "elysia" ;
2+ import { prisma } from "../../lib/prisma" ;
3+ import jwt from "jsonwebtoken" ;
4+ import crypto from "crypto" ;
5+
6+ const AuthSchema = t . Object ( {
7+ authToken : t . String ( ) ,
8+ } )
9+
10+ export default {
11+ post : {
12+ handler : async ( { body, request } : { body : Static < typeof AuthSchema > , request : Request } ) => {
13+ if ( process . env . UNAUTHORIZED_BYPASS_ENABLED !== "true" && request . headers . get ( "origin" ) !== process . env . MAIN_WEBSITE_URL ) return new Response ( "Unauthorized" , { status : 401 } )
14+ let tokenpayload ;
15+ try {
16+ tokenpayload = JSON . parse ( Buffer . from ( body . authToken . split ( "." ) [ 1 ] , "base64" ) . toString ( "utf-8" ) )
17+ } catch {
18+ return new Response ( "Unauthorized" , { status : 401 } ) ;
19+ }
20+ if ( ! tokenpayload ?. token ) return new Response ( "Unauthorized" , { status : 401 } ) ;
21+ const [ encryptedtoken , icodev ] = String ( tokenpayload . token ) . split ( ":" )
22+ if ( ! encryptedtoken || ! icodev ) return new Response ( "Unauthorized" , { status : 401 } ) ;
23+ const device = await prisma . devices . findUnique ( {
24+ where : {
25+ icodev : icodev ,
26+ } ,
27+ include : {
28+ user : true
29+ }
30+ } )
31+ if ( ! device || ! device ?. user || ! device ?. token ) return new Response ( "Unauthorized" , { status : 401 } ) ;
32+ let jsondecoded ;
33+ try {
34+ jsondecoded = jwt . verify ( body . authToken , device . user . randompwd )
35+ } catch ( err : any ) {
36+ return new Response ( "Unauthorized" , { status : 401 } ) ;
37+ }
38+ if ( typeof jsondecoded !== "object" || jsondecoded === null || ! ( jsondecoded as any ) . token ) return new Response ( "Unauthorized" , { status : 401 } ) ;
39+ const tokensep = String ( ( jsondecoded as any ) . token ) . split ( ":" )
40+ if ( tokensep . length !== 2 ) return new Response ( "Unauthorized" , { status : 401 } ) ;
41+ const key = new TextEncoder ( ) . encode ( device . user . hashedpwd . split ( '' ) . reverse ( ) . join ( '' ) ) . slice ( 0 , 32 )
42+ const ivcode = Buffer . from ( device . ivcode , "base64" ) ;
43+ const decipher = crypto . createDecipheriv ( "aes-256-cbc" , key , ivcode )
44+ let decrypted = decipher . update ( tokensep [ 0 ] , "hex" , "utf8" )
45+ decrypted += decipher . final ( "utf8" )
46+ const decrypteddata = JSON . parse ( decrypted ) ;
47+ if ( ! decrypteddata || typeof decrypteddata !== "object" ) return new Response ( "Unauthorized" , { status : 401 } ) ;
48+ if ( decrypteddata . tokenExpiry < Date . now ( ) ) return new Response ( "Unauthorized" , { status : 401 } ) ;
49+ if ( decrypteddata . reftoken !== device . token ) return new Response ( "Unauthorized" , { status : 401 } ) ;
50+ if ( decrypteddata . email !== device . user . email || decrypteddata . id !== device . user . id || decrypteddata . verified !== device . user . emailVerified || decrypteddata . createdAt !== device . user . createdAt . toISOString ( ) || decrypteddata . passwordchangeAt !== device . user . passwordchangeAt ?. toISOString ( ) || decrypteddata . role !== device . user . platformRole ) return new Response ( "Unauthorized" , { status : 401 } ) ;
51+ if ( device . user . emailVerified ) return new Response ( "Email already verified" , { status : 400 } ) ;
52+
53+ } ,
54+ schema : {
55+ body : AuthSchema
56+ }
57+ }
58+ }
0 commit comments