@privy-io/js-sdk-core 0.52.2 → 0.52.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/action/crossApp/linkWithCrossAppAuth.js +1 -1
- package/dist/cjs/action/crossApp/loginWithCrossAppAuth.js +1 -1
- package/dist/cjs/action/crossApp/wallet/utils/sendCrossAppRequest.js +1 -1
- package/dist/cjs/client/PrivyInternal.js +1 -1
- package/dist/dts/index.d.mts +2 -2
- package/dist/dts/index.d.ts +2 -2
- package/dist/esm/action/crossApp/linkWithCrossAppAuth.mjs +1 -1
- package/dist/esm/action/crossApp/loginWithCrossAppAuth.mjs +1 -1
- package/dist/esm/action/crossApp/wallet/utils/sendCrossAppRequest.mjs +1 -1
- package/dist/esm/client/PrivyInternal.mjs +1 -1
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var t=require("../../Error.js");exports.linkWithCrossAppAuth=({client:r,openAuthSession:e})=>async({providerAppId:i,redirectUrl:a})=>{let n=`privy:${i}`,{url:o}=await r.auth.oauth.generateURL(n,a),s=await e(o),u=s.privy_oauth_state,h=s.privy_oauth_code;if(!u||!h)throw new t.PrivyClientError({code:"login_with_oauth_returned_with_invalid_credentials",error:"Auth session oauth returned invalid credentials"});let d=r.addOAuthTokensListener((t=>{r.crossApp.updateOnCrossAppAuthentication(i,t)})),{user:p}=await r.auth.oauth.linkWithCode(h,u,n);return d.unsubscribe(),p};
|
|
1
|
+
"use strict";var t=require("../../Error.js");exports.linkWithCrossAppAuth=({client:r,openAuthSession:e})=>async({providerAppId:i,redirectUrl:a})=>{let n=`privy:${i}`,{url:o}=await r.auth.oauth.generateURL(n,a),s=await e(o,a),u=s.privy_oauth_state,h=s.privy_oauth_code;if(!u||!h)throw new t.PrivyClientError({code:"login_with_oauth_returned_with_invalid_credentials",error:"Auth session oauth returned invalid credentials"});let d=r.addOAuthTokensListener((t=>{r.crossApp.updateOnCrossAppAuthentication(i,t)})),{user:p}=await r.auth.oauth.linkWithCode(h,u,n);return d.unsubscribe(),p};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var t=require("../../Error.js");exports.loginWithCrossAppAuth=({client:r,openAuthSession:e})=>async({providerAppId:i,redirectUrl:a})=>{let o=`privy:${i}`,{url:n}=await r.auth.oauth.generateURL(o,a),s=await e(n),u=s.privy_oauth_state,h=s.privy_oauth_code;if(!u||!h)throw new t.PrivyClientError({code:"login_with_oauth_returned_with_invalid_credentials",error:"Auth session oauth returned invalid credentials"});let p=await r.auth.oauth.loginWithCode(h,u,o),d=p.oauth_tokens;return d&&await r.crossApp.updateOnCrossAppAuthentication(i,d),p};
|
|
1
|
+
"use strict";var t=require("../../Error.js");exports.loginWithCrossAppAuth=({client:r,openAuthSession:e})=>async({providerAppId:i,redirectUrl:a})=>{let o=`privy:${i}`,{url:n}=await r.auth.oauth.generateURL(o,a),s=await e(n,a),u=s.privy_oauth_state,h=s.privy_oauth_code;if(!u||!h)throw new t.PrivyClientError({code:"login_with_oauth_returned_with_invalid_credentials",error:"Auth session oauth returned invalid credentials"});let p=await r.auth.oauth.loginWithCode(h,u,o),d=p.oauth_tokens;return d&&await r.crossApp.updateOnCrossAppAuthentication(i,d),p};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var r=require("../../../../Error.js"),e=require("./getCrossAppAccountByWalletAddress.js"),t=require("./getProviderAccessTokenOrRelink.js");require("../../linkWithCrossAppAuth.js");exports.sendCrossAppRequest=({client:s,openAuthSession:o})=>{let i=t.getProviderAccessTokenOrRelink({client:s,openAuthSession:o});return async({user:t,request:a,redirectUrl:n,address:p})=>{let c=e.getCrossAppAccountByWalletAddress(t,p);if(!c)throw new r.PrivyClientError({code:"cross_app_invalid_wallet",error:"Cannot operate with this wallet address"});let l=c.provider_app_id,{connections:d}=await s.crossApp.getCrossAppConnections(),_=d.find((r=>r.provider_app_id===l));if(!_)throw new r.PrivyClientError({code:"cross_app_invalid_app",error:"Invalid connected app"});if(_.read_only)throw new r.PrivyClientError({code:"cross_app_read_only",error:"Cannot transact against a read-only provider app"});let u=await i({providerAppId:l,redirectUrl:n});if(!u)throw new r.PrivyClientError({code:"cross_app_request_error",error:"Wallet operations require a valid token"});let v={content:{request:{request:a}},timestamp:Date.now(),callbackUrl:n},w=new URL(`${_.provider_app_custom_api_url}/oauth/transact`);w.searchParams.set("communicationMode","redirect"),w.searchParams.set("token",u),w.searchParams.set("request",JSON.stringify(v));let{privy_cross_app_type:C,privy_cross_app_payload:A}=await o(w.href);if(!C||!A)throw new r.PrivyClientError({code:"cross_app_request_error",error:"Cross app request returned an invalid result"});if("PRIVY_CROSS_APP_ACTION_ERROR"===C)throw new r.PrivyClientError({code:"cross_app_request_error",error:A});if("PRIVY_CROSS_APP_ACTION_RESPONSE"!==C)throw new r.PrivyClientError({code:"cross_app_request_error",error:"Cross app request returned an invalid result"});return{result:A}}};
|
|
1
|
+
"use strict";var r=require("../../../../Error.js"),e=require("./getCrossAppAccountByWalletAddress.js"),t=require("./getProviderAccessTokenOrRelink.js");require("../../linkWithCrossAppAuth.js");exports.sendCrossAppRequest=({client:s,openAuthSession:o})=>{let i=t.getProviderAccessTokenOrRelink({client:s,openAuthSession:o});return async({user:t,request:a,redirectUrl:n,address:p})=>{let c=e.getCrossAppAccountByWalletAddress(t,p);if(!c)throw new r.PrivyClientError({code:"cross_app_invalid_wallet",error:"Cannot operate with this wallet address"});let l=c.provider_app_id,{connections:d}=await s.crossApp.getCrossAppConnections(),_=d.find((r=>r.provider_app_id===l));if(!_)throw new r.PrivyClientError({code:"cross_app_invalid_app",error:"Invalid connected app"});if(_.read_only)throw new r.PrivyClientError({code:"cross_app_read_only",error:"Cannot transact against a read-only provider app"});let u=await i({providerAppId:l,redirectUrl:n});if(!u)throw new r.PrivyClientError({code:"cross_app_request_error",error:"Wallet operations require a valid token"});let v={content:{request:{request:a}},timestamp:Date.now(),callbackUrl:n},w=new URL(`${_.provider_app_custom_api_url}/oauth/transact`);w.searchParams.set("communicationMode","redirect"),w.searchParams.set("token",u),w.searchParams.set("request",JSON.stringify(v));let{privy_cross_app_type:C,privy_cross_app_payload:A}=await o(w.href,n);if(!C||!A)throw new r.PrivyClientError({code:"cross_app_request_error",error:"Cross app request returned an invalid result"});if("PRIVY_CROSS_APP_ACTION_ERROR"===C)throw new r.PrivyClientError({code:"cross_app_request_error",error:A});if("PRIVY_CROSS_APP_ACTION_RESPONSE"!==C)throw new r.PrivyClientError({code:"cross_app_request_error",error:"Cross app request returned an invalid result"});return{result:A}}};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var e=require("fetch-retry"),t=require("uuid"),s=require("@privy-io/api-base"),i=require("@privy-io/public-api"),r=require("../Error.js"),a=require("../Session.js"),n=require("../toAbortSignalTimeout.js"),o=require("../utils/toSearchParams.js"),h=require("./logger.js");function l(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}require("eventemitter3"),require("jose"),require("js-cookie"),require("../Token.js"),require("../utils/allSettled.js"),require("../utils/noop.js");var c=/*#__PURE__*/l(e);let g="privy:caid";exports.PrivyInternal=class{setCallbacks(e){this.callbacks={...this.callbacks,...e}}get isReady(){return!!this._config}get config(){return this._config}get caid(){return this._analyticsId}async _initialize(){if(this.isReady)this.callbacks?.setIsReady?.(!0);else{if(!await this.isStorageAccessible())throw new r.PrivyClientError({code:"storage_error",error:"Unable to access storage"});this._config=await this.getAppConfig(),this._config?.custom_api_url&&(this.baseUrl=this._config.custom_api_url,this.session.isUsingServerCookies=!0),this.callbacks?.setIsReady?.(!0),this._sdkVersion.startsWith("react-auth:")||this.createAnalyticsEvent("sdk_initialize",{})}}getPath(e,{params:t,query:s}){return`${this.baseUrl}${i.getPathWithParams(e.path,t)}${o.toSearchParams(s)}`}async fetch(e,{body:t,params:s,query:i,headers:a,onRequest:n=this._beforeRequest.bind(this)}){let o=new Request(this.getPath(e,{params:s,query:i}),{method:e.method,body:JSON.stringify(t),headers:a}),h=await n(o),l=await this._fetch(o,h),c=await l.json();if(l.status>299)throw new r.PrivyApiError(c);return c}async _beforeRequestWithoutInitialize(e){let t=await this.session.getToken(),s=new Headers(e.headers);s.set("privy-app-id",this.appId),this.appClientId&&s.set("privy-client-id",this.appClientId),s.set("privy-client",this._sdkVersion),t&&s.set("Authorization",`Bearer ${t}`),s.set("Content-Type","application/json"),s.set("Accept","application/json");let i=await this._getOrGenerateClientAnalyticsId();return i&&s.set("privy-ca-id",i),this.nativeAppIdentifier&&s.set("x-native-app-identifier",this.nativeAppIdentifier),{signal:n.default(2e4),headers:s,credentials:"include"}}async beforeRequestWithoutRefresh(e){return await this._initialize(),this._beforeRequestWithoutInitialize(e)}async _beforeRequest(e){return await this._initialize(),await this.getAccessToken(),this.beforeRequestWithoutRefresh(e)}async getAppConfig(){return await this.fetch(i.AppConfig,{params:{app_id:this.appId},onRequest:this._beforeRequestWithoutInitialize.bind(this)})}async _getOrGenerateClientAnalyticsId(){if(this._analyticsId)return this._analyticsId;try{let e=await this._storage.get(g);if("string"==typeof e&&e.length>0)return this._analyticsId=e,e}catch(e){this.logger.error("Unable to load clientId",e)}try{this._analyticsId=t.v4()}catch(e){this.logger.error("Unable to generate uuidv4",e)}if(!this._analyticsId)return null;try{await this._storage.put(g,this._analyticsId)}catch(e){this.logger.error(`Unable to store clientId: ${this._analyticsId}`,e)}return this._analyticsId}async destroyClientAnalyticsId(){try{return await this._storage.del(g)}catch(e){this.logger.error("Unable to delete clientId",e)}}async createAnalyticsEvent(e,t){try{await this.fetch(i.AnalyticsEvent,{body:{event_name:e,client_id:await this._getOrGenerateClientAnalyticsId(),payload:t},onRequest:this.beforeRequestWithoutRefresh.bind(this)})}catch(e){}}async refreshSession(e=!1){if(!await this.isStorageAccessible())throw new r.PrivyClientError({code:"storage_error",error:"Unable to access storage"});let t=await this.session.getRefreshToken()??void 0,s=t??"key",i=this._cache.get(s);if(i)return this.logger.debug("[privy:refresh] found in-flight session refresh request, deduping"),await i;let a=this._refreshSession(t,e);this._cache.set(s,a);try{return await a}finally{this._cache.delete(s)}}async _refreshSession(e,t){let a=await this.session.getToken();if(!this.session.hasRefreshCredentials(a,e??null))throw this.logger.debug("[privy:refresh] missing tokens, skipping request"),await this._initialize(),new r.PrivyApiError({code:s.PrivyErrorCode.MISSING_OR_INVALID_TOKEN,error:"No tokens found in storage"});try{this.logger.debug(`[privy:refresh] fetching: ${i.RefreshSession.path}`);let s=await this.fetch(i.RefreshSession,{body:{refresh_token:e},onRequest:this.beforeRequestWithoutRefresh.bind(this)}),r=s.session_update_action;return this.logger.debug(`[privy:refresh] response: ${r}`),t||this.callbacks?.setUser?.(s.user),"set"===r&&(await this.session.updateWithTokensResponse(s),this.logger.debug("[privy:refresh] tokens stored")),"clear"===r&&(await this.session.destroyLocalState(),this.logger.debug("[privy:refresh] tokens cleared"),t||this.callbacks?.setUser?.(null)),"ignore"===r&&s.token&&(await this.session.storeToken(s.token),this.logger.debug("[privy:refresh] access token stored"),s.identity_token&&(this.logger.debug("[privy:refresh] identity token stored"),await this.session.storeIdentityToken(s.identity_token))),this.logger.debug("[privy:refresh] returning response"),s}catch(e){throw this.logger.debug(`[privy:refresh] error: ${e.message??"unknown error"}`),e instanceof r.PrivyApiError&&e.code===s.PrivyErrorCode.MISSING_OR_INVALID_TOKEN&&(await this.session.destroyLocalState(),t||this.callbacks?.setUser?.(null)),e}}async getAccessToken(){let[e,t]=await Promise.all([this.session.getToken(),this.session.getRefreshToken()]);if(!this.session.tokenIsActive(e)&&this.session.hasRefreshCredentials(e,t)){let t=await this.refreshSession(),s=await this.session.getToken();return t.token||this.logger.debug("[privy:getAccessToken] expected token received null"),t.token===e&&this.logger.debug("[privy:getAccessToken] expected new token in response received existing"),s===e&&this.logger.debug("[privy:getAccessToken] expected new token in storage received existing"),t.token??s}return e}async getIdentityToken(){return await this.session.getIdentityToken()}async isStorageAccessible(){try{let e=`privy:__storage__test-${t.v4()}`,s="blobby";await this._storage.put(e,s);let i=await this._storage.get(e);return await this._storage.del(e),i===s}catch(e){return this.logger.error(e),!1}}constructor(e){this._sdkVersion="js-sdk-core:0.52.
|
|
1
|
+
"use strict";var e=require("fetch-retry"),t=require("uuid"),s=require("@privy-io/api-base"),i=require("@privy-io/public-api"),r=require("../Error.js"),a=require("../Session.js"),n=require("../toAbortSignalTimeout.js"),o=require("../utils/toSearchParams.js"),h=require("./logger.js");function l(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}require("eventemitter3"),require("jose"),require("js-cookie"),require("../Token.js"),require("../utils/allSettled.js"),require("../utils/noop.js");var c=/*#__PURE__*/l(e);let g="privy:caid";exports.PrivyInternal=class{setCallbacks(e){this.callbacks={...this.callbacks,...e}}get isReady(){return!!this._config}get config(){return this._config}get caid(){return this._analyticsId}async _initialize(){if(this.isReady)this.callbacks?.setIsReady?.(!0);else{if(!await this.isStorageAccessible())throw new r.PrivyClientError({code:"storage_error",error:"Unable to access storage"});this._config=await this.getAppConfig(),this._config?.custom_api_url&&(this.baseUrl=this._config.custom_api_url,this.session.isUsingServerCookies=!0),this.callbacks?.setIsReady?.(!0),this._sdkVersion.startsWith("react-auth:")||this.createAnalyticsEvent("sdk_initialize",{})}}getPath(e,{params:t,query:s}){return`${this.baseUrl}${i.getPathWithParams(e.path,t)}${o.toSearchParams(s)}`}async fetch(e,{body:t,params:s,query:i,headers:a,onRequest:n=this._beforeRequest.bind(this)}){let o=new Request(this.getPath(e,{params:s,query:i}),{method:e.method,body:JSON.stringify(t),headers:a}),h=await n(o),l=await this._fetch(o,h),c=await l.json();if(l.status>299)throw new r.PrivyApiError(c);return c}async _beforeRequestWithoutInitialize(e){let t=await this.session.getToken(),s=new Headers(e.headers);s.set("privy-app-id",this.appId),this.appClientId&&s.set("privy-client-id",this.appClientId),s.set("privy-client",this._sdkVersion),t&&s.set("Authorization",`Bearer ${t}`),s.set("Content-Type","application/json"),s.set("Accept","application/json");let i=await this._getOrGenerateClientAnalyticsId();return i&&s.set("privy-ca-id",i),this.nativeAppIdentifier&&s.set("x-native-app-identifier",this.nativeAppIdentifier),{signal:n.default(2e4),headers:s,credentials:"include"}}async beforeRequestWithoutRefresh(e){return await this._initialize(),this._beforeRequestWithoutInitialize(e)}async _beforeRequest(e){return await this._initialize(),await this.getAccessToken(),this.beforeRequestWithoutRefresh(e)}async getAppConfig(){return await this.fetch(i.AppConfig,{params:{app_id:this.appId},onRequest:this._beforeRequestWithoutInitialize.bind(this)})}async _getOrGenerateClientAnalyticsId(){if(this._analyticsId)return this._analyticsId;try{let e=await this._storage.get(g);if("string"==typeof e&&e.length>0)return this._analyticsId=e,e}catch(e){this.logger.error("Unable to load clientId",e)}try{this._analyticsId=t.v4()}catch(e){this.logger.error("Unable to generate uuidv4",e)}if(!this._analyticsId)return null;try{await this._storage.put(g,this._analyticsId)}catch(e){this.logger.error(`Unable to store clientId: ${this._analyticsId}`,e)}return this._analyticsId}async destroyClientAnalyticsId(){try{return await this._storage.del(g)}catch(e){this.logger.error("Unable to delete clientId",e)}}async createAnalyticsEvent(e,t){try{await this.fetch(i.AnalyticsEvent,{body:{event_name:e,client_id:await this._getOrGenerateClientAnalyticsId(),payload:t},onRequest:this.beforeRequestWithoutRefresh.bind(this)})}catch(e){}}async refreshSession(e=!1){if(!await this.isStorageAccessible())throw new r.PrivyClientError({code:"storage_error",error:"Unable to access storage"});let t=await this.session.getRefreshToken()??void 0,s=t??"key",i=this._cache.get(s);if(i)return this.logger.debug("[privy:refresh] found in-flight session refresh request, deduping"),await i;let a=this._refreshSession(t,e);this._cache.set(s,a);try{return await a}finally{this._cache.delete(s)}}async _refreshSession(e,t){let a=await this.session.getToken();if(!this.session.hasRefreshCredentials(a,e??null))throw this.logger.debug("[privy:refresh] missing tokens, skipping request"),await this._initialize(),new r.PrivyApiError({code:s.PrivyErrorCode.MISSING_OR_INVALID_TOKEN,error:"No tokens found in storage"});try{this.logger.debug(`[privy:refresh] fetching: ${i.RefreshSession.path}`);let s=await this.fetch(i.RefreshSession,{body:{refresh_token:e},onRequest:this.beforeRequestWithoutRefresh.bind(this)}),r=s.session_update_action;return this.logger.debug(`[privy:refresh] response: ${r}`),t||this.callbacks?.setUser?.(s.user),"set"===r&&(await this.session.updateWithTokensResponse(s),this.logger.debug("[privy:refresh] tokens stored")),"clear"===r&&(await this.session.destroyLocalState(),this.logger.debug("[privy:refresh] tokens cleared"),t||this.callbacks?.setUser?.(null)),"ignore"===r&&s.token&&(await this.session.storeToken(s.token),this.logger.debug("[privy:refresh] access token stored"),s.identity_token&&(this.logger.debug("[privy:refresh] identity token stored"),await this.session.storeIdentityToken(s.identity_token))),this.logger.debug("[privy:refresh] returning response"),s}catch(e){throw this.logger.debug(`[privy:refresh] error: ${e.message??"unknown error"}`),e instanceof r.PrivyApiError&&e.code===s.PrivyErrorCode.MISSING_OR_INVALID_TOKEN&&(await this.session.destroyLocalState(),t||this.callbacks?.setUser?.(null)),e}}async getAccessToken(){let[e,t]=await Promise.all([this.session.getToken(),this.session.getRefreshToken()]);if(!this.session.tokenIsActive(e)&&this.session.hasRefreshCredentials(e,t)){let t=await this.refreshSession(),s=await this.session.getToken();return t.token||this.logger.debug("[privy:getAccessToken] expected token received null"),t.token===e&&this.logger.debug("[privy:getAccessToken] expected new token in response received existing"),s===e&&this.logger.debug("[privy:getAccessToken] expected new token in storage received existing"),t.token??s}return e}async getIdentityToken(){return await this.session.getIdentityToken()}async isStorageAccessible(){try{let e=`privy:__storage__test-${t.v4()}`,s="blobby";await this._storage.put(e,s);let i=await this._storage.get(e);return await this._storage.del(e),i===s}catch(e){return this.logger.error(e),!1}}constructor(e){this._sdkVersion="js-sdk-core:0.52.3",this._cache=new Map,this.logger=h.toConsoleLogger({level:e.logLevel??"ERROR"}),this._storage=e.storage,this._analyticsId=null,this._getOrGenerateClientAnalyticsId(),this.baseUrl=e.baseUrl??"https://auth.privy.io",this.appId=e.appId,this.appClientId=e.appClientId,this._sdkVersion=e.sdkVersion??this._sdkVersion,this.callbacks=e.callbacks,this.nativeAppIdentifier=e.nativeAppIdentifier,this.session=new a.Session({storage:this._storage,isUsingServerCookies:!1,appId:e.appId}),this._fetch=c.default(globalThis.fetch,{retries:3,retryDelay:e=>3**e*500,retryOn:[408,409,425,500,502,503,504]}),this.session.on("error_storing_tokens",(e=>{this.createAnalyticsEvent("error_updating_tokens_in_storage",{reason:e})}))}};
|
package/dist/dts/index.d.mts
CHANGED
|
@@ -8227,7 +8227,7 @@ declare namespace index$2 {
|
|
|
8227
8227
|
|
|
8228
8228
|
interface LoginWithCrossAppAuthDependencies {
|
|
8229
8229
|
client: Privy;
|
|
8230
|
-
openAuthSession: (url: string) => Promise<Record<string, string>>;
|
|
8230
|
+
openAuthSession: (url: string, redirectUrl?: string) => Promise<Record<string, string>>;
|
|
8231
8231
|
}
|
|
8232
8232
|
interface LoginWithCrossAppAuthInput {
|
|
8233
8233
|
providerAppId: string;
|
|
@@ -8245,7 +8245,7 @@ declare const loginWithCrossAppAuth: ({ client, openAuthSession }: LoginWithCros
|
|
|
8245
8245
|
|
|
8246
8246
|
interface LinkWithCrossAppAuthDependencies {
|
|
8247
8247
|
client: Privy;
|
|
8248
|
-
openAuthSession: (url: string) => Promise<Record<string, string>>;
|
|
8248
|
+
openAuthSession: (url: string, redirectUrl?: string) => Promise<Record<string, string>>;
|
|
8249
8249
|
}
|
|
8250
8250
|
interface LinkWithCrossAppAuthInput {
|
|
8251
8251
|
providerAppId: string;
|
package/dist/dts/index.d.ts
CHANGED
|
@@ -8227,7 +8227,7 @@ declare namespace index$2 {
|
|
|
8227
8227
|
|
|
8228
8228
|
interface LoginWithCrossAppAuthDependencies {
|
|
8229
8229
|
client: Privy;
|
|
8230
|
-
openAuthSession: (url: string) => Promise<Record<string, string>>;
|
|
8230
|
+
openAuthSession: (url: string, redirectUrl?: string) => Promise<Record<string, string>>;
|
|
8231
8231
|
}
|
|
8232
8232
|
interface LoginWithCrossAppAuthInput {
|
|
8233
8233
|
providerAppId: string;
|
|
@@ -8245,7 +8245,7 @@ declare const loginWithCrossAppAuth: ({ client, openAuthSession }: LoginWithCros
|
|
|
8245
8245
|
|
|
8246
8246
|
interface LinkWithCrossAppAuthDependencies {
|
|
8247
8247
|
client: Privy;
|
|
8248
|
-
openAuthSession: (url: string) => Promise<Record<string, string>>;
|
|
8248
|
+
openAuthSession: (url: string, redirectUrl?: string) => Promise<Record<string, string>>;
|
|
8249
8249
|
}
|
|
8250
8250
|
interface LinkWithCrossAppAuthInput {
|
|
8251
8251
|
providerAppId: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{PrivyClientError as t}from"../../Error.mjs";const e=({client:e,openAuthSession:r})=>async({providerAppId:i,redirectUrl:o})=>{let a=`privy:${i}`,{url:n}=await e.auth.oauth.generateURL(a,o),s=await r(n),u=s.privy_oauth_state,h=s.privy_oauth_code;if(!u||!h)throw new t({code:"login_with_oauth_returned_with_invalid_credentials",error:"Auth session oauth returned invalid credentials"});let d=e.addOAuthTokensListener((t=>{e.crossApp.updateOnCrossAppAuthentication(i,t)})),{user:p}=await e.auth.oauth.linkWithCode(h,u,a);return d.unsubscribe(),p};export{e as linkWithCrossAppAuth};
|
|
1
|
+
import{PrivyClientError as t}from"../../Error.mjs";const e=({client:e,openAuthSession:r})=>async({providerAppId:i,redirectUrl:o})=>{let a=`privy:${i}`,{url:n}=await e.auth.oauth.generateURL(a,o),s=await r(n,o),u=s.privy_oauth_state,h=s.privy_oauth_code;if(!u||!h)throw new t({code:"login_with_oauth_returned_with_invalid_credentials",error:"Auth session oauth returned invalid credentials"});let d=e.addOAuthTokensListener((t=>{e.crossApp.updateOnCrossAppAuthentication(i,t)})),{user:p}=await e.auth.oauth.linkWithCode(h,u,a);return d.unsubscribe(),p};export{e as linkWithCrossAppAuth};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{PrivyClientError as t}from"../../Error.mjs";const e=({client:e,openAuthSession:r})=>async({providerAppId:i,redirectUrl:o})=>{let a=`privy:${i}`,{url:n}=await e.auth.oauth.generateURL(a,o),u=await r(n),h=u.privy_oauth_state,s=u.privy_oauth_code;if(!h||!s)throw new t({code:"login_with_oauth_returned_with_invalid_credentials",error:"Auth session oauth returned invalid credentials"});let p=await e.auth.oauth.loginWithCode(s,h,a),d=p.oauth_tokens;return d&&await e.crossApp.updateOnCrossAppAuthentication(i,d),p};export{e as loginWithCrossAppAuth};
|
|
1
|
+
import{PrivyClientError as t}from"../../Error.mjs";const e=({client:e,openAuthSession:r})=>async({providerAppId:i,redirectUrl:o})=>{let a=`privy:${i}`,{url:n}=await e.auth.oauth.generateURL(a,o),u=await r(n,o),h=u.privy_oauth_state,s=u.privy_oauth_code;if(!h||!s)throw new t({code:"login_with_oauth_returned_with_invalid_credentials",error:"Auth session oauth returned invalid credentials"});let p=await e.auth.oauth.loginWithCode(s,h,a),d=p.oauth_tokens;return d&&await e.crossApp.updateOnCrossAppAuthentication(i,d),p};export{e as loginWithCrossAppAuth};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{PrivyClientError as r}from"../../../../Error.mjs";import{getCrossAppAccountByWalletAddress as e}from"./getCrossAppAccountByWalletAddress.mjs";import{getProviderAccessTokenOrRelink as o}from"./getProviderAccessTokenOrRelink.mjs";import"../../linkWithCrossAppAuth.mjs";const t=({client:t,openAuthSession:s})=>{let a=o({client:t,openAuthSession:s});return async({user:o,request:p,redirectUrl:n,address:i})=>{let c=e(o,i);if(!c)throw new r({code:"cross_app_invalid_wallet",error:"Cannot operate with this wallet address"});let _=c.provider_app_id,{connections:d}=await t.crossApp.getCrossAppConnections(),l=d.find((r=>r.provider_app_id===_));if(!l)throw new r({code:"cross_app_invalid_app",error:"Invalid connected app"});if(l.read_only)throw new r({code:"cross_app_read_only",error:"Cannot transact against a read-only provider app"});let u=await a({providerAppId:_,redirectUrl:n});if(!u)throw new r({code:"cross_app_request_error",error:"Wallet operations require a valid token"});let w={content:{request:{request:p}},timestamp:Date.now(),callbackUrl:n},m=new URL(`${l.provider_app_custom_api_url}/oauth/transact`);m.searchParams.set("communicationMode","redirect"),m.searchParams.set("token",u),m.searchParams.set("request",JSON.stringify(w));let{privy_cross_app_type:h,privy_cross_app_payload:A}=await s(m.href);if(!h||!A)throw new r({code:"cross_app_request_error",error:"Cross app request returned an invalid result"});if("PRIVY_CROSS_APP_ACTION_ERROR"===h)throw new r({code:"cross_app_request_error",error:A});if("PRIVY_CROSS_APP_ACTION_RESPONSE"!==h)throw new r({code:"cross_app_request_error",error:"Cross app request returned an invalid result"});return{result:A}}};export{t as sendCrossAppRequest};
|
|
1
|
+
import{PrivyClientError as r}from"../../../../Error.mjs";import{getCrossAppAccountByWalletAddress as e}from"./getCrossAppAccountByWalletAddress.mjs";import{getProviderAccessTokenOrRelink as o}from"./getProviderAccessTokenOrRelink.mjs";import"../../linkWithCrossAppAuth.mjs";const t=({client:t,openAuthSession:s})=>{let a=o({client:t,openAuthSession:s});return async({user:o,request:p,redirectUrl:n,address:i})=>{let c=e(o,i);if(!c)throw new r({code:"cross_app_invalid_wallet",error:"Cannot operate with this wallet address"});let _=c.provider_app_id,{connections:d}=await t.crossApp.getCrossAppConnections(),l=d.find((r=>r.provider_app_id===_));if(!l)throw new r({code:"cross_app_invalid_app",error:"Invalid connected app"});if(l.read_only)throw new r({code:"cross_app_read_only",error:"Cannot transact against a read-only provider app"});let u=await a({providerAppId:_,redirectUrl:n});if(!u)throw new r({code:"cross_app_request_error",error:"Wallet operations require a valid token"});let w={content:{request:{request:p}},timestamp:Date.now(),callbackUrl:n},m=new URL(`${l.provider_app_custom_api_url}/oauth/transact`);m.searchParams.set("communicationMode","redirect"),m.searchParams.set("token",u),m.searchParams.set("request",JSON.stringify(w));let{privy_cross_app_type:h,privy_cross_app_payload:A}=await s(m.href,n);if(!h||!A)throw new r({code:"cross_app_request_error",error:"Cross app request returned an invalid result"});if("PRIVY_CROSS_APP_ACTION_ERROR"===h)throw new r({code:"cross_app_request_error",error:A});if("PRIVY_CROSS_APP_ACTION_RESPONSE"!==h)throw new r({code:"cross_app_request_error",error:"Cross app request returned an invalid result"});return{result:A}}};export{t as sendCrossAppRequest};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import e from"fetch-retry";import{v4 as t}from"uuid";import{PrivyErrorCode as s}from"@privy-io/api-base";import{getPathWithParams as i,AppConfig as r,AnalyticsEvent as a,RefreshSession as n}from"@privy-io/public-api";import{PrivyClientError as o,PrivyApiError as h}from"../Error.mjs";import{Session as l}from"../Session.mjs";import c from"../toAbortSignalTimeout.mjs";import{toSearchParams as g}from"../utils/toSearchParams.mjs";import{toConsoleLogger as d}from"./logger.mjs";import"eventemitter3";import"jose";import"js-cookie";import"../Token.mjs";import"../utils/allSettled.mjs";import"../utils/noop.mjs";let p="privy:caid";class u{setCallbacks(e){this.callbacks={...this.callbacks,...e}}get isReady(){return!!this._config}get config(){return this._config}get caid(){return this._analyticsId}async _initialize(){if(this.isReady)this.callbacks?.setIsReady?.(!0);else{if(!await this.isStorageAccessible())throw new o({code:"storage_error",error:"Unable to access storage"});this._config=await this.getAppConfig(),this._config?.custom_api_url&&(this.baseUrl=this._config.custom_api_url,this.session.isUsingServerCookies=!0),this.callbacks?.setIsReady?.(!0),this._sdkVersion.startsWith("react-auth:")||this.createAnalyticsEvent("sdk_initialize",{})}}getPath(e,{params:t,query:s}){return`${this.baseUrl}${i(e.path,t)}${g(s)}`}async fetch(e,{body:t,params:s,query:i,headers:r,onRequest:a=this._beforeRequest.bind(this)}){let n=new Request(this.getPath(e,{params:s,query:i}),{method:e.method,body:JSON.stringify(t),headers:r}),o=await a(n),l=await this._fetch(n,o),c=await l.json();if(l.status>299)throw new h(c);return c}async _beforeRequestWithoutInitialize(e){let t=await this.session.getToken(),s=new Headers(e.headers);s.set("privy-app-id",this.appId),this.appClientId&&s.set("privy-client-id",this.appClientId),s.set("privy-client",this._sdkVersion),t&&s.set("Authorization",`Bearer ${t}`),s.set("Content-Type","application/json"),s.set("Accept","application/json");let i=await this._getOrGenerateClientAnalyticsId();return i&&s.set("privy-ca-id",i),this.nativeAppIdentifier&&s.set("x-native-app-identifier",this.nativeAppIdentifier),{signal:c(2e4),headers:s,credentials:"include"}}async beforeRequestWithoutRefresh(e){return await this._initialize(),this._beforeRequestWithoutInitialize(e)}async _beforeRequest(e){return await this._initialize(),await this.getAccessToken(),this.beforeRequestWithoutRefresh(e)}async getAppConfig(){return await this.fetch(r,{params:{app_id:this.appId},onRequest:this._beforeRequestWithoutInitialize.bind(this)})}async _getOrGenerateClientAnalyticsId(){if(this._analyticsId)return this._analyticsId;try{let e=await this._storage.get(p);if("string"==typeof e&&e.length>0)return this._analyticsId=e,e}catch(e){this.logger.error("Unable to load clientId",e)}try{this._analyticsId=t()}catch(e){this.logger.error("Unable to generate uuidv4",e)}if(!this._analyticsId)return null;try{await this._storage.put(p,this._analyticsId)}catch(e){this.logger.error(`Unable to store clientId: ${this._analyticsId}`,e)}return this._analyticsId}async destroyClientAnalyticsId(){try{return await this._storage.del(p)}catch(e){this.logger.error("Unable to delete clientId",e)}}async createAnalyticsEvent(e,t){try{await this.fetch(a,{body:{event_name:e,client_id:await this._getOrGenerateClientAnalyticsId(),payload:t},onRequest:this.beforeRequestWithoutRefresh.bind(this)})}catch(e){}}async refreshSession(e=!1){if(!await this.isStorageAccessible())throw new o({code:"storage_error",error:"Unable to access storage"});let t=await this.session.getRefreshToken()??void 0,s=t??"key",i=this._cache.get(s);if(i)return this.logger.debug("[privy:refresh] found in-flight session refresh request, deduping"),await i;let r=this._refreshSession(t,e);this._cache.set(s,r);try{return await r}finally{this._cache.delete(s)}}async _refreshSession(e,t){let i=await this.session.getToken();if(!this.session.hasRefreshCredentials(i,e??null))throw this.logger.debug("[privy:refresh] missing tokens, skipping request"),await this._initialize(),new h({code:s.MISSING_OR_INVALID_TOKEN,error:"No tokens found in storage"});try{this.logger.debug(`[privy:refresh] fetching: ${n.path}`);let s=await this.fetch(n,{body:{refresh_token:e},onRequest:this.beforeRequestWithoutRefresh.bind(this)}),i=s.session_update_action;return this.logger.debug(`[privy:refresh] response: ${i}`),t||this.callbacks?.setUser?.(s.user),"set"===i&&(await this.session.updateWithTokensResponse(s),this.logger.debug("[privy:refresh] tokens stored")),"clear"===i&&(await this.session.destroyLocalState(),this.logger.debug("[privy:refresh] tokens cleared"),t||this.callbacks?.setUser?.(null)),"ignore"===i&&s.token&&(await this.session.storeToken(s.token),this.logger.debug("[privy:refresh] access token stored"),s.identity_token&&(this.logger.debug("[privy:refresh] identity token stored"),await this.session.storeIdentityToken(s.identity_token))),this.logger.debug("[privy:refresh] returning response"),s}catch(e){throw this.logger.debug(`[privy:refresh] error: ${e.message??"unknown error"}`),e instanceof h&&e.code===s.MISSING_OR_INVALID_TOKEN&&(await this.session.destroyLocalState(),t||this.callbacks?.setUser?.(null)),e}}async getAccessToken(){let[e,t]=await Promise.all([this.session.getToken(),this.session.getRefreshToken()]);if(!this.session.tokenIsActive(e)&&this.session.hasRefreshCredentials(e,t)){let t=await this.refreshSession(),s=await this.session.getToken();return t.token||this.logger.debug("[privy:getAccessToken] expected token received null"),t.token===e&&this.logger.debug("[privy:getAccessToken] expected new token in response received existing"),s===e&&this.logger.debug("[privy:getAccessToken] expected new token in storage received existing"),t.token??s}return e}async getIdentityToken(){return await this.session.getIdentityToken()}async isStorageAccessible(){try{let e=`privy:__storage__test-${t()}`,s="blobby";await this._storage.put(e,s);let i=await this._storage.get(e);return await this._storage.del(e),i===s}catch(e){return this.logger.error(e),!1}}constructor(t){this._sdkVersion="js-sdk-core:0.52.
|
|
1
|
+
import e from"fetch-retry";import{v4 as t}from"uuid";import{PrivyErrorCode as s}from"@privy-io/api-base";import{getPathWithParams as i,AppConfig as r,AnalyticsEvent as a,RefreshSession as n}from"@privy-io/public-api";import{PrivyClientError as o,PrivyApiError as h}from"../Error.mjs";import{Session as l}from"../Session.mjs";import c from"../toAbortSignalTimeout.mjs";import{toSearchParams as g}from"../utils/toSearchParams.mjs";import{toConsoleLogger as d}from"./logger.mjs";import"eventemitter3";import"jose";import"js-cookie";import"../Token.mjs";import"../utils/allSettled.mjs";import"../utils/noop.mjs";let p="privy:caid";class u{setCallbacks(e){this.callbacks={...this.callbacks,...e}}get isReady(){return!!this._config}get config(){return this._config}get caid(){return this._analyticsId}async _initialize(){if(this.isReady)this.callbacks?.setIsReady?.(!0);else{if(!await this.isStorageAccessible())throw new o({code:"storage_error",error:"Unable to access storage"});this._config=await this.getAppConfig(),this._config?.custom_api_url&&(this.baseUrl=this._config.custom_api_url,this.session.isUsingServerCookies=!0),this.callbacks?.setIsReady?.(!0),this._sdkVersion.startsWith("react-auth:")||this.createAnalyticsEvent("sdk_initialize",{})}}getPath(e,{params:t,query:s}){return`${this.baseUrl}${i(e.path,t)}${g(s)}`}async fetch(e,{body:t,params:s,query:i,headers:r,onRequest:a=this._beforeRequest.bind(this)}){let n=new Request(this.getPath(e,{params:s,query:i}),{method:e.method,body:JSON.stringify(t),headers:r}),o=await a(n),l=await this._fetch(n,o),c=await l.json();if(l.status>299)throw new h(c);return c}async _beforeRequestWithoutInitialize(e){let t=await this.session.getToken(),s=new Headers(e.headers);s.set("privy-app-id",this.appId),this.appClientId&&s.set("privy-client-id",this.appClientId),s.set("privy-client",this._sdkVersion),t&&s.set("Authorization",`Bearer ${t}`),s.set("Content-Type","application/json"),s.set("Accept","application/json");let i=await this._getOrGenerateClientAnalyticsId();return i&&s.set("privy-ca-id",i),this.nativeAppIdentifier&&s.set("x-native-app-identifier",this.nativeAppIdentifier),{signal:c(2e4),headers:s,credentials:"include"}}async beforeRequestWithoutRefresh(e){return await this._initialize(),this._beforeRequestWithoutInitialize(e)}async _beforeRequest(e){return await this._initialize(),await this.getAccessToken(),this.beforeRequestWithoutRefresh(e)}async getAppConfig(){return await this.fetch(r,{params:{app_id:this.appId},onRequest:this._beforeRequestWithoutInitialize.bind(this)})}async _getOrGenerateClientAnalyticsId(){if(this._analyticsId)return this._analyticsId;try{let e=await this._storage.get(p);if("string"==typeof e&&e.length>0)return this._analyticsId=e,e}catch(e){this.logger.error("Unable to load clientId",e)}try{this._analyticsId=t()}catch(e){this.logger.error("Unable to generate uuidv4",e)}if(!this._analyticsId)return null;try{await this._storage.put(p,this._analyticsId)}catch(e){this.logger.error(`Unable to store clientId: ${this._analyticsId}`,e)}return this._analyticsId}async destroyClientAnalyticsId(){try{return await this._storage.del(p)}catch(e){this.logger.error("Unable to delete clientId",e)}}async createAnalyticsEvent(e,t){try{await this.fetch(a,{body:{event_name:e,client_id:await this._getOrGenerateClientAnalyticsId(),payload:t},onRequest:this.beforeRequestWithoutRefresh.bind(this)})}catch(e){}}async refreshSession(e=!1){if(!await this.isStorageAccessible())throw new o({code:"storage_error",error:"Unable to access storage"});let t=await this.session.getRefreshToken()??void 0,s=t??"key",i=this._cache.get(s);if(i)return this.logger.debug("[privy:refresh] found in-flight session refresh request, deduping"),await i;let r=this._refreshSession(t,e);this._cache.set(s,r);try{return await r}finally{this._cache.delete(s)}}async _refreshSession(e,t){let i=await this.session.getToken();if(!this.session.hasRefreshCredentials(i,e??null))throw this.logger.debug("[privy:refresh] missing tokens, skipping request"),await this._initialize(),new h({code:s.MISSING_OR_INVALID_TOKEN,error:"No tokens found in storage"});try{this.logger.debug(`[privy:refresh] fetching: ${n.path}`);let s=await this.fetch(n,{body:{refresh_token:e},onRequest:this.beforeRequestWithoutRefresh.bind(this)}),i=s.session_update_action;return this.logger.debug(`[privy:refresh] response: ${i}`),t||this.callbacks?.setUser?.(s.user),"set"===i&&(await this.session.updateWithTokensResponse(s),this.logger.debug("[privy:refresh] tokens stored")),"clear"===i&&(await this.session.destroyLocalState(),this.logger.debug("[privy:refresh] tokens cleared"),t||this.callbacks?.setUser?.(null)),"ignore"===i&&s.token&&(await this.session.storeToken(s.token),this.logger.debug("[privy:refresh] access token stored"),s.identity_token&&(this.logger.debug("[privy:refresh] identity token stored"),await this.session.storeIdentityToken(s.identity_token))),this.logger.debug("[privy:refresh] returning response"),s}catch(e){throw this.logger.debug(`[privy:refresh] error: ${e.message??"unknown error"}`),e instanceof h&&e.code===s.MISSING_OR_INVALID_TOKEN&&(await this.session.destroyLocalState(),t||this.callbacks?.setUser?.(null)),e}}async getAccessToken(){let[e,t]=await Promise.all([this.session.getToken(),this.session.getRefreshToken()]);if(!this.session.tokenIsActive(e)&&this.session.hasRefreshCredentials(e,t)){let t=await this.refreshSession(),s=await this.session.getToken();return t.token||this.logger.debug("[privy:getAccessToken] expected token received null"),t.token===e&&this.logger.debug("[privy:getAccessToken] expected new token in response received existing"),s===e&&this.logger.debug("[privy:getAccessToken] expected new token in storage received existing"),t.token??s}return e}async getIdentityToken(){return await this.session.getIdentityToken()}async isStorageAccessible(){try{let e=`privy:__storage__test-${t()}`,s="blobby";await this._storage.put(e,s);let i=await this._storage.get(e);return await this._storage.del(e),i===s}catch(e){return this.logger.error(e),!1}}constructor(t){this._sdkVersion="js-sdk-core:0.52.3",this._cache=new Map,this.logger=d({level:t.logLevel??"ERROR"}),this._storage=t.storage,this._analyticsId=null,this._getOrGenerateClientAnalyticsId(),this.baseUrl=t.baseUrl??"https://auth.privy.io",this.appId=t.appId,this.appClientId=t.appClientId,this._sdkVersion=t.sdkVersion??this._sdkVersion,this.callbacks=t.callbacks,this.nativeAppIdentifier=t.nativeAppIdentifier,this.session=new l({storage:this._storage,isUsingServerCookies:!1,appId:t.appId}),this._fetch=e(globalThis.fetch,{retries:3,retryDelay:e=>3**e*500,retryOn:[408,409,425,500,502,503,504]}),this.session.on("error_storing_tokens",(e=>{this.createAnalyticsEvent("error_updating_tokens_in_storage",{reason:e})}))}}export{u as PrivyInternal};
|