Skip to content

Commit

Permalink
fix getAuthIdByaAuthMethod
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Long committed Dec 14, 2023
1 parent ca87b6d commit a18a27e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 17 deletions.
10 changes: 6 additions & 4 deletions packages/lit-auth-client/src/lib/lit-auth-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -238,7 +240,7 @@ export class LitAuthClient {

private log(... args: any) {
if (this.debug) {
log(... args);
getLoggerbyId('auth-client').debug(...args);
}
}
}
53 changes: 40 additions & 13 deletions packages/lit-auth-client/src/lib/providers/StytchAuthFactorOtp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
Expand All @@ -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<string>} - Auth method id
*/
public async getAuthMethodId(authMethod: AuthMethod): Promise<string> {
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.
Expand All @@ -87,14 +101,31 @@ export default class StytchAuthFactorOtpProvider<
*
* @returns {Promise<string>} - Auth method id
*/
public async getAuthMethodId(
public static async authMethodId(
authMethod: AuthMethod,
options?: any
): Promise<string> {
return new Promise<string>((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) {
Expand All @@ -103,7 +134,7 @@ export default class StytchAuthFactorOtpProvider<
});
}

private _resolveAuthFactor(factor: T): {
private static _resolveAuthFactor(factor: FactorParser): {
parser: Function;
authMethodType: AuthMethodType;
} {
Expand All @@ -129,18 +160,14 @@ export default class StytchAuthFactorOtpProvider<
authMethodType: AuthMethodType.StytchTotpFactorOtp,
};
}

throw new Error(
'Unable to determine factor, are you using one of the supported factor types?'
);
}

/**
*
* @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');
Expand Down

0 comments on commit a18a27e

Please sign in to comment.