-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathjwt-utils.js
More file actions
33 lines (29 loc) · 1.24 KB
/
jwt-utils.js
File metadata and controls
33 lines (29 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
* Copyright (c) 2021, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import {createRemoteJWKSet as joseCreateRemoteJWKSet, jwtVerify} from 'jose'
import {getAppOrigin} from '@salesforce/pwa-kit-react-sdk/utils/url'
import {getConfig} from '@salesforce/pwa-kit-runtime/utils/ssr-config'
const throwSlasTokenValidationError = (message, code) => {
throw new Error(`SLAS Token Validation Error: ${message}`, code)
}
export const createRemoteJWKSet = () => {
const appOrigin = getAppOrigin()
const {app: appConfig} = getConfig()
const shortCode = appConfig.commerceAPI.parameters.shortCode
const tenantId = appConfig.commerceAPI.parameters.organizationId.replace(/^f_ecom_/, '')
const JWKS_URI = `${appOrigin}/${shortCode}/${tenantId}/oauth2/jwks`
return joseCreateRemoteJWKSet(new URL(JWKS_URI))
}
export const validateSlasCallbackToken = async (token) => {
try {
const jwks = createRemoteJWKSet()
const {payload} = await jwtVerify(token, jwks, {})
return payload
} catch (error) {
throwSlasTokenValidationError(error.message, 401)
}
}