Skip to content

Commit

Permalink
feat: login_hint from sp does not trigger login prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
rdubigny committed Sep 20, 2024
1 parent 436aafa commit f2defb0
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 11 deletions.
6 changes: 4 additions & 2 deletions cypress/e2e/signin_from_agentconnect_client/fixtures.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ INSERT INTO users
(id, email, email_verified, email_verified_at, encrypted_password, created_at, updated_at, given_name, family_name,
phone_number, job)
VALUES
(1, '[email protected]', true, CURRENT_TIMESTAMP, '$2a$10$kzY3LINL6..50Fy9shWCcuNlRfYq0ft5lS.KCcJ5PzrhlWfKK4NIO', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 'Jean', 'Jean', '0123456789', 'Sbire');
(1, '[email protected]', true, CURRENT_TIMESTAMP, '$2a$10$kzY3LINL6..50Fy9shWCcuNlRfYq0ft5lS.KCcJ5PzrhlWfKK4NIO', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 'Jean', 'Jean', '0123456789', 'Sbire'),
(2, '[email protected]', true, CURRENT_TIMESTAMP, '$2a$10$kzY3LINL6..50Fy9shWCcuNlRfYq0ft5lS.KCcJ5PzrhlWfKK4NIO', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 'Jean', 'Jean', '0123456789', 'Sbire');


INSERT INTO organizations
Expand All @@ -13,7 +14,8 @@ VALUES
INSERT INTO users_organizations
(user_id, organization_id, is_external, verification_type, has_been_greeted)
VALUES
(1, 1, false, 'domain', true);
(1, 1, false, 'domain', true),
(2, 1, false, 'domain', true);

INSERT INTO oidc_clients
(client_name, client_id, client_secret, redirect_uris,
Expand Down
29 changes: 28 additions & 1 deletion cypress/e2e/signin_from_agentconnect_client/index.cy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//

describe("sign-in from agentconnect client", () => {
it("should sign-in", function () {
it("should sign-in", () => {
cy.visit("http://localhost:4001");
cy.get("button.moncomptepro-button").click();

Expand All @@ -14,4 +14,31 @@ describe("sign-in from agentconnect client", () => {
cy.contains("[email protected]");
cy.contains("21340126800130");
});

it("should not prompt for password if a session is already opened", () => {
cy.visit("/");
cy.login("[email protected]");

cy.visit("http://localhost:4001");
cy.get("button.moncomptepro-button").click();

cy.contains("moncomptepro-agentconnect-client");
cy.contains("[email protected]");
})

it("login_hint should take precedence over existing session", () => {
cy.visit("/");
cy.login("[email protected]");

cy.visit("http://localhost:4001");
cy.get("button.moncomptepro-button").click();

cy.get('[name="password"]').type("password123");
cy.get('[action="/users/sign-in"] [type="submit"]')
.contains("S’identifier")
.click();

cy.contains("moncomptepro-agentconnect-client");
cy.contains("[email protected]");
})
});
11 changes: 10 additions & 1 deletion cypress/e2e/signin_from_standard_client/index.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,14 @@ describe("sign-in from standard client", () => {
cy.contains("Commune de lamalou-les-bains - Mairie");
});

it("should prompt for organization selection", function () {});
it("should not prompt for password if a session is already opened", () => {
cy.visit("/");
cy.login("[email protected]");

cy.visit("http://localhost:4000");
cy.get("button.moncomptepro-button").click();

cy.contains("moncomptepro-standard-client");
cy.contains("[email protected]");
})
});
2 changes: 1 addition & 1 deletion src/config/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class WebauthnAuthenticationFailedError extends Error {}

export class UserNotLoggedInError extends Error {}

export class NoEmailFoundInLoggedOutSessionError extends Error {}
export class NoEmailFoundInUnauthenticatedSessionError extends Error {}

export class InvalidTotpTokenError extends Error {}

Expand Down
15 changes: 13 additions & 2 deletions src/controllers/interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ENABLE_FIXED_ACR } from "../config/env";
import {
getSessionStandardizedAuthenticationMethodsReferences,
getUserFromAuthenticatedSession,
isWithinAuthenticatedSession,
isWithinTwoFactorAuthenticatedSession,
} from "../managers/session/authenticated";
import { setEmailInUnauthenticatedSession } from "../managers/session/unauthenticated";
Expand Down Expand Up @@ -41,8 +42,18 @@ export const interactionStartControllerFactory =

if (prompt.name === "login" || prompt.name === "choose_organization") {
if (login_hint) {
req.body.login = login_hint;
return postStartSignInController(req, res, next);
const isAuthenticated = isWithinAuthenticatedSession(req.session);
const authenticatedUserEmail = isAuthenticated
? getUserFromAuthenticatedSession(req).email
: null;
const isDifferentEmail = authenticatedUserEmail !== login_hint;

if (!isAuthenticated || (isAuthenticated && isDifferentEmail)) {
setEmailInUnauthenticatedSession(req, login_hint);
req.body.login = login_hint;

return postStartSignInController(req, res, next);
}
}

return res.redirect(`/interaction/${interactionId}/login`);
Expand Down
4 changes: 2 additions & 2 deletions src/managers/session/unauthenticated.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Request } from "express";
import { isEmpty } from "lodash-es";
import { NoEmailFoundInLoggedOutSessionError } from "../../config/errors";
import { NoEmailFoundInUnauthenticatedSessionError } from "../../config/errors";
import { findByEmail, update } from "../../repositories/user";

export const getEmailFromUnauthenticatedSession = (req: Request) => {
Expand Down Expand Up @@ -42,7 +42,7 @@ export const updatePartialUserFromUnauthenticatedSession = async (
needs_inclusionconnect_welcome_page: boolean;
}> => {
if (!req.session.email) {
throw new NoEmailFoundInLoggedOutSessionError();
throw new NoEmailFoundInUnauthenticatedSessionError();
}

req.session.needsInclusionconnectWelcomePage =
Expand Down
4 changes: 2 additions & 2 deletions src/types/express-session.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface LoggedOutSessionData {
export interface UnauthenticatedSessionData {
email?: string;
needsInclusionconnectWelcomePage?: boolean;
interactionId?: string;
Expand All @@ -25,7 +25,7 @@ export interface AuthenticatedSessionData {
}

declare module "express-session" {
export interface SessionData extends LoggedOutSessionData {
export interface SessionData extends UnauthenticatedSessionData {
user?: User;
temporaryEncryptedTotpKey?: string;
amr?: AmrValue[];
Expand Down

0 comments on commit f2defb0

Please sign in to comment.