Skip to content

Commit

Permalink
fix: set siwe domain as host
Browse files Browse the repository at this point in the history
  • Loading branch information
JGiter committed Feb 21, 2023
1 parent 633d394 commit 3f777e7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
43 changes: 37 additions & 6 deletions e2e/auth.e2e.ts
Expand Up @@ -25,7 +25,7 @@ describe('Authentication tests', () => {
url: SSI_HUB_URL,
cacheServerSupportsAuth: true,
auth: {
domain: 'localhost',
domain: 'https://switchboard-dev.energyweb.org',
},
});
});
Expand Down Expand Up @@ -175,17 +175,44 @@ describe('Authentication tests', () => {
.reply(201, { nonce });
});

afterEach(() => {
expect(authInitScope.isDone()).toBe(true);
});

const checkTokens = (tokens: AuthTokens) => {
expect(
cacheClient['_httpClient'].defaults.headers.common.Authorization
).toBe(`Bearer ${tokens.token}`);
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',
Expand Down Expand Up @@ -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);
});
Expand Down Expand Up @@ -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);
});
Expand Down Expand Up @@ -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);
});
Expand All @@ -296,6 +326,7 @@ describe('Authentication tests', () => {

await expect(cacheClient.authenticate()).rejects.toBeDefined();

expect(authInitScope.isDone()).toBe(true);
expect(loginScope.isDone()).toBe(true);
});
});
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion jest.config.js
Expand Up @@ -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: {
Expand Down
4 changes: 2 additions & 2 deletions src/modules/cache-client/auth.ts
Expand Up @@ -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',
Expand Down

0 comments on commit 3f777e7

Please sign in to comment.