@@ -73,6 +73,40 @@ let Authenticator = class Authenticator {
7373 console . error ( "Error creating log:" , error ) ;
7474 }
7575 }
76+ async verifyKey ( req , res ) {
77+ const { code, userId } = req . body ;
78+ if ( ! userId ) {
79+ await this . logAction ( req , "verifyKey" , 400 ) ;
80+ return res . status ( 400 ) . send ( { message : "User ID is required" } ) ;
81+ }
82+ try {
83+ const user = await this . userService . getUser ( userId ) ;
84+ if ( ! user ) {
85+ await this . logAction ( req , "verifyKey" , 404 ) ;
86+ return res . status ( 404 ) . send ( { message : "User not found" } ) ;
87+ }
88+ const key = user . authenticator_secret ;
89+ if ( ! key || ! code ) {
90+ await this . logAction ( req , "verifyKey" , 400 ) ;
91+ return res . status ( 400 ) . send ( { message : "Key and code are required" } ) ;
92+ }
93+ const isValid = time2fa_1 . Totp . validate ( { secret : key , passcode : code } ) ;
94+ if ( isValid ) {
95+ await this . logAction ( req , "verifyKey" , 200 ) ;
96+ const apiKey = ( 0 , GenKey_1 . genKey ) ( user . user_id ) ;
97+ const jwtToken = ( 0 , Jwt_1 . generateUserJwt ) ( user , apiKey ) ;
98+ return res . status ( 200 ) . send ( { message : "Key verified successfully" , token : jwtToken } ) ;
99+ }
100+ else {
101+ await this . logAction ( req , "verifyKey" , 400 ) ;
102+ return res . status ( 400 ) . send ( { message : "Invalid key or code" } ) ;
103+ }
104+ }
105+ catch ( error ) {
106+ await this . logAction ( req , "verifyKey" , 500 , { error } ) ;
107+ handleError ( res , error , "Error verifying key" ) ;
108+ }
109+ }
76110 async handleAuthenticatorActions ( req , res ) {
77111 const action = req . params . action ;
78112 const user = req . user ;
@@ -132,54 +166,20 @@ let Authenticator = class Authenticator {
132166 handleError ( res , error , `Error in ${ action } ` ) ;
133167 }
134168 }
135- async verifyKey ( req , res ) {
136- const { code, userId } = req . body ;
137- if ( ! userId ) {
138- await this . logAction ( req , "verifyKey" , 400 ) ;
139- return res . status ( 400 ) . send ( { message : "User ID is required" } ) ;
140- }
141- try {
142- const user = await this . userService . getUser ( userId ) ;
143- if ( ! user ) {
144- await this . logAction ( req , "verifyKey" , 404 ) ;
145- return res . status ( 404 ) . send ( { message : "User not found" } ) ;
146- }
147- const key = user . authenticator_secret ;
148- if ( ! key || ! code ) {
149- await this . logAction ( req , "verifyKey" , 400 ) ;
150- return res . status ( 400 ) . send ( { message : "Key and code are required" } ) ;
151- }
152- const isValid = time2fa_1 . Totp . validate ( { secret : key , passcode : code } ) ;
153- if ( isValid ) {
154- await this . logAction ( req , "verifyKey" , 200 ) ;
155- const apiKey = ( 0 , GenKey_1 . genKey ) ( user . user_id ) ;
156- const jwtToken = ( 0 , Jwt_1 . generateUserJwt ) ( user , apiKey ) ;
157- return res . status ( 200 ) . send ( { message : "Key verified successfully" , token : jwtToken } ) ;
158- }
159- else {
160- await this . logAction ( req , "verifyKey" , 400 ) ;
161- return res . status ( 400 ) . send ( { message : "Invalid key or code" } ) ;
162- }
163- }
164- catch ( error ) {
165- await this . logAction ( req , "verifyKey" , 500 , { error } ) ;
166- handleError ( res , error , "Error verifying key" ) ;
167- }
168- }
169169} ;
170170exports . Authenticator = Authenticator ;
171171__decorate ( [
172- ( 0 , inversify_express_utils_1 . httpPost ) ( "/:action" , LoggedCheck_1 . LoggedCheck . middleware ) ,
172+ ( 0 , inversify_express_utils_1 . httpPost ) ( "/verifyKey" ) ,
173173 __metadata ( "design:type" , Function ) ,
174174 __metadata ( "design:paramtypes" , [ Object , Object ] ) ,
175175 __metadata ( "design:returntype" , Promise )
176- ] , Authenticator . prototype , "handleAuthenticatorActions " , null ) ;
176+ ] , Authenticator . prototype , "verifyKey " , null ) ;
177177__decorate ( [
178- ( 0 , inversify_express_utils_1 . httpPost ) ( "/verifyKey" ) ,
178+ ( 0 , inversify_express_utils_1 . httpPost ) ( "/:action" , LoggedCheck_1 . LoggedCheck . middleware ) ,
179179 __metadata ( "design:type" , Function ) ,
180180 __metadata ( "design:paramtypes" , [ Object , Object ] ) ,
181181 __metadata ( "design:returntype" , Promise )
182- ] , Authenticator . prototype , "verifyKey " , null ) ;
182+ ] , Authenticator . prototype , "handleAuthenticatorActions " , null ) ;
183183exports . Authenticator = Authenticator = __decorate ( [
184184 ( 0 , inversify_express_utils_1 . controller ) ( "/authenticator" ) ,
185185 __param ( 0 , ( 0 , inversify_1 . inject ) ( "UserService" ) ) ,
0 commit comments