@commercelayer/js-auth
Advanced tools
Comparing version 6.0.1 to 6.1.0
@@ -85,3 +85,5 @@ interface TBaseOptions { | ||
interface TPasswordOptions extends TBaseOptions { | ||
/** The customer's email address. */ | ||
username: string; | ||
/** The customer's password */ | ||
password: string; | ||
@@ -104,4 +106,13 @@ } | ||
interface TAuthorizationCodeOptions extends TBaseOptions { | ||
/** | ||
* The authorization code that [you got](https://docs.commercelayer.io/core/authentication/authorization-code#getting-an-authorization-code) from the redirect URI query string. | ||
*/ | ||
code: string; | ||
/** | ||
* Your application's redirect URI. | ||
*/ | ||
redirectUri: string; | ||
/** | ||
* Your application's client secret. | ||
*/ | ||
clientSecret: string; | ||
@@ -118,2 +129,6 @@ } | ||
interface TClientCredentialsOptions extends TBaseOptions { | ||
/** | ||
* Your application's client secret | ||
* (required for [confidential](https://docs.commercelayer.io/core/authentication/client-credentials#integration) API credentials). | ||
*/ | ||
clientSecret?: string; | ||
@@ -154,3 +169,10 @@ } | ||
interface TRefreshTokenOptions extends TBaseOptions { | ||
/** | ||
* A valid `refresh_token`. | ||
*/ | ||
refreshToken: string; | ||
/** | ||
* Your application's client secret | ||
* (required for confidential API credentials — i.e. in case of [authorization code flow](https://docs.commercelayer.io/core/authentication/refresh-token#webapp-application-with-authorization-code-flow)). | ||
*/ | ||
clientSecret?: string; | ||
@@ -160,7 +182,10 @@ } | ||
/** | ||
* The grant type. | ||
* The type of OAuth 2.0 grant being used for authentication. | ||
*/ | ||
type GrantType = 'password' | 'refresh_token' | 'client_credentials' | 'authorization_code' | 'urn:ietf:params:oauth:grant-type:jwt-bearer'; | ||
/** The options type for the `authenticate` helper. */ | ||
type AuthenticateOptions<TGrantType extends GrantType> = TGrantType extends 'urn:ietf:params:oauth:grant-type:jwt-bearer' ? TJwtBearerOptions : TGrantType extends 'password' ? TPasswordOptions : TGrantType extends 'refresh_token' ? TRefreshTokenOptions : TGrantType extends 'client_credentials' ? TClientCredentialsOptions : TGrantType extends 'authorization_code' ? TAuthorizationCodeOptions : never; | ||
/** The return type of the `authenticate` helper. */ | ||
type AuthenticateReturn<TGrantType extends GrantType> = TGrantType extends 'urn:ietf:params:oauth:grant-type:jwt-bearer' ? TJwtBearerReturn : TGrantType extends 'password' ? TPasswordReturn : TGrantType extends 'refresh_token' ? TPasswordReturn : TGrantType extends 'client_credentials' ? TBaseReturn : TGrantType extends 'authorization_code' ? TAuthorizationCodeReturn : never; | ||
/** The options type for the `revoke` helper. */ | ||
type RevokeOptions = Pick<TBaseOptions, 'clientId' | 'domain'> & { | ||
@@ -172,6 +197,41 @@ /** Your application's client secret (required for confidential API credentials and non-confidential API credentials without a customer or a user in the JWT only). */ | ||
}; | ||
/** The return type of the `revoke` helper. */ | ||
type RevokeReturn = Pick<TError, 'errors'>; | ||
declare function authenticate<G extends GrantType>(grantType: G, { domain, headers, ...options }: AuthenticateOptions<G>): Promise<AuthenticateReturn<G>>; | ||
/** | ||
* Authenticate helper used to get the access token. | ||
* | ||
* _Please note that the authentication endpoint is subject to a [rate limit](https://docs.commercelayer.io/core/rate-limits) | ||
* of **max 30 reqs / 1 min** both in live and test mode._ | ||
* @param grantType The type of OAuth 2.0 grant being used for authentication. | ||
* @param options Authenticate options | ||
* @returns | ||
* @example | ||
* ```ts | ||
* import { authenticate } from '@commercelayer/js-auth' | ||
* | ||
* const auth = await authenticate('client_credentials', { | ||
* clientId: '{{ clientId }}', | ||
* scope: 'market:id:DGzAouppwn' | ||
* }) | ||
* | ||
* console.log(auth.accessToken) | ||
* ``` | ||
*/ | ||
declare function authenticate<TGrantType extends GrantType>(grantType: TGrantType, { domain, headers, ...options }: AuthenticateOptions<TGrantType>): Promise<AuthenticateReturn<TGrantType>>; | ||
/** | ||
* Revoke a previously generated access token (refresh tokens included) before its natural expiration date. | ||
* | ||
* @param options Revoke options | ||
* @returns | ||
* @example | ||
* ```ts | ||
* await revoke({ | ||
* clientId: '{{ integrationClientId }}', | ||
* clientSecret: '{{ integrationClientSecret }}', | ||
* token: authenticateResponse.accessToken | ||
* }) | ||
* ``` | ||
*/ | ||
declare function revoke({ domain, ...options }: RevokeOptions): Promise<RevokeReturn>; | ||
@@ -288,6 +348,31 @@ | ||
}; | ||
/** | ||
* Checks if the provided payload represents a `user`. | ||
* @param payload The payload to be checked. | ||
* @returns | ||
*/ | ||
declare function jwtIsUser(payload: Payload): payload is JWTUser; | ||
/** | ||
* Checks if the provided payload represents a `dashboard`. | ||
* @param payload The payload to be checked. | ||
* @returns | ||
*/ | ||
declare function jwtIsDashboard(payload: Payload): payload is JWTDashboard; | ||
/** | ||
* Checks if the provided payload represents an `integration`. | ||
* @param payload The payload to be checked. | ||
* @returns | ||
*/ | ||
declare function jwtIsIntegration(payload: Payload): payload is JWTIntegration; | ||
/** | ||
* Checks if the provided payload represents a `sales_channel`. | ||
* @param payload The payload to be checked. | ||
* @returns | ||
*/ | ||
declare function jwtIsSalesChannel(payload: Payload): payload is JWTSalesChannel; | ||
/** | ||
* Checks if the provided payload represents a `webapp`. | ||
* @param payload The payload to be checked. | ||
* @returns | ||
*/ | ||
declare function jwtIsWebApp(payload: Payload): payload is JWTWebApp; | ||
@@ -294,0 +379,0 @@ |
@@ -1,2 +0,2 @@ | ||
function p(e){return e.replace(/[A-Z]/g,function(n){return"_"+n.toLowerCase()})}function i(e,n){return Object.keys(e).reduce((t,a)=>{let r=n(a);return t[r]=e[a],t},{})}function d(e){return e.replace(/([-_][a-z])/g,n=>n.toUpperCase().replace("-","").replace("_",""))}async function u(e,{domain:n="commercelayer.io",headers:t,...a}){let r=i({grant_type:e,...a},p),s=await(await fetch(`https://auth.${n}/oauth/token`,{method:"POST",headers:{"Content-Type":"application/json",Accept:"application/json",...t},body:JSON.stringify(r)})).json();return s.expires=new Date(Date.now()+s.expires_in*1e3),i(s,d)}async function g({domain:e="commercelayer.io",...n}){let t=i(n,p);return await(await fetch(`https://auth.${e}/oauth/revoke`,{method:"POST",headers:{"Content-Type":"application/json",Accept:"application/json"},body:JSON.stringify(t)})).json()}function c(e){return typeof window<"u"?window.btoa(e).replaceAll("=","").replaceAll("+","-").replaceAll("/","_"):Buffer.from(e,"binary").toString("base64url")}function l(e){return typeof window<"u"?window.atob(e.replaceAll("-","+").replaceAll("_","/")):Buffer.from(e,"base64url").toString("binary")}function m(e){let[n,t]=e.split(".");return{header:JSON.parse(n!=null?l(n):"null"),payload:JSON.parse(t!=null?l(t):"null")}}function y(e){return e.application.kind==="user"}function f(e){return e.application.kind==="dashboard"}function C(e){return e.application.kind==="integration"}function T(e){return e.application.kind==="sales_channel"}function w(e){return e.application.kind==="webapp"}async function k({payload:e}){return await h(e,"cl")}async function h(e,n){let a=c(JSON.stringify({alg:"HS512",typ:"JWT"})),r=c(JSON.stringify({...e,iat:Math.floor(new Date().getTime()/1e3)})),o=`${a}.${r}`,s=await S(o,n);return`${o}.${s}`}async function S(e,n){let t=new TextEncoder,a={name:"HMAC",hash:"SHA-512"},r=await crypto.subtle.importKey("raw",t.encode(n),a,!1,["sign","verify"]),o=await crypto.subtle.sign(a.name,r,t.encode(e));return c(String.fromCharCode(...new Uint8Array(o)))}export{u as authenticate,k as createAssertion,m as jwtDecode,f as jwtIsDashboard,C as jwtIsIntegration,T as jwtIsSalesChannel,y as jwtIsUser,w as jwtIsWebApp,g as revoke}; | ||
function p(e){return e.replace(/[A-Z]/g,function(n){return"_"+n.toLowerCase()})}function i(e,n){return Object.keys(e).reduce((t,a)=>{let r=n(a);return t[r]=e[a],t},{})}function d(e){return e.replace(/([-_][a-z])/g,n=>n.toUpperCase().replace("-","").replace("_",""))}async function u(e,{domain:n="commercelayer.io",headers:t,...a}){let r=i({grant_type:e,...a},p),s=await(await fetch(`https://auth.${n}/oauth/token`,{method:"POST",headers:{"Content-Type":"application/json",Accept:"application/json",...t},body:JSON.stringify(r)})).json();return s.expires=new Date(Date.now()+s.expires_in*1e3),i(s,d)}async function y({domain:e="commercelayer.io",...n}){let t=i(n,p);return await(await fetch(`https://auth.${e}/oauth/revoke`,{method:"POST",headers:{"Content-Type":"application/json",Accept:"application/json"},body:JSON.stringify(t)})).json()}function c(e){return typeof window<"u"?window.btoa(e).replaceAll("=","").replaceAll("+","-").replaceAll("/","_"):Buffer.from(e,"binary").toString("base64url")}function l(e){return typeof window<"u"?window.atob(e.replaceAll("-","+").replaceAll("_","/")):Buffer.from(e,"base64url").toString("binary")}function g(e){let[n,t]=e.split(".");return{header:JSON.parse(n!=null?l(n):"null"),payload:JSON.parse(t!=null?l(t):"null")}}function m(e){return e.application.kind==="user"}function f(e){return e.application.kind==="dashboard"}function T(e){return e.application.kind==="integration"}function C(e){return e.application.kind==="sales_channel"}function w(e){return e.application.kind==="webapp"}async function k({payload:e}){return await h(e,"cl")}async function h(e,n){let a=c(JSON.stringify({alg:"HS512",typ:"JWT"})),r=c(JSON.stringify({...e,iat:Math.floor(new Date().getTime()/1e3)})),o=`${a}.${r}`,s=await S(o,n);return`${o}.${s}`}async function S(e,n){let t=new TextEncoder,a={name:"HMAC",hash:"SHA-512"},r=await crypto.subtle.importKey("raw",t.encode(n),a,!1,["sign","verify"]),o=await crypto.subtle.sign(a.name,r,t.encode(e));return c(String.fromCharCode(...new Uint8Array(o)))}export{u as authenticate,k as createAssertion,g as jwtDecode,f as jwtIsDashboard,T as jwtIsIntegration,C as jwtIsSalesChannel,m as jwtIsUser,w as jwtIsWebApp,y as revoke}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@commercelayer/js-auth", | ||
"version": "6.0.1", | ||
"description": "Commerce Layer Javascript Auth", | ||
"version": "6.1.0", | ||
"description": "A JavaScript library designed to simplify authentication when interacting with the Commerce Layer API.", | ||
"repository": { | ||
@@ -56,4 +56,5 @@ "url": "https://github.com/commercelayer/commercelayer-js-auth.git" | ||
"test:watch": "vitest --silent", | ||
"build": "tsup" | ||
"build": "tsup", | ||
"jsr:publish": "pnpm dlx jsr publish" | ||
} | ||
} |
# Commerce Layer JS Auth | ||
A JavaScript Library wrapper that helps you use the Commerce Layer API for [Authentication](https://docs.commercelayer.io/developers/authentication). | ||
A JavaScript library designed to simplify [authentication](https://docs.commercelayer.io/developers/authentication) when interacting with the Commerce Layer API. | ||
@@ -5,0 +5,0 @@ ## What is Commerce Layer? |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
84039
430