From 3f777e77788739274ece3cde0eeebd045eda21d7 Mon Sep 17 00:00:00 2001 From: JGiter Date: Tue, 21 Feb 2023 14:54:42 +0200 Subject: [PATCH] fix: set siwe domain as host --- e2e/auth.e2e.ts | 43 +++++++++++++++++++++++++++----- jest.config.js | 2 +- src/modules/cache-client/auth.ts | 4 +-- 3 files changed, 40 insertions(+), 9 deletions(-) diff --git a/e2e/auth.e2e.ts b/e2e/auth.e2e.ts index 3006c997..f0b02afe 100644 --- a/e2e/auth.e2e.ts +++ b/e2e/auth.e2e.ts @@ -25,7 +25,7 @@ describe('Authentication tests', () => { url: SSI_HUB_URL, cacheServerSupportsAuth: true, auth: { - domain: 'localhost', + domain: 'https://switchboard-dev.energyweb.org', }, }); }); @@ -175,10 +175,6 @@ describe('Authentication tests', () => { .reply(201, { nonce }); }); - afterEach(() => { - expect(authInitScope.isDone()).toBe(true); - }); - const checkTokens = (tokens: AuthTokens) => { expect( cacheClient['_httpClient'].defaults.headers.common.Authorization @@ -186,6 +182,37 @@ describe('Authentication tests', () => { expect(cacheClient['refresh_token']).toBe(tokens.refreshToken); }; + it('should refresh tokens', async () => { + const newTokens = { + token: 'new-token', + refreshToken: 'new-refresh-token', + }; + const oldRefreshToken = 'old-token'; + + cacheClient['refresh_token'] = oldRefreshToken; + + const refreshScope = getNockScope() + .get(`/refresh_token?refresh_token=${oldRefreshToken}`) + .reply(200, newTokens); + + const loginScope = getNockScope() + .post(`/login/siwe/verify`) + .reply(200, newTokens); + + const statusScope = getNockScope().get(TEST_LOGIN_ENDPOINT).reply(200, { + user: signerService.did, + }); + + await cacheClient.authenticate(); + + checkTokens(newTokens); + + expect(authInitScope.isDone()).toBe(false); + expect(refreshScope.isDone()).toBe(true); + expect(loginScope.isDone()).toBe(false); + expect(statusScope.isDone()).toBe(true); + }); + it('should perform login when refresh token is empty', async () => { const newTokens = { token: 'new-token', @@ -217,6 +244,7 @@ describe('Authentication tests', () => { checkTokens(newTokens); expect(refreshScope.isDone()).toBe(false); + expect(authInitScope.isDone()).toBe(true); expect(loginScope.isDone()).toBe(true); expect(statusScope.isDone()).toBe(false); }); @@ -256,6 +284,7 @@ describe('Authentication tests', () => { checkTokens(newTokens); expect(refreshScope.isDone()).toBe(true); + expect(authInitScope.isDone()).toBe(true); expect(loginScope.isDone()).toBe(true); expect(statusScope.isDone()).toBe(false); }); @@ -287,6 +316,7 @@ describe('Authentication tests', () => { checkTokens(newTokens); expect(refreshScope.isDone()).toBe(true); + expect(authInitScope.isDone()).toBe(true); expect(loginScope.isDone()).toBe(true); expect(statusScope.isDone()).toBe(true); }); @@ -296,6 +326,7 @@ describe('Authentication tests', () => { await expect(cacheClient.authenticate()).rejects.toBeDefined(); + expect(authInitScope.isDone()).toBe(true); expect(loginScope.isDone()).toBe(true); }); }); @@ -535,7 +566,7 @@ describe('Authentication tests', () => { .reply(201, { nonce: 47 }) .post('/login/siwe/initiate') .once() - .reply(202, { nonce: 48 }); + .reply(201, { nonce: 48 }); const loginScope = getNockScope() .post('/login/siwe/verify') .reply(500) diff --git a/jest.config.js b/jest.config.js index 0983ad64..cd888ec7 100644 --- a/jest.config.js +++ b/jest.config.js @@ -6,7 +6,7 @@ module.exports = { }, transformIgnorePatterns: ["node_modules/!(@energyweb/ekc)", "node_modules/.+\\.!(mjs)$"], testEnvironment: "node", - testRegex: "(/(e2e|src)/(.|\\.)*auth\\.(e2e|spec)\\.ts$)", + testRegex: "(/(e2e|src)/(.|\\.)*\\.(e2e|spec)\\.ts$)", moduleFileExtensions: ["ts", "tsx", "js", "json"], coveragePathIgnorePatterns: ["/node_modules/", "/e2e/", "/test/"], coverageThreshold: { diff --git a/src/modules/cache-client/auth.ts b/src/modules/cache-client/auth.ts index a6c128db..cdd73096 100644 --- a/src/modules/cache-client/auth.ts +++ b/src/modules/cache-client/auth.ts @@ -29,10 +29,10 @@ export class SsiAuth { const { data: { nonce }, } = await this.http.post<{ nonce: string }>('/login/siwe/initiate'); - console.dir(nonce); const siweMessage = new SiweMessage({ + statement: `I accept the Terms of Service: ${this.config.domain}`, nonce, - domain: this.config.domain, + domain: new URL(this.config.domain).host, address: this.signerService.address, uri: `${cacheConfigs()[this.signerService.chainId].url}login/siwe/verify`, version: '1',