From 74ff4a8b364d68ce57d054e6ab6c9ffb659dcab6 Mon Sep 17 00:00:00 2001 From: Timo Glastra Date: Thu, 7 Nov 2024 18:16:56 +0530 Subject: [PATCH] fix: content type parsing Signed-off-by: Timo Glastra --- .../oauth2/src/metadata/fetch-well-known-metadata.ts | 2 +- .../oid4vci/src/credential-offer/credential-offer.ts | 2 +- packages/oid4vci/src/notification/notification.ts | 9 ++++----- packages/utils/src/content-type.ts | 12 ++++++++++++ packages/utils/src/index.ts | 2 +- packages/utils/src/valibot-fetcher.ts | 4 ++-- 6 files changed, 21 insertions(+), 10 deletions(-) diff --git a/packages/oauth2/src/metadata/fetch-well-known-metadata.ts b/packages/oauth2/src/metadata/fetch-well-known-metadata.ts index e32d635..873b436 100644 --- a/packages/oauth2/src/metadata/fetch-well-known-metadata.ts +++ b/packages/oauth2/src/metadata/fetch-well-known-metadata.ts @@ -28,7 +28,7 @@ export async function fetchWellKnownMetadata( if (!response.ok) { throw new Oauth2InvalidFetchResponseError( - `Fetching well known metadata from '${wellKnownMetadataUrl}' did resulted in an unsuccessfull response with status '${response.status}'.`, + `Fetching well known metadata from '${wellKnownMetadataUrl}' resulted in an unsuccessfull response with status '${response.status}'.`, await response.clone().text(), response ) diff --git a/packages/oid4vci/src/credential-offer/credential-offer.ts b/packages/oid4vci/src/credential-offer/credential-offer.ts index 5265f6f..3cea665 100644 --- a/packages/oid4vci/src/credential-offer/credential-offer.ts +++ b/packages/oid4vci/src/credential-offer/credential-offer.ts @@ -55,7 +55,7 @@ export async function resolveCredentialOffer( const { response, result } = await fetchWithValibot(vCredentialOfferObject, parsedQueryParams.credential_offer_uri) if (!response.ok || !result) { throw new Oauth2InvalidFetchResponseError( - `Fetching well known metadata from '${parsedQueryParams.credential_offer_uri}' did resulted in an unsuccesfull response with status '${response.status}'`, + `Fetching credential offer from '${parsedQueryParams.credential_offer_uri}' resulted in an unsuccesfull response with status '${response.status}'`, await response.clone().text(), response ) diff --git a/packages/oid4vci/src/notification/notification.ts b/packages/oid4vci/src/notification/notification.ts index fc18cc6..33167df 100644 --- a/packages/oid4vci/src/notification/notification.ts +++ b/packages/oid4vci/src/notification/notification.ts @@ -8,7 +8,7 @@ import { type ResourceRequestResponseOk, resourceRequest, } from '@animo-id/oauth2' -import { ContentType, parseWithErrorHandling } from '@animo-id/oauth2-utils' +import { ContentType, isResponseContentType, parseWithErrorHandling } from '@animo-id/oauth2-utils' import type { IssuerMetadataResult } from '../metadata/fetch-issuer-metadata' import { type NotificationEvent, @@ -106,10 +106,9 @@ export async function sendNotifcation( }) if (!resourceResponse.ok) { - const notificationErrorResponseResult = - resourceResponse.response.headers.get('Content-Type') === ContentType.Json - ? v.safeParse(vNotificationErrorResponse, await resourceResponse.response.clone().json()) - : undefined + const notificationErrorResponseResult = isResponseContentType(ContentType.Json, resourceResponse.response) + ? v.safeParse(vNotificationErrorResponse, await resourceResponse.response.clone().json()) + : undefined return { ...resourceResponse, diff --git a/packages/utils/src/content-type.ts b/packages/utils/src/content-type.ts index 3bf99ed..8061e9b 100644 --- a/packages/utils/src/content-type.ts +++ b/packages/utils/src/content-type.ts @@ -1,4 +1,16 @@ +import type { FetchResponse } from './globals' + export enum ContentType { XWwwFormUrlencoded = 'application/x-www-form-urlencoded', Json = 'application/json', } + +export function isContentType(contentType: ContentType, value: string) { + return value.toLowerCase().trim().split(';')[0] === contentType +} + +export function isResponseContentType(contentType: ContentType, response: FetchResponse) { + const header = response.headers.get('Content-Type') + if (!header) return false + return isContentType(contentType, header) +} diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index bc3047f..0153f14 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -32,5 +32,5 @@ export { type WwwAuthenticateHeaderChallenge, encodeWwwAuthenticateHeader, } from './www-authenticate' -export { ContentType } from './content-type' +export { ContentType, isContentType, isResponseContentType } from './content-type' export { setGlobalConfig, type Oid4vcTsConfig, getGlobalConfig } from './config' diff --git a/packages/utils/src/valibot-fetcher.ts b/packages/utils/src/valibot-fetcher.ts index 6ea1295..ddc60f2 100644 --- a/packages/utils/src/valibot-fetcher.ts +++ b/packages/utils/src/valibot-fetcher.ts @@ -1,5 +1,5 @@ import * as v from 'valibot' -import { ContentType } from './content-type' +import { ContentType, isResponseContentType } from './content-type' import type { Fetch } from './globals' // biome-ignore lint/suspicious/noExplicitAny: any type needed for generic @@ -46,7 +46,7 @@ export function createValibotFetcher( return { response, result: - response.ok && response.headers.get('Content-Type') === ContentType.Json + response.ok && isResponseContentType(ContentType.Json, response) ? v.safeParse(schema, await response.json()) : undefined, }