diff --git a/src/events/http/createJWTAuthScheme.js b/src/events/http/createJWTAuthScheme.js index 41b8dfdc1..e65acb577 100644 --- a/src/events/http/createJWTAuthScheme.js +++ b/src/events/http/createJWTAuthScheme.js @@ -3,6 +3,7 @@ import { log } from '@serverless/utils/log.js' import { decodeJwt } from 'jose' const { isArray } = Array +const { now } = Date export default function createAuthScheme(jwtOptions) { const authorizerName = jwtOptions.name @@ -38,7 +39,7 @@ export default function createAuthScheme(jwtOptions) { const claims = decodeJwt(jwtToken) const expirationDate = new Date(claims.exp * 1000) - if (expirationDate.valueOf() < Date.now()) { + if (expirationDate.valueOf() < now()) { return Boom.unauthorized('JWT Token expired') } diff --git a/tests/manual/websocket/RouteSelection/test/e2e/ws.e2e.js b/tests/manual/websocket/RouteSelection/test/e2e/ws.e2e.js index 15a0e056c..8e8dad2e2 100644 --- a/tests/manual/websocket/RouteSelection/test/e2e/ws.e2e.js +++ b/tests/manual/websocket/RouteSelection/test/e2e/ws.e2e.js @@ -10,6 +10,7 @@ const WebSocketTester = require('../support/WebSocketTester.js') const endpoint = env.npm_config_endpoint || 'ws://localhost:3005' const timeout = env.npm_config_timeout ? +env.npm_config_timeout : 1000 +const { now } = Date const { stringify } = JSON describe('serverless', () => { @@ -53,12 +54,15 @@ describe('serverless', () => { it("should call action 'echo' handler located at service.do", async () => { const ws = await createWebSocket() - const now = `${Date.now()}` - const payload = stringify({ message: now, service: { do: 'echo' } }) + const timestamp = `${now()}` + const payload = stringify({ + message: timestamp, + service: { do: 'echo' }, + }) ws.send(payload) - expect(await ws.receive1()).to.equal(`${now}`) + expect(await ws.receive1()).to.equal(`${timestamp}`) }).timeout(timeout) }) }) diff --git a/tests/manual/websocket/main/test/e2e/ws.e2e.js b/tests/manual/websocket/main/test/e2e/ws.e2e.js index e17c92d78..e8268d9f2 100644 --- a/tests/manual/websocket/main/test/e2e/ws.e2e.js +++ b/tests/manual/websocket/main/test/e2e/ws.e2e.js @@ -19,6 +19,7 @@ const timeout = env.npm_config_timeout const WebSocketTester = require('../support/WebSocketTester.js') const { expect } = chai +const { now } = Date const { parse, stringify } = JSON const { keys } = Object @@ -99,11 +100,11 @@ describe('serverless', () => { .replace('wss://', 'https://')}`, ) .keepOpen() - let res = await request.get(`/${Date.now()}`) // .set('Authorization', user.accessToken); + let res = await request.get(`/${now()}`) // .set('Authorization', user.accessToken); expect(res).to.have.status(426) - res = await request.get(`/${Date.now()}/${Date.now()}`) // .set('Authorization', user.accessToken); + res = await request.get(`/${now()}/${now()}`) // .set('Authorization', user.accessToken); expect(res).to.have.status(426) }).timeout(timeout) @@ -127,7 +128,7 @@ describe('serverless', () => { it('should call default handler when no such action exists', async () => { const ws = await createWebSocket() - const payload = stringify({ action: `action${Date.now()}` }) + const payload = stringify({ action: `action${now()}` }) ws.send(payload) expect(await ws.receive1()).to.equal( @@ -408,7 +409,7 @@ describe('serverless', () => { // connect const c = await createClient() const connect = parse(await ws.receive1()) - let now = Date.now() + let timestamp = now() let expectedCallInfo = { context: createExpectedContext(connect.info.context), event: { @@ -438,12 +439,12 @@ describe('serverless', () => { connect.info.event.requestContext.requestTimeEpoch + 10, ) expect(connect.info.event.requestContext.connectedAt).to.be.within( - now - timeout, - now, + timestamp - timeout, + timestamp, ) expect(connect.info.event.requestContext.requestTimeEpoch).to.be.within( - now - timeout, - now, + timestamp - timeout, + timestamp, ) expect( moment @@ -453,7 +454,7 @@ describe('serverless', () => { ) .toDate() .getTime(), - ).to.be.within(now - timeout, now) + ).to.be.within(timestamp - timeout, timestamp) if (endpoint.startsWith('ws://locahost')) { expect(connect.info.event.headers['X-Forwarded-For']).to.be.equal( @@ -464,7 +465,7 @@ describe('serverless', () => { // getCallInfo c.ws.send(stringify({ action: 'getCallInfo' })) const callInfo = parse(await c.ws.receive1()) - now = Date.now() + timestamp = now() expectedCallInfo = { context: createExpectedContext(callInfo.info.context), event: { @@ -489,12 +490,12 @@ describe('serverless', () => { callInfo.info.event.requestContext.requestTimeEpoch, ) expect(callInfo.info.event.requestContext.connectedAt).to.be.within( - now - timeout, - now, + timestamp - timeout, + timestamp, ) expect(callInfo.info.event.requestContext.requestTimeEpoch).to.be.within( - now - timeout, - now, + timestamp - timeout, + timestamp, ) expect( moment @@ -504,12 +505,12 @@ describe('serverless', () => { ) .toDate() .getTime(), - ).to.be.within(now - timeout, now) + ).to.be.within(timestamp - timeout, timestamp) // disconnect c.ws.close() const disconnect = parse(await ws.receive1()) - now = Date.now() + timestamp = now() expectedCallInfo = { context: createExpectedContext(disconnect.info.context), event: { @@ -538,13 +539,13 @@ describe('serverless', () => { }).timeout(timeout) it('should be able to parse query string', async () => { - const now = `${Date.now()}` + const timestamp = `${now()}` const ws = await createWebSocket() ws.send(stringify({ action: 'registerListener' })) await ws.receive1() await createClient() - await createClient(`now=${now}&before=123456789`) + await createClient(`now=${timestamp}&before=123456789`) expect(parse(await ws.receive1()).info.event.queryStringParameters).to.be .undefined @@ -552,7 +553,7 @@ describe('serverless', () => { parse(await ws.receive1()).info.event.queryStringParameters, ).to.deep.equal({ before: '123456789', - now, + timestamp, }) }).timeout(timeout)