1- import { FastifyRequest } from 'fastify'
1+ import { FastifyRequest , FastifyReply } from 'fastify'
22import _ from 'lodash'
33import { TenantConfiguration , tenants } from '../../data/user/tenants.js'
44import { apiUsersAndPasswords } from '../../data/user/users.js'
@@ -24,18 +24,29 @@ const localTenants = Object.values(globalTenantIdToLocalTenantIdMapping)
2424 *
2525 * @throws UnauthorizedError
2626 */
27- export function validateUserAuthorization ( username : string , password : string , req : FastifyRequest ) : void {
28- if ( apiUsersAndPasswords [ username ] && apiUsersAndPasswords [ username ] . password === password ) {
29- const tenantId = apiUsersAndPasswords [ username ] . tenantId
30- // Add user info to the request that we've validated
31- req . user = {
32- userName : username ,
33- tenantId,
34- tenantConfiguration : tenants [ tenantId ] ,
27+ export function validateUserAuthorization (
28+ username : string ,
29+ password : string ,
30+ req : FastifyRequest ,
31+ _reply : FastifyReply ,
32+ done : ( error ?: Error ) => void ,
33+ ) : void {
34+ try {
35+ if ( apiUsersAndPasswords [ username ] && apiUsersAndPasswords [ username ] . password === password ) {
36+ const tenantId = apiUsersAndPasswords [ username ] . tenantId
37+ // Add user info to the request that we've validated
38+ req . user = {
39+ userName : username ,
40+ tenantId,
41+ tenantConfiguration : tenants [ tenantId ] ,
42+ }
43+ req . log . info ( `User "${ username } " of tenant "${ tenantId } " authenticated successfully.` )
44+ done ( )
45+ } else {
46+ done ( new UnauthorizedError ( `Unknown username "${ username } " and password combination` ) )
3547 }
36- req . log . info ( `User "${ username } " of tenant "${ tenantId } " authenticated successfully.` )
37- } else {
38- throw new UnauthorizedError ( `Unknown username "${ username } " and password combination` )
48+ } catch ( error ) {
49+ done ( error instanceof Error ? error : new Error ( 'Authentication failed' ) )
3950 }
4051}
4152
0 commit comments