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

DO NOT MERGE - Add a separate function for calling getAccessToken #138

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
87 changes: 87 additions & 0 deletions src/static/helpers/slasHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,70 @@ export async function authorize(
return {url: redirectUrlString, ...getCodeAndUsidFromUrl(redirectUrlString)};
}

/**
* Wrapper for the /token endpoint. Depending on the parameters provided, will set up different grant types.
* @param slasClient a configured instance of the ShopperLogin SDK client
*/
export async function getAccessToken(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a catch-all wrapper for the getAccessToken endpoint that can handle refresh tokens, pkce (public client) logins, and client-credentials (private client) logins.

Later on, we could refactor the other helper methods to call this rather than setting up the /token call for a specific case in many places.

slasClient: ShopperLogin<{
shortCode: string;
organizationId: string;
clientId: string;
siteId: string;
}>,
parameters: {
redirectURI: string;
refreshToken?: string;
codeVerifier?: string;
code?: string;
usid?: string;
},
credentials?: {
client_id: string;
client_secret: string;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the PWA Kit, we're using a proxy to intercept the call to SLAS and inject the client secret there. So PWA Kit does not set this arg.

However, this arg is here to enable non-PWA Kit projects that may want to use a private client.

): Promise<TokenResponse> {

let tokenHeader = {}
if (credentials) {
const authorization = `Basic ${stringToBase64(
`${credentials.client_id}:${credentials.client_secret}`
)}`;

tokenHeader = {
Authorization: authorization
}
}

const grantType = parameters.refreshToken ? 'refresh_token'
: parameters.codeVerifier ? 'authorization_code_pkce'
: parameters.code ? 'authorization_code'
: 'client_credentials'
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 parameters we provide to /token affect the grant_type and what the endpoint ultimately does.

I think the order of this conditional is the correct priority order for how to handle scenarios where multiple parameters corresponding to different grant_types are provided.


const tokenBody: TokenRequest = {
client_id: slasClient.clientConfig.parameters.clientId,
channel_id: slasClient.clientConfig.parameters.siteId,
grant_type: grantType,
redirect_uri: parameters.redirectURI
};

if (parameters.refreshToken) {
tokenBody.refresh_token = parameters.refreshToken
} else if (parameters.codeVerifier) {
tokenBody.code = parameters.code,
tokenBody.code_verifier = parameters.codeVerifier
} else if (parameters.code) {
tokenBody.code = parameters.code
}

if (parameters.usid) {
tokenBody.usid = parameters.usid
}

return slasClient.getAccessToken({headers: tokenHeader, body: tokenBody});
}


/**
* A single function to execute the ShopperLogin Public Client Guest Login with proof key for code exchange flow as described in the [API documentation](https://developer.salesforce.com/docs/commerce/commerce-api/references?meta=shopper-login:Summary).
* @param slasClient a configured instance of the ShopperLogin SDK client.
Expand Down Expand Up @@ -188,6 +252,29 @@ export async function loginGuestUser(
return slasClient.getAccessToken({body: tokenBody});
}

export async function loginGuestUserPrivateClient(
slasClient: ShopperLogin<{
shortCode: string;
organizationId: string;
clientId: string;
siteId: string;
}>,
parameters: {
redirectURI: string;
refreshToken?: string;
codeVerifier?: string;
code?: string;
usid?: string;
},
credentials?: {
client_id: string;
client_secret: string;
}
): Promise<TokenResponse> {

return await getAccessToken(slasClient, parameters, credentials);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

commerce-sdk-isomorphic assumes we are using a SLAS public client

When using a SLAS private client, we want to skip the call to /authorize. We can immediately call the /token endpoint

/**
* A single function to execute the ShopperLogin Public Client Registered User B2C Login with proof key for code exchange flow as described in the [API documentation](https://developer.salesforce.com/docs/commerce/commerce-api/references?meta=shopper-login:Summary).
* @param slasClient a configured instance of the ShopperLogin SDK client.
Expand Down
Loading