@commercelayer/js-auth
Advanced tools
Comparing version 6.3.1 to 6.4.0
@@ -259,2 +259,3 @@ interface TBaseOptions { | ||
public: boolean; | ||
client_id: string; | ||
}; | ||
@@ -271,3 +272,3 @@ /** Scope used to restrict access to a specific active market and/or stock location. */ | ||
iat: number; | ||
/** Who created and signed this token (e.g. `"https://commercelayer.io"`). */ | ||
/** Who created and signed this token (e.g. `"https://auth.commercelayer.io"`). */ | ||
iss: string; | ||
@@ -396,3 +397,6 @@ } | ||
*/ | ||
declare function jwtVerify(accessToken: string, { ignoreExpiration, domain }?: JwtVerifyOptions): Promise<CommerceLayerJWT>; | ||
declare function jwtVerify(accessToken: string, { ignoreExpiration, domain, jwk }?: JwtVerifyOptions): Promise<CommerceLayerJWT>; | ||
type CommerceLayerJsonWebKey = JsonWebKey & { | ||
kid: string; | ||
}; | ||
interface JwtVerifyOptions { | ||
@@ -408,2 +412,10 @@ /** | ||
ignoreExpiration?: boolean; | ||
/** | ||
* Json Web Key used to verify the signature. | ||
* | ||
* The `kid` must match the `kid` from decoded accessToken. | ||
* | ||
* By default, we pick the jwk from https://auth.commercelayer.io/.well-known/jwks.json using the `kid` from the accessToken. | ||
*/ | ||
jwk?: CommerceLayerJsonWebKey; | ||
} | ||
@@ -470,8 +482,15 @@ | ||
/** | ||
* The token is not valid. | ||
*/ | ||
declare class InvalidTokenError extends TokenError { | ||
constructor(message: string); | ||
} | ||
/** | ||
* The token expired. | ||
*/ | ||
declare class TokenExpiredError extends Error { | ||
declare class TokenExpiredError extends TokenError { | ||
constructor(); | ||
} | ||
export { type AuthenticateOptions, type AuthenticateReturn, type GrantType, type JWTDashboard, type JWTIntegration, type JWTSalesChannel, type JWTUser, type JWTWebApp, type RevokeOptions, type RevokeReturn, TokenError, TokenExpiredError, authenticate, createAssertion, jwtDecode, jwtIsDashboard, jwtIsIntegration, jwtIsSalesChannel, jwtIsUser, jwtIsWebApp, jwtVerify, revoke }; | ||
export { type AuthenticateOptions, type AuthenticateReturn, type GrantType, InvalidTokenError, type JWTDashboard, type JWTIntegration, type JWTSalesChannel, type JWTUser, type JWTWebApp, type RevokeOptions, type RevokeReturn, TokenError, TokenExpiredError, authenticate, createAssertion, jwtDecode, jwtIsDashboard, jwtIsIntegration, jwtIsSalesChannel, jwtIsUser, jwtIsWebApp, jwtVerify, revoke }; |
@@ -1,2 +0,2 @@ | ||
function d(e){return e.replace(/[A-Z]/g,function(t){return"_"+t.toLowerCase()})}function p(e,t){return Object.keys(e).reduce((r,n)=>{let o=t(n);return r[o]=e[n],r},{})}function m(e){return e.replace(/([-_][a-z])/g,t=>t.toUpperCase().replace("-","").replace("_",""))}async function w(e,{domain:t="commercelayer.io",headers:r,...n}){let o=p({grant_type:e,...n},d),s=await(await fetch(`https://auth.${t}/oauth/token`,{method:"POST",headers:{"Content-Type":"application/json",Accept:"application/json",...r},body:JSON.stringify(o)})).json();return s.errors==null&&(s.expires=new Date(Date.now()+s.expires_in*1e3)),p(s,m)}async function k({domain:e="commercelayer.io",...t}){let r=p(t,d);return await(await fetch(`https://auth.${e}/oauth/revoke`,{method:"POST",headers:{"Content-Type":"application/json",Accept:"application/json"},body:JSON.stringify(r)})).json()}var i=class extends Error{constructor(t){super(t),this.name="TokenError"}};function y(e){return typeof btoa<"u"?btoa(e).replaceAll("=","").replaceAll("+","-").replaceAll("/","_"):Buffer.from(e,"binary").toString("base64url")}function c(e){return typeof atob<"u"?atob(e.replaceAll("-","+").replaceAll("_","/")):Buffer.from(e,"base64url").toString("binary")}function u(e){let[t,r,n]=`${e}`.split(".");if(t==null||r==null||n==null)throw new i("Invalid token format");return{header:JSON.parse(c(t)),payload:JSON.parse(c(r)),signature:n}}function C(e){return e.application.kind==="user"}function h(e){return e.application.kind==="dashboard"}function x(e){return e.application.kind==="integration"}function S(e){return e.application.kind==="sales_channel"}function J(e){return e.application.kind==="webapp"}var l=class extends Error{constructor(){super("Token expired"),this.name="TokenExpiredError"}};async function b(e,{ignoreExpiration:t=!1,domain:r}={}){let n=u(e),o=await W(n.header.kid,{domain:r,ignoreExpiration:t});if(o==null)throw new i('Invalid token "kid"');if(!t&&Date.now()>=n.payload.exp*1e3)throw new l;let a={name:"RSASSA-PKCS1-v1_5",hash:"SHA-512"},s=await crypto.subtle.importKey("jwk",o,a,!0,["verify"]),f=new Uint8Array(Array.from(c(n.signature),T=>T.charCodeAt(0))),g=new TextEncoder().encode(e.split(".").slice(0,2).join("."));if(!await crypto.subtle.verify(a,s,f,g))throw new i("Invalid signature");return n}async function W(e,{domain:t="commercelayer.io"}){return(await fetch(`https://auth.${t}/.well-known/jwks.json`).then(async n=>await n.json())).keys.find(n=>n.kid===e)}async function A({payload:e}){return await R(e,"cl")}async function R(e,t){let n=y(JSON.stringify({alg:"HS512",typ:"JWT"})),o=y(JSON.stringify({...e,iat:Math.floor(new Date().getTime()/1e3)})),a=`${n}.${o}`,s=await j(a,t);return`${a}.${s}`}async function j(e,t){let r=new TextEncoder,n={name:"HMAC",hash:"SHA-512"},o=await crypto.subtle.importKey("raw",r.encode(t),n,!1,["sign","verify"]),a=await crypto.subtle.sign(n.name,o,r.encode(e));return y(String.fromCharCode(...new Uint8Array(a)))}export{i as TokenError,l as TokenExpiredError,w as authenticate,A as createAssertion,u as jwtDecode,h as jwtIsDashboard,x as jwtIsIntegration,S as jwtIsSalesChannel,C as jwtIsUser,J as jwtIsWebApp,b as jwtVerify,k as revoke}; | ||
function y(e){return e.replace(/[A-Z]/g,function(n){return"_"+n.toLowerCase()})}function c(e,n){return Object.keys(e).reduce((t,r)=>{let o=n(r);return t[o]=e[r],t},{})}function g(e){return e.replace(/([-_][a-z])/g,n=>n.toUpperCase().replace("-","").replace("_",""))}async function h(e,{domain:n="commercelayer.io",headers:t,...r}){let o=c({grant_type:e,...r},y),s=await(await fetch(`https://auth.${n}/oauth/token`,{method:"POST",headers:{"Content-Type":"application/json",Accept:"application/json",...t},body:JSON.stringify(o)})).json();return s.errors==null&&(s.expires=new Date(Date.now()+s.expires_in*1e3)),c(s,g)}async function x({domain:e="commercelayer.io",...n}){let t=c(n,y);return await(await fetch(`https://auth.${e}/oauth/revoke`,{method:"POST",headers:{"Content-Type":"application/json",Accept:"application/json"},body:JSON.stringify(t)})).json()}var i=class extends Error{constructor(n){super(n),this.name="TokenError"}};var p=class extends i{constructor(n){super(n),this.name="InvalidTokenError"}};function u(e){return typeof btoa<"u"?btoa(e).replaceAll("=","").replaceAll("+","-").replaceAll("/","_"):Buffer.from(e,"binary").toString("base64url")}function l(e){return typeof atob<"u"?atob(e.replaceAll("-","+").replaceAll("_","/")):Buffer.from(e,"base64url").toString("binary")}function f(e){let[n,t,r]=`${e}`.split(".");if(n==null||t==null||r==null)throw new p("Invalid token format");return{header:JSON.parse(l(n)),payload:JSON.parse(l(t)),signature:r}}function J(e){return e.application.kind==="user"}function S(e){return e.application.kind==="dashboard"}function b(e){return e.application.kind==="integration"}function W(e){return e.application.kind==="sales_channel"}function A(e){return e.application.kind==="webapp"}var d=class extends i{constructor(){super("Token expired"),this.name="TokenExpiredError"}};async function j(e,{ignoreExpiration:n=!1,domain:t,jwk:r}={}){let o=f(e),a=r??await R(o.header.kid,{domain:t});if(a==null||a.kid!==o.header.kid)throw new p('Invalid token "kid"');if(!n&&Date.now()>=o.payload.exp*1e3)throw new d;let s={name:"RSASSA-PKCS1-v1_5",hash:"SHA-512"},T=await crypto.subtle.importKey("jwk",a,s,!0,["verify"]),w=new Uint8Array(Array.from(l(o.signature),C=>C.charCodeAt(0))),k=new TextEncoder().encode(e.split(".").slice(0,2).join("."));if(!await crypto.subtle.verify(s,T,w,k))throw new p("Invalid signature");return o}var m={};async function R(e,n){if(m[e]!=null)return m[e];let t=await P(n);return m[e]=t.find(r=>r.kid===e),m[e]}async function P({domain:e="commercelayer.io"}){let n=`https://auth.${e}/.well-known/jwks.json`,t=await fetch(n).then(async r=>await r.json());if(t.keys==null)throw new i(`Invalid jwks response from "${n}": ${JSON.stringify(t)}`);return t.keys}async function O({payload:e}){return await I(e,"cl")}async function I(e,n){let r=u(JSON.stringify({alg:"HS512",typ:"JWT"})),o=u(JSON.stringify({...e,iat:Math.floor(new Date().getTime()/1e3)})),a=`${r}.${o}`,s=await K(a,n);return`${a}.${s}`}async function K(e,n){let t=new TextEncoder,r={name:"HMAC",hash:"SHA-512"},o=await crypto.subtle.importKey("raw",t.encode(n),r,!1,["sign","verify"]),a=await crypto.subtle.sign(r.name,o,t.encode(e));return u(String.fromCharCode(...new Uint8Array(a)))}export{p as InvalidTokenError,i as TokenError,d as TokenExpiredError,h as authenticate,O as createAssertion,f as jwtDecode,S as jwtIsDashboard,b as jwtIsIntegration,W as jwtIsSalesChannel,J as jwtIsUser,A as jwtIsWebApp,j as jwtVerify,x as revoke}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@commercelayer/js-auth", | ||
"version": "6.3.1", | ||
"version": "6.4.0", | ||
"description": "A JavaScript library designed to simplify authentication when interacting with the Commerce Layer API.", | ||
@@ -9,4 +9,4 @@ "repository": { | ||
"author": { | ||
"name": "Alessandro Casazza", | ||
"email": "alessandro@commercelayer.io" | ||
"name": "Commerce Layer", | ||
"email": "support@commercelayer.io" | ||
}, | ||
@@ -16,5 +16,5 @@ "type": "module", | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"import": "./dist/index.js", | ||
"require": "./dist/index.cjs", | ||
"types": "./dist/index.d.ts" | ||
"require": "./dist/index.cjs" | ||
} | ||
@@ -43,7 +43,8 @@ }, | ||
"@types/jsonwebtoken": "^9.0.6", | ||
"@types/node": "^20.14.9", | ||
"@types/node": "^20", | ||
"jsonwebtoken": "^9.0.2", | ||
"tsup": "^8.1.0", | ||
"typescript": "^5.5.2", | ||
"vitest": "^1.6.0" | ||
"tsup": "^8.2.4", | ||
"typescript": "^5.5.4", | ||
"vitest": "^2.0.5", | ||
"vitest-fetch-mock": "^0.3.0" | ||
}, | ||
@@ -50,0 +51,0 @@ "engines": { |
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
108563
509
7