Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for SLAS DNT (do not track) parameter #167

Merged
merged 6 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## v3.1.0

### Enhancements
- Update SLAS helpers to support DNT parameter [#167](https://github.com/SalesforceCommerceCloud/commerce-sdk-isomorphic/pull/167)

## v3.0.0

### :warning: Planned API Changes :warning:
Expand Down
9 changes: 6 additions & 3 deletions src/static/helpers/slasHelper.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @jest-environment node
*/
/* eslint header/header: "off" */
/* eslint header/header: "off", max-lines:"off" */
/*
* Copyright (c) 2022, salesforce.com, inc.
* All rights reserved.
Expand Down Expand Up @@ -47,6 +47,7 @@ const parameters = {
refreshToken: 'refresh_token',
usid: 'usid',
hint: 'hint',
dnt: true,
};

const url =
Expand All @@ -55,9 +56,7 @@ const url =
const authenticateCustomerMock = jest.fn(() => ({url}));

const getAccessTokenMock = jest.fn(() => expectedTokenResponse);

const logoutCustomerMock = jest.fn(() => expectedTokenResponse);

const generateCodeChallengeMock = jest.fn(() => 'code_challenge');

const createMockSlasClient = () =>
Expand Down Expand Up @@ -220,6 +219,7 @@ describe('Guest user flow', () => {
grant_type: 'authorization_code_pkce',
redirect_uri: 'redirect_uri',
usid: '048adcfb-aa93-4978-be9e-09cb569fdcb9',
dnt: 'true',
},
};
const mockSlasClient = createMockSlasClient();
Expand Down Expand Up @@ -257,6 +257,7 @@ describe('Guest user flow', () => {
grant_type: 'client_credentials',
channel_id: 'site_id',
usid: 'usid',
dnt: 'true',
},
};
expect(getAccessTokenMock).toBeCalledWith(expectedReqOptions);
Expand Down Expand Up @@ -300,6 +301,7 @@ describe('Registered B2C user flow', () => {
organizationId: 'organization_id',
redirect_uri: 'redirect_uri',
usid: '048adcfb-aa93-4978-be9e-09cb569fdcb9',
dnt: 'true',
},
};

Expand Down Expand Up @@ -338,6 +340,7 @@ describe('Registered B2C user flow', () => {
channel_id: 'site_id',
organizationId: 'organization_id',
usid: '048adcfb-aa93-4978-be9e-09cb569fdcb9',
dnt: 'true',
},
};
// slasClient is copied and tries to make an actual API call
Expand Down
9 changes: 9 additions & 0 deletions src/static/helpers/slasHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ export async function authorize(
* @param credentials.clientSecret - secret associated with client ID
* @param parameters - parameters to pass in the API calls.
* @param parameters.usid? - Unique Shopper Identifier to enable personalization.
* @param parameters.dnt? - Optional parameter to enable Do Not Track (DNT) for the user.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it is boolean, does it make sense to set default value instead of making the parameter optional?dnt=true

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the default is controlled at the API level, if this parameter is not present, SLAS default to dnt=false.

* @returns TokenResponse
*/
export async function loginGuestUserPrivate(
Expand All @@ -177,6 +178,7 @@ export async function loginGuestUserPrivate(
}>,
parameters: {
usid?: string;
dnt?: boolean;
},
credentials: {
clientSecret: string;
Expand All @@ -200,6 +202,7 @@ export async function loginGuestUserPrivate(
grant_type: 'client_credentials',
channel_id: slasClient.clientConfig.parameters.siteId,
...(parameters.usid && {usid: parameters.usid}),
...(parameters.dnt && {dnt: parameters.dnt.toString()}),
},
};

Expand All @@ -212,6 +215,7 @@ export async function loginGuestUserPrivate(
* @param parameters - parameters to pass in the API calls.
* @param parameters.redirectURI - Per OAuth standard, a valid app route. Must be listed in your SLAS configuration. On server, this will not be actually called. On browser, this will be called, but ignored.
* @param parameters.usid? - Unique Shopper Identifier to enable personalization.
* @param parameters.dnt? - Optional parameter to enable Do Not Track (DNT) for the user.
* @returns TokenResponse
*/
export async function loginGuestUser(
Expand All @@ -224,6 +228,7 @@ export async function loginGuestUser(
parameters: {
redirectURI: string;
usid?: string;
dnt?: boolean;
}
): Promise<TokenResponse> {
const codeVerifier = createCodeVerifier();
Expand All @@ -242,6 +247,7 @@ export async function loginGuestUser(
grant_type: 'authorization_code_pkce',
redirect_uri: parameters.redirectURI,
usid: authResponse.usid,
...(parameters.dnt && {dnt: parameters.dnt.toString()}),
};

return slasClient.getAccessToken({body: tokenBody});
Expand All @@ -258,6 +264,7 @@ export async function loginGuestUser(
* @param parameters - parameters to pass in the API calls.
* @param parameters.redirectURI - Per OAuth standard, a valid app route. Must be listed in your SLAS configuration. On server, this will not be actually called. On browser, this will be called, but ignored.
* @param parameters.usid? - Unique Shopper Identifier to enable personalization.
* @param parameters.dnt? - Optional parameter to enable Do Not Track (DNT) for the user.
* @returns TokenResponse
*/
export async function loginRegisteredUserB2C(
Expand All @@ -275,6 +282,7 @@ export async function loginRegisteredUserB2C(
parameters: {
redirectURI: string;
usid?: string;
dnt?: boolean;
}
): Promise<TokenResponse> {
const codeVerifier = createCodeVerifier();
Expand Down Expand Up @@ -331,6 +339,7 @@ export async function loginRegisteredUserB2C(
organizationId: slasClient.clientConfig.parameters.organizationId,
redirect_uri: parameters.redirectURI,
usid: authResponse.usid,
...(parameters.dnt && {dnt: parameters.dnt.toString()}),
};
// using slas private client
if (credentials.clientSecret) {
Expand Down
Loading