-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add verification signer middleware (#378)
- Loading branch information
Melisa Anabella Rossi
authored
Jun 17, 2024
1 parent
623112e
commit baabcbf
Showing
3 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) | ||
}) |