import { inject } from '@loopback/context'; import { FindRoute, HttpErrors, InvokeMethod, ParseParams, Reject, RequestContext, RestBindings, Send, SequenceHandler, } from '@loopback/rest'; import { AuthenticationBindings, AuthenticateFn } from 'loopback4-authentication'; import { AuthorizationBindings, AuthorizeErrorKeys, AuthorizeFn } from 'loopback4-authorization'; import { AuthPayLoad } from './models'; import { repository } from '@loopback/repository'; import { BlockedUserRepository, ProviderRepository } from './repositories'; import { UserBlockingService } from './services/user-blocking.service'; const SequenceActions = RestBindings.SequenceActions; export class MySequence implements SequenceHandler { // Declare the clientMiddleware property constructor( @inject(SequenceActions.FIND_ROUTE) protected findRoute: FindRoute, @inject(SequenceActions.PARSE_PARAMS) protected parseParams: ParseParams, @inject(SequenceActions.INVOKE_METHOD) protected invoke: InvokeMethod, @inject(SequenceActions.SEND) public send: Send, @inject(SequenceActions.REJECT) public reject: Reject, @inject(AuthenticationBindings.USER_AUTH_ACTION) protected authenticateRequest: AuthenticateFn, @inject(AuthorizationBindings.AUTHORIZE_ACTION) protected checkAuthorisation: AuthorizeFn, @repository(ProviderRepository) public providersRepository: ProviderRepository, @repository(BlockedUserRepository) public blockedUserRepository: BlockedUserRepository, @inject('services.UserBlockingService') private userBlockingService: UserBlockingService, ) { } async handle(context: RequestContext) { try { // Execute client middleware const { request, response } = context; const route = this.findRoute(request); const args = await this.parseParams(request, route); request.body = args[args.length - 1]; if (request.path.startsWith('/api')) { // Check if the user associated with this request is blocked const ip = request.ip; const device = request.headers['user-agent'] || ''; // You can use user-agent for device info const blockedUser = await this.userBlockingService.getTimeUntilUnblock(ip!, device) if (blockedUser) { throw new HttpErrors.Forbidden('Your account has been temporarily blocked due to suspicious activity try after ' + blockedUser + "."); } const clientId = request.headers['x-client-id']; const clientSecret = request.headers['x-client-secret']; if (!clientId || !clientSecret) { const headersToLog = { ...request.headers }; // 3. Convert to JSON string for easier logging/reading const headersString = JSON.stringify(headersToLog, null, 2); // Pretty-print with indentation // 4. Add headers string to the error message or a custom property const message = `\n\nRequest Headers:\n${headersString}`; throw new HttpErrors.Unauthorized('Client ID or secret is not provided in the headers.' + message); } // Find a provider with the given client_id and client_secret const provider = await this.providersRepository.findOne({ where: { client_id: clientId, client_secret: clientSecret, is_deleted: false }, }); if (!provider) { // If the provider is not found throw new HttpErrors.Unauthorized('Invalid Client ID or secret.'); } } const authUser: AuthPayLoad = await this.authenticateRequest( request, response ); const isAccessAllowed: boolean = await this.checkAuthorisation( authUser?.permissions, request, ); if (!isAccessAllowed) { throw new HttpErrors.Forbidden(AuthorizeErrorKeys.NotAllowedAccess); } const result = await this.invoke(route, args); this.send(response, result); } catch (err) { this.reject(context, err); } } }