Skip to content

Commit

Permalink
Merge pull request #5216 from novuhq/fix-api-key-migration-widget-ini…
Browse files Browse the repository at this point in the history
…tilize-failure

fix: api key migration widget initialize failure
  • Loading branch information
djabarovgeorge authored Feb 20, 2024
2 parents 81e4c8a + daf321c commit 100cce6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
16 changes: 15 additions & 1 deletion apps/api/src/app/widgets/e2e/initialize-widget-session.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
InvalidateCacheService,
} from '@novu/application-generic';

import { encryptApiKeysMigration } from '../../../../migrations/encrypt-api-keys/encrypt-api-keys-migration';

const integrationRepository = new IntegrationRepository();
const subscriberId = '12345';

Expand Down Expand Up @@ -97,11 +99,23 @@ describe('Initialize Session - /widgets/session/initialize (POST)', async () =>
const invalidSecretKey = 'invalid-secret-key';
const invalidSubscriberHmacHash = createHash(invalidSecretKey, subscriberId);

const responseInvalidSecretKey = await initWidgetSession(subscriberId, session, invalidSecretKey);
const responseInvalidSecretKey = await initWidgetSession(subscriberId, session, invalidSubscriberHmacHash);

expect(responseInvalidSecretKey.body?.data?.profile).to.not.exist;
expect(responseInvalidSecretKey.body.message).to.contain('Please provide a valid HMAC hash');
});

it('should pass api key migration regression tests', async function () {
const validSecretKey = session.environment.apiKeys[0].key;

const invalidSubscriberHmacHash = createHash(validSecretKey, subscriberId);

await encryptApiKeysMigration();

const response = await initWidgetSession(subscriberId, session, invalidSubscriberHmacHash);

expect(response.status).to.equal(201);
});
});

async function initWidgetSession(subscriberIdentifier: string, session, hmacHash?: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
SelectIntegration,
AuthService,
createHash,
decryptApiKey,
} from '@novu/application-generic';

import { ApiException } from '../../../shared/exceptions/api.exception';
Expand Down Expand Up @@ -84,7 +85,8 @@ export class InitializeSession {
}

function validateNotificationCenterEncryption(environment, command: InitializeSessionCommand) {
const hmacHash = createHash(environment.apiKeys[0].key, command.subscriberId);
const key = decryptApiKey(environment.apiKeys[0].key);
const hmacHash = createHash(key, command.subscriberId);
if (hmacHash !== command.hmacHash) {
throw new ApiException('Please provide a valid HMAC hash');
}
Expand Down

0 comments on commit 100cce6

Please sign in to comment.