import { inject , Provider } from '@loopback/context' ; import { HttpErrors } from '@loopback/rest' ; import { verify } from 'jsonwebtoken' ; import { VerifyFunction } from 'loopback4-authentication' ; import moment from 'moment-timezone' ; import { ILogger , LOGGER } from '../../logger-extension' ; import { IAuthUserWithPermissions } from '../keys' ; export class ServicesBearerTokenVerifyProvider implements Provider < VerifyFunction . BearerFn > { constructor ( @ inject ( LOGGER . LOGGER_INJECT ) public logger ) { } value ( ) { return async ( token ) => { let user ; try { user = verify ( token , process . env . JWT_SECRET as string , { issuer : process . env . JWT_ISSUER , algorithms : [ 'HS256' ] , } ) as IAuthUserWithPermissions ; } catch ( error ) { this . logger . error ( JSON . stringify ( error ) ) ; throw new HttpErrors . Unauthorized ( 'TokenExpired' ) ; } if ( user . passwordExpiryTime && moment ( ) . isSameOrAfter ( moment ( user . passwordExpiryTime ) ) ) { throw new HttpErrors . Unauthorized ( 'PasswordExpiryError' ) ; } return user ; } ; } }