Skip to content
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

feat: hide keypass button if not configured #703

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/controllers/user/signin-signup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { createAuthenticatedSession } from "../../managers/session/authenticated
import {
getAndRemoveLoginHintFromUnauthenticatedSession,
getEmailFromUnauthenticatedSession,
getPartialUserFromUnauthenticatedSession,
setEmailInUnauthenticatedSession,
setPartialUserFromUnauthenticatedSession,
updatePartialUserFromUnauthenticatedSession,
Expand Down Expand Up @@ -84,11 +85,13 @@ export const postStartSignInController = async (
email,
userExists,
hasAPassword,
hasWebauthnConfigured,
needsInclusionconnectWelcomePage,
} = await startLogin(login);
setPartialUserFromUnauthenticatedSession(req, {
email,
needsInclusionconnectWelcomePage,
hasWebauthnConfigured,
});

if (needsInclusionconnectWelcomePage) {
Expand Down Expand Up @@ -158,11 +161,15 @@ export const getSignInController = async (
next: NextFunction,
) => {
try {
const { email, hasWebauthnConfigured } =
getPartialUserFromUnauthenticatedSession(req);

return res.render("user/sign-in", {
pageTitle: "Accéder au compte",
notifications: await getNotificationsFromRequest(req),
csrfToken: csrfToken(req),
email: getEmailFromUnauthenticatedSession(req),
email,
showPasskeySection: hasWebauthnConfigured,
});
} catch (error) {
next(error);
Expand Down
6 changes: 3 additions & 3 deletions src/controllers/webauthn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export const getSignInWithPasskeyController = async (
}
};

export const getGenerateAuthenticationOptions =
export const getGenerateAuthenticationOptionsControllerFactory =
(isSecondFactorAuthentication: boolean) =>
async (req: Request, res: Response, next: NextFunction) => {
try {
Expand All @@ -168,10 +168,10 @@ export const getGenerateAuthenticationOptions =
};

export const getGenerateAuthenticationOptionsForFirstFactorController =
getGenerateAuthenticationOptions(false);
getGenerateAuthenticationOptionsControllerFactory(false);

export const getGenerateAuthenticationOptionsForSecondFactorController =
getGenerateAuthenticationOptions(true);
getGenerateAuthenticationOptionsControllerFactory(true);

export const postVerifyAuthenticationController =
(isSecondFactorVerification: boolean) =>
Expand Down
9 changes: 8 additions & 1 deletion src/managers/session/unauthenticated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,25 @@ export const getPartialUserFromUnauthenticatedSession = (req: Request) => {
email: req.session.email,
needsInclusionconnectWelcomePage:
req.session.needsInclusionconnectWelcomePage,
hasWebauthnConfigured: req.session.hasWebauthnConfigured,
};
};
export const setPartialUserFromUnauthenticatedSession = (
req: Request,
{
email,
needsInclusionconnectWelcomePage,
}: { email: string; needsInclusionconnectWelcomePage: boolean },
hasWebauthnConfigured,
}: {
email: string;
needsInclusionconnectWelcomePage: boolean;
hasWebauthnConfigured: boolean;
},
) => {
req.session.email = email;
req.session.needsInclusionconnectWelcomePage =
needsInclusionconnectWelcomePage;
req.session.hasWebauthnConfigured = hasWebauthnConfigured;
};
export const updatePartialUserFromUnauthenticatedSession = async (
req: Request,
Expand Down
4 changes: 4 additions & 0 deletions src/managers/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ import {
isPasswordSecure,
validatePassword,
} from "../services/security";
import { isWebauthnConfiguredForUser } from "./webauthn";

export const startLogin = async (
email: string,
): Promise<{
email: string;
userExists: boolean;
hasAPassword: boolean;
hasWebauthnConfigured: boolean;
needsInclusionconnectWelcomePage: boolean;
}> => {
const user = await findByEmail(email);
Expand All @@ -55,6 +57,7 @@ export const startLogin = async (
email,
userExists: true,
hasAPassword: !!user.encrypted_password,
hasWebauthnConfigured: await isWebauthnConfiguredForUser(user.id),
needsInclusionconnectWelcomePage:
user?.needs_inclusionconnect_welcome_page,
};
Expand All @@ -75,6 +78,7 @@ export const startLogin = async (
email,
userExists: false,
hasAPassword: false,
hasWebauthnConfigured: false,
needsInclusionconnectWelcomePage: false,
};
};
Expand Down
1 change: 1 addition & 0 deletions src/types/express-session.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export interface UnauthenticatedSessionData {
email?: string;
loginHint?: string;
needsInclusionconnectWelcomePage?: boolean;
hasWebauthnConfigured?: boolean;
interactionId?: string;
mustReturnOneOrganizationInPayload?: boolean;
mustUse2FA?: boolean;
Expand Down
20 changes: 11 additions & 9 deletions src/views/user/sign-in.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@
<a href="/users/reset-password" class="fr-link">Mot de passe oublié ?</a>
</p>
</form>
<% if (showPasskeySection) { %>

<p class="fr-hr-or">ou</p>

<a
class="fr-btn fr-mb-2w btn--fullwidth fr-btn--secondary fr-btn--icon-left fr-icon-lock-unlock-line"
href="/users/sign-in-with-passkey"
>
Se connecter avec une clé d’accès
</a>
<% } %>

<p class="fr-hr-or">ou</p>

Expand All @@ -71,15 +82,6 @@
</button>
</form>

<p class="fr-hr-or">ou</p>

<a
class="fr-btn fr-mb-2w btn--fullwidth fr-btn--secondary fr-btn--icon-left fr-icon-lock-unlock-line"
href="/users/sign-in-with-passkey"
>
Se connecter avec une clé d’accès
</a>

<div class="card-button-container fr-mt-2w">
<span>
<button
Expand Down