Skip to content

Commit

Permalink
fix error check
Browse files Browse the repository at this point in the history
  • Loading branch information
DarianM committed Nov 1, 2024
1 parent 97afcdf commit dd1b8e3
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions packages/backend/src/open_payments/auth/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,31 +67,26 @@ function toOpenPaymentsAccess(
export function createTokenIntrospectionMiddleware({
requestType,
requestAction,
canSkipAuthValidation = false
bypassError = false
}: {
requestType: AccessType
requestAction: RequestAction
canSkipAuthValidation?: boolean
bypassError?: boolean
}) {
return async (
ctx: WalletAddressUrlContext,
next: () => Promise<void>
): Promise<void> => {
const config = await ctx.container.use('config')
try {
if (canSkipAuthValidation && !ctx.request.headers.authorization) {
await next()
return
}

const authSplit = ctx.request.headers.authorization?.split(' ')
if (authSplit?.length !== 2 || authSplit[0] !== 'GNAP') {
const parts = ctx.request.headers.authorization?.split(' ')
if (parts?.length !== 2 || parts[0] !== 'GNAP') {
throw new OpenPaymentsServerRouteError(
401,
'Missing or invalid authorization header value'
)
}
const token = authSplit[1]
const token = parts[1]
const tokenIntrospectionClient = await ctx.container.use(
'tokenIntrospectionClient'
)
Expand Down Expand Up @@ -151,11 +146,15 @@ export function createTokenIntrospectionMiddleware({
}
}
} catch (err) {
if (!(err instanceof OpenPaymentsServerRouteError)) {
ctx.set('WWW-Authenticate', `GNAP as_uri=${config.authServerGrantUrl}`)
if (err instanceof OpenPaymentsServerRouteError) {
throw err
}

throw err
ctx.set('WWW-Authenticate', `GNAP as_uri=${config.authServerGrantUrl}`)

if (!bypassError) {
throw err
}
}

await next()
Expand All @@ -167,11 +166,6 @@ export const authenticatedStatusMiddleware = async (
next: () => Promise<unknown>
): Promise<void> => {
ctx.authenticated = false
if (!ctx.request.headers.authorization) {
await next()
return
}

try {
await throwIfSignatureInvalid(ctx)
ctx.authenticated = true
Expand Down

0 comments on commit dd1b8e3

Please sign in to comment.