Andrey Antukh 0ea6d166f0
✨ Add a session origin model to auth events (#12856)
* 🐛 Return the full profile from the LDAP invitation branch

The invitation branch responded with only {:invitation-token token},
so the session start received a map without id or auth-backend. With
the new auth-origin vocabulary that map fails validation and the login
breaks in development and tests; in production the event would be
emitted without a channel.

Return the profile plus the token, like the regular branch, and cover
both branches with RPC-level tests.

Closes #12849

AI-assisted-by: deepseek-v4.1-flash

* 🐛 Mint an auth token in the OIDC callback with the invitation inside

The callback used the invitation JWE as the session token, so the
frontend landed directly on token verification and never ran the
regular session start: no signin event, no teams, no host hook.

Always mint an :auth token and carry the invitation in its claims.
Token verification returns the claims verbatim, so login-from-token
attaches the invitation to the profile and logged-in does the rest,
the same pattern the verify-email token already uses.

Closes #12849

AI-assisted-by: deepseek-v4.1-flash

* ✨ Keep the session origin in the telemetry shadow rows

The anonymisation filter drops every string unless it is on a short
allowlist. Add source and activation next to lang and auth-backend so
the shadow copy keeps them; the email and the full name are still
dropped and the email domain is still derived from the email.

The filter keeps its two bindings: the transducer discards everything
that is not a UUID, a boolean or a number, so the allowlist is applied
on the filtered map, never on the raw props.

Closes #12849

AI-assisted-by: deepseek-v4.1-flash

* ✨ Describe the signup outcome with activation, not with action

The three success branches of register-profile labelled a new account
as "login", "email-verification" or "accept-invitation", so the event
contradicted its own name and the value only lived in context, where
the analytics consumers never read it.

Add activation to the event props and drop action from those branches:
the name already says it is a signup, and a constant action would only
add noise. The retry branches keep their action in context because
there it distinguishes four different outcomes.

Closes #12849

AI-assisted-by: deepseek-v4.1-flash

* ✨ Describe the session origin in the signin event

The event carried the profile but never said why the session started:
the only field that tried was frontend metadata that never leaves the
browser, and several flows filled it with the wrong value.

Add a small closed vocabulary (source, auth-backend, activation,
from-invitation) and compose it from the profile when signin is
emitted. The assertion fails in development and tests and is elided in
the release build, so a bad value cannot break a real login.

Closes #12849

AI-assisted-by: deepseek-v4.1-flash

* ✨ Emit the session origin from every login entry point

The four emitters stamped a free-form string as metadata, so the
session event could not rely on it: two flows that were signups wrote
the same value as a login.

Route them through origin-meta: password and LDAP logins declare
source login, register declares source register with activation
immediate, and login-from-token derives both from the token issuer:
:auth for a login and :verify-email for a signup that needed the
email confirmation.

Closes #12849

AI-assisted-by: deepseek-v4.1-flash

* 🐛 Send the invitation signups through the regular session start

Registering from a team invitation link navigated by hand to token
verification, so the session start never ran: no signin event, no
teams, no host hook. The same navigation was copied in four places.

Teach login-from-register to carry an optional invitation token,
extract the profile decoration into with-register-origin, and call it
from the four sites instead of the hand-written navigation.

Closes #12849

AI-assisted-by: deepseek-v4.1-flash

* 🐛 Resolve the invitation without waiting for the teams

Registering from an invitation link now goes through the session
start, and that start only navigates after the teams request
completes. fetch-teams has no error handling, so a network failure
there would leave the invitation unaccepted and the user on the signup
screen with a session already minted.

Move the invitation navigation out of the teams path: it emits as soon
as the profile carries a token, and the teams path emits nothing for
an invited profile so it cannot move the user away from the token
verification screen.

Closes #12849

AI-assisted-by: deepseek-v4.1-flash

* 📚 Document the session origin vocabulary and the invitation flow

Add a focused memory with the four fields, where each value comes from,
the table of flows, the LDAP exception and the invitation path through
logged-in, and link it from the frontend routing.

Update the audit-log memory: source and activation are now on the
telemetry allowlist, activation lives in props and action only exists
on the retry rows.

Closes #12849

AI-assisted-by: deepseek-v4.1-flash

* 📎 Cover the invitation session start and fix the auth memories

The invitation flows and the token decoration in login-from-token were
only covered by helper tests, so removing the guard that keeps the
teams path from navigating, or the token attach, broke the behaviour
silently.

Run the events through ptk/watch with a teams-fetched event as input:
an invited profile navigates to the token verification screen and
never to the dashboard, and the signin keeps from-invitation. Add the
login-from-token cases with and without the token, and fix the memory
statements about action, the flow table and from-invitation.

Closes #12849

AI-assisted-by: deepseek-v4.1-flash
2026-11-09 10:11:54 +01:00

185 lines
7.9 KiB
Clojure

;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) KALEIDOS SUBSIDIARY SL
(ns frontend-tests.data.auth-test
(:require
[app.common.uuid :as uuid]
[app.main.data.auth :as da]
[app.main.data.event :as ev]
[app.main.data.team :as dtm]
[beicon.v2.core :as rx]
[cljs.test :as t :include-macros true]
[frontend-tests.helpers.async :as async]
[potok.v2.core :as ptk]))
;; --- Event runtime helpers
(defn- ^:async watch-events
"Runs the `watch` method of `event` with an empty state and the given
input `stream`, and returns the events it emits."
[event stream]
(let [events (atom [])]
(await (async/observe (ptk/watch event {} stream)
:on-next #(swap! events conj %)))
@events))
(defn- find-data-event
"First `ptk/data-event` in `events` whose `::ev/name` is `name`."
[events name]
(some (fn [event]
(when (and (= :app.main.data.event/event (ptk/type event))
(= name (::ev/name (deref event))))
event))
events))
(defn- navigate-events
[events]
(filter #(= :app.main.router/navigate (ptk/type %)) events))
(t/deftest auth-origin-keeps-only-the-fields-that-apply
(t/is (= {:source "login" :auth-backend "penpot"}
(da/auth-origin {:source "login" :auth-backend "penpot"})))
(t/is (= {:source "register" :auth-backend "google"
:activation "email-verification"}
(da/auth-origin {:source "register" :auth-backend "google"
:activation "email-verification"})))
(t/is (= {:source "register" :auth-backend "penpot"
:from-invitation true}
(da/auth-origin {:source "register" :auth-backend "penpot"
:from-invitation true}))))
(t/deftest auth-origin-rejects-values-outside-the-vocabulary
(t/is (thrown? js/Error
(da/auth-origin {:source "login-with-token"
:auth-backend "penpot"})))
(t/is (thrown? js/Error
(da/auth-origin {:source "login"
:auth-backend "foo"}))))
(t/deftest origin-meta-omits-the-fields-that-do-not-apply
(let [origin-meta @#'da/origin-meta]
(t/is (= {::ev/source "login"}
(origin-meta {:source "login"})))
(t/is (= {::ev/source "register" ::ev/activation "immediate"}
(origin-meta {:source "register" :activation "immediate"})))))
(t/deftest iss-maps-to-an-origin
(let [iss->origin @#'da/iss->origin]
(t/is (= {:source "login"} (iss->origin :auth)))
(t/is (= {:source "register" :activation "email-verification"}
(iss->origin :verify-email)))
(t/is (nil? (iss->origin :unknown)))))
(t/deftest profile-origin-composes-the-event-props
(let [profile-origin @#'da/profile-origin]
(t/is (= {:source "login" :auth-backend "google"
:from-invitation false}
(profile-origin
(with-meta {:auth-backend "google"} {::ev/source "login"}))))
(t/is (= {:source "login" :auth-backend "google"
:from-invitation true}
(profile-origin
(with-meta {:auth-backend "google" :invitation-token "tok"}
{::ev/source "login"}))))
(t/is (= {:source "register" :auth-backend "penpot"
:activation "email-verification"
:from-invitation false}
(profile-origin
(with-meta {:auth-backend "penpot"}
{::ev/source "register" ::ev/activation "email-verification"}))))))
(t/deftest with-register-origin-attaches-the-invitation-token
(let [with-register-origin @#'da/with-register-origin
profile (with-register-origin {:id "x"} "tok")]
(t/is (= {:id "x" :invitation-token "tok"} profile))
(t/is (= {::ev/source "register" ::ev/activation "immediate"}
(meta profile)))))
(t/deftest with-register-origin-keeps-the-profile-without-a-token
(let [with-register-origin @#'da/with-register-origin
profile (with-register-origin {:id "x"} nil)]
(t/is (= {:id "x"} profile))
(t/is (not (contains? profile :invitation-token)))
(t/is (= {::ev/source "register" ::ev/activation "immediate"}
(meta profile)))))
(t/deftest invitation-nav-only-emits-with-an-invitation-token
(let [get-invitation-nav @#'da/get-invitation-nav
collect (fn [profile]
(let [events (atom [])]
(rx/subs! #(swap! events conj %)
(get-invitation-nav profile))
@events))]
(t/is (= [] (collect {:auth-backend "penpot"})))
(let [events (collect {:auth-backend "penpot"
:invitation-token "tok"})
event (deref (first events))]
(t/is (= 1 (count events)))
(t/is (= :auth-verify-token (:id event)))
(t/is (= {:token "tok"} (:params event))))))
;; --- logged-in: the invitation does not wait for the teams
(t/deftest ^:async logged-in-with-invitation-navigates-to-token-verification
(let [logged-in @#'da/logged-in
origin-meta @#'da/origin-meta
profile (with-meta {:id (uuid/next)
:auth-backend "penpot"
:invitation-token "tok"}
(origin-meta {:source "login"}))
;; The teams arrive: the invitation must not be diverted to the
;; default team dashboard.
teams-fetched (ptk/data-event ::dtm/teams-fetched [])
events (await (watch-events (logged-in profile)
(rx/of teams-fetched)))
navs (navigate-events events)
nav (some-> (first navs) deref)
signin (some-> (find-data-event events "signin") deref)]
(t/is (= 1 (count navs)))
(t/is (= :auth-verify-token (:id nav)))
(t/is (= {:token "tok"} (:params nav)))
(t/is (not-any? #(= :app.main.data.common/go-to-dashboard-recent (ptk/type %))
events))
(t/is (true? (:from-invitation signin)))))
(t/deftest ^:async logged-in-without-invitation-goes-to-the-dashboard
(let [logged-in @#'da/logged-in
origin-meta @#'da/origin-meta
profile (with-meta {:id (uuid/next) :auth-backend "penpot"}
(origin-meta {:source "login"}))
teams-fetched (ptk/data-event ::dtm/teams-fetched [])
events (await (watch-events (logged-in profile)
(rx/of teams-fetched)))]
(t/is (empty? (navigate-events events)))
(t/is (some #(= :app.main.data.common/go-to-dashboard-recent (ptk/type %))
events))))
;; --- login-from-token: the invitation token survives the decoration
(t/deftest ^:async login-from-token-with-invitation-keeps-the-token
(let [[event] (await (watch-events
(da/login-from-token
{:profile {:id (uuid/next) :auth-backend "penpot"}
:iss :auth
:invitation-token "tok"})
(rx/empty)))
events (await (watch-events event (rx/empty)))
signin (some-> (find-data-event events "signin") deref)]
(t/is (= :app.main.data.auth/logged-in (ptk/type event)))
(t/is (= "login" (:source signin)))
(t/is (true? (:from-invitation signin)))))
(t/deftest ^:async login-from-token-without-invitation-has-no-invitation
(let [[event] (await (watch-events
(da/login-from-token
{:profile {:id (uuid/next) :auth-backend "penpot"}
:iss :auth})
(rx/empty)))
events (await (watch-events event (rx/empty)))
signin (some-> (find-data-event events "signin") deref)]
(t/is (false? (:from-invitation signin)))))