Skip to content

Commit bd87faf

Browse files
committed
Add Webauthn methods to Auth class
1 parent cf0cec0 commit bd87faf

File tree

4 files changed

+160
-20
lines changed

4 files changed

+160
-20
lines changed

packages/commerce-sdk-react/src/auth/index.ts

Lines changed: 154 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,6 +1315,19 @@ class Auth {
13151315
return token
13161316
}
13171317

1318+
/**
1319+
* Get Basic auth header for private client requests.
1320+
* Returns undefined if not using a private client.
1321+
*/
1322+
private getBasicAuthHeader(client: ShopperLogin<ApiClientConfigParams>): string | undefined {
1323+
if (!this.clientSecret) {
1324+
return undefined
1325+
}
1326+
return `Basic ${stringToBase64(
1327+
`${client.clientConfig.parameters.clientId}:${this.clientSecret}`
1328+
)}`
1329+
}
1330+
13181331
/**
13191332
* A wrapper method for the SLAS endpoint: getPasswordResetToken.
13201333
*
@@ -1340,10 +1353,9 @@ class Auth {
13401353
}
13411354

13421355
// Only set authorization header if using private client
1343-
if (this.clientSecret) {
1344-
options.headers.Authorization = `Basic ${stringToBase64(
1345-
`${slasClient.clientConfig.parameters.clientId}:${this.clientSecret}`
1346-
)}`
1356+
const authHeader = this.getBasicAuthHeader(slasClient)
1357+
if (authHeader) {
1358+
options.headers.Authorization = authHeader
13471359
}
13481360

13491361
const res = await slasClient.getPasswordResetToken(options)
@@ -1371,10 +1383,9 @@ class Auth {
13711383
}
13721384

13731385
// Only set authorization header if using private client
1374-
if (this.clientSecret) {
1375-
options.headers.Authorization = `Basic ${stringToBase64(
1376-
`${slasClient.clientConfig.parameters.clientId}:${this.clientSecret}`
1377-
)}`
1386+
const authHeader = this.getBasicAuthHeader(slasClient)
1387+
if (authHeader) {
1388+
options.headers.Authorization = authHeader
13781389
}
13791390
// TODO: no code verifier needed with the fix blair has made, delete this when the fix has been merged to production
13801391
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
@@ -1424,19 +1435,148 @@ class Auth {
14241435
}
14251436
}
14261437

1427-
async startWebauthnRegistration() {
1438+
/**
1439+
* A wrapper method for the SLAS endpoint: authorizeWebauthnRegistration.
1440+
*/
1441+
async authorizeWebauthnRegistration(parameters: ShopperLoginTypes.authorizeWebauthnRegistrationBodyType) {
1442+
const slasClient = this.client
1443+
1444+
const options = {
1445+
headers: {
1446+
Authorization: ''
1447+
},
1448+
body: {
1449+
// Required params
1450+
user_id: parameters.user_id,
1451+
mode: parameters.mode,
1452+
channel_id: parameters.channel_id || slasClient.clientConfig.parameters.siteId,
1453+
}
1454+
}
1455+
1456+
const authHeader = this.getBasicAuthHeader(slasClient)
1457+
if (authHeader) {
1458+
options.headers.Authorization = authHeader
1459+
}
1460+
1461+
const res = await slasClient.authorizeWebauthnRegistration(options)
1462+
1463+
return res
14281464
}
14291465

1430-
async finishWebauthnRegistration() {
1466+
/**
1467+
* A wrapper method for the SLAS endpoint: startWebauthnUserRegistration.
1468+
*/
1469+
async startWebauthnUserRegistration(parameters: ShopperLoginTypes.startWebauthnUserRegistrationBodyType) {
1470+
const slasClient = this.client
1471+
1472+
const options ={
1473+
headers: {
1474+
Authorization: ''
1475+
},
1476+
body: {
1477+
display_name: parameters.display_name,
1478+
nick_name: parameters.nick_name,
1479+
client_id: parameters.client_id || slasClient.clientConfig.parameters.clientId,
1480+
// Required params
1481+
channel_id: parameters.channel_id || slasClient. clientConfig.parameters.siteId,
1482+
pwd_action_token: parameters.pwd_action_token,
1483+
user_id: parameters.user_id
1484+
}
1485+
}
1486+
1487+
const authHeader = this.getBasicAuthHeader(slasClient)
1488+
if (authHeader) {
1489+
options.headers.Authorization = authHeader
1490+
}
1491+
1492+
const res = await slasClient.startWebauthnUserRegistration(options)
1493+
return res
14311494
}
14321495

1433-
async authorizeWebauthnRegistration() {
1496+
/**
1497+
* A wrapper method for the SLAS endpoint: finishWebauthnUserRegistration.
1498+
*/
1499+
async finishWebauthnUserRegistration(parameters: ShopperLoginTypes.finishWebauthnUserRegistrationBodyType) {
1500+
const slasClient = this.client
1501+
1502+
const options = {
1503+
headers: {
1504+
Authorization: ''
1505+
},
1506+
body: {
1507+
// Required params
1508+
client_id: parameters.client_id || slasClient.clientConfig.parameters.clientId,
1509+
channel_id: parameters.channel_id || slasClient.clientConfig.parameters.siteId,
1510+
pwd_action_token: parameters.pwd_action_token,
1511+
username: parameters.username,
1512+
credential_id: parameters.credential_id,
1513+
}
1514+
}
1515+
1516+
const authHeader = this.getBasicAuthHeader(slasClient)
1517+
if (authHeader) {
1518+
options.headers.Authorization = authHeader
1519+
}
1520+
1521+
const res = await slasClient.finishWebauthnUserRegistration(options)
1522+
return res
14341523
}
14351524

1436-
async startWebauthnAuthentication() {
1525+
/**
1526+
* A wrapper method for the SLAS endpoint: startWebauthnAuthentication.
1527+
*/
1528+
async startWebauthnAuthentication(parameters: ShopperLoginTypes.startWebauthnAuthenticationBodyType) {
1529+
const slasClient = this.client
1530+
1531+
const options = {
1532+
headers: {
1533+
Authorization: ''
1534+
},
1535+
body: {
1536+
// Required params
1537+
client_id: parameters.client_id || slasClient.clientConfig.parameters.clientId,
1538+
channel_id: parameters.channel_id || slasClient.clientConfig.parameters.siteId,
1539+
user_id: parameters.user_id,
1540+
}
1541+
}
1542+
1543+
const authHeader = this.getBasicAuthHeader(slasClient)
1544+
if (authHeader) {
1545+
options.headers.Authorization = authHeader
1546+
}
1547+
1548+
const res = await slasClient.startWebauthnAuthentication(options)
1549+
return res
14371550
}
14381551

1439-
async finishWebauthnAuthentication() {
1440-
}
1552+
/**
1553+
* A wrapper method for the SLAS endpoint: finishWebauthnAuthentication.
1554+
*/
1555+
async finishWebauthnAuthentication(parameters: ShopperLoginTypes.finishWebauthnAuthenticationBodyType) {
1556+
const slasClient = this.client
1557+
1558+
const options = {
1559+
headers: {
1560+
Authorization: ''
1561+
},
1562+
body: {
1563+
// Required params
1564+
client_id: parameters.client_id || slasClient.clientConfig.parameters.clientId,
1565+
channel_id: parameters.channel_id || slasClient.clientConfig.parameters.siteId,
1566+
credential: parameters.credential
1567+
}
1568+
}
1569+
1570+
const authHeader = this.getBasicAuthHeader(slasClient)
1571+
if (authHeader) {
1572+
options.headers.Authorization = authHeader
1573+
}
14411574

1575+
const res = await slasClient.finishWebauthnAuthentication(options)
1576+
const tokenResponse = res.tokenResponse
1577+
this.handleTokenResponse(tokenResponse, false)
1578+
1579+
return tokenResponse
1580+
}
1581+
}
14421582
export default Auth

packages/commerce-sdk-react/src/hooks/ShopperLogin/cache.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ export const cacheUpdateMatrix: CacheUpdateMatrix<Client> = {
2929
getPasswordLessAccessToken: noop,
3030
revokeToken: noop,
3131
introspectToken: noop,
32-
startWebauthnRegistration: noop,
33-
finishWebauthnRegistration: noop,
32+
startWebauthnUserRegistration: noop,
33+
finishWebauthnUserRegistration: noop,
3434
authorizeWebauthnRegistration: noop,
3535
startWebauthnAuthentication: noop,
3636
finishWebauthnAuthentication: noop

packages/commerce-sdk-react/src/hooks/ShopperLogin/mutation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,12 @@ The value of the `_sfdc_client_auth` header must be a Base64-encoded string. The
102102
* Start WebAuthn passkey registration. Starts the WebAuthn registration process by generating credential creation options. Returns the challenge and other parameters needed by the authenticator to create a new credential.
103103
* @returns A TanStack Query mutation hook for interacting with the Shopper Login `startWebauthnRegistration` endpoint.
104104
*/
105-
StartWebauthnRegistration: 'startWebauthnRegistration',
105+
StartWebauthnUserRegistration: 'startWebauthnUserRegistration',
106106
/**
107107
* Finish WebAuthn passkey registration. Completes the WebAuthn registration process by verifying the credential created by the authenticator. Stores the public key and credential information for future authentication.
108108
* @returns A TanStack Query mutation hook for interacting with the Shopper Login `finishWebauthnRegistration` endpoint.
109109
*/
110-
FinishWebauthnRegistration: 'finishWebauthnRegistration',
110+
FinishWebauthnUserRegistration: 'finishWebauthnUserRegistration',
111111
/**
112112
* Authorize WebAuthn passkey registration. Authorizes a user to register a WebAuthn credential (passkey). This endpoint validates the user's credentials and creates a password action token that can be used to start the registration process. The token is sent to the user via the specified channel (email or SMS).
113113
* @returns A TanStack Query mutation hook for interacting with the Shopper Login `authorizeWebauthnRegistration` endpoint.

packages/commerce-sdk-react/src/hooks/useAuthHelper.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ export const AuthHelpers = {
3232
Register: 'register',
3333
ResetPassword: 'resetPassword',
3434
UpdateCustomerPassword: 'updateCustomerPassword',
35-
StartWebauthnRegistration: 'startWebauthnRegistration',
36-
FinishWebauthnRegistration: 'finishWebauthnRegistration',
35+
StartWebauthnUserRegistration: 'startWebauthnRegistration',
36+
FinishWebauthnUserRegistration: 'finishWebauthnRegistration',
3737
AuthorizeWebauthnRegistration: 'authorizeWebauthnRegistration',
3838
StartWebauthnAuthentication: 'startWebauthnAuthentication',
3939
FinishWebauthnAuthentication: 'finishWebauthnAuthentication'

0 commit comments

Comments
 (0)