import { Injectable } from "@nestjs/common"; export type JwtConfig = { secret: string; expiry: string; issuer: string; }; export type ValidateRequest = (request: any, claims: any) => boolean; export type MutateUserResponseAccess = (userResponseAccess: any) => any; export class ApiJwtConfig { secret: string; expiry: string; issuer: string; constructor(options: ApiJwtConfigOptions) { this.issuer = options.issuer; this.secret = options.secret this.expiry = options.expiry ? options.expiry : '1d'; } } export type LaunchpadApiClientAuthConfigCustomMethods = { /** * Optional method to validate the request of a specific route against the claims on the Api's jwt */ validateApiRequest?: ValidateRequest; /** * Optional method to mutate the response given by the LaunchPad api get user info route into a formate consumable by this api */ mutateUserResponseAccess?: MutateUserResponseAccess; }; export type ApiJwtConfigOptions = { secret: string; issuer: string; expiry?: string; }; @Injectable() export class LaunchpadApiClientAuthConfig { /** * Shared api key */ launchPadApiUrl: string; /** * Shared jwt secret */ launchPadJwtSecret: string; /** * Shared api key */ launchPadApiKey: string; /** * The descriptive name of the api where this code is executed - for logging and signing of jwts */ apiName: string; /** * Optional jwtConfig for this api's signing of jwt tokens with its client app. For setting custom token expiry, persisent secrets, and signatures */ apiJwtConfig: JwtConfig; /** * Optional methods to validate a requests and mutate responses */ customMethods: LaunchpadApiClientAuthConfigCustomMethods; constructor( launchPadApiUrl: string, launchPadJwtSecret: string, launchPadApiKey: string, apiName: string, apiJwtSecret: string, customMethods: LaunchpadApiClientAuthConfigCustomMethods, ) { this.launchPadApiUrl = launchPadApiUrl; this.launchPadApiKey = launchPadApiKey; this.launchPadJwtSecret = launchPadJwtSecret; this.apiName = apiName; this.apiJwtConfig = new ApiJwtConfig({ issuer: apiName, secret: apiJwtSecret }); this.customMethods = customMethods; } }