From 58efac3074258343f2ed09a5592304348897283c Mon Sep 17 00:00:00 2001 From: Timo Glastra Date: Thu, 7 Nov 2024 11:14:59 +0530 Subject: [PATCH] feat: encode unauthorized header value Signed-off-by: Timo Glastra --- .../src/error/Oauth2ResourceUnauthorizedError.ts | 16 +++++++++++++++- packages/utils/src/index.ts | 6 +++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/packages/oauth2/src/error/Oauth2ResourceUnauthorizedError.ts b/packages/oauth2/src/error/Oauth2ResourceUnauthorizedError.ts index 7012eec..d221d5d 100644 --- a/packages/oauth2/src/error/Oauth2ResourceUnauthorizedError.ts +++ b/packages/oauth2/src/error/Oauth2ResourceUnauthorizedError.ts @@ -1,4 +1,4 @@ -import { parseWwwAuthenticateHeader } from '@animo-id/oauth2-utils' +import { encodeWwwAuthenticateHeader, parseWwwAuthenticateHeader } from '@animo-id/oauth2-utils' import type { SupportedAuthenticationScheme } from '../access-token/verify-access-token' import type { Oauth2ErrorCodes } from '../common/v-oauth2-error' import { Oauth2Error } from './Oauth2Error' @@ -56,4 +56,18 @@ export class Oauth2ResourceUnauthorizedError extends Oauth2Error { ) ) } + + public toHeaderValue() { + return encodeWwwAuthenticateHeader( + this.wwwAuthenticateHeaders.map((header) => ({ + scheme: header.scheme, + payload: { + error: header.error ?? null, + error_description: header.error_description ?? null, + scope: header.scope ?? null, + ...header.additionalPayload, + }, + })) + ) + } } diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index c46e911..8e890fc 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -19,5 +19,9 @@ export { type ValibotFetcher, createValibotFetcher, defaultFetcher } from './val export { type HttpMethod, vHttpMethod, vHttpsUrl, vInteger } from './validation' export { mergeDeep } from './object' export { arrayEqualsIgnoreOrder } from './array' -export { parseWwwAuthenticateHeader, type WwwAuthenticateHeaderChallenge } from './www-authenticate' +export { + parseWwwAuthenticateHeader, + type WwwAuthenticateHeaderChallenge, + encodeWwwAuthenticateHeader, +} from './www-authenticate' export { ContentType } from './content-type'