diff --git a/packages/lit-auth-client/src/lib/lit-auth-client.ts b/packages/lit-auth-client/src/lib/lit-auth-client.ts index 7db18b6e6d..22709d2c58 100644 --- a/packages/lit-auth-client/src/lib/lit-auth-client.ts +++ b/packages/lit-auth-client/src/lib/lit-auth-client.ts @@ -18,9 +18,8 @@ import EthWalletProvider from './providers/EthWalletProvider'; import WebAuthnProvider from './providers/WebAuthnProvider'; import { StytchOtpProvider } from './providers/StytchOtpProvider'; import AppleProvider from './providers/AppleProvider'; -import StytchEmailOtpProvider from './providers/StytchAuthFactorOtp'; import StytchAuthFactorOtpProvider from './providers/StytchAuthFactorOtp'; -import { bootstrapLogManager, log } from '@lit-protocol/misc'; +import { bootstrapLogManager, getLoggerbyId, log } from '@lit-protocol/misc'; /** * Class that handles authentication through Lit login @@ -225,7 +224,10 @@ export class LitAuthClient { authId = await GoogleProvider.authMethodId(authMethod); break; case AuthMethodType.StytchOtp: - authId = await GoogleProvider.authMethodId(authMethod); + authId = await StytchOtpProvider.authMethodId(authMethod); + break; + case AuthMethodType.StytchEmailFactorOtp: + authId = await StytchAuthFactorOtpProvider.authMethodId(authMethod); break; default: throw new Error( @@ -238,7 +240,7 @@ export class LitAuthClient { private log(... args: any) { if (this.debug) { - log(... args); + getLoggerbyId('auth-client').debug(...args); } } } diff --git a/packages/lit-auth-client/src/lib/providers/StytchAuthFactorOtp.ts b/packages/lit-auth-client/src/lib/providers/StytchAuthFactorOtp.ts index 4555cee158..5cc45f2f3e 100644 --- a/packages/lit-auth-client/src/lib/providers/StytchAuthFactorOtp.ts +++ b/packages/lit-auth-client/src/lib/providers/StytchAuthFactorOtp.ts @@ -21,8 +21,9 @@ export default class StytchAuthFactorOtpProvider< T extends FactorParser > extends BaseProvider { private _params: StytchOtpProviderOptions; - private _provider: string = 'https://stytch.com/session'; private _factor: T; + private static _provider: string = 'https://stytch.com/session'; + constructor( params: BaseProviderOptions, config: StytchOtpProviderOptions, @@ -62,11 +63,11 @@ export default class StytchAuthFactorOtpProvider< ); } - const parsedToken: StytchToken = this._parseJWT(accessToken); - const factorParser = this._resolveAuthFactor(this._factor); + const parsedToken: StytchToken = StytchAuthFactorOtpProvider._parseJWT(accessToken); + const factorParser = StytchAuthFactorOtpProvider._resolveAuthFactor(this._factor); try { - factorParser.parser(parsedToken, this._provider); + factorParser.parser(parsedToken, StytchAuthFactorOtpProvider._provider); } catch (e) { reject(e); } @@ -78,6 +79,19 @@ export default class StytchAuthFactorOtpProvider< }); } + + /** + * Get auth method id that can be used to look up and interact with + * PKPs associated with the given auth method + * + * @param {AuthMethod} authMethod - Auth method object + * + * @returns {Promise} - Auth method id + */ + public async getAuthMethodId(authMethod: AuthMethod): Promise { + return StytchAuthFactorOtpProvider.authMethodId(authMethod); + } + /** * Get auth method id that can be used to look up and interact with * PKPs associated with the given auth method. @@ -87,14 +101,31 @@ export default class StytchAuthFactorOtpProvider< * * @returns {Promise} - Auth method id */ - public async getAuthMethodId( + public static async authMethodId( authMethod: AuthMethod, options?: any ): Promise { return new Promise((resolve, reject) => { const accessToken = authMethod.accessToken; - const parsedToken: StytchToken = this._parseJWT(accessToken); - const factorParser = this._resolveAuthFactor(this._factor).parser; + const parsedToken: StytchToken = StytchAuthFactorOtpProvider._parseJWT(accessToken); + let factor: FactorParser = 'email'; + switch(authMethod.authMethodType) { + case AuthMethodType.StytchEmailFactorOtp: + factor = 'email'; + break; + case AuthMethodType.StytchSmsFactorOtp: + factor = 'sms'; + break; + case AuthMethodType.StytchWhatsAppFactorOtp: + factor = 'whatsApp'; + break; + case AuthMethodType.StytchTotpFactorOtp: + factor = 'totp'; + break; + default: + throw new Error("Unsupport stytch auth type"); + } + const factorParser = this._resolveAuthFactor(factor).parser; try { resolve(factorParser(parsedToken, this._provider)); } catch(e) { @@ -103,7 +134,7 @@ export default class StytchAuthFactorOtpProvider< }); } - private _resolveAuthFactor(factor: T): { + private static _resolveAuthFactor(factor: FactorParser): { parser: Function; authMethodType: AuthMethodType; } { @@ -129,10 +160,6 @@ export default class StytchAuthFactorOtpProvider< authMethodType: AuthMethodType.StytchTotpFactorOtp, }; } - - throw new Error( - 'Unable to determine factor, are you using one of the supported factor types?' - ); } /** @@ -140,7 +167,7 @@ export default class StytchAuthFactorOtpProvider< * @param jwt token to parse * @returns {string}- userId contained within the token message */ - private _parseJWT(jwt: string): StytchToken { + private static _parseJWT(jwt: string): StytchToken { const parts = jwt.split('.'); if (parts.length !== 3) { throw new Error('Invalid token length');