Skip to content

Commit

Permalink
Add jwt headers to requests
Browse files Browse the repository at this point in the history
Removed APIHeaders type as RequestMetadata can be used to create the Jwt header instead. Moved callers passing subscriptionId as a separate parameter and including it in their RequestMetadata parameter instead to utilize RequestMetadata's subscriptionId property (furthering implementation of e5c2a10). Future consideration: add AliasPair to RequestMetadata. Additionally, renamed parameters and method name in OneSignalApiBase for more clarity
  • Loading branch information
shepherd-l committed Oct 18, 2024
1 parent 5797e88 commit 1b4bc37
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 131 deletions.
10 changes: 8 additions & 2 deletions __test__/unit/http/sdkVersion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,17 @@ describe('Sdk Version Header Tests', () => {
expectHeaderToBeSent();
});
test('PATCH /subscriptions/<subscription_id>: header is sent', () => {
RequestService.updateSubscription({ appId: APP_ID }, DUMMY_EXTERNAL_ID, {});
RequestService.updateSubscription(
{ appId: APP_ID, subscriptionId: DUMMY_SUBSCRIPTION_ID },
{},
);
expectHeaderToBeSent();
});
test('DELETE /subscriptions/<subscription_id>: header is sent', () => {
RequestService.deleteSubscription({ appId: APP_ID }, DUMMY_EXTERNAL_ID);
RequestService.updateSubscription(
{ appId: APP_ID, subscriptionId: DUMMY_SUBSCRIPTION_ID },
{},
);
expectHeaderToBeSent();
});
});
6 changes: 3 additions & 3 deletions src/core/models/RequestMetadata.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { APIHeaders } from '../../shared/models/APIHeaders';

export type RequestMetadata = {
// path parameters
appId: string;
subscriptionId?: string;
jwtHeader?: APIHeaders;
// sent as header
jwtToken?: string;
};
148 changes: 75 additions & 73 deletions src/core/requestService/RequestService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,25 @@ import {
SdkInitError,
SdkInitErrorKind,
} from '../../shared/errors/SdkInitError';
import { addJwtHeader, addOneSignalSubscriptionIdHeader } from './helpers';

export class RequestService {
/* U S E R O P E R A T I O N S */

/**
* Creates a new user
* @param requestMetadata - { appId }
* @param requestMetadata - { appId, subscriptionId, jwtToken }
* @param requestBody - The user's properties, identity, and subscriptions
*/
static async createUser(
requestMetadata: RequestMetadata,
requestBody: CreateUserPayload,
): Promise<OneSignalApiBaseResponse> {
const { appId, subscriptionId } = requestMetadata;

const subscriptionHeader = subscriptionId
? { 'OneSignal-Subscription-Id': subscriptionId }
: undefined;

let headers = {};
const { appId, subscriptionId, jwtToken } = requestMetadata;

if (subscriptionHeader) {
headers = { ...headers, ...subscriptionHeader };
}

if (requestMetadata.jwtHeader) {
headers = { ...headers, ...requestMetadata.jwtHeader };
}
const headers = new Headers();
addOneSignalSubscriptionIdHeader(headers, subscriptionId);
addJwtHeader(headers, jwtToken);

requestBody['refresh_device_metadata'] = true;

Expand All @@ -51,52 +42,46 @@ export class RequestService {

/**
* Returns the user's properties, aliases, and subscriptions
* @param requestMetadata - { appId }
* @param requestMetadata - { appId, jwtToken }
* @param alias - The user's alias
* @returns - A promise that resolves with the user's properties, identity, and subscriptions
*/
static async getUser(
requestMetadata: RequestMetadata,
alias: AliasPair,
): Promise<OneSignalApiBaseResponse> {
const { appId } = requestMetadata;
const { appId, jwtToken } = requestMetadata;

const headers = new Headers();
addJwtHeader(headers, jwtToken);

return OneSignalApiBase.get(
`apps/${appId}/users/by/${alias.label}/${alias.id}`,
requestMetadata.jwtHeader,
headers,
);
}

/**
* Updates an existing user's properties
* - Aliases and subscriptions are managed via other endpoints
* @param requestMetadata - { appId }
* @param requestMetadata - { appId, subscriptionId, jwtToken }
* @param alias - alias label & id
* @param payload - update user payload
* @param requestBody - update user payload
* @returns no body
*/
static async updateUser(
requestMetadata: RequestMetadata,
alias: AliasPair,
payload: UpdateUserPayload,
requestBody: UpdateUserPayload,
): Promise<OneSignalApiBaseResponse> {
const { appId, subscriptionId } = requestMetadata;
const { appId, subscriptionId, jwtToken } = requestMetadata;
if (!OneSignalUtils.isValidUuid(appId)) {
throw new SdkInitError(SdkInitErrorKind.InvalidAppId);
}

const subscriptionHeader = subscriptionId
? { 'OneSignal-Subscription-Id': subscriptionId }
: undefined;

let headers = {};

if (subscriptionHeader) {
headers = { ...headers, ...subscriptionHeader };
}

if (requestMetadata.jwtHeader) {
headers = { ...headers, ...requestMetadata.jwtHeader };
}
const headers = new Headers();
addOneSignalSubscriptionIdHeader(headers, subscriptionId);
addJwtHeader(headers, jwtToken);

const sanitizedAlias = {
label: encodeRFC3986URIComponent(alias.label),
Expand All @@ -105,32 +90,36 @@ export class RequestService {

return OneSignalApiBase.patch(
`apps/${appId}/users/by/${sanitizedAlias.label}/${sanitizedAlias.id}`,
payload,
requestBody,
headers,
);
}

/**
* Removes the user identified by the given alias pair, and all subscriptions and aliases
* @param requestMetadata - { appId }
* @param requestMetadata - { appId, jwtToken }
* @param alias - alias label & id
*/
static async deleteUser(
requestMetadata: RequestMetadata,
alias: AliasPair,
): Promise<OneSignalApiBaseResponse> {
const { appId } = requestMetadata;
const { appId, jwtToken } = requestMetadata;

const headers = new Headers();
addJwtHeader(headers, jwtToken);

return OneSignalApiBase.delete(
`apps/${appId}/users/by/${alias.label}/${alias.id}`,
requestMetadata.jwtHeader,
headers,
);
}

/* I D E N T I T Y O P E R A T I O N S */

/**
* Upserts one or more aliases for the user identified by the given alias pair
* @param requestMetadata - { appId }
* @param requestMetadata - { appId, jwtToken }
* @param alias - alias label & id
* @param identity - identity label & id
*/
Expand All @@ -139,33 +128,41 @@ export class RequestService {
alias: AliasPair,
identity: SupportedIdentity,
): Promise<OneSignalApiBaseResponse> {
const { appId } = requestMetadata;
const { appId, jwtToken } = requestMetadata;

const headers = new Headers();
addJwtHeader(headers, jwtToken);

return OneSignalApiBase.patch(
`apps/${appId}/users/by/${alias.label}/${alias.id}/identity`,
{ identity },
requestMetadata.jwtHeader,
headers,
);
}

/**
* Lists all aliases for the user identified by the given alias pair
* @param requestMetadata - { appId }
* @param requestMetadata - { appId, jwtToken }
* @param alias - alias label & id
*/
static async getUserIdentity(
requestMetadata: RequestMetadata,
alias: AliasPair,
): Promise<OneSignalApiBaseResponse> {
const { appId } = requestMetadata;
const { appId, jwtToken } = requestMetadata;

const headers = new Headers();
addJwtHeader(headers, jwtToken);

return OneSignalApiBase.get(
`apps/${appId}/users/by/${alias.label}/${alias.id}/identity`,
requestMetadata.jwtHeader,
headers,
);
}

/**
* Deletes an alias for the user identified by the given alias pair
* @param requestMetadata - { appId }
* @param requestMetadata - { appId, jwtToken }
* @param alias - alias label & id
* @param labelToRemove - label of identity to remove
*/
Expand All @@ -174,10 +171,14 @@ export class RequestService {
alias: AliasPair,
labelToRemove: string,
): Promise<OneSignalApiBaseResponse> {
const { appId } = requestMetadata;
const { appId, jwtToken } = requestMetadata;

const headers = new Headers();
addJwtHeader(headers, jwtToken);

return OneSignalApiBase.delete(
`apps/${appId}/users/by/${alias.label}/${alias.id}/identity/${labelToRemove}`,
requestMetadata.jwtHeader,
headers,
);
}

Expand All @@ -186,7 +187,7 @@ export class RequestService {
/**
* Creates a new subscription for the user identified by the given alias pair
* Useful to add email or SMS subscriptions to a user
* @param requestMetadata - { appId }
* @param requestMetadata - { appId, jwtToken }
* @param alias - alias label & id
* @param subscription - subscription label & id
*/
Expand All @@ -195,26 +196,29 @@ export class RequestService {
alias: AliasPair,
subscription: { subscription: FutureSubscriptionModel },
): Promise<OneSignalApiBaseResponse> {
const { appId } = requestMetadata;
const { appId, jwtToken } = requestMetadata;

const headers = new Headers();
addJwtHeader(headers, jwtToken);

return OneSignalApiBase.post(
`apps/${appId}/users/by/${alias.label}/${alias.id}/subscriptions`,
subscription,
requestMetadata.jwtHeader,
headers,
);
}

/**
* Updates an existing Subscription’s properties.
* @param requestMetadata - { appId }
* @param subscriptionId - subscription id
* @param requestMetadata - { appId, subscriptionId }
* @param subscription - subscription object
*/
static async updateSubscription(
requestMetadata: RequestMetadata,
subscriptionId: string,
subscription: Partial<SubscriptionModel>,
): Promise<OneSignalApiBaseResponse> {
const { appId } = requestMetadata;
const { appId, subscriptionId } = requestMetadata;

return OneSignalApiBase.patch(
`apps/${appId}/subscriptions/${subscriptionId}`,
{ subscription },
Expand All @@ -224,57 +228,56 @@ export class RequestService {
/**
* Deletes the subscription.
* Creates an "orphan" user record if the user has no other subscriptions.
* @param requestMetadata - { appId }
* @param subscriptionId - subscription id
* @param requestMetadata - { appId, subscriptionId, jwtToken }
*/
static async deleteSubscription(
requestMetadata: RequestMetadata,
subscriptionId: string,
): Promise<OneSignalApiBaseResponse> {
const { appId } = requestMetadata;
const { appId, subscriptionId, jwtToken } = requestMetadata;

const headers = new Headers();
addJwtHeader(headers, jwtToken);

return OneSignalApiBase.delete(
`apps/${appId}/subscriptions/${subscriptionId}`,
headers,
);
}

/**
* Lists all aliases for the user identified by the given subscription id
* @param requestMetadata - { appId }
* @param subscriptionId - subscription id
* @param requestMetadata - { appId, subscriptionId }
*/
static async fetchAliasesForSubscription(
requestMetadata: RequestMetadata,
subscriptionId: string,
): Promise<OneSignalApiBaseResponse> {
const { appId } = requestMetadata;
const { appId, subscriptionId } = requestMetadata;

return OneSignalApiBase.get(
`apps/${appId}/subscriptions/${subscriptionId}/identity`,
);
}

/**
* Upserts one or more aliases for the user identified by the given subscription id
* @param requestMetadata - { appId }
* @param subscriptionId - subscription id
* @param requestMetadata - { appId, subscriptionId }
* @param identity - identity label & id
*/
static async identifyUserForSubscription(
requestMetadata: RequestMetadata,
subscriptionId: string,
identity: IdentityModel,
): Promise<OneSignalApiBaseResponse> {
const { appId } = requestMetadata;
const { appId, subscriptionId } = requestMetadata;

return OneSignalApiBase.patch(
`apps/${appId}/users/by/subscriptions/${subscriptionId}/identity`,
{ identity },
requestMetadata.jwtHeader,
);
}

/**
* Transfers this Subscription to the User identified by the identity in the payload.
* @param requestMetadata - { appId }
* @param subscriptionId - subscription id
* @param requestMetadata - { appId, subscriptionId }
* @param identity - identity label & id
* @param retainPreviousOwner - if true *AND* subscription is last subscription for the previous
* user, an orphan user will be created. Otherwise, the previous user will be deleted. Useful when going
Expand All @@ -283,18 +286,17 @@ export class RequestService {
*/
static async transferSubscription(
requestMetadata: RequestMetadata,
subscriptionId: string,
identity: SupportedIdentity,
retainPreviousOwner: boolean,
): Promise<OneSignalApiBaseResponse> {
const { appId } = requestMetadata;
const { appId, subscriptionId } = requestMetadata;

return OneSignalApiBase.patch(
`apps/${appId}/subscriptions/${subscriptionId}/owner`,
{
identity: { ...identity },
retain_previous_owner: retainPreviousOwner,
},
requestMetadata.jwtHeader,
);
}
}
Loading

0 comments on commit 1b4bc37

Please sign in to comment.