Skip to content

Commit

Permalink
Rename some functions to more align with the actual functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
deepjyoti30-st committed Dec 18, 2024
1 parent e38fff8 commit dbe0aa2
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 44 deletions.
48 changes: 26 additions & 22 deletions lib/ts/recipe/webauthn/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,42 +306,42 @@ export default class RecipeWrapper {
}

/**
* Register user with the passed options by using native webauthn functions.
* Register credential with the passed options by using native webauthn functions.
*
* It uses `@simplewebauthn/browser` to make the webauthn calls.
*
* @param registrationOptions Options to pass for the registration.
*
* @returns `{ status: "OK", ...}` if successful along with registration response received
*/
static registerUser(input: { registrationOptions: RegistrationOptions }): Promise<
static registerCredential(input: { registrationOptions: RegistrationOptions }): Promise<
| {
status: "OK";
registrationResponse: RegistrationResponseJSON;
}
| { status: "AUTHENTICATOR_ALREADY_REGISTERED" }
| { status: "FAILED_TO_REGISTER_USER"; error: any }
> {
return Recipe.getInstanceOrThrow().recipeImplementation.registerUser(input);
return Recipe.getInstanceOrThrow().recipeImplementation.registerCredential(input);
}

/**
* Authenticate the user with the passed options by using native webauthn functions.
* Authenticate the credential with the passed options by using native webauthn functions.
*
* It uses `@simplewebauthn/browser` to make the webauthn calls.
*
* @param authenticationOptions Options to pass for the authentication.
*
* @returns `{ status: "OK", ...}` if successful along with authentication response received
*/
static authenticateUser(input: { authenticationOptions: AuthenticationOptions }): Promise<
static authenticateCredential(input: { authenticationOptions: AuthenticationOptions }): Promise<
| {
status: "OK";
authenticationResponse: AuthenticationResponseJSON;
}
| { status: "FAILED_TO_AUTHENTICATE_USER"; error: any }
> {
return Recipe.getInstanceOrThrow().recipeImplementation.authenticateUser(input);
return Recipe.getInstanceOrThrow().recipeImplementation.authenticateCredential(input);
}

/**
Expand All @@ -357,7 +357,11 @@ export default class RecipeWrapper {
*
* @returns `{ status: "OK", ...}` if successful along a description of the user details (id, etc.) and email
*/
static registerUserWithSignUp(input: { email: string; options?: RecipeFunctionOptions; userContext: any }): Promise<
static registerCredentialWithSignUp(input: {
email: string;
options?: RecipeFunctionOptions;
userContext: any;
}): Promise<
| {
status: "OK";
user: User;
Expand Down Expand Up @@ -386,7 +390,7 @@ export default class RecipeWrapper {
| { status: "AUTHENTICATOR_ALREADY_REGISTERED" }
| { status: "FAILED_TO_REGISTER_USER"; error: any }
> {
return Recipe.getInstanceOrThrow().recipeImplementation.registerUserWithSignUp({
return Recipe.getInstanceOrThrow().recipeImplementation.registerCredentialWithSignUp({
...input,
userContext: input?.userContext,
});
Expand All @@ -405,7 +409,7 @@ export default class RecipeWrapper {
*
* @returns `{ status: "OK", ...}` if successful along a description of the user details (id, etc.) and email
*/
static authenticateUserWithSignIn(input: {
static authenticateCredentialWithSignIn(input: {
email: string;
options?: RecipeFunctionOptions;
userContext: any;
Expand All @@ -428,7 +432,7 @@ export default class RecipeWrapper {
| { status: "FAILED_TO_AUTHENTICATE_USER"; error: any }
| GeneralErrorResponse
> {
return Recipe.getInstanceOrThrow().recipeImplementation.authenticateUserWithSignIn({
return Recipe.getInstanceOrThrow().recipeImplementation.authenticateCredentialWithSignIn({
...input,
userContext: input?.userContext,
});
Expand All @@ -447,7 +451,7 @@ export default class RecipeWrapper {
*
* @returns `{ status: "OK", ...}` if successful along a description of the user details (id, etc.) and email
*/
static registerUserWithRecoverAccount(input: {
static registerCredentialWithRecoverAccount(input: {
recoverAccountToken: string;
options?: RecipeFunctionOptions;
userContext: any;
Expand Down Expand Up @@ -475,7 +479,7 @@ export default class RecipeWrapper {
| { status: "AUTHENTICATOR_ALREADY_REGISTERED" }
| { status: "FAILED_TO_REGISTER_USER"; error: any }
> {
return Recipe.getInstanceOrThrow().recipeImplementation.registerUserWithRecoverAccount({
return Recipe.getInstanceOrThrow().recipeImplementation.registerCredentialWithRecoverAccount({
...input,
userContext: input?.userContext,
});
Expand All @@ -490,11 +494,11 @@ const signIn = RecipeWrapper.signIn;
const getEmailExists = RecipeWrapper.getEmailExists;
const generateRecoverAccountToken = RecipeWrapper.generateRecoverAccountToken;
const recoverAccount = RecipeWrapper.recoverAccount;
const registerUserWithSignUp = RecipeWrapper.registerUserWithSignUp;
const authenticateUserWithSignIn = RecipeWrapper.authenticateUserWithSignIn;
const registerUserWithRecoverAccount = RecipeWrapper.registerUserWithRecoverAccount;
const registerUser = RecipeWrapper.registerUser;
const authenticateUser = RecipeWrapper.authenticateUser;
const registerCredentialWithSignUp = RecipeWrapper.registerCredentialWithSignUp;
const authenticateCredentialWithSignIn = RecipeWrapper.authenticateCredentialWithSignIn;
const registerCredentialWithRecoverAccount = RecipeWrapper.registerCredentialWithRecoverAccount;
const registerCredential = RecipeWrapper.registerCredential;
const authenticateCredential = RecipeWrapper.authenticateCredential;

export {
init,
Expand All @@ -505,9 +509,9 @@ export {
getEmailExists,
generateRecoverAccountToken,
recoverAccount,
registerUserWithSignUp,
authenticateUserWithSignIn,
registerUserWithRecoverAccount,
registerUser,
authenticateUser,
registerCredentialWithSignUp,
authenticateCredentialWithSignIn,
registerCredentialWithRecoverAccount,
registerCredential,
authenticateCredential,
};
36 changes: 19 additions & 17 deletions lib/ts/recipe/webauthn/recipeImplementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ export default function getRecipeImplementation(
fetchResponse,
};
},
registerUser: async function ({ registrationOptions }) {
registerCredential: async function ({ registrationOptions }) {
let registrationResponse: RegistrationResponseJSON;
try {
registrationResponse = await startRegistration({ optionsJSON: registrationOptions });
Expand All @@ -374,7 +374,7 @@ export default function getRecipeImplementation(
registrationResponse,
};
},
registerUserWithSignUp: async function ({ email, options, userContext }) {
registerCredentialWithSignUp: async function ({ email, options, userContext }) {
// Get the registration options by using the passed email ID.
const registrationOptions = await this.getRegisterOptions({ options, userContext, email });
if (registrationOptions?.status !== "OK") {
Expand All @@ -391,21 +391,21 @@ export default function getRecipeImplementation(
}

// We should have received a valid registration options response.
const registerUserResponse = await this.registerUser({ registrationOptions });
if (registerUserResponse.status !== "OK") {
return registerUserResponse;
const registerCredentialResponse = await this.registerCredential({ registrationOptions });
if (registerCredentialResponse.status !== "OK") {
return registerCredentialResponse;
}

// We should have a valid registration response for the passed credentials
// and we are good to go ahead and verify them.
return await this.signUp({
webauthnGeneratedOptionsId: registrationOptions.webauthnGeneratedOptionsId,
credential: registerUserResponse.registrationResponse,
credential: registerCredentialResponse.registrationResponse,
options,
userContext,
});
},
authenticateUser: async function ({ authenticationOptions }) {
authenticateCredential: async function ({ authenticationOptions }) {
let authenticationResponse: AuthenticationResponseJSON;
try {
authenticationResponse = await startAuthentication({ optionsJSON: authenticationOptions });
Expand All @@ -421,7 +421,7 @@ export default function getRecipeImplementation(
authenticationResponse: authenticationResponse,
};
},
authenticateUserWithSignIn: async function ({ email, options, userContext }) {
authenticateCredentialWithSignIn: async function ({ email, options, userContext }) {
// Make a call to get the sign in options using the entered email ID.
const signInOptions = await this.getSignInOptions({ email, options, userContext });
if (signInOptions?.status !== "OK") {
Expand All @@ -430,21 +430,23 @@ export default function getRecipeImplementation(
}

// We should have the options ready and are good to start the authentication
const authenticateUserResponse = await this.authenticateUser({ authenticationOptions: signInOptions });
if (authenticateUserResponse.status !== "OK") {
return authenticateUserResponse;
const authenticateCredentialResponse = await this.authenticateCredential({
authenticationOptions: signInOptions,
});
if (authenticateCredentialResponse.status !== "OK") {
return authenticateCredentialResponse;
}

// We should have a valid authentication response at this point so we can
// go ahead and sign in the user.
return await this.signIn({
webauthnGeneratedOptionsId: signInOptions.webauthnGeneratedOptionsId,
credential: authenticateUserResponse.authenticationResponse,
credential: authenticateCredentialResponse.authenticationResponse,
options: options,
userContext: userContext,
});
},
registerUserWithRecoverAccount: async function ({ recoverAccountToken, options, userContext }) {
registerCredentialWithRecoverAccount: async function ({ recoverAccountToken, options, userContext }) {
// Get the registration options based on the recoverAccountToken and
// register the device against the user.
const registrationOptions = await this.getRegisterOptions({ options, userContext, recoverAccountToken });
Expand All @@ -462,15 +464,15 @@ export default function getRecipeImplementation(
}

// We should have received a valid registration options response.
const registerUserResponse = await this.registerUser({ registrationOptions });
if (registerUserResponse.status !== "OK") {
return registerUserResponse;
const registerCredentialResponse = await this.registerCredential({ registrationOptions });
if (registerCredentialResponse.status !== "OK") {
return registerCredentialResponse;
}

return await this.recoverAccount({
token: recoverAccountToken,
webauthnGeneratedOptionsId: registrationOptions.webauthnGeneratedOptionsId,
credential: registerUserResponse.registrationResponse,
credential: registerCredentialResponse.registrationResponse,
options,
userContext,
});
Expand Down
14 changes: 9 additions & 5 deletions lib/ts/recipe/webauthn/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,22 +229,26 @@ export type RecipeInterface = {
| { status: "INVALID_GENERATED_OPTIONS_ERROR"; fetchResponse: Response }
| { status: "INVALID_AUTHENTICATOR_ERROR"; reason: string; fetchResponse: Response }
>;
registerUser: (input: { registrationOptions: RegistrationOptions }) => Promise<
registerCredential: (input: { registrationOptions: RegistrationOptions }) => Promise<
| {
status: "OK";
registrationResponse: RegistrationResponseJSON;
}
| { status: "AUTHENTICATOR_ALREADY_REGISTERED" }
| { status: "FAILED_TO_REGISTER_USER"; error: any }
>;
authenticateUser: (input: { authenticationOptions: AuthenticationOptions }) => Promise<
authenticateCredential: (input: { authenticationOptions: AuthenticationOptions }) => Promise<
| {
status: "OK";
authenticationResponse: AuthenticationResponseJSON;
}
| { status: "FAILED_TO_AUTHENTICATE_USER"; error: any }
>;
registerUserWithSignUp: (input: { email: string; options?: RecipeFunctionOptions; userContext: any }) => Promise<
registerCredentialWithSignUp: (input: {
email: string;
options?: RecipeFunctionOptions;
userContext: any;
}) => Promise<
| {
status: "OK";
user: User;
Expand Down Expand Up @@ -272,7 +276,7 @@ export type RecipeInterface = {
| { status: "AUTHENTICATOR_ALREADY_REGISTERED" }
| { status: "FAILED_TO_REGISTER_USER"; error: any }
>;
authenticateUserWithSignIn: (input: {
authenticateCredentialWithSignIn: (input: {
email: string;
options?: RecipeFunctionOptions;
userContext: any;
Expand All @@ -295,7 +299,7 @@ export type RecipeInterface = {
| { status: "FAILED_TO_AUTHENTICATE_USER"; error: any }
| GeneralErrorResponse
>;
registerUserWithRecoverAccount: (input: {
registerCredentialWithRecoverAccount: (input: {
recoverAccountToken: string;
options?: RecipeFunctionOptions;
userContext: any;
Expand Down

0 comments on commit dbe0aa2

Please sign in to comment.