ocapi-auth
Simple no dependency module, which provide jwt or oauth2 authentication for Salesforce Commerce Cloud (Demandware) OCAPI
Table of contents
Installation
$ npm i ocapi-auth
Usage
const createJwtToken = const jwt = await
API
createJwtToken(options, httpOptions)
Create and return JWT token
options:
raw
- iftrue
, will return a response as object in{header, body}
format (default:false
)version
- OCAPI version. (default:18.7
)hostname
- target host for shop API calls without path, e.gaccount.demandware.com
.site
- target sitetype
- type of authorization, can becredentials
,guest
,refresh
,session
clientId
- OCAPI client Id. If empty - default developer client id will be usedauthorization
- authorization info- for
guest
type - leave it empty - for
credentials
type - base64 encode client login:password. Can be withBasic
keyword or without - for
session
type -dwsid
anddwsecuretoken
cookie in string format e.g'dwsid=value;dwsecuretoken_d6=value'
- for
refresh
type - JWT token to refresh. Can be withBearer
keyword or without
- for
httpOptions
- Http options from http module.
Default options for
rejectUnauthorized
istrue
only for productionAPP_ENV
startSession(options, httpOptions)
Create a session bridge using JWT token. Returns set-cookie
value from response
Options:
raw
- will return a response as object in{header, body}
formatversion
- OCAPI version. (default:18.7
)hostname
- target host for shop API calls without path, e.gaccount.demandware.com
.site
- target siteauthorization
- JWT token to exchange
httpOptions
- Http options from http module.
Default options for
rejectUnauthorized
istrue
only for productionAPP_ENV
createOauthToken(options, httpOptions)
Create a OAUTH token and return response body.Can obtain Client Credentials
and Business Manager
user grants
Options:
raw
- will return a response as object in{header, body}
formathostname
- target host for calls without path. If empty, Client Credentials grant will be obtained fromaccount.demandware.com
authorization
-User Login:User Password:Client Password
in base64 encoding. If empty, demo values will be used. Can be withBasic
keyword or withoutclientId
- OCAPI client Id. If empty - default developer client id will be used
httpOptions
- Http options from http module.
Default options for
rejectUnauthorized
istrue
only for productionAPP_ENV
getPayload(jwt)
Return an object with decoded JWT token payload
jwt
- OCAPI jwt token
getHeader(jwt)
Return an object with decoded OCAPI JWT token header
jwt
- OCAPI jwt token
getSignature(jwt)
Return an OCAPI JWT token signature
jwt
- OCAPI jwt token
Examples
Refresh JWT token
const jwt = await console//Bearer eyJfdiI6IjEiLCJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.11.dl8XgldAVo8SGRDrrSAdnbD_tnRnfYwIrohjhsPW78JgTif2kukQqnB74RgKHRx6U5CTBee8ktTVwqnmtguRTwnI1NbYsMyf3pCjz67qrqB6NMoNOscFQpcCpgwh0xvlPUqj8j8bBnYTciUues66WeQI38pvTfH4j3oaucbjiNjwkUGCPs_LS_JYaTNhkdqjlRDBcyYo3h8ArKx_5YJK18aynZ00coTdezBKLTzMb7ByQTjiYk7tOi-111-DBFFb11jtaxaA-LwzP8XZrXJ6c_lBPysEJ_y7GwuPU3HuE
Create session bridge
const session = await console/*[ 'dwsecuretoken_d6ba5aaa95d9e677ab103d74c76dc070=7lEE0m7klEXyyP5H5-Ela-9_NTg5JvfQFA==; Version=1; Comment="Demandware Secure Token for site Sites-adidas-GB-Site"; Path=/; Secure; HttpOnly', 'dwsid=hxx0vh8XPy7sbyGezjyvGhwkgew84x30r-TKPJtP_m1AUKmVswzkBS31flmUlwC1U7meiMxpCby4I8hYR1CXZw==; Path=/; HttpOnly', '__cq_dnt=0; Path=/', 'dw_dnt=0; Path=/', 'dwanonymous_d6ba5aaa95d9e677ab103d74c76dc070=ac3MRVWXuTV1eapHtdKXuQL3Bt; Version=1; Comment="Demandware anonymous cookie for site Sites-adidas-GB-Site"; Max-Age=15552000; Expires=Mon, 11-Feb-2019 12:03:23 GMT; Path=/' ] */
Obtain OAUTH Client Credentials grants
const oauth = await console//{"access_token":"903648f4-3c6b-45ee-9655-f8742f0a538e","scope":"mail","token_type":"Bearer","expires_in":1799}
Obtain OAUTH Business Manager grants
const oauth = await console//{"access_token":"781d451f-0162-4457-a378-6b265038f6be","expires_in":899,"token_type":"Bearer"}