Skip to content

Commit

Permalink
Merge pull request #514 from snyk/refactoring/unify-hash-token-function
Browse files Browse the repository at this point in the history
refactor: unify hash token token
  • Loading branch information
pavel-snyk authored Mar 20, 2023
2 parents 0a8462b + 2898118 commit 4387a8a
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 31 deletions.
12 changes: 3 additions & 9 deletions lib/client/dispatcher/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import crypto = require('crypto');
import logger = require('../../log');
import { Config } from '../config';
import { hashToken } from '../../token';
import { HttpDispatcherServiceClient } from './client/api';
import { ServerId, getServerIdFromDispatcher } from './dispatcher-service';

Expand Down Expand Up @@ -41,14 +41,14 @@ export async function getServerId(
const baseUrl =
haConfig.BROKER_DISPATCHER_BASE_URL || defaultBrokerDispatcherBaseUrl;
const client = new HttpDispatcherServiceClient(baseUrl);
const token = hash(config.BROKER_TOKEN);
const hashedToken = hashToken(config.BROKER_TOKEN);

const maxRetries = 30;
for (let attempt = 0; attempt <= maxRetries; attempt++) {
try {
return await getServerIdFromDispatcher(client, {
brokerClientId: brokerClientId,
hashedBrokerToken: token,
hashedBrokerToken: hashedToken,
});
} catch (err) {
const timeout = 2 ** attempt * 100;
Expand All @@ -66,9 +66,3 @@ export async function getServerId(
function getHAConfig(config: any): Config {
return config as Config;
}

function hash(token: string): string {
const shasum = crypto.createHash('sha256');
shasum.update(token);
return shasum.digest('hex');
}
14 changes: 3 additions & 11 deletions lib/dispatcher.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const { maskToken } = require('./token');
const { hashToken, maskToken } = require('./token');
const logger = require('./log');
const config = require('./config');
const crypto = require('crypto');
const { axiosInstance } = require('./axios');
const { v4: uuid } = require('uuid');

Expand Down Expand Up @@ -46,7 +45,7 @@ class DispatcherClient {
const maskedToken = maskToken(token);
await this.#makeRequest(
{ maskedToken, clientId, requestType: 'client-connected' },
`${this.#url}/internal/brokerservers/${this.#id}/connections/${hash(
`${this.#url}/internal/brokerservers/${this.#id}/connections/${hashToken(
token,
)}${clientId ? `?broker_client_id=${clientId}` : ''}`,
'post',
Expand All @@ -64,7 +63,7 @@ class DispatcherClient {
const maskedToken = maskToken(token);
await this.#makeRequest(
{ maskedToken, clientId, requestType: 'client-disconnected' },
`${this.#url}/internal/brokerservers/${this.#id}/connections/${hash(
`${this.#url}/internal/brokerservers/${this.#id}/connections/${hashToken(
token,
)}${clientId ? `?broker_client_id=${clientId}` : ''}`,
'delete',
Expand Down Expand Up @@ -131,12 +130,6 @@ class DispatcherClient {
}
}

function hash(token) {
const shasum = crypto.createHash('sha256');
shasum.update(token);
return shasum.digest('hex');
}

let clientConnected;
let clientDisconnected;
let serverStarting;
Expand Down Expand Up @@ -192,5 +185,4 @@ module.exports = {
clientDisconnected,
serverStarting,
serverStopping,
hash,
};
11 changes: 0 additions & 11 deletions lib/token.js

This file was deleted.

15 changes: 15 additions & 0 deletions lib/token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import crypto = require('crypto');

export function hashToken(token: string): string {
const shasum = crypto.createHash('sha256');
shasum.update(token);
return shasum.digest('hex');
}

export function maskToken(token) {
if (!token || token === '') {
return '';
}

return token.slice(0, 4) + '-...-' + token.slice(-4);
}
36 changes: 36 additions & 0 deletions test/unit/token.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { hashToken, maskToken } from '../../lib/token';

describe('token', () => {
describe('hashToken', () => {
it('should hash input tokens using sha256', async () => {
const expectedSha256ForEmptyString =
'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855';
const expectedSha256ForTokenWord =
'3c469e9d6c5875d37a43f353d4f88e61fcf812c66eee3457465a40b0da4153e0';

const hashedTokenForEmptyString = hashToken('');
const hashedTokenForTokenWord = hashToken('token');

expect(hashedTokenForEmptyString).toEqual(expectedSha256ForEmptyString);
expect(hashedTokenForTokenWord).toEqual(expectedSha256ForTokenWord);
});
});

describe('maskToken', () => {
it('should return empty string if token is empty or null', async () => {
const maskedTokenForEmpty = maskToken('');
const maskedTokenForNull = maskToken('');

expect(maskedTokenForEmpty).toEqual('');
expect(maskedTokenForNull).toEqual('');
});

it('should return four first and last characters when masking', async () => {
const maskedToken = maskToken('12345');
const maskedUUIDToken = maskToken('aaaabbbb-0160-4126-a00d-ccccccccdddd');

expect(maskedToken).toEqual('1234-...-2345');
expect(maskedUUIDToken).toEqual('aaaa-...-dddd');
});
});
});

0 comments on commit 4387a8a

Please sign in to comment.