-
Notifications
You must be signed in to change notification settings - Fork 21
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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( | ||
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; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The parameters we provide to 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. | ||
|
@@ -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); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
/** | ||
* 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. | ||
|
There was a problem hiding this comment.
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.