Skip to content

Commit 291dec2

Browse files
committed
adds session key support + remove unused settings
1 parent 3423267 commit 291dec2

File tree

3 files changed

+36
-151
lines changed

3 files changed

+36
-151
lines changed

src/core/AuthProvider.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ export class AuthProvider {
2727

2828
public initialized: boolean = false;
2929

30-
private targetOrigin: string;
31-
3230
private iframeElem: HTMLIFrameElement;
3331

3432
private iframeLoadPromise: Promise<void> | null = null;
@@ -39,8 +37,10 @@ export class AuthProvider {
3937

4038
constructor({ sdkUrl }: { sdkUrl: string }) {
4139
this.sdkUrl = sdkUrl;
42-
this.targetOrigin = new URL(this.sdkUrl).origin;
43-
log.info("target origin", this.targetOrigin);
40+
}
41+
42+
get targetOrigin(): string {
43+
return new URL(this.sdkUrl).origin;
4444
}
4545

4646
async loadIframe(): Promise<void> {
@@ -122,24 +122,24 @@ export class AuthProvider {
122122
}
123123

124124
private setupMessageListener() {
125-
log.info("setting up message listener");
126125
window.addEventListener("message", this.handleMessage.bind(this));
127126
}
128127

129128
private handleMessage(event: MessageEvent) {
130-
log.info("message events in auth provider", event, this.targetOrigin);
131129
const { origin, data } = event as {
132130
origin: string;
133131
data: { type: string; data: { sessionId?: string; sessionNamespace?: string; error?: string } };
134132
};
133+
// the origin should be the same as the target origin
135134
if (origin !== this.targetOrigin) return;
136-
const { type, data: messageData } = data;
135+
const { type } = data;
136+
const messageData = data.data;
137137
switch (type) {
138138
case JRPC_METHODS.LOGIN_FAILED:
139139
this.loginCallbackFailed?.(messageData?.error || "Login failed, reason: unknown");
140140
break;
141141
case JRPC_METHODS.LOGIN_SUCCESS:
142-
log.info("LOGIN_SUCCESS", messageData, this.loginCallbackSuccess);
142+
log.info("LOGIN_SUCCESS", messageData);
143143
if (messageData?.sessionId) this.loginCallbackSuccess?.(messageData);
144144
break;
145145
default:

src/core/auth.ts

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
AuthSessionData,
1212
AuthUserInfo,
1313
BaseLoginParams,
14-
BaseRedirectParams,
1514
BrowserStorage,
1615
BUILD_ENV,
1716
cloneDeep,
@@ -100,8 +99,7 @@ export class Auth {
10099
if (!options.mfaSettings) options.mfaSettings = {};
101100
if (!options.storageServerUrl) options.storageServerUrl = SESSION_SERVER_API_URL;
102101
if (!options.sessionSocketUrl) options.sessionSocketUrl = SESSION_SERVER_SOCKET_URL;
103-
if (!options.storageKey) options.storageKey = "local";
104-
if (!options.webauthnTransports) options.webauthnTransports = ["internal"];
102+
if (!options.storage) options.storage = "local";
105103
if (!options.sessionTime) options.sessionTime = 86400;
106104

107105
this.options = options;
@@ -166,8 +164,9 @@ export class Auth {
166164
const params = getHashQueryParams(this.options.replaceUrlOnRedirect);
167165
if (params.sessionNamespace) this.options.sessionNamespace = params.sessionNamespace;
168166

169-
const storageKey = this.options.sessionNamespace ? `${this._storageBaseKey}_${this.options.sessionNamespace}` : this._storageBaseKey;
170-
this.currentStorage = BrowserStorage.getInstance(storageKey, this.options.storageKey);
167+
const storageKey =
168+
this.options.sessionKey || this.options.sessionNamespace ? `${this._storageBaseKey}_${this.options.sessionNamespace}` : this._storageBaseKey;
169+
this.currentStorage = BrowserStorage.getInstance(storageKey, this.options.storage);
171170

172171
const sessionId = this.currentStorage.get<string>("sessionId");
173172

@@ -232,18 +231,11 @@ export class Auth {
232231
return finalConfig;
233232
}
234233

235-
async login(params: LoginParams & Partial<BaseRedirectParams>): Promise<{ privKey: string } | null> {
234+
async login(params: LoginParams): Promise<{ privKey: string } | null> {
236235
if (!params.loginProvider) throw LoginError.invalidLoginParams(`loginProvider is required`);
237236

238-
// in case of redirect mode, redirect url will be dapp specified
239-
// in case of popup mode, redirect url will be sdk specified
240-
const defaultParams: BaseRedirectParams = {
241-
redirectUrl: this.options.redirectUrl,
242-
};
243-
244237
const loginParams: LoginParams = {
245238
loginProvider: params.loginProvider,
246-
...defaultParams,
247239
...params,
248240
};
249241

@@ -266,7 +258,7 @@ export class Auth {
266258
return { privKey: this.privKey };
267259
}
268260

269-
async postLoginInitiatedMessage(params: LoginParams & Partial<BaseRedirectParams>, nonce?: string): Promise<void> {
261+
async postLoginInitiatedMessage(params: LoginParams, nonce?: string): Promise<void> {
270262
if (this.options.sdkMode !== SDK_MODE.IFRAME) throw LoginError.invalidLoginParams("Cannot perform this action in default mode.");
271263
if (!this.authProvider || !this.authProvider.initialized) throw InitializationError.notInitialized();
272264

@@ -329,17 +321,11 @@ export class Auth {
329321
async enableMFA(params: Partial<LoginParams>): Promise<boolean> {
330322
if (!this.sessionId) throw LoginError.userNotLoggedIn();
331323
if (this.state.userInfo.isMfaEnabled) throw LoginError.mfaAlreadyEnabled();
332-
// in case of redirect mode, redirect url will be dapp specified
333-
// in case of popup mode, redirect url will be sdk specified
334-
const defaultParams: BaseRedirectParams = {
335-
redirectUrl: this.options.redirectUrl,
336-
};
337324

338325
const dataObject: AuthSessionConfig = {
339326
actionType: AUTH_ACTIONS.ENABLE_MFA,
340327
options: this.options,
341328
params: {
342-
...defaultParams,
343329
...params,
344330
loginProvider: this.state.userInfo.typeOfLogin,
345331
extraLoginOptions: {
@@ -407,20 +393,13 @@ export class Auth {
407393
window.open(loginUrl, "_blank");
408394
}
409395

410-
async manageSocialFactor(actionType: AUTH_ACTIONS_TYPE, params: SocialMfaModParams & Partial<BaseRedirectParams>): Promise<boolean> {
396+
async manageSocialFactor(actionType: AUTH_ACTIONS_TYPE, params: SocialMfaModParams & Pick<LoginParams, "appState">): Promise<boolean> {
411397
if (!this.sessionId) throw LoginError.userNotLoggedIn();
412398

413-
// in case of redirect mode, redirect url will be dapp specified
414-
// in case of popup mode, redirect url will be sdk specified
415-
const defaultParams: BaseRedirectParams = {
416-
redirectUrl: this.options.redirectUrl,
417-
};
418-
419399
const dataObject: AuthSessionConfig = {
420400
actionType,
421401
options: this.options,
422402
params: {
423-
...defaultParams,
424403
...params,
425404
},
426405
sessionId: this.sessionId,
@@ -432,20 +411,13 @@ export class Auth {
432411
return true;
433412
}
434413

435-
async addAuthenticatorFactor(params: Partial<BaseRedirectParams>): Promise<boolean> {
414+
async addAuthenticatorFactor(params: Pick<LoginParams, "appState">): Promise<boolean> {
436415
if (!this.sessionId) throw LoginError.userNotLoggedIn();
437416

438-
// in case of redirect mode, redirect url will be dapp specified
439-
// in case of popup mode, redirect url will be sdk specified
440-
const defaultParams: BaseRedirectParams = {
441-
redirectUrl: this.options.redirectUrl,
442-
};
443-
444417
const dataObject: AuthSessionConfig = {
445418
actionType: AUTH_ACTIONS.ADD_AUTHENTICATOR_FACTOR,
446419
options: this.options,
447420
params: {
448-
...defaultParams,
449421
...params,
450422
loginProvider: LOGIN_PROVIDER.AUTHENTICATOR,
451423
},
@@ -458,20 +430,13 @@ export class Auth {
458430
return true;
459431
}
460432

461-
async addPasskeyFactor(params: Partial<BaseRedirectParams>): Promise<boolean> {
433+
async addPasskeyFactor(params: Pick<LoginParams, "appState">): Promise<boolean> {
462434
if (!this.sessionId) throw LoginError.userNotLoggedIn();
463435

464-
// in case of redirect mode, redirect url will be dapp specified
465-
// in case of popup mode, redirect url will be sdk specified
466-
const defaultParams: BaseRedirectParams = {
467-
redirectUrl: this.options.redirectUrl,
468-
};
469-
470436
const dataObject: AuthSessionConfig = {
471437
actionType: AUTH_ACTIONS.ADD_PASSKEY_FACTOR,
472438
options: this.options,
473439
params: {
474-
...defaultParams,
475440
...params,
476441
loginProvider: LOGIN_PROVIDER.PASSKEYS,
477442
},

src/utils/interfaces.ts

Lines changed: 19 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,6 @@ export type UserData = {
1515
[P in string]: string;
1616
};
1717

18-
export type BaseRedirectParams = {
19-
/**
20-
* redirectUrl is the dapp's url where user will be redirected after login.
21-
*
22-
* @remarks
23-
* Register this url at {@link "https://dashboard.web3auth.io"| developer dashboard}
24-
* else initialization will give error.
25-
*/
26-
redirectUrl?: string;
27-
/**
28-
* Any custom state you wish to pass along. This will be returned to you post redirect.
29-
* Use this to store data that you want to be available to the dapp after login.
30-
*/
31-
appState?: string;
32-
};
33-
3418
/**
3519
* {@label loginProviderType}
3620
*/
@@ -128,20 +112,6 @@ export interface ExtraLoginOptions extends BaseLoginOptions {
128112
* The Client ID found on your Application settings page
129113
*/
130114
client_id?: string;
131-
/**
132-
* The default URL where Auth0 will redirect your browser to with
133-
* the authentication result. It must be whitelisted in
134-
* the "Allowed Callback URLs" field in your Auth0 Application's
135-
* settings. If not provided here, it should be provided in the other
136-
* methods that provide authentication.
137-
*/
138-
redirect_uri?: string;
139-
/**
140-
* The value in seconds used to account for clock skew in JWT expirations.
141-
* Typically, this value is no more than a minute or two at maximum.
142-
* Defaults to 60s.
143-
*/
144-
leeway?: number;
145115
/**
146116
* The field in jwt token which maps to verifier id
147117
*/
@@ -153,7 +123,13 @@ export interface ExtraLoginOptions extends BaseLoginOptions {
153123
isVerifierIdCaseSensitive?: boolean;
154124
}
155125

156-
export type LoginParams = BaseRedirectParams & {
126+
export type LoginParams = {
127+
/**
128+
* Any custom state you wish to pass along. This will be returned to you post redirect.
129+
* Use this to store data that you want to be available to the dapp after login.
130+
*/
131+
appState?: string;
132+
157133
/**
158134
* loginProvider sets the oauth login method to be used.
159135
* You can use any of the valid loginProvider from the supported list.
@@ -342,21 +318,15 @@ export type WhiteLabelData = {
342318
/**
343319
* Language specific link for terms and conditions on torus-website. See (examples/vue-app) to configure
344320
* e.g.
345-
* tncLink: {
346-
* en: "http://example.com/tnc/en",
347-
* ja: "http://example.com/tnc/ja",
348-
* }
321+
* tncLink: http://example.com/tnc
349322
*/
350-
tncLink?: Partial<Record<LANGUAGE_TYPE, string>>;
323+
tncLink?: string;
351324
/**
352325
* Language specific link for privacy policy on torus-website. See (examples/vue-app) to configure
353326
* e.g.
354-
* privacyPolicy: {
355-
* en: "http://example.com/tnc/en",
356-
* ja: "http://example.com/tnc/ja",
357-
* }
327+
* privacyPolicy: http://example.com/privacy
358328
*/
359-
privacyPolicy?: Partial<Record<LANGUAGE_TYPE, string>>;
329+
privacyPolicy?: string;
360330
};
361331

362332
export type TypeOfLogin =
@@ -424,58 +394,12 @@ export type LoginConfigItem = {
424394
*/
425395
typeOfLogin: TypeOfLogin;
426396

427-
/**
428-
* Display Name. If not provided, we use the default for auth app
429-
*/
430-
name?: string;
431-
432-
/**
433-
* Description for button. If provided, it renders as a full length button. else, icon button
434-
*/
435-
description?: string;
436-
437397
/**
438398
* Custom client_id. If not provided, we use the default for auth app
439399
*/
440400
clientId?: string;
441401

442402
verifierSubIdentifier?: string;
443-
444-
/**
445-
* Logo to be shown on mouse hover. If not provided, we use the default for auth app
446-
*/
447-
logoHover?: string;
448-
449-
/**
450-
* Logo to be shown on dark background (dark theme). If not provided, we use the default for auth app
451-
*/
452-
logoLight?: string;
453-
454-
/**
455-
* Logo to be shown on light background (light theme). If not provided, we use the default for auth app
456-
*/
457-
logoDark?: string;
458-
459-
/**
460-
* Show login button on the main list
461-
*/
462-
mainOption?: boolean;
463-
464-
/**
465-
* Whether to show the login button on modal or not
466-
*/
467-
showOnModal?: boolean;
468-
469-
/**
470-
* Whether to show the login button on desktop
471-
*/
472-
showOnDesktop?: boolean;
473-
474-
/**
475-
* Whether to show the login button on mobile
476-
*/
477-
showOnMobile?: boolean;
478-
479403
/**
480404
* If we are using social logins as a backup factor,
481405
* then this option will be used to show the type of social login
@@ -644,17 +568,6 @@ export type AuthOptions = {
644568
*/
645569
loginConfig?: LoginConfig;
646570

647-
/**
648-
* webauthnTransport enables you to configure the transport type user can use
649-
* for saving their share.
650-
*
651-
* @defaultValue ["internal"]
652-
*
653-
* @remarks
654-
* This is only available for v1 users.
655-
*/
656-
webauthnTransports?: AuthenticatorTransport[];
657-
658571
/**
659572
* sdkUrl is for internal development use only and is used to override the
660573
* `network` parameter.
@@ -693,7 +606,14 @@ export type AuthOptions = {
693606
*
694607
* @defaultValue "local"
695608
*/
696-
storageKey?: "session" | "local";
609+
storage?: "session" | "local";
610+
611+
/**
612+
* sessionKey is the key to be used to override the default key used to store session data.
613+
*
614+
* @defaultValue auth_store
615+
*/
616+
sessionKey?: string;
697617

698618
/**
699619
* How long should a login session last at a minimum in seconds

0 commit comments

Comments
 (0)