@descope/node-sdk
Advanced tools
Comparing version 1.0.4-alpha.7 to 1.0.4-alpha.8
@@ -1,2 +0,2 @@ | ||
"use strict";var e=require("@descope/core-js-sdk"),t=require("jose"),s=require("node-fetch"),o=require("tslib");function r(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var a=r(e),n=r(s);const i=(e,t,s)=>`${e}=${t}; Domain=${(null==s?void 0:s.cookieDomain)||""}; Max-Age=${(null==s?void 0:s.cookieMaxAge)||""}; Path=${(null==s?void 0:s.cookiePath)||"/"}; HttpOnly; SameSite=Strict`,c=e=>async(...t)=>{var s,r,a;const n=await e(...t);if(!n.data)return n;let c=n.data,{sessionJwt:l,refreshJwt:d}=c,u=o.__rest(c,["sessionJwt","refreshJwt"]);const h=[i("DS",l,u)];return d?h.push(i("DSR",d,u)):(null===(s=n.response)||void 0===s?void 0:s.headers.get("set-cookie"))&&(d=((e,t)=>{const s=null==e?void 0:e.match(RegExp(`(?:^|;\\s*)${t}=([^;]*)`));return s?s[1]:null})(null===(r=n.response)||void 0===r?void 0:r.headers.get("set-cookie"),"DSR"),h.push(null===(a=n.response)||void 0===a?void 0:a.headers.get("set-cookie"))),Object.assign(Object.assign({},n),{data:Object.assign(Object.assign({},n.data),{refreshJwt:d,cookies:h})})},l=(e,t,s)=>{if(!e)return;const o="string"==typeof t?t.split("."):t,r=o.shift()||"";if(0===o.length||"*"===r){const t=t=>{if(!t||"function"!=typeof e[t])throw Error(`cannot wrap value at key "${t.toString()}"`);e[t]=s(e[t])};"*"===r?Object.keys(e).forEach(t):t(r)}else l(e[r],o,s)};function d(e,t,s){var o,r;const a=s?null===(r=null===(o=e.token.tenants)||void 0===o?void 0:o[s])||void 0===r?void 0:r[t]:e.token[t];return Array.isArray(a)?a:[]}globalThis.fetch||(globalThis.fetch=n.default,globalThis.Headers=s.Headers,globalThis.Request=s.Request,globalThis.Response=s.Response);const u=(...e)=>{const s=[...e];s[0].hooks=s[0].hooks||{};const o=s[0].hooks.beforeRequest;s[0].hooks.beforeRequest=e=>{var t;const s=e;return s.headers=Object.assign(Object.assign({},s.headers),{"x-descope-sdk-name":"nodejs","x-descope-sdk-node-version":(null===(t=null===process||void 0===process?void 0:process.versions)||void 0===t?void 0:t.node)||"","x-descope-sdk-version":"1.0.4-alpha.7"}),(null==o?void 0:o(s))||s};const r=a.default(...s);var n,i;n=r,i=c,["otp.verify.*","magicLink.verify","magicLink.crossDevice.signUp.*","magicLink.crossDevice.signIn.*","oauth.exchange","saml.exchange","totp.verify","webauthn.signIn.finish","webauthn.signUp.finish","refresh"].forEach((e=>l(n,e,i)));const{projectId:u,logger:h}=e[0],v={},f=Object.assign(Object.assign({},r),{async getKey(e){if(!(null==e?void 0:e.kid))throw Error("header.kid must not be empty");if(v[e.kid])return v[e.kid];if(Object.assign(v,await(async()=>{const e=await r.httpClient.get(`v1/keys/${u}`).then((e=>e.json()));return Array.isArray(e)?(await Promise.all(e.map((async e=>[e.kid,await t.importJWK(e)])))).reduce(((e,[t,s])=>t?Object.assign(Object.assign({},e),{[t.toString()]:s}):e),{}):{}})()),!v[e.kid])throw Error("failed to fetch matching key");return v[e.kid]},validateJwt:async e=>({jwt:e,token:(await t.jwtVerify(e,f.getKey,{issuer:u,clockTolerance:5})).payload}),async validateSession(e,t){var s,o;if(!e&&!t)throw Error("both refresh token and session token are empty");if(e)try{return await f.validateJwt(e)}catch(e){if(!t)throw null==h||h.error("failed to validate session token and no refresh token provided",e),Error("could not validate tokens")}if(t)try{await f.validateJwt(t);const e=await f.refresh(t);if(e.ok){return await f.validateJwt(null===(s=e.data)||void 0===s?void 0:s.sessionJwt)}throw Error(null===(o=e.error)||void 0===o?void 0:o.message)}catch(e){throw null==h||h.error("failed to validate refresh token",e),Error("could not validate tokens")}throw Error("could not validate token")},async exchangeAccessKey(e){if(!e)throw Error("access key must not be empty");let t;try{t=await f.accessKey.exchange(e)}catch(e){throw null==h||h.error("failed to exchange access key",e),Error("could not exchange access key")}const{sessionJwt:s}=t.data;if(!s)throw null==h||h.error("failed to parse exchange access key response"),Error("could not exchange access key");try{return await f.validateJwt(s)}catch(e){throw null==h||h.error("failed to parse jwt from access key",e),Error("could not exchange access key")}},validatePermissions:(e,t)=>f.validateTenantPermissions(e,null,t),validateTenantPermissions(e,t,s){const o=d(e,"permissions",t);return s.every((e=>o.includes(e)))},validateRoles:(e,t)=>f.validateTenantRoles(e,null,t),validateTenantRoles(e,t,s){const o=d(e,"roles",t);return s.every((e=>o.includes(e)))}});return f};u.DeliveryMethods=a.default.DeliveryMethods,u.RefreshTokenCookieName="DSR",u.SessionTokenCookieName="DS",module.exports=u; | ||
"use strict";var e=require("@descope/core-js-sdk"),t=require("jose"),s=require("node-fetch"),o=require("tslib");function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var a=n(e),r=n(s);const i=(e,t,s)=>`${e}=${t}; Domain=${(null==s?void 0:s.cookieDomain)||""}; Max-Age=${(null==s?void 0:s.cookieMaxAge)||""}; Path=${(null==s?void 0:s.cookiePath)||"/"}; HttpOnly; SameSite=Strict`,l=e=>async(...t)=>{var s,n,a;const r=await e(...t);if(!r.data)return r;let l=r.data,{sessionJwt:d,refreshJwt:c}=l,u=o.__rest(l,["sessionJwt","refreshJwt"]);const h=[i("DS",d,u)];return c?h.push(i("DSR",c,u)):(null===(s=r.response)||void 0===s?void 0:s.headers.get("set-cookie"))&&(c=((e,t)=>{const s=null==e?void 0:e.match(RegExp(`(?:^|;\\s*)${t}=([^;]*)`));return s?s[1]:null})(null===(n=r.response)||void 0===n?void 0:n.headers.get("set-cookie"),"DSR"),h.push(null===(a=r.response)||void 0===a?void 0:a.headers.get("set-cookie"))),Object.assign(Object.assign({},r),{data:Object.assign(Object.assign({},r.data),{refreshJwt:c,cookies:h})})},d=(e,t,s)=>{if(!e)return;const o="string"==typeof t?t.split("."):t,n=o.shift()||"";if(0===o.length||"*"===n){const t=t=>{if(!t||"function"!=typeof e[t])throw Error(`cannot wrap value at key "${t.toString()}"`);e[t]=s(e[t])};"*"===n?Object.keys(e).forEach(t):t(n)}else d(e[n],o,s)};function c(e,t,s){var o,n;const a=s?null===(n=null===(o=e.token.tenants)||void 0===o?void 0:o[s])||void 0===n?void 0:n[t]:e.token[t];return Array.isArray(a)?a:[]}var u={create:"/v1/mgmt/user/create",update:"/v1/mgmt/user/update",delete:"/v1/mgmt/user/delete",load:"/v1/mgmt/user/load",search:"/v1/mgmt/user/search"},h={create:"/v1/mgmt/tenant/create",update:"/v1/mgmt/tenant/update",delete:"/v1/mgmt/tenant/delete"},p={update:"/v1/mgmt/jwt/update"};const v=(t,s)=>({create:(o,n,a,r,i,l)=>e.transformResponse(t.httpClient.post(u.create,{identifier:o,email:n,phone:a,displayName:r,roleNames:i,userTenants:l},{token:s}),(e=>e.user)),update:(o,n,a,r,i,l)=>e.transformResponse(t.httpClient.post(u.update,{identifier:o,email:n,phone:a,displayName:r,roleNames:i,userTenants:l},{token:s}),(e=>e.user)),delete:o=>e.transformResponse(t.httpClient.post(u.delete,{identifier:o},{token:s})),load:o=>e.transformResponse(t.httpClient.get(u.load,{queryParams:{identifier:o},token:s}),(e=>e.user)),searchAll:(o,n,a)=>e.transformResponse(t.httpClient.post(u.search,{tenantIds:o,roleNames:n,limit:a},{token:s}),(e=>e.users))}),m=(t,s)=>({create:(o,n)=>e.transformResponse(t.httpClient.post(h.create,{name:o,selfProvisioningDomains:n},{token:s})),createWithId:(o,n,a)=>e.transformResponse(t.httpClient.post(h.create,{tenantId:o,name:n,selfProvisioningDomains:a},{token:s})),update:(o,n,a)=>e.transformResponse(t.httpClient.post(h.update,{tenantId:o,name:n,selfProvisioningDomains:a},{token:s})),delete:o=>e.transformResponse(t.httpClient.post(h.delete,{tenantId:o},{token:s}))}),f=(t,s)=>({update:(o,n)=>e.transformResponse(t.httpClient.post(p.update,{jwt:o,customClaims:n},{token:s}))});globalThis.fetch||(globalThis.fetch=r.default,globalThis.Headers=s.Headers,globalThis.Request=s.Request,globalThis.Response=s.Response);const k=e=>{e.hooks=e.hooks||{};const s=e.hooks.beforeRequest;e.hooks.beforeRequest=e=>{var t;const o=e;return o.headers=Object.assign(Object.assign({},o.headers),{"x-descope-sdk-name":"nodejs","x-descope-sdk-node-version":(null===(t=null===process||void 0===process?void 0:process.versions)||void 0===t?void 0:t.node)||"","x-descope-sdk-version":"1.0.4-alpha.8"}),(null==s?void 0:s(o))||o};const o=a.default(e);var n,r;n=o,r=l,["otp.verify.*","magicLink.verify","enchantedLink.signUp.*","enchantedLink.signIn.*","oauth.exchange","saml.exchange","totp.verify","webauthn.signIn.finish","webauthn.signUp.finish","refresh"].forEach((e=>d(n,e,r)));const{projectId:i,logger:u}=e,h={},p=((e,t)=>({user:v(e,t),tenant:m(e,t),jwt:f(e,t)}))(o,e.managementKey),k=Object.assign(Object.assign({},o),{management:p,async getKey(e){if(!(null==e?void 0:e.kid))throw Error("header.kid must not be empty");if(h[e.kid])return h[e.kid];if(Object.assign(h,await(async()=>{const e=await o.httpClient.get(`v1/keys/${i}`).then((e=>e.json()));return Array.isArray(e)?(await Promise.all(e.map((async e=>[e.kid,await t.importJWK(e)])))).reduce(((e,[t,s])=>t?Object.assign(Object.assign({},e),{[t.toString()]:s}):e),{}):{}})()),!h[e.kid])throw Error("failed to fetch matching key");return h[e.kid]},async validateJwt(e){var s;const o=(await t.jwtVerify(e,k.getKey,{clockTolerance:5})).payload;if(o&&(o.iss=null===(s=o.iss)||void 0===s?void 0:s.split("/").pop(),o.iss!==i))throw new t.errors.JWTClaimValidationFailed('unexpected "iss" claim value',"iss","check_failed");return{jwt:e,token:o}},async validateSession(e,t){var s,o;if(!e&&!t)throw Error("both refresh token and session token are empty");if(e)try{return await k.validateJwt(e)}catch(e){if(!t)throw null==u||u.error("failed to validate session token and no refresh token provided",e),Error("could not validate tokens")}if(t)try{await k.validateJwt(t);const e=await k.refresh(t);if(e.ok){return await k.validateJwt(null===(s=e.data)||void 0===s?void 0:s.sessionJwt)}throw Error(null===(o=e.error)||void 0===o?void 0:o.message)}catch(e){throw null==u||u.error("failed to validate refresh token",e),Error("could not validate tokens")}throw Error("could not validate token")},async exchangeAccessKey(e){if(!e)throw Error("access key must not be empty");let t;try{t=await k.accessKey.exchange(e)}catch(e){throw null==u||u.error("failed to exchange access key",e),Error("could not exchange access key")}const{sessionJwt:s}=t.data;if(!s)throw null==u||u.error("failed to parse exchange access key response"),Error("could not exchange access key");try{return await k.validateJwt(s)}catch(e){throw null==u||u.error("failed to parse jwt from access key",e),Error("could not exchange access key")}},validatePermissions:(e,t)=>k.validateTenantPermissions(e,null,t),validateTenantPermissions(e,t,s){const o=c(e,"permissions",t);return s.every((e=>o.includes(e)))},validateRoles:(e,t)=>k.validateTenantRoles(e,null,t),validateTenantRoles(e,t,s){const o=c(e,"roles",t);return s.every((e=>o.includes(e)))}});return k};k.DeliveryMethods=a.default.DeliveryMethods,k.RefreshTokenCookieName="DSR",k.SessionTokenCookieName="DS",module.exports=k; | ||
//# sourceMappingURL=index.cjs.js.map |
import * as _descope_core_js_sdk from '@descope/core-js-sdk'; | ||
import _descope_core_js_sdk__default, { RequestConfig, SdkResponse, ExchangeAccessKeyResponse } from '@descope/core-js-sdk'; | ||
import _descope_core_js_sdk__default, { SdkResponse, ExchangeAccessKeyResponse } from '@descope/core-js-sdk'; | ||
export { DeliveryMethod, OAuthProvider } from '@descope/core-js-sdk'; | ||
import { JWTHeaderParameters, KeyLike } from 'jose'; | ||
/** Represents a tenant association for a User. The tenantId is required to denote | ||
* which tenant the user belongs to. The roleNames array is an optional list of | ||
* roles for the user in this specific tenant. | ||
*/ | ||
declare type UserTenant = { | ||
tenantId: string; | ||
roleNames: string[]; | ||
}; | ||
/** The tenantId of a newly created tenant */ | ||
declare type CreateTenantResponse = { | ||
tenantId: string; | ||
}; | ||
/** UpdateJWT response with a new JWT value with the added custom claims */ | ||
declare type UpdateJWTResponse = { | ||
jwt: string; | ||
}; | ||
/** Parsed JWT token */ | ||
@@ -25,2 +42,6 @@ interface Token { | ||
/** Configuration arguments which include the Descope core SDK args and an optional management key */ | ||
declare type NodeSdkArgs = Parameters<typeof _descope_core_js_sdk__default>[0] & { | ||
managementKey?: string; | ||
}; | ||
/** Descope SDK client with delivery methods enum. | ||
@@ -42,25 +63,26 @@ * | ||
*/ | ||
declare const sdkWithAttributes: ((args_0: { | ||
projectId: string; | ||
logger?: { | ||
error: { | ||
(...data: any[]): void; | ||
(message?: any, ...optionalParams: any[]): void; | ||
declare const sdkWithAttributes: ((args: NodeSdkArgs) => { | ||
/** | ||
* Provides various APIs for managing a Descope project programmatically. A management key must | ||
* be provided as an argument when initializing the SDK to use these APIs. Management keys can be | ||
* generated in the Descope console. | ||
*/ | ||
management: { | ||
user: { | ||
create: (identifier: string, email?: string, phone?: string, displayName?: string, roleNames?: string[], userTenants?: UserTenant[]) => Promise<SdkResponse<_descope_core_js_sdk.UserResponse>>; | ||
update: (identifier: string, email?: string, phone?: string, displayName?: string, roleNames?: string[], userTenants?: UserTenant[]) => Promise<SdkResponse<_descope_core_js_sdk.UserResponse>>; | ||
delete: (identifier: string) => Promise<SdkResponse<never>>; | ||
load: (identifier: string) => Promise<SdkResponse<_descope_core_js_sdk.UserResponse>>; | ||
searchAll: (tenantIds?: string[], roleNames?: string[], limit?: number) => Promise<SdkResponse<_descope_core_js_sdk.UserResponse[]>>; | ||
}; | ||
debug: { | ||
(...data: any[]): void; | ||
(message?: any, ...optionalParams: any[]): void; | ||
tenant: { | ||
create: (name: string, selfProvisioningDomains?: string[]) => Promise<SdkResponse<CreateTenantResponse>>; | ||
createWithId: (tenantId: string, name: string, selfProvisioningDomains?: string[]) => Promise<SdkResponse<never>>; | ||
update: (tenantId: string, name: string, selfProvisioningDomains?: string[]) => Promise<SdkResponse<never>>; | ||
delete: (tenantId: string) => Promise<SdkResponse<never>>; | ||
}; | ||
log: { | ||
(...data: any[]): void; | ||
(message?: any, ...optionalParams: any[]): void; | ||
jwt: { | ||
update: (jwt: string, customClaims?: Record<string, any>) => Promise<SdkResponse<UpdateJWTResponse>>; | ||
}; | ||
}; | ||
baseUrl?: string; | ||
hooks?: { | ||
beforeRequest?: (config: RequestConfig) => RequestConfig; | ||
afterRequest?: (req: RequestConfig, res: globalThis.Response) => void; | ||
}; | ||
cookiePolicy?: RequestCredentials; | ||
}) => { | ||
/** Get the key that can validate the given JWT KID in the header. Can retrieve the public key from local cache or from Descope. */ | ||
@@ -198,48 +220,18 @@ getKey(header: JWTHeaderParameters): Promise<KeyLike | Uint8Array>; | ||
}; | ||
crossDevice: { | ||
verify: (token: string) => Promise<SdkResponse<_descope_core_js_sdk.JWTResponse>>; | ||
signIn: { | ||
email: (identifier: string, uri: string) => Promise<SdkResponse<_descope_core_js_sdk.PendingRefResponse>>; | ||
sms: (identifier: string, uri: string) => Promise<SdkResponse<_descope_core_js_sdk.PendingRefResponse>>; | ||
whatsapp: (identifier: string, uri: string) => Promise<SdkResponse<_descope_core_js_sdk.PendingRefResponse>>; | ||
}; | ||
signUpOrIn: { | ||
email: (identifier: string, uri: string) => Promise<SdkResponse<_descope_core_js_sdk.PendingRefResponse>>; | ||
sms: (identifier: string, uri: string) => Promise<SdkResponse<_descope_core_js_sdk.PendingRefResponse>>; | ||
whatsapp: (identifier: string, uri: string) => Promise<SdkResponse<_descope_core_js_sdk.PendingRefResponse>>; | ||
}; | ||
signUp: { | ||
email: (identifier: string, uri: string, user?: { | ||
email?: string; | ||
name?: string; | ||
phone?: string; | ||
}) => Promise<SdkResponse<_descope_core_js_sdk.PendingRefResponse>>; | ||
sms: (identifier: string, uri: string, user?: { | ||
email?: string; | ||
name?: string; | ||
phone?: string; | ||
}) => Promise<SdkResponse<_descope_core_js_sdk.PendingRefResponse>>; | ||
whatsapp: (identifier: string, uri: string, user?: { | ||
email?: string; | ||
name?: string; | ||
phone?: string; | ||
}) => Promise<SdkResponse<_descope_core_js_sdk.PendingRefResponse>>; | ||
}; | ||
waitForSession: (pendingRef: string, config?: { | ||
pollingIntervalMs: number; | ||
timeoutMs: number; | ||
}) => Promise<SdkResponse<_descope_core_js_sdk.JWTResponse>>; /** | ||
* Make sure that all given roles exist on the parsed JWT tenant claims | ||
* @param authInfo JWT parsed info | ||
* @param roles list of roles to make sure they exist on te JWT claims | ||
* @returns true if all roles exist, false otherwise | ||
*/ | ||
update: { | ||
email: (identifier: string, email: string, uri: string, token?: string) => Promise<SdkResponse<never>>; | ||
phone: { | ||
email: (identifier: string, phone: string) => Promise<SdkResponse<never>>; | ||
sms: (identifier: string, phone: string) => Promise<SdkResponse<never>>; | ||
whatsapp: (identifier: string, phone: string) => Promise<SdkResponse<never>>; | ||
}; | ||
}; | ||
}; | ||
enchantedLink: { | ||
verify: (token: string) => Promise<SdkResponse<never>>; | ||
signIn: (identifier: string, uri: string) => Promise<SdkResponse<_descope_core_js_sdk.PendingRefResponse>>; | ||
signUpOrIn: (identifier: string, uri: string) => Promise<SdkResponse<_descope_core_js_sdk.PendingRefResponse>>; | ||
signUp: (identifier: string, uri: string, user?: { | ||
email?: string; | ||
name?: string; | ||
phone?: string; | ||
}) => Promise<SdkResponse<_descope_core_js_sdk.PendingRefResponse>>; | ||
waitForSession: (pendingRef: string, config?: { | ||
pollingIntervalMs: number; | ||
timeoutMs: number; | ||
}) => Promise<SdkResponse<_descope_core_js_sdk.JWTResponse>>; | ||
update: { | ||
email: (identifier: string, email: string, uri: string, token?: string) => Promise<SdkResponse<_descope_core_js_sdk.PendingRefResponse>>; | ||
}; | ||
@@ -280,6 +272,3 @@ }; | ||
}; | ||
exchange: (code: string, loginOptions?: { | ||
stepup: boolean; | ||
customClaims: Map<string, any>; | ||
}, token?: string) => Promise<SdkResponse<_descope_core_js_sdk.JWTResponse>>; | ||
exchange: (code: string) => Promise<SdkResponse<_descope_core_js_sdk.JWTResponse>>; | ||
}; | ||
@@ -292,6 +281,3 @@ saml: { | ||
} ? undefined : SdkResponse<_descope_core_js_sdk.URLResponse>>; | ||
exchange: (code: string, loginOptions?: { | ||
stepup: boolean; | ||
customClaims: Map<string, any>; | ||
}, token?: string) => Promise<SdkResponse<_descope_core_js_sdk.JWTResponse>>; | ||
exchange: (code: string) => Promise<SdkResponse<_descope_core_js_sdk.JWTResponse>>; | ||
}; | ||
@@ -305,4 +291,5 @@ totp: { | ||
verify: (identifier: string, code: string, loginOptions?: { | ||
stepup: boolean; | ||
customClaims: Map<string, any>; | ||
stepup?: boolean; | ||
mfa?: boolean; | ||
customClaims?: Record<string, any>; | ||
}, token?: string) => Promise<SdkResponse<_descope_core_js_sdk.JWTResponse>>; | ||
@@ -313,17 +300,34 @@ update: (identifier: string, token?: string) => Promise<SdkResponse<_descope_core_js_sdk.TOTPResponse>>; | ||
signUp: { | ||
start: (identifier: string, origin: string, name: string) => Promise<SdkResponse<_descope_core_js_sdk.ResponseData>>; | ||
finish: (transactionId: string, response: string, loginOptions?: { | ||
stepup: boolean; | ||
customClaims: Map<string, any>; | ||
}, token?: string) => Promise<SdkResponse<_descope_core_js_sdk.ResponseData>>; | ||
start: (identifier: string, origin: string, name: string) => Promise<SdkResponse<{ | ||
transactionId: string; | ||
options: string; | ||
create: boolean; | ||
}>>; | ||
finish: (transactionId: string, response: string) => Promise<SdkResponse<_descope_core_js_sdk.JWTResponse>>; | ||
}; | ||
signIn: { | ||
start: (identifier: string, origin: string) => Promise<SdkResponse<_descope_core_js_sdk.ResponseData>>; | ||
finish: (transactionId: string, response: string, loginOptions?: { | ||
stepup: boolean; | ||
customClaims: Map<string, any>; | ||
}, token?: string) => Promise<SdkResponse<_descope_core_js_sdk.ResponseData>>; | ||
start: (identifier: string, origin: string, loginOptions?: { | ||
stepup?: boolean; | ||
mfa?: boolean; | ||
customClaims?: Record<string, any>; | ||
}, token?: string) => Promise<SdkResponse<{ | ||
transactionId: string; | ||
options: string; | ||
create: boolean; | ||
}>>; | ||
finish: (transactionId: string, response: string) => Promise<SdkResponse<_descope_core_js_sdk.JWTResponse>>; | ||
}; | ||
signUpOrIn: { | ||
start: (identifier: string, origin: string) => Promise<SdkResponse<{ | ||
transactionId: string; | ||
options: string; | ||
create: boolean; | ||
}>>; | ||
}; | ||
update: { | ||
start: (identifier: string, origin: string, token: string) => Promise<SdkResponse<_descope_core_js_sdk.ResponseData>>; | ||
start: (identifier: string, origin: string, token: string) => Promise<SdkResponse<{ | ||
transactionId: string; | ||
options: string; | ||
create: boolean; | ||
}>>; | ||
finish: (transactionId: string, response: string) => Promise<SdkResponse<_descope_core_js_sdk.ResponseData>>; | ||
@@ -333,3 +337,14 @@ }; | ||
flow: { | ||
start: (flowId: string) => Promise<SdkResponse<_descope_core_js_sdk.FlowResponse>>; | ||
start: (flowId: string, options?: { | ||
redirectUrl?: string; | ||
tenant?: string; | ||
deviceInfo?: { | ||
webAuthnSupport?: boolean; | ||
}; | ||
lastUser?: { | ||
authMethod?: "otp" | "totp" | "webauthn" | "magiclink" | "social" | "sso"; | ||
oauthProvider?: string; | ||
externalId?: string; | ||
}; | ||
}) => Promise<SdkResponse<_descope_core_js_sdk.FlowResponse>>; | ||
next: (executionId: string, stepId: string, interactionId: string, input?: Record<string, FormDataEntryValue>) => Promise<SdkResponse<_descope_core_js_sdk.FlowResponse>>; | ||
@@ -339,3 +354,3 @@ }; | ||
logout: (token?: string) => Promise<SdkResponse<never>>; | ||
deleteCookies: (token?: string) => Promise<SdkResponse<never>>; | ||
logoutAll: (token?: string) => Promise<SdkResponse<never>>; | ||
me: (token?: string) => Promise<SdkResponse<_descope_core_js_sdk.UserResponse>>; | ||
@@ -381,2 +396,2 @@ isJwtExpired: (token: string) => boolean; | ||
export { sdkWithAttributes as default }; | ||
export { NodeSdkArgs, sdkWithAttributes as default }; |
@@ -1,2 +0,2 @@ | ||
import e from"@descope/core-js-sdk";import{jwtVerify as o,importJWK as t}from"jose";import s,{Headers as r,Request as a,Response as n}from"node-fetch";import{__rest as i}from"tslib";const c=(e,o,t)=>`${e}=${o}; Domain=${(null==t?void 0:t.cookieDomain)||""}; Max-Age=${(null==t?void 0:t.cookieMaxAge)||""}; Path=${(null==t?void 0:t.cookiePath)||"/"}; HttpOnly; SameSite=Strict`,l=e=>async(...o)=>{var t,s,r;const a=await e(...o);if(!a.data)return a;let n=a.data,{sessionJwt:l,refreshJwt:d}=n,h=i(n,["sessionJwt","refreshJwt"]);const u=[c("DS",l,h)];return d?u.push(c("DSR",d,h)):(null===(t=a.response)||void 0===t?void 0:t.headers.get("set-cookie"))&&(d=((e,o)=>{const t=null==e?void 0:e.match(RegExp(`(?:^|;\\s*)${o}=([^;]*)`));return t?t[1]:null})(null===(s=a.response)||void 0===s?void 0:s.headers.get("set-cookie"),"DSR"),u.push(null===(r=a.response)||void 0===r?void 0:r.headers.get("set-cookie"))),Object.assign(Object.assign({},a),{data:Object.assign(Object.assign({},a.data),{refreshJwt:d,cookies:u})})},d=(e,o,t)=>{if(!e)return;const s="string"==typeof o?o.split("."):o,r=s.shift()||"";if(0===s.length||"*"===r){const o=o=>{if(!o||"function"!=typeof e[o])throw Error(`cannot wrap value at key "${o.toString()}"`);e[o]=t(e[o])};"*"===r?Object.keys(e).forEach(o):o(r)}else d(e[r],s,t)};function h(e,o,t){var s,r;const a=t?null===(r=null===(s=e.token.tenants)||void 0===s?void 0:s[t])||void 0===r?void 0:r[o]:e.token[o];return Array.isArray(a)?a:[]}globalThis.fetch||(globalThis.fetch=s,globalThis.Headers=r,globalThis.Request=a,globalThis.Response=n);const u=(...s)=>{const r=[...s];r[0].hooks=r[0].hooks||{};const a=r[0].hooks.beforeRequest;r[0].hooks.beforeRequest=e=>{var o;const t=e;return t.headers=Object.assign(Object.assign({},t.headers),{"x-descope-sdk-name":"nodejs","x-descope-sdk-node-version":(null===(o=null===process||void 0===process?void 0:process.versions)||void 0===o?void 0:o.node)||"","x-descope-sdk-version":"1.0.4-alpha.7"}),(null==a?void 0:a(t))||t};const n=e(...r);var i,c;i=n,c=l,["otp.verify.*","magicLink.verify","magicLink.crossDevice.signUp.*","magicLink.crossDevice.signIn.*","oauth.exchange","saml.exchange","totp.verify","webauthn.signIn.finish","webauthn.signUp.finish","refresh"].forEach((e=>d(i,e,c)));const{projectId:u,logger:v}=s[0],f={},k=Object.assign(Object.assign({},n),{async getKey(e){if(!(null==e?void 0:e.kid))throw Error("header.kid must not be empty");if(f[e.kid])return f[e.kid];if(Object.assign(f,await(async()=>{const e=await n.httpClient.get(`v1/keys/${u}`).then((e=>e.json()));return Array.isArray(e)?(await Promise.all(e.map((async e=>[e.kid,await t(e)])))).reduce(((e,[o,t])=>o?Object.assign(Object.assign({},e),{[o.toString()]:t}):e),{}):{}})()),!f[e.kid])throw Error("failed to fetch matching key");return f[e.kid]},validateJwt:async e=>({jwt:e,token:(await o(e,k.getKey,{issuer:u,clockTolerance:5})).payload}),async validateSession(e,o){var t,s;if(!e&&!o)throw Error("both refresh token and session token are empty");if(e)try{return await k.validateJwt(e)}catch(e){if(!o)throw null==v||v.error("failed to validate session token and no refresh token provided",e),Error("could not validate tokens")}if(o)try{await k.validateJwt(o);const e=await k.refresh(o);if(e.ok){return await k.validateJwt(null===(t=e.data)||void 0===t?void 0:t.sessionJwt)}throw Error(null===(s=e.error)||void 0===s?void 0:s.message)}catch(e){throw null==v||v.error("failed to validate refresh token",e),Error("could not validate tokens")}throw Error("could not validate token")},async exchangeAccessKey(e){if(!e)throw Error("access key must not be empty");let o;try{o=await k.accessKey.exchange(e)}catch(e){throw null==v||v.error("failed to exchange access key",e),Error("could not exchange access key")}const{sessionJwt:t}=o.data;if(!t)throw null==v||v.error("failed to parse exchange access key response"),Error("could not exchange access key");try{return await k.validateJwt(t)}catch(e){throw null==v||v.error("failed to parse jwt from access key",e),Error("could not exchange access key")}},validatePermissions:(e,o)=>k.validateTenantPermissions(e,null,o),validateTenantPermissions(e,o,t){const s=h(e,"permissions",o);return t.every((e=>s.includes(e)))},validateRoles:(e,o)=>k.validateTenantRoles(e,null,o),validateTenantRoles(e,o,t){const s=h(e,"roles",o);return t.every((e=>s.includes(e)))}});return k};u.DeliveryMethods=e.DeliveryMethods,u.RefreshTokenCookieName="DSR",u.SessionTokenCookieName="DS";export{u as default}; | ||
import e,{transformResponse as t}from"@descope/core-js-sdk";import{jwtVerify as s,errors as o,importJWK as a}from"jose";import n,{Headers as r,Request as i,Response as l}from"node-fetch";import{__rest as d}from"tslib";const c=(e,t,s)=>`${e}=${t}; Domain=${(null==s?void 0:s.cookieDomain)||""}; Max-Age=${(null==s?void 0:s.cookieMaxAge)||""}; Path=${(null==s?void 0:s.cookiePath)||"/"}; HttpOnly; SameSite=Strict`,h=e=>async(...t)=>{var s,o,a;const n=await e(...t);if(!n.data)return n;let r=n.data,{sessionJwt:i,refreshJwt:l}=r,h=d(r,["sessionJwt","refreshJwt"]);const u=[c("DS",i,h)];return l?u.push(c("DSR",l,h)):(null===(s=n.response)||void 0===s?void 0:s.headers.get("set-cookie"))&&(l=((e,t)=>{const s=null==e?void 0:e.match(RegExp(`(?:^|;\\s*)${t}=([^;]*)`));return s?s[1]:null})(null===(o=n.response)||void 0===o?void 0:o.headers.get("set-cookie"),"DSR"),u.push(null===(a=n.response)||void 0===a?void 0:a.headers.get("set-cookie"))),Object.assign(Object.assign({},n),{data:Object.assign(Object.assign({},n.data),{refreshJwt:l,cookies:u})})},u=(e,t,s)=>{if(!e)return;const o="string"==typeof t?t.split("."):t,a=o.shift()||"";if(0===o.length||"*"===a){const t=t=>{if(!t||"function"!=typeof e[t])throw Error(`cannot wrap value at key "${t.toString()}"`);e[t]=s(e[t])};"*"===a?Object.keys(e).forEach(t):t(a)}else u(e[a],o,s)};function p(e,t,s){var o,a;const n=s?null===(a=null===(o=e.token.tenants)||void 0===o?void 0:o[s])||void 0===a?void 0:a[t]:e.token[t];return Array.isArray(n)?n:[]}var v={create:"/v1/mgmt/user/create",update:"/v1/mgmt/user/update",delete:"/v1/mgmt/user/delete",load:"/v1/mgmt/user/load",search:"/v1/mgmt/user/search"},m={create:"/v1/mgmt/tenant/create",update:"/v1/mgmt/tenant/update",delete:"/v1/mgmt/tenant/delete"},k={update:"/v1/mgmt/jwt/update"};const f=(e,s)=>({create:(o,a,n,r,i,l)=>t(e.httpClient.post(v.create,{identifier:o,email:a,phone:n,displayName:r,roleNames:i,userTenants:l},{token:s}),(e=>e.user)),update:(o,a,n,r,i,l)=>t(e.httpClient.post(v.update,{identifier:o,email:a,phone:n,displayName:r,roleNames:i,userTenants:l},{token:s}),(e=>e.user)),delete:o=>t(e.httpClient.post(v.delete,{identifier:o},{token:s})),load:o=>t(e.httpClient.get(v.load,{queryParams:{identifier:o},token:s}),(e=>e.user)),searchAll:(o,a,n)=>t(e.httpClient.post(v.search,{tenantIds:o,roleNames:a,limit:n},{token:s}),(e=>e.users))}),g=(e,s)=>({create:(o,a)=>t(e.httpClient.post(m.create,{name:o,selfProvisioningDomains:a},{token:s})),createWithId:(o,a,n)=>t(e.httpClient.post(m.create,{tenantId:o,name:a,selfProvisioningDomains:n},{token:s})),update:(o,a,n)=>t(e.httpClient.post(m.update,{tenantId:o,name:a,selfProvisioningDomains:n},{token:s})),delete:o=>t(e.httpClient.post(m.delete,{tenantId:o},{token:s}))}),y=(e,s)=>({update:(o,a)=>t(e.httpClient.post(k.update,{jwt:o,customClaims:a},{token:s}))});globalThis.fetch||(globalThis.fetch=n,globalThis.Headers=r,globalThis.Request=i,globalThis.Response=l);const w=t=>{t.hooks=t.hooks||{};const n=t.hooks.beforeRequest;t.hooks.beforeRequest=e=>{var t;const s=e;return s.headers=Object.assign(Object.assign({},s.headers),{"x-descope-sdk-name":"nodejs","x-descope-sdk-node-version":(null===(t=null===process||void 0===process?void 0:process.versions)||void 0===t?void 0:t.node)||"","x-descope-sdk-version":"1.0.4-alpha.8"}),(null==n?void 0:n(s))||s};const r=e(t);var i,l;i=r,l=h,["otp.verify.*","magicLink.verify","enchantedLink.signUp.*","enchantedLink.signIn.*","oauth.exchange","saml.exchange","totp.verify","webauthn.signIn.finish","webauthn.signUp.finish","refresh"].forEach((e=>u(i,e,l)));const{projectId:d,logger:c}=t,v={},m=((e,t)=>({user:f(e,t),tenant:g(e,t),jwt:y(e,t)}))(r,t.managementKey),k=Object.assign(Object.assign({},r),{management:m,async getKey(e){if(!(null==e?void 0:e.kid))throw Error("header.kid must not be empty");if(v[e.kid])return v[e.kid];if(Object.assign(v,await(async()=>{const e=await r.httpClient.get(`v1/keys/${d}`).then((e=>e.json()));return Array.isArray(e)?(await Promise.all(e.map((async e=>[e.kid,await a(e)])))).reduce(((e,[t,s])=>t?Object.assign(Object.assign({},e),{[t.toString()]:s}):e),{}):{}})()),!v[e.kid])throw Error("failed to fetch matching key");return v[e.kid]},async validateJwt(e){var t;const a=(await s(e,k.getKey,{clockTolerance:5})).payload;if(a&&(a.iss=null===(t=a.iss)||void 0===t?void 0:t.split("/").pop(),a.iss!==d))throw new o.JWTClaimValidationFailed('unexpected "iss" claim value',"iss","check_failed");return{jwt:e,token:a}},async validateSession(e,t){var s,o;if(!e&&!t)throw Error("both refresh token and session token are empty");if(e)try{return await k.validateJwt(e)}catch(e){if(!t)throw null==c||c.error("failed to validate session token and no refresh token provided",e),Error("could not validate tokens")}if(t)try{await k.validateJwt(t);const e=await k.refresh(t);if(e.ok){return await k.validateJwt(null===(s=e.data)||void 0===s?void 0:s.sessionJwt)}throw Error(null===(o=e.error)||void 0===o?void 0:o.message)}catch(e){throw null==c||c.error("failed to validate refresh token",e),Error("could not validate tokens")}throw Error("could not validate token")},async exchangeAccessKey(e){if(!e)throw Error("access key must not be empty");let t;try{t=await k.accessKey.exchange(e)}catch(e){throw null==c||c.error("failed to exchange access key",e),Error("could not exchange access key")}const{sessionJwt:s}=t.data;if(!s)throw null==c||c.error("failed to parse exchange access key response"),Error("could not exchange access key");try{return await k.validateJwt(s)}catch(e){throw null==c||c.error("failed to parse jwt from access key",e),Error("could not exchange access key")}},validatePermissions:(e,t)=>k.validateTenantPermissions(e,null,t),validateTenantPermissions(e,t,s){const o=p(e,"permissions",t);return s.every((e=>o.includes(e)))},validateRoles:(e,t)=>k.validateTenantRoles(e,null,t),validateTenantRoles(e,t,s){const o=p(e,"roles",t);return s.every((e=>o.includes(e)))}});return k};w.DeliveryMethods=e.DeliveryMethods,w.RefreshTokenCookieName="DSR",w.SessionTokenCookieName="DS";export{w as default}; | ||
//# sourceMappingURL=index.esm.js.map |
{ | ||
"name": "@descope/node-sdk", | ||
"version": "1.0.4-alpha.7", | ||
"version": "1.0.4-alpha.8", | ||
"description": "Node.js library used to integrate with Descope", | ||
@@ -98,4 +98,4 @@ "typings": "./dist/index.d.ts", | ||
"dependencies": { | ||
"@descope/core-js-sdk": "0.0.41-alpha.24", | ||
"jose": "4.10.0", | ||
"@descope/core-js-sdk": "0.0.41-alpha.31", | ||
"jose": "4.11.1", | ||
"node-fetch": "2.6.7" | ||
@@ -102,0 +102,0 @@ }, |
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 too big to display
Sorry, the diff of this file is not supported yet
841590
1815
+ Added@descope/core-js-sdk@0.0.41-alpha.31(transitive)
+ Addedjose@4.11.1(transitive)
- Removed@descope/core-js-sdk@0.0.41-alpha.24(transitive)
- Removedjose@4.10.0(transitive)
Updatedjose@4.11.1