Skip to content

Commit

Permalink
Merge 07de8bb into a6436d3
Browse files Browse the repository at this point in the history
  • Loading branch information
meelrossi committed Jun 17, 2024
2 parents a6436d3 + 07de8bb commit acee8e6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/entities/Auth/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,18 @@ export function withChainHeader(options: AuthOptions = {}) {
return middleware(
async (req: Pick<Request, 'method' | 'baseUrl' | 'path' | 'headers'>) => {
try {
const data = await verify(
const data = await verify<Record<string, string>>(
req.method,
req.baseUrl + req.path,
req.headers
)
if (
data.authMetadata &&
'signer' in data.authMetadata &&
data.authMetadata.signer === 'decentraland-kernel-scene'
) {
throw new RequestError('Invalid signer', RequestError.BadRequest)
}
Object.assign(req, data)
} catch (err) {
if (err.statusCode === 401) {
Expand Down
24 changes: 24 additions & 0 deletions src/entities/Auth/routes/withDecentralandAuth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ describe(`withAuth`, () => {
)
})

test('should fail for requests with an invalid signer', async () => {
const logger = new Logger({}, { disabled: true })
const errors = jest.spyOn(logger, 'error')
errors.mockImplementation(() => null)
const request = signRequest(new Request('/'), {
identity,
metadata: { signer: 'decentraland-kernel-scene' },
})

await expect(() => withAuth({ request, logger })).rejects.toThrow(
'Invalid signer'
)
})

test(`should return auth data for signed request`, async () => {
const request = signRequest(new Request('/'), {
identity,
Expand Down Expand Up @@ -83,6 +97,16 @@ describe(`withAuthOptional`, () => {
expect(await withAuthOptional({ request, logger })).toBe(null)
})

test('should return null for requests with an invalid signer', async () => {
const logger = new Logger({}, { disabled: true })
const request = signRequest(new Request('/'), {
identity,
metadata: { signer: 'decentraland-kernel-scene' },
})

expect(await withAuthOptional({ request, logger })).toBe(null)
})

test(`should return auth data for signed request`, async () => {
const request = signRequest(new Request('/'), {
identity,
Expand Down
16 changes: 15 additions & 1 deletion src/entities/Auth/routes/withDecentralandAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
import verify from 'decentraland-crypto-middleware/lib/verify'

import globalLogger from '../../Development/logger'
import RequestError from '../../Route/error'
import Context from '../../Route/wkc/context/Context'
import ErrorResponse from '../../Route/wkc/response/ErrorResponse'
import Router from '../../Route/wkc/routes/Router'
Expand Down Expand Up @@ -55,7 +56,20 @@ function withDecentralandAuth(options: WithDecentralandAuthOptions = {}) {
})

try {
const data = await verify(method, path, headers, options)
const data = await verify<Record<string, string>>(
method,
path,
headers,
options
)

if (
data.authMetadata &&
'signer' in data.authMetadata &&
data.authMetadata.signer === 'decentraland-kernel-scene'
) {
throw new RequestError('Invalid signer', RequestError.BadRequest)
}

return {
address: data.auth,
Expand Down

0 comments on commit acee8e6

Please sign in to comment.