Skip to content

Commit

Permalink
fix: change middleware typing
Browse files Browse the repository at this point in the history
  • Loading branch information
njlie committed Dec 12, 2022
1 parent b54bcba commit 20ff4fd
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 72 deletions.
121 changes: 50 additions & 71 deletions packages/auth/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,20 @@ import Router from '@koa/router'
import { IAppConfig } from './config/app'
import { ClientService } from './client/service'
import { GrantService } from './grant/service'
import { AccessTokenRoutes } from './accessToken/routes'
import {
CreateContext,
ContinueContext,
StartContext,
GetContext,
ChooseContext,
FinishContext
} from './grant/routes'
import {
AccessTokenRoutes,
IntrospectContext,
RevokeContext,
RotateContext
} from './accessToken/routes'
import { createValidatorMiddleware, HttpMethod } from 'openapi'

import {
Expand All @@ -36,13 +49,6 @@ export interface AppContextData extends DefaultContext {

export type AppContext = Koa.ParameterizedContext<DefaultState, AppContextData>

type ContextType<T> = T extends (
ctx: infer Context
// eslint-disable-next-line @typescript-eslint/no-explicit-any
) => any
? Context
: never

export interface DatabaseCleanupRule {
/**
* the name of the column containing the starting time from which the age will be computed
Expand Down Expand Up @@ -209,13 +215,10 @@ export class App {
} = grantRoutes
this.publicRouter.post(
'/',
createValidatorMiddleware<ContextType<typeof grantCreate>>(
openApi.authServerSpec,
{
path: '/',
method: HttpMethod.POST
}
),
createValidatorMiddleware<CreateContext>(openApi.authServerSpec, {
path: '/',
method: HttpMethod.POST
}),
this.config.bypassSignatureValidation
? (ctx, next) => next()
: grantInitiationHttpsigMiddleware,
Expand All @@ -225,13 +228,10 @@ export class App {
// Grant Continue
this.publicRouter.post(
'/continue/:id',
createValidatorMiddleware<ContextType<typeof grantContinue>>(
openApi.authServerSpec,
{
path: '/continue/{id}',
method: HttpMethod.POST
}
),
createValidatorMiddleware<ContinueContext>(openApi.authServerSpec, {
path: '/continue/{id}',
method: HttpMethod.POST
}),
this.config.bypassSignatureValidation
? (ctx, next) => next()
: grantContinueHttpsigMiddleware,
Expand All @@ -250,13 +250,10 @@ export class App {
} = accessTokenRoutes
this.publicRouter.post(
'/token/:id',
createValidatorMiddleware<ContextType<typeof tokenRotate>>(
openApi.authServerSpec,
{
path: '/token/{id}',
method: HttpMethod.POST
}
),
createValidatorMiddleware<RotateContext>(openApi.authServerSpec, {
path: '/token/{id}',
method: HttpMethod.POST
}),
this.config.bypassSignatureValidation
? (ctx, next) => next()
: tokenHttpsigMiddleware,
Expand All @@ -266,13 +263,10 @@ export class App {
// Token Revocation
this.publicRouter.delete(
'/token/:id',
createValidatorMiddleware<ContextType<typeof tokenRevoke>>(
openApi.authServerSpec,
{
path: '/token/{id}',
method: HttpMethod.DELETE
}
),
createValidatorMiddleware<RevokeContext>(openApi.authServerSpec, {
path: '/token/{id}',
method: HttpMethod.DELETE
}),
this.config.bypassSignatureValidation
? (ctx, next) => next()
: tokenHttpsigMiddleware,
Expand All @@ -283,13 +277,10 @@ export class App {
// Token Introspection
this.publicRouter.post(
'/introspect',
createValidatorMiddleware<ContextType<typeof tokenIntrospect>>(
openApi.tokenIntrospectionSpec,
{
path: '/introspect',
method: HttpMethod.POST
}
),
createValidatorMiddleware<IntrospectContext>(openApi.tokenIntrospectionSpec, {
path: '/introspect',
method: HttpMethod.POST
}),
tokenIntrospect
)

Expand All @@ -311,52 +302,40 @@ export class App {
// Interaction start
this.publicRouter.get(
'/interact/:id/:nonce',
createValidatorMiddleware<ContextType<typeof interactionStart>>(
openApi.idpSpec,
{
path: '/interact/{id}/{nonce}',
method: HttpMethod.GET
}
),
createValidatorMiddleware<StartContext>(openApi.idpSpec, {
path: '/interact/{id}/{nonce}',
method: HttpMethod.GET
}),
interactionStart
)

// Interaction finish
this.publicRouter.get(
'/interact/:id/:nonce/finish',
createValidatorMiddleware<ContextType<typeof interactionFinish>>(
openApi.idpSpec,
{
path: '/interact/{id}/{nonce}/finish',
method: HttpMethod.GET
}
),
createValidatorMiddleware<FinishContext>(openApi.idpSpec, {
path: '/interact/{id}/{nonce}/finish',
method: HttpMethod.GET
}),
interactionFinish
)

// Grant lookup
this.publicRouter.get(
'/grant/:id/:nonce',
createValidatorMiddleware<ContextType<typeof interactionDetails>>(
openApi.idpSpec,
{
path: '/grant/{id}/{nonce}',
method: HttpMethod.GET
}
),
createValidatorMiddleware<GetContext>(openApi.idpSpec, {
path: '/grant/{id}/{nonce}',
method: HttpMethod.GET
}),
interactionDetails
)

// Grant accept/reject
this.publicRouter.post(
'/grant/:id/:nonce/:choice',
createValidatorMiddleware<ContextType<typeof interactionAcceptOrReject>>(
openApi.idpSpec,
{
path: '/grant/{id}/{nonce}/{choice}',
method: HttpMethod.POST
}
),
createValidatorMiddleware<ChooseContext>(openApi.idpSpec, {
path: '/grant/{id}/{nonce}/{choice}',
method: HttpMethod.POST
}),
interactionAcceptOrReject
)

Expand Down
2 changes: 1 addition & 1 deletion packages/openapi/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { OpenAPI, RequestOptions, isValidationError } from './'

import Koa from 'koa'

export function createValidatorMiddleware<T extends Koa.ParameterizedContext>(
export function createValidatorMiddleware<T extends Koa.DefaultContext>(
spec: OpenAPI,
options: RequestOptions
): (ctx: Koa.Context, next: () => Promise<unknown>) => Promise<void> {
Expand Down

0 comments on commit 20ff4fd

Please sign in to comment.