Skip to content

Commit

Permalink
fix: user authorizer
Browse files Browse the repository at this point in the history
  • Loading branch information
mbasadi committed May 14, 2024
1 parent 5241ca5 commit 627a8e3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "notificationapi-node-server-sdk",
"version": "2.0.0",
"version": "2.0.1",
"description": "NotificationAPI server-side library for Node.js",
"keywords": [
"notificationapi",
Expand Down
14 changes: 14 additions & 0 deletions src/__tests__/notificationapi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,9 @@ describe('deleteUserPreferences without subNotificationId', () => {
const clientSecret = 'testClientSecret';
const userId = 'testUserId';
const notificationId = 'testNotificationId';
const hashedUserId = `${createHmac('sha256', clientSecret)
.update(userId)
.digest('base64')}`;

test('makes API calls to the correct end-point', async () => {
axiosMock.onDelete(retractEndPointRegex).reply(200);
Expand All @@ -629,6 +632,10 @@ describe('deleteUserPreferences without subNotificationId', () => {
`https://api.notificationapi.com/${clientId}/users/${userId}/preferences`
);
expect(axiosMock.history.delete[0].params).toEqual({ notificationId });
expect(axiosMock.history.delete[0].headers.Authorization).toEqual(
'Basic ' +
Buffer.from(`${clientId}:${userId}:${hashedUserId}`).toString('base64')
);
});
});
describe('deleteUserPreferences with subNotificationId', () => {
Expand All @@ -638,6 +645,9 @@ describe('deleteUserPreferences with subNotificationId', () => {
const userId = 'testUserId';
const notificationId = 'testNotificationId';
const subNotificationId = 'testSubNotificationId';
const hashedUserId = `${createHmac('sha256', clientSecret)
.update(userId)
.digest('base64')}`;

test('makes API calls to the correct end-point', async () => {
axiosMock.onDelete(retractEndPointRegex).reply(200);
Expand All @@ -655,6 +665,10 @@ describe('deleteUserPreferences with subNotificationId', () => {
notificationId,
subNotificationId
});
expect(axiosMock.history.delete[0].headers.Authorization).toEqual(
'Basic ' +
Buffer.from(`${clientId}:${userId}:${hashedUserId}`).toString('base64')
);
});
});
describe('Identify user', () => {
Expand Down
12 changes: 10 additions & 2 deletions src/notificationapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const DEFAULT_BASE_URL = 'https://api.notificationapi.com';

class NotificationAPIService {
private USER_AGENT = 'notificationapi-node-server-sdk';
private VERSION = '2.0.0';
private VERSION = '2.0.1';

clientId: null | string = null;
clientSecret: null | string = null;
Expand Down Expand Up @@ -105,11 +105,19 @@ class NotificationAPIService {
/** The subNotificationId is used to specify further subcategories within a notification. Optional */
subNotificationId?: string
): Promise<AxiosResponse> => {
const hashedUserId = `${createHmac('sha256', this.clientSecret as string)
.update(userId)
.digest('base64')}`;
const customAuthorization =
'Basic ' +
Buffer.from(`${this.clientId}:${userId}:${hashedUserId}`).toString(
'base64'
);
return this.request(
'DELETE',
`users/${userId}/preferences`,
null,
undefined,
customAuthorization,
subNotificationId
? { notificationId, subNotificationId }
: { notificationId }
Expand Down

0 comments on commit 627a8e3

Please sign in to comment.