-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use verify from @dcl/platform-crypto-middleware (#699)
* fix: use verify from @dcl/platform-crypto-middleware * remove unused fetch * retrocompatibility * forward error from validateSignature * use independent fetcher for signature * improvements * test: fix broken tests * chore: imports order and prettier fix * test: add tests for the authentication middleware logic --------- Signed-off-by: Mateo Miccino <[email protected]> Co-authored-by: Juanma Hidalgo <[email protected]>
- Loading branch information
1 parent
4fc551a
commit 9980fbf
Showing
6 changed files
with
214 additions
and
59 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,125 @@ | ||
import { Request } from 'express' | ||
import { Authenticator } from '@dcl/crypto' | ||
import { verify } from '@dcl/platform-crypto-middleware' | ||
import { | ||
INVALID_AUTH_CHAIN_MESSAGE, | ||
MISSING_ETH_ADDRESS_ERROR, | ||
decodeAuthChain, | ||
} from './authentication' | ||
|
||
jest.mock('@dcl/crypto') | ||
jest.mock('@dcl/platform-crypto-middleware') | ||
|
||
describe('decodeAuthChain', () => { | ||
let mockRequest: Request | ||
|
||
beforeEach(() => { | ||
mockRequest = { | ||
headers: {}, | ||
method: 'GET', | ||
path: '/', | ||
} as Request | ||
}) | ||
|
||
describe('when the auth chain is invalid', () => { | ||
it('should throw an error for an invalid auth chain', async () => { | ||
mockRequest.headers = { | ||
'x-identity-auth-chain-0': '{"invalidPart": "data"}', | ||
} | ||
|
||
await expect(decodeAuthChain(mockRequest)).rejects.toThrow( | ||
INVALID_AUTH_CHAIN_MESSAGE | ||
) | ||
}) | ||
}) | ||
|
||
describe('when the auth chain is valid', () => { | ||
beforeEach(() => { | ||
;(Authenticator.isValidAuthChain as jest.Mock).mockReturnValue(true) | ||
}) | ||
|
||
afterEach(() => { | ||
;(Authenticator.isValidAuthChain as jest.Mock).mockRestore() | ||
}) | ||
|
||
describe('and it is missing an ETH address', () => { | ||
it('should throw an error with the missing ETH address message', async () => { | ||
await expect(decodeAuthChain(mockRequest)).rejects.toThrow( | ||
MISSING_ETH_ADDRESS_ERROR | ||
) | ||
}) | ||
}) | ||
|
||
describe('and it has the ETH address defined', () => { | ||
let validAddress = '0x12345' | ||
beforeEach(() => { | ||
;(Authenticator.ownerAddress as jest.Mock).mockReturnValue(validAddress) | ||
}) | ||
|
||
afterEach(() => { | ||
;(Authenticator.isValidAuthChain as jest.Mock).mockRestore() | ||
}) | ||
|
||
describe('and the verify method does not throw an error', () => { | ||
beforeEach(() => { | ||
;(verify as jest.Mock).mockReturnValue(validAddress) | ||
}) | ||
|
||
afterEach(() => { | ||
;(verify as jest.Mock).mockRestore() | ||
}) | ||
|
||
it('should return the eth address without throwing an error', async () => { | ||
const result = await decodeAuthChain(mockRequest) | ||
expect(result).toBe(validAddress) | ||
await expect(decodeAuthChain(mockRequest)).resolves.not.toThrow() | ||
}) | ||
}) | ||
|
||
describe('and the verify method throws an error', () => { | ||
beforeEach(() => { | ||
;(verify as jest.Mock).mockRejectedValue('Error') | ||
}) | ||
|
||
afterEach(() => { | ||
;(verify as jest.Mock).mockRestore() | ||
}) | ||
|
||
describe('and the validateSignature function does not throw an error', () => { | ||
beforeEach(() => { | ||
;(Authenticator.validateSignature as jest.Mock).mockReturnValue({ | ||
ok: true, | ||
}) | ||
}) | ||
|
||
afterEach(() => { | ||
;(Authenticator.validateSignature as jest.Mock).mockRestore() | ||
}) | ||
it('should return the eth address without throwing an error', async () => { | ||
const result = await decodeAuthChain(mockRequest) | ||
expect(result).toBe(validAddress) | ||
await expect(decodeAuthChain(mockRequest)).resolves.not.toThrow() | ||
}) | ||
}) | ||
|
||
describe('and the validateSignature method throws an error', () => { | ||
let error: string | ||
beforeEach(() => { | ||
error = 'validateSignature failed' | ||
;(Authenticator.validateSignature as jest.Mock).mockReturnValue({ | ||
ok: false, | ||
message: error, | ||
}) | ||
}) | ||
|
||
afterEach(() => { | ||
;(Authenticator.validateSignature as jest.Mock).mockRestore() | ||
}) | ||
it('should throw the error', async () => { | ||
await expect(decodeAuthChain(mockRequest)).rejects.toThrow(error) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.