Skip to content

Commit

Permalink
feat: add verification signer middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
meelrossi committed Jun 17, 2024
1 parent 623112e commit 60de423
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/adapters/routes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Router } from '@well-known-components/http-server'
import * as authorizationMiddleware from 'decentraland-crypto-middleware'
import { withSignerValidation } from '../middlewares/withSignerValidation'
import { GlobalContext } from '../types'
import { createBidsHandler } from './handlers/bids'
import { createOrdersHandler } from './handlers/orders'
Expand Down Expand Up @@ -39,6 +40,7 @@ export async function setupRoutes(globalContext: GlobalContext) {
optional: true,
expiration: FIVE_MINUTES,
}),
withSignerValidation,
createNFTsHandler(components)
)
router.get(
Expand All @@ -47,6 +49,7 @@ export async function setupRoutes(globalContext: GlobalContext) {
optional: true,
expiration: FIVE_MINUTES,
}),
withSignerValidation,
createItemsHandler(components)
)
router.get('/contracts', createContractsHandler(components))
Expand All @@ -71,6 +74,7 @@ export async function setupRoutes(globalContext: GlobalContext) {
optional: true,
expiration: FIVE_MINUTES,
}),
withSignerValidation,
createCatalogHandler(components)
)

Expand Down
20 changes: 20 additions & 0 deletions src/middlewares/withSignerValidation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { IHttpServerComponent } from '@well-known-components/interfaces'

export async function withSignerValidation(
ctx: IHttpServerComponent.DefaultContext<any>,
next: () => Promise<IHttpServerComponent.IResponse>
): Promise<IHttpServerComponent.IResponse> {
if (
ctx.verification &&
ctx.verification.authMetadata &&
typeof ctx.verification.authMetadata === 'object' &&
ctx.verification.authMetadata.signer === 'decentraland-kernel-scene'
) {
return {
status: 400,
body: 'Invalid signer',
}
}

return await next()
}
64 changes: 64 additions & 0 deletions src/tests/middlewares/withSignerValidation.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { IHttpServerComponent } from "@well-known-components/interfaces"
import { withSignerValidation } from "../../middlewares/withSignerValidation"

let ctx: IHttpServerComponent.DefaultContext<any>

describe("withSignerValidation", () => {
describe("when signer is decentraland-kernel-scene", () => {
beforeEach(() => {
ctx = {
verification: {
authMetadata: {
signer: "decentraland-kernel-scene",
},
},
}
})

it('should return 400 status and "Invalid signer" body', async () => {
const next = jest.fn()
const result = await withSignerValidation(ctx, next)
expect(result).toEqual({
status: 400,
body: "Invalid signer",
})
expect(next).not.toHaveBeenCalled()
})
})

describe("when signer is not defined", () => {
beforeEach(() => {
ctx = {
verification: {
authMetadata: {},
},
}
})

it("should call next() and return its result", async () => {
const nextResult = { status: 200, body: "Success" }
const next = jest.fn().mockResolvedValue(nextResult)
const result = await withSignerValidation(ctx, next)
expect(result).toEqual(nextResult)
expect(next).toHaveBeenCalled()
})
})

describe('when signer is not "decentraland-kernel-scene"', () => {
beforeEach(() => {
ctx = {
verification: {
authMetadata: { signer: "other-signer" },
},
}
})

it("should call next() and return its result", async () => {
const nextResult = { status: 200, body: "Success" }
const next = jest.fn().mockResolvedValue(nextResult)
const result = await withSignerValidation(ctx, next)
expect(result).toEqual(nextResult)
expect(next).toHaveBeenCalled()
})
})
})

0 comments on commit 60de423

Please sign in to comment.