-
Notifications
You must be signed in to change notification settings - Fork 197
DO NOT MERGE: new auth method #1483
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,7 +40,7 @@ import { | |
type SessionManagerParams, | ||
} from "./session/manager.js"; | ||
import type { SessionManagerEvents } from "./session/types"; | ||
import type { AuthParams } from "./signer"; | ||
import type { AuthParams, AuthStepResult } from "./signer"; | ||
import { SolanaSigner } from "./solanaSigner.js"; | ||
import { | ||
AlchemySignerStatus, | ||
|
@@ -312,6 +312,34 @@ export abstract class BaseAlchemySigner<TClient extends BaseSignerClient> | |
} | ||
); | ||
|
||
authenticateStep: (params: AuthParams) => Promise<AuthStepResult> = | ||
SignerLogger.profiled( | ||
"BaseAlchemySigner.authenticateStep", | ||
async (params) => { | ||
const { type } = params; | ||
const result = (() => { | ||
switch (type) { | ||
case "email": | ||
return this.authenticateWithEmailNew(params); | ||
case "otp": | ||
return this.authenticateWithOtpNew(params); | ||
default: | ||
throw new Error("type not implemented"); | ||
|
||
} | ||
})(); | ||
|
||
this.trackAuthenticateType(params); | ||
|
||
return result.catch((error) => { | ||
this.store.setState({ | ||
error: toErrorInfo(error), | ||
status: AlchemySignerStatus.DISCONNECTED, | ||
}); | ||
throw error; | ||
}); | ||
} | ||
); | ||
|
||
private trackAuthenticateType = (params: AuthParams) => { | ||
const { type } = params; | ||
switch (type) { | ||
|
@@ -836,6 +864,53 @@ export abstract class BaseAlchemySigner<TClient extends BaseSignerClient> | |
return new SolanaSigner(this.inner); | ||
}; | ||
|
||
private authenticateWithEmailNew = async ( | ||
params: Extract<AuthParams, { type: "email" }> | ||
): Promise<AuthStepResult> => { | ||
if ("bundle" in params) { | ||
const user = await this.completeEmailAuth(params); | ||
return { | ||
status: AlchemySignerStatus.CONNECTED, | ||
user, | ||
}; | ||
} | ||
|
||
if (!("email" in params)) { | ||
|
||
throw new Error("Email is required"); | ||
} | ||
|
||
const { orgId, otpId, multiFactors, isNewUser } = | ||
await this.initOrCreateEmailUser( | ||
params.email, | ||
params.emailMode ?? "otp", | ||
params.multiFactors, | ||
params.redirectParams | ||
); | ||
|
||
const isMfaRequired = multiFactors ? multiFactors?.length > 0 : false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Very nit) No need for the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh yeah good catch |
||
|
||
this.sessionManager.setTemporarySession({ | ||
orgId, | ||
isNewUser, | ||
isMfaRequired, | ||
}); | ||
|
||
this.store.setState({ | ||
status: AlchemySignerStatus.AWAITING_EMAIL_AUTH, | ||
otpId, | ||
error: null, | ||
mfaStatus: { | ||
mfaRequired: isMfaRequired, | ||
mfaFactorId: multiFactors?.[0]?.multiFactorId, | ||
}, | ||
}); | ||
|
||
return { | ||
status: AlchemySignerStatus.AWAITING_EMAIL_AUTH, | ||
user: undefined, | ||
}; | ||
}; | ||
|
||
private authenticateWithEmail = async ( | ||
params: Extract<AuthParams, { type: "email" }> | ||
): Promise<User> => { | ||
|
@@ -993,6 +1068,61 @@ export abstract class BaseAlchemySigner<TClient extends BaseSignerClient> | |
return user; | ||
}; | ||
|
||
private authenticateWithOtpNew = async ( | ||
args: Extract<AuthParams, { type: "otp" }> | ||
): Promise<AuthStepResult> => { | ||
const tempSession = this.sessionManager.getTemporarySession(); | ||
const { orgId, isNewUser, isMfaRequired } = tempSession ?? {}; | ||
const { otpId } = this.store.getState(); | ||
if (!orgId) { | ||
throw new Error("orgId not found in session"); | ||
} | ||
if (!otpId) { | ||
throw new Error("otpId not found in session"); | ||
} | ||
if (isMfaRequired && !args.multiFactors) { | ||
throw new Error(`MFA is required.`); | ||
} | ||
|
||
const response = await this.inner.submitOtpCode({ | ||
orgId, | ||
otpId, | ||
otpCode: args.otpCode, | ||
expirationSeconds: this.getExpirationSeconds(), | ||
multiFactors: args.multiFactors, | ||
}); | ||
|
||
if (response.mfaRequired) { | ||
this.handleMfaRequired(response.encryptedPayload, response.multiFactors); | ||
|
||
return { | ||
status: AlchemySignerStatus.AWAITING_MFA_AUTH, | ||
user: undefined, | ||
multiFactors: response.multiFactors, | ||
}; | ||
} | ||
|
||
const user = await this.inner.completeAuthWithBundle({ | ||
bundle: response.bundle, | ||
orgId, | ||
connectedEventName: "connectedOtp", | ||
authenticatingType: "otp", | ||
}); | ||
|
||
this.emitNewUserEvent(isNewUser); | ||
if (tempSession) { | ||
this.sessionManager.setTemporarySession({ | ||
...tempSession, | ||
isNewUser: false, | ||
}); | ||
} | ||
|
||
return { | ||
user, | ||
status: AlchemySignerStatus.CONNECTED, | ||
}; | ||
}; | ||
|
||
private handleOauthReturn = ({ | ||
bundle, | ||
orgId, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit because this also starts and progresses the flow should the name be
handleAuth
,authenticateOrAdvance
, orprogressAuth
?Should we break up starting and progressing authentication into two calls? Such as
login
andverify
?