Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/client/Mppx.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type * as Challenge from '../Challenge.js'
import * as Errors from '../Errors.js'
import type * as Method from '../Method.js'
import type * as z from '../zod.js'
import * as Fetch from './internal/Fetch.js'
Expand Down Expand Up @@ -81,6 +82,11 @@ export function create<
async createCredential(response: Transport.ResponseOf<transport>, context?: unknown) {
const challenge = transport.getChallenge(response as never) as Challenge.Challenge

// Validate challenge expiration before creating credential (client-side early rejection)
if (challenge.expires && new Date(challenge.expires) < new Date()) {
throw new Errors.PaymentExpiredError({ expires: challenge.expires })
}

const mi = methods.find((m) => m.name === challenge.method && m.intent === challenge.intent)
if (!mi)
throw new Error(
Expand Down
11 changes: 11 additions & 0 deletions src/client/internal/Fetch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as Challenge from '../../Challenge.js'
import * as Errors from '../../Errors.js'
import type * as Method from '../../Method.js'
import type * as z from '../../zod.js'

Expand Down Expand Up @@ -72,6 +73,9 @@ export function from<const methods extends readonly Method.AnyClient[]>(
`No method found for challenges: ${challenges.map((c) => `${c.method}.${c.intent}`).join(', ')}. Available: ${methods.map((m) => `${m.name}.${m.intent}`).join(', ')}`,
)

// Validate challenge expiration before creating credential (client-side early rejection)
validateChallengeExpiration(challenge)

const onChallengeCredential = onChallenge
? await onChallenge(challenge, {
createCredential: async (overrideContext?: AnyContextFor<methods>) =>
Expand Down Expand Up @@ -240,6 +244,13 @@ function validateCredentialHeaderValue(credential: string): void {
}
}

/** @internal Validates that a challenge has not expired. */
function validateChallengeExpiration(challenge: Challenge.Challenge): void {
if (challenge.expires && new Date(challenge.expires) < new Date()) {
throw new Errors.PaymentExpiredError({ expires: challenge.expires })
}
}

/** @internal */
async function resolveCredential(
challenge: Challenge.Challenge,
Expand Down