Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(auth): bypassOrCallHttpsigMiddleware #792

Closed
wants to merge 3 commits into from
Closed
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
32 changes: 19 additions & 13 deletions packages/auth/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import { createValidatorMiddleware, HttpMethod } from 'openapi'
import {
grantInitiationHttpsigMiddleware,
grantContinueHttpsigMiddleware,
tokenHttpsigMiddleware
tokenHttpsigMiddleware,
bypassOrCallHttpsigMiddleware
} from './signature/middleware'

export interface AppContextData extends DefaultContext {
Expand Down Expand Up @@ -199,9 +200,11 @@ export class App {
path: '/',
method: HttpMethod.POST
}),
this.config.bypassSignatureValidation
? (ctx, next) => next()
: grantInitiationHttpsigMiddleware,
bypassOrCallHttpsigMiddleware(
this.config.bypassSignatureValidation,
grantInitiationHttpsigMiddleware,
{ injectClientKeyId: true }
),
grantRoutes.create
)

Expand All @@ -212,9 +215,10 @@ export class App {
path: '/continue/{id}',
method: HttpMethod.POST
}),
this.config.bypassSignatureValidation
? (ctx, next) => next()
: grantContinueHttpsigMiddleware,
bypassOrCallHttpsigMiddleware(
this.config.bypassSignatureValidation,
grantContinueHttpsigMiddleware
),
grantRoutes.continue
)

Expand All @@ -225,9 +229,10 @@ export class App {
path: '/token/{id}',
method: HttpMethod.POST
}),
this.config.bypassSignatureValidation
? (ctx, next) => next()
: tokenHttpsigMiddleware,
bypassOrCallHttpsigMiddleware(
this.config.bypassSignatureValidation,
tokenHttpsigMiddleware
),
accessTokenRoutes.rotate
)

Expand All @@ -238,9 +243,10 @@ export class App {
path: '/token/{id}',
method: HttpMethod.DELETE
}),
this.config.bypassSignatureValidation
? (ctx, next) => next()
: tokenHttpsigMiddleware,
bypassOrCallHttpsigMiddleware(
this.config.bypassSignatureValidation,
tokenHttpsigMiddleware
),
accessTokenRoutes.revoke
)

Expand Down
63 changes: 56 additions & 7 deletions packages/auth/src/signature/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,11 @@ async function verifySigFromClient(
client: string,
ctx: HttpSigContext
): Promise<boolean> {
const clientService = await ctx.container.use('clientService')
const clientKey = await clientService.getKey({
const clientKey = await getClientKeyOrThrow(ctx, {
client,
keyId: ctx.clientKeyId
clientKeyId: ctx.clientKeyId
})

if (!clientKey) {
ctx.throw(400, 'invalid client', { error: 'invalid_client' })
}

return verifySigAndChallenge(clientKey, ctx)
}

Expand Down Expand Up @@ -277,3 +272,57 @@ export async function tokenHttpsigMiddleware(
await verifySigFromBoundKey(grant, ctx)
await next()
}

type Middleware = (ctx: AppContext, next: () => Promise<any>) => Promise<void>

export async function getClientKeyOrThrow(
ctx: AppContext | HttpSigContext,
{ client, clientKeyId }: { client: string; clientKeyId: string }
): Promise<JWK> {
const clientService = await ctx.container.use('clientService')
const clientKey = await clientService.getKey({
client,
keyId: clientKeyId
})

if (!clientKey) {
ctx.throw(400, 'invalid client', { error: 'invalid_client' })
}

return clientKey
}

export function bypassOrCallHttpsigMiddleware(
bypassSignatureValidation: boolean,
middleware: Middleware,
args?: { injectClientKeyId: boolean }
): Middleware {
if (!bypassSignatureValidation) {
return middleware
}

return async (ctx: AppContext, next) => {
ctx.logger.info('Skipping httpsig validation')

if (args?.injectClientKeyId) {
const clientKeyId = getSigInputKeyId(
ctx.headers['signature-input'] as string
)

if (!clientKeyId) {
ctx.throw(400, 'invalid client', { error: 'invalid_client' })
}

const clientPaymentPointer = ctx.request.body?.client as string

await getClientKeyOrThrow(ctx, {
client: clientPaymentPointer,
clientKeyId
})

ctx.clientKeyId = clientKeyId
}

await next()
}
}