Skip to content

Commit

Permalink
fix: Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LautaroPetaccio committed Mar 16, 2023
1 parent d67ee4a commit 4b10b06
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/verify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
AUTH_CHAIN_HEADER_PREFIX,
AUTH_METADATA_HEADER,
AUTH_TIMESTAMP_HEADER,
DEFAULT_EXPIRATION,
} from './types'
import verifyAuthChainHeaders, {
isEIP1664AuthChain,
Expand Down Expand Up @@ -191,6 +192,7 @@ describe(`src/verifyAuthChainHeaders`, () => {

test(`should throw an error if timestamp is expired`, async () => {
const timestamp = 0
const now = Date.now()
const metadata = {}
const method = 'get'
const path = '/path/to/resource'
Expand All @@ -199,10 +201,15 @@ describe(`src/verifyAuthChainHeaders`, () => {
.toLowerCase()
const chain = Authenticator.signPayload(identity, payload)
const headers = createAuthChainHeaders(chain, timestamp, metadata)
jest.spyOn(Date, 'now').mockReturnValue(now)

await expect(() =>
verifyAuthChainHeaders(method, path, headers)
).rejects.toThrowError('Expired signature')
).rejects.toThrowError(
`Expired signature: signature timestamp: ${timestamp}, timestamp expiration: ${
timestamp + DEFAULT_EXPIRATION
}, local timestamp: ${now}`
)
})

test(`should throw an error if timestamp header wasn't signed`, async () => {
Expand Down
10 changes: 8 additions & 2 deletions src/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,14 @@ export function verifyExpiration(
options: VerifyAuthChainHeadersOptions = {}
) {
const expiration = options.expiration ?? DEFAULT_EXPIRATION
if (timestamp + expiration < Date.now()) {
throw new RequestError(`Expired signature: signature timestamp: ${timestamp}, timestamp expiration: ${expiration + Date.now()}`, 401)
const now = Date.now()
if (timestamp + expiration < now) {
throw new RequestError(
`Expired signature: signature timestamp: ${timestamp}, timestamp expiration: ${
timestamp + expiration
}, local timestamp: ${now}`,
401
)
}

return true
Expand Down

0 comments on commit 4b10b06

Please sign in to comment.