import { DiagnosticSeverity } from '@stoplight/types'; import * as E from 'fp-ts/Either'; import { pipe } from 'fp-ts/function'; import { identity } from 'fp-ts/function'; import type { IPrismDiagnostic } from '@stoplight/prism-core'; import type { IHttpRequest } from '../../../../types'; export type ValidateSecurityFn = ( input: Pick, name: string ) => E.Either; export const genRespForScheme = ( isSchemeProper: boolean, isCredsGiven: boolean, msg: string ): E.Either => pipe( isSchemeProper, E.fromPredicate(identity, () => genUnauthorisedErr(msg)), E.chain(() => when(isCredsGiven, undefined)) ); export const genUnauthorisedErr = (msg?: string): IPrismDiagnostic => ({ severity: DiagnosticSeverity.Error, message: 'Invalid security scheme used', code: 401, tags: msg ? [msg] : [], }); export function isScheme(shouldBeScheme: string, authScheme: string) { return authScheme.toLowerCase() === shouldBeScheme; } export const when = (condition: boolean, errorMessage?: string): E.Either => pipe( condition, E.fromPredicate(identity, () => genUnauthorisedErr(errorMessage)) );