@@ -4,7 +4,6 @@ import { CredentialInterface } from "../interface/RepositoryInterface"
44import { MongoClientDB } from "../Bootstrap"
55import { ObjectId } from "mongodb"
66import { jwtVerify , SignJWT } from "jose"
7- const path = require ( 'path' ) ;
87
98const { isLocked, recordFailedAttempts, clearAttempts } = require ( "../../utils/accountLockout" )
109
@@ -94,34 +93,14 @@ export class CredentialRepository implements CredentialInterface {
9493 const JWT_SECRET = process . env . SECRET_KEY as string
9594 const secret_key = new TextEncoder ( ) . encode ( JWT_SECRET )
9695
97- const { privateDecrypt, constants } = require ( "crypto" )
98- const fs = require ( "fs" )
99-
100- const privateKeyPath = path . resolve ( process . cwd ( ) , 'private_key.pem' ) ;
101- const privateKey = fs . readFileSync ( privateKeyPath , 'utf8' ) ;
102- const encryptedPassword = secretKey
103- let decrypted
104- try {
105- decrypted = privateDecrypt (
106- {
107- key : privateKey ,
108- padding : constants . RSA_PKCS1_OAEP_PADDING ,
109- oaepHash : "sha256" ,
110- } ,
111- Buffer . from ( encryptedPassword , "base64" )
112- ) . toString ( "utf8" )
113- } catch ( error ) {
114- console . error ( "Decryption error:" , error )
115- }
116-
117- if ( isLocked ( decrypted ) ) {
96+ if ( isLocked ( secretKey ) ) {
11897 throw new Error ( "403.Account has been logged out, please try again later" )
11998 }
12099 const res = await MongoClientDB . collection ( "credential" ) . findOne ( { _deleted : false , access_key : accessKey } )
121100
122101 if ( res ?. length !== 0 && res !== null ) {
123102 const secretKeyDecrypted = Decrypt ( res ?. secret_key , "AES256" )
124- if ( decrypted === secretKeyDecrypted ) {
103+ if ( secretKey === secretKeyDecrypted ) {
125104 // Generating jwt access token
126105 try {
127106 res . access_token = await new SignJWT ( { user_id : res . _id , origin : res . origin } )
@@ -137,20 +116,18 @@ export class CredentialRepository implements CredentialInterface {
137116 . sign ( secret_key )
138117
139118 const { payload, protectedHeader } = await jwtVerify ( res . refresh_token , secret_key )
140- clearAttempts ( decrypted )
119+ clearAttempts ( secretKey )
141120 } catch ( err ) {
142121 console . log ( err )
143122 }
144-
145123 res . typeId = res ?. id
146-
147124 return res as any
148125 } else {
149- recordFailedAttempts ( decrypted )
126+ recordFailedAttempts ( secretKey )
150127 throw new Error ( "403.no-such-credentials" )
151128 }
152129 } else {
153- recordFailedAttempts ( decrypted )
130+ recordFailedAttempts ( secretKey )
154131 throw new Error ( "403.no-such-credentials" )
155132 }
156133 }
0 commit comments