@@ -2,20 +2,11 @@ import express from 'express';
22import jwt from 'jsonwebtoken' ;
33import { Logger } from '../../common/logger' ;
44import { AuthenticationResult } from '../../domain.types/auth/auth.domain.types' ;
5- import { CurrentClient } from '../../domain.types/miscellaneous/current.client' ;
6- import { ApiClientService } from '../../services/api.client.service' ;
7- import { Loader } from '../../startup/loader' ;
85import { IAuthenticator } from '../authenticator.interface' ;
96
107//////////////////////////////////////////////////////////////
118
129export class CustomAuthenticator implements IAuthenticator {
13- _clientService : ApiClientService = null ;
14-
15- constructor ( ) {
16- this . _clientService = Loader . container . resolve ( ApiClientService ) ;
17- }
18-
1910 public authenticateUser = async ( request : express . Request ) : Promise < AuthenticationResult > => {
2011 try {
2112 var res : AuthenticationResult = {
@@ -57,43 +48,4 @@ export class CustomAuthenticator implements IAuthenticator {
5748 }
5849 return res ;
5950 } ;
60-
61- public authenticateClient = async ( request : express . Request ) : Promise < AuthenticationResult > => {
62- try {
63- var res : AuthenticationResult = {
64- Result : true ,
65- Message : 'Authenticated' ,
66- HttpErrorCode : 200 ,
67- } ;
68- let apiKey : string = request . headers [ 'x-api-key' ] as string ;
69- if ( ! apiKey ) {
70- res = {
71- Result : false ,
72- Message : 'Missing API key for the client' ,
73- HttpErrorCode : 401 ,
74- } ;
75- return res ;
76- }
77- apiKey = apiKey . trim ( ) ;
78-
79- const client : CurrentClient = await this . _clientService . isApiKeyValid ( apiKey ) ;
80- if ( ! client ) {
81- res = {
82- Result : false ,
83- Message : 'Invalid API Key: Forebidden access' ,
84- HttpErrorCode : 403 ,
85- } ;
86- return res ;
87- }
88- request . currentClient = client ;
89- } catch ( err ) {
90- Logger . instance ( ) . log ( JSON . stringify ( err , null , 2 ) ) ;
91- res = {
92- Result : false ,
93- Message : 'Error authenticating client' ,
94- HttpErrorCode : 401 ,
95- } ;
96- }
97- return res ;
98- } ;
9951}
0 commit comments