Skip to content

Commit

Permalink
feat(backend): adding OpenPaymentsServerRouteError (#2635)
Browse files Browse the repository at this point in the history
* feat(backend): adding OpenPaymentsServerRouteError

* chore(backend): updating auth middleware, adding tests

* chore(backend): adding tests for openPaymentServerErrorMiddleware

* chore(backend): rename middleware

* chore(backend): fix lint

* chore(backend): validate OP RS responses in dev environments

* chore(backend): rename errors file to route-errors

* feat(backend): use throwIfMissingWalletAddress type assertion fn in routes

* feat(backend): use throwIfMissingWalletAddress type assertion fn in routes

* chore(backend): cleanup route error, add request body to log

* chore(backend): add tests for lists function for model
  • Loading branch information
mkurapov authored Apr 16, 2024
1 parent 91bbc9b commit 99df005
Show file tree
Hide file tree
Showing 20 changed files with 682 additions and 386 deletions.
2 changes: 1 addition & 1 deletion packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"@graphql-tools/schema": "^10.0.3",
"@interledger/http-signature-utils": "2.0.2",
"@interledger/open-payments": "6.8.0",
"@interledger/openapi": "1.2.1",
"@interledger/openapi": "2.0.1",
"@interledger/pay": "0.4.0-alpha.9",
"@interledger/stream-receiver": "^0.3.3-alpha.3",
"@koa/cors": "^5.0.0",
Expand Down
103 changes: 71 additions & 32 deletions packages/backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ import { PaymentMethodHandlerService } from './payment-method/handler/service'
import { IlpPaymentService } from './payment-method/ilp/service'
import { TelemetryService } from './telemetry/service'
import { ApolloArmor } from '@escape.tech/graphql-armor'
import { openPaymentsServerErrorMiddleware } from './open_payments/route-errors'
export interface AppContextData {
logger: Logger
container: AppContainer
Expand Down Expand Up @@ -388,6 +389,7 @@ export class App {
router.get('/healthz', (ctx: AppContext): void => {
ctx.status = 200
})
router.use(openPaymentsServerErrorMiddleware)

const walletAddressKeyRoutes = await this.container.use(
'walletAddressKeyRoutes'
Expand All @@ -403,17 +405,26 @@ export class App {
const { resourceServerSpec, walletAddressServerSpec } =
await this.container.use('openApi')

const validatorMiddlewareOptions = {
validateRequest: true,
validateResponse: process.env.NODE_ENV !== 'production'
}

// POST /incoming-payments
// Create incoming payment
router.post<DefaultState, SignedCollectionContext<IncomingCreateBody>>(
'/incoming-payments',
createWalletAddressMiddleware(),
createValidatorMiddleware<
ContextType<SignedCollectionContext<IncomingCreateBody>>
>(resourceServerSpec, {
path: '/incoming-payments',
method: HttpMethod.POST
}),
>(
resourceServerSpec,
{
path: '/incoming-payments',
method: HttpMethod.POST
},
validatorMiddlewareOptions
),
createTokenIntrospectionMiddleware({
requestType: AccessType.IncomingPayment,
requestAction: RequestAction.Create
Expand All @@ -432,10 +443,14 @@ export class App {
createWalletAddressMiddleware(),
createValidatorMiddleware<
ContextType<SignedCollectionContext<never, GetCollectionQuery>>
>(resourceServerSpec, {
path: '/incoming-payments',
method: HttpMethod.GET
}),
>(
resourceServerSpec,
{
path: '/incoming-payments',
method: HttpMethod.GET
},
validatorMiddlewareOptions
),
createTokenIntrospectionMiddleware({
requestType: AccessType.IncomingPayment,
requestAction: RequestAction.List
Expand All @@ -451,10 +466,14 @@ export class App {
createWalletAddressMiddleware(),
createValidatorMiddleware<
ContextType<SignedCollectionContext<OutgoingCreateBody>>
>(resourceServerSpec, {
path: '/outgoing-payments',
method: HttpMethod.POST
}),
>(
resourceServerSpec,
{
path: '/outgoing-payments',
method: HttpMethod.POST
},
validatorMiddlewareOptions
),
createTokenIntrospectionMiddleware({
requestType: AccessType.OutgoingPayment,
requestAction: RequestAction.Create
Expand All @@ -473,10 +492,14 @@ export class App {
createWalletAddressMiddleware(),
createValidatorMiddleware<
ContextType<SignedCollectionContext<never, GetCollectionQuery>>
>(resourceServerSpec, {
path: '/outgoing-payments',
method: HttpMethod.GET
}),
>(
resourceServerSpec,
{
path: '/outgoing-payments',
method: HttpMethod.GET
},
validatorMiddlewareOptions
),
createTokenIntrospectionMiddleware({
requestType: AccessType.OutgoingPayment,
requestAction: RequestAction.List
Expand All @@ -492,10 +515,14 @@ export class App {
createWalletAddressMiddleware(),
createValidatorMiddleware<
ContextType<SignedCollectionContext<QuoteCreateBody>>
>(resourceServerSpec, {
path: '/quotes',
method: HttpMethod.POST
}),
>(
resourceServerSpec,
{
path: '/quotes',
method: HttpMethod.POST
},
validatorMiddlewareOptions
),
createTokenIntrospectionMiddleware({
requestType: AccessType.Quote,
requestAction: RequestAction.Create
Expand All @@ -511,10 +538,14 @@ export class App {
createWalletAddressMiddleware(),
createValidatorMiddleware<
ContextType<SubresourceContextWithAuthenticatedStatus>
>(resourceServerSpec, {
path: '/incoming-payments/{id}',
method: HttpMethod.GET
}),
>(
resourceServerSpec,
{
path: '/incoming-payments/{id}',
method: HttpMethod.GET
},
validatorMiddlewareOptions
),
createTokenIntrospectionMiddleware({
requestType: AccessType.IncomingPayment,
requestAction: RequestAction.Read,
Expand All @@ -534,7 +565,8 @@ export class App {
{
path: '/incoming-payments/{id}/complete',
method: HttpMethod.POST
}
},
validatorMiddlewareOptions
),
createTokenIntrospectionMiddleware({
requestType: AccessType.IncomingPayment,
Expand All @@ -554,7 +586,8 @@ export class App {
{
path: '/outgoing-payments/{id}',
method: HttpMethod.GET
}
},
validatorMiddlewareOptions
),
createTokenIntrospectionMiddleware({
requestType: AccessType.OutgoingPayment,
Expand All @@ -574,7 +607,8 @@ export class App {
{
path: '/quotes/{id}',
method: HttpMethod.GET
}
},
validatorMiddlewareOptions
),
createTokenIntrospectionMiddleware({
requestType: AccessType.Quote,
Expand All @@ -592,7 +626,8 @@ export class App {
{
path: '/jwks.json',
method: HttpMethod.GET
}
},
validatorMiddlewareOptions
),
async (ctx: WalletAddressKeysContext): Promise<void> =>
await walletAddressKeyRoutes.getKeysByWalletAddressId(ctx)
Expand All @@ -604,10 +639,14 @@ export class App {
WALLET_ADDRESS_PATH,
createWalletAddressMiddleware(),
createSpspMiddleware(this.config.spspEnabled),
createValidatorMiddleware<WalletAddressContext>(walletAddressServerSpec, {
path: '/',
method: HttpMethod.GET
}),
createValidatorMiddleware<WalletAddressContext>(
walletAddressServerSpec,
{
path: '/',
method: HttpMethod.GET
},
validatorMiddlewareOptions
),
walletAddressRoutes.get
)

Expand Down
1 change: 1 addition & 0 deletions packages/backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ export function initIocContainer(
container.singleton('walletAddressKeyRoutes', async (deps) => {
return createWalletAddressKeyRoutes({
config: await deps.use('config'),
logger: await deps.use('logger'),
walletAddressKeyService: await deps.use('walletAddressKeyService'),
walletAddressService: await deps.use('walletAddressService')
})
Expand Down
Loading

0 comments on commit 99df005

Please sign in to comment.