sofie-identity 0.4.0

Accounts, their area memberships, and signing up, in and out: the identity an app opts into.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
//! Signing in as a member, for a permitted principal of another area: each time recorded
//! with its reason and raised as an event, the session it opens marked as such to the member
//! and ended by itself after half an hour, and the impersonator signed back in at its end.

use std::time::Duration;

use sofie::core::Instant;
use sofie::db::{Outcome, Plan, Resolve, Stamp, Uuid};
use sofie::http::{
    Area, CurrentSession, NeedsPermission, Permission, Permissions, RequestContext, Routes, Shipped,
};
use sofie::prelude::*;

use crate::account::Account;
use crate::membership::{self, held};
use crate::signing_in::{SignedIn, sign_in};
use crate::{AccountArea, AccountId, MemberArea, Unscoped, account_of};

const READ: Permission = Permission::new("sofie_identity.impersonation.*.read");
const CREATE: Permission = Permission::new("sofie_identity.impersonation.*.create");
/// Long enough to look into a problem, short enough that a forgotten one ends soon.
const LASTS: Duration = Duration::from_mins(30);
const SHOWN: u64 = 50;
const NOT_A_MEMBER: &str = "has no account that is a member there";
const PERMITTED_ELSEWHERE: &str =
    "is a member of the area impersonating it, which nobody impersonates";
const NOT_IMPERSONATING: &str = "This session is no impersonation.";

/// The audit record: who signed in as whom, where, why, and until when. The member owns
/// it, so its own area reads and ends it.
#[derive(Aggregate)]
pub struct Impersonation {
    meta: Meta,
    #[owner]
    account: AccountId,
    impersonator: AccountId,
    /// As it was then, as an audit record keeps it.
    impersonator_email: Email,
    area: String,
    reason: String,
    session: Uuid,
    /// The impersonator's own session, which must still live for stopping to sign back in.
    started_from: Option<Uuid>,
    ends_at: Instant,
    ended_at: Option<Instant>,
    /// The member's permissions at the start, which the impersonator's covered; a request
    /// finding the member holding another ends the impersonation, as it may reach beyond them.
    granted: String,
    impersonator_area: String,
    /// Likewise the impersonator's at the start: a request finding one gone ends it, as what
    /// is left may no longer cover the member's.
    impersonator_granted: String,
}

const GRANTED_SEPARATOR: char = ' ';

impl Impersonation {
    pub(crate) fn end(&mut self, now: Instant) -> Result<()> {
        self.ended_at = Some(now);
        self.meta.raise(ImpersonationEnded {
            impersonation: self.id().uuid(),
        })
    }

    fn still_covers(&self, member_holds: &[String], impersonator_holds: &[String]) -> bool {
        let member_within = member_holds
            .iter()
            .all(|permission| listed(&self.granted).any(|granted| granted == permission));
        let impersonator_kept = listed(&self.impersonator_granted)
            .all(|granted| impersonator_holds.iter().any(|held| held == granted));
        member_within && impersonator_kept
    }
}

fn listed(granted: &str) -> impl Iterator<Item = &str> {
    granted
        .split(GRANTED_SEPARATOR)
        .filter(|permission| !permission.is_empty())
}

fn joined(granted: &[String]) -> String {
    granted.join(&GRANTED_SEPARATOR.to_string())
}

/// Raised when an impersonation starts; an app keeps its own audit log by `#[on]`.
#[derive(Event)]
pub struct ImpersonationStarted {
    pub impersonation: Uuid,
    pub impersonator: Uuid,
    pub member: Uuid,
    pub area: String,
    pub reason: String,
}

#[derive(Event)]
pub struct ImpersonationEnded {
    pub impersonation: Uuid,
}

/// In the permitted area `S`: the record of impersonations, and starting one as a member of `M`.
pub fn routes<S, M>(routes: &mut Routes<S>)
where
    S: AccountArea,
    M: MemberArea,
{
    let describe: sofie::http::Describe =
        |screens, key| screens.describe_form::<Starting>(key, "impersonate");
    routes
        .shipped(
            "/impersonation",
            "impersonation",
            (
                include_str!("screen.html"),
                include_bytes!(concat!(
                    env!("OUT_DIR"),
                    "/shipped/impersonation/screen.sft"
                )),
                Some(describe),
            ),
            record,
        )
        .name("list_impersonations")
        .permission(READ);
    routes
        .post("/impersonation", start::<S, M>)
        .name("impersonate")
        .permission(CREATE)
        .then_redirect(M::HOME);
}

/// In the member area `M`: ending the impersonation and returning to `S`.
pub fn stop_routes<M, S>(routes: &mut Routes<M>)
where
    M: MemberArea,
    S: Area,
{
    routes
        .post("/impersonation/stop", stop::<M, S>)
        .name("stop_impersonating")
        .then_redirect(&format!("{}/impersonation", S::PREFIX));
}

#[derive(ReadModel)]
pub struct Record {
    impersonations: Vec<Listed>,
}

#[derive(ReadModel)]
struct Listed {
    impersonator: Email,
    member: Email,
    area: String,
    reason: String,
    created_at: Option<Instant>,
    ended_at: Option<Instant>,
}

#[derive(ReadModel)]
#[read(from = Account)]
struct Member {
    id: AccountId,
    email: Email,
}

async fn record(read: Read, clock: Clock) -> Result<Record> {
    let now = clock.now();
    let held = Impersonation::query()
        .latest()
        .limit(SHOWN)
        .all(&read)
        .await?;
    let members = Member::query()
        .where_(Account::ID.in_(held.iter().map(|impersonation| impersonation.account)))
        .all(&read)
        .await?;
    let email_of = |account| {
        members
            .iter()
            .find(|member| member.id == account)
            .map(|member| member.email.clone())
            .ok_or(Error::NotFound)
    };
    let impersonations = held
        .into_iter()
        .map(|impersonation| {
            Ok(Listed {
                member: email_of(impersonation.account)?,
                created_at: impersonation.created_at(),
                impersonator: impersonation.impersonator_email,
                area: impersonation.area,
                reason: impersonation.reason,
                ended_at: impersonation
                    .ended_at
                    .or(Some(impersonation.ends_at).filter(|ends_at| *ends_at <= now)),
            })
        })
        .collect::<Result<_>>()?;
    Ok(Record { impersonations })
}

#[derive(Input)]
pub struct Starting {
    email: Email,
    /// Kept in the record, so every impersonation says what it was for.
    #[min(3)]
    #[max(500)]
    reason: String,
}

pub struct Started {
    impersonation: Impersonation,
    signed_in: SignedIn,
}

async fn start<S, M>(
    principal: Principal<S>,
    permissions: Permissions,
    starting: Starting,
    read: Read,
    session: Session,
    clock: Clock,
) -> Result<Started>
where
    S: AccountArea,
    M: MemberArea,
{
    let reason = starting.reason.trim();
    if reason.chars().count() < 3 {
        return Err(Error::invalid(
            "reason",
            "must say what the impersonation is for",
        ));
    }
    let impersonator = Account::stored(&read, account_of(&principal)).await?;
    let member = Account::of_email(&read, starting.email)
        .await?
        .filter(|member| !member.is_closed())
        .ok_or_else(|| Error::invalid("email", NOT_A_MEMBER))?;
    if held(&read, member.id(), M::NAME).await?.is_none() {
        return Err(Error::invalid("email", NOT_A_MEMBER));
    }
    if held(&read, member.id(), S::NAME).await?.is_some() {
        return Err(Error::invalid("email", PERMITTED_ELSEWHERE));
    }
    let granted = refuse_unless_within(&read, &permissions, member.id(), M::NAME).await?;
    let impersonator_granted =
        membership::roles::granted(&read, S::NAME, impersonator.id()).await?;
    let now = clock.now();
    let ends_at = now.checked_add(LASTS)?;
    let started_from = session.current();
    let signed_in =
        sign_in::<M>(&session, member.id(), &clock)?.impersonated(impersonator.email(), ends_at);
    let current = signed_in.session();
    let mut impersonation = Impersonation {
        meta: Meta::new()?,
        account: member.id(),
        impersonator: impersonator.id(),
        impersonator_email: impersonator.email().clone(),
        area: M::NAME.to_owned(),
        reason: reason.to_owned(),
        session: current,
        started_from,
        ends_at,
        ended_at: None,
        granted: joined(&granted),
        impersonator_area: S::NAME.to_owned(),
        impersonator_granted: joined(&impersonator_granted),
    };
    impersonation.meta.raise(ImpersonationStarted {
        impersonation: impersonation.id().uuid(),
        impersonator: impersonator.id().uuid(),
        member: member.id().uuid(),
        area: M::NAME.to_owned(),
        reason: impersonation.reason.clone(),
    })?;
    Ok(Started {
        impersonation,
        signed_in,
    })
}

/// The rule role grants follow: nobody acts beyond its own permissions, here as another.
async fn refuse_unless_within(
    read: &Read,
    impersonator: &Permissions,
    member: AccountId,
    area: &str,
) -> Result<Vec<String>> {
    let granted = membership::roles::granted(read, area, member).await?;
    match granted
        .iter()
        .find(|permission| !impersonator.can_named(permission))
    {
        Some(beyond) => Err(Error::invalid(
            "email",
            format!(
                "holds `{beyond}`, which your own permissions do not cover, so you cannot impersonate it"
            ),
        )),
        None => Ok(granted),
    }
}

impl Outcome for Started {
    fn plan(&self, plan: &mut Plan<'_>) -> Result<()> {
        self.impersonation.plan(plan)?;
        self.signed_in.plan(plan)
    }

    fn committed(&mut self, stamp: Stamp) {
        self.impersonation.committed(stamp);
        self.signed_in.committed(stamp);
    }
}

async fn stop<M, S>(
    _member: Principal<M>,
    read: Read,
    unscoped: Unscoped,
    session: Session,
    clock: Clock,
) -> Result<()>
where
    M: MemberArea,
    S: Area,
{
    let mut impersonation = of_session(&read, session.current())
        .await?
        .ok_or_else(|| Error::Rule(NOT_IMPERSONATING.to_owned()))?;
    impersonation.end(clock.now())?;
    let sessions = crate::sessions::ending_current(&read, &session, &clock).await?;
    match returning::<S>(&unscoped.0.read(), &impersonation).await? {
        Some(resumed) => crate::signing_in::resume::<S>(&session, &resumed),
        None => session.sign_out(),
    }
    unscoped.0.commit(&mut (impersonation, sessions)).await
}

/// The member's session outlives a password reset or "sign out everywhere" of the
/// impersonator's account, so only the impersonator's own session, still live, is signed
/// back in: never a new one, which would pass for a recent sign-in.
async fn returning<S: Area>(
    read: &Read,
    impersonation: &Impersonation,
) -> Result<Option<crate::sessions::Resumable>> {
    let Some(started_from) = impersonation.started_from else {
        return Ok(None);
    };
    let impersonator = impersonation.impersonator;
    let Some(resumed) = crate::sessions::recorded(read, started_from)
        .await?
        .and_then(|record| record.resumable(impersonator, S::NAME))
    else {
        return Ok(None);
    };
    Ok(held(read, impersonator, S::NAME).await?.map(|_| resumed))
}

#[cfg(feature = "account-data")]
pub(crate) async fn exported(read: &Read, account: AccountId) -> Result<sofie::serde_json::Value> {
    let impersonations = Impersonation::query()
        .where_(Impersonation::ACCOUNT.eq(account))
        .all(read)
        .await?;
    Ok(sofie::serde_json::json!(
        impersonations
            .iter()
            .map(|impersonation| sofie::serde_json::json!({
                "by": impersonation.impersonator_email.as_str(),
                "area": impersonation.area,
                "reason": impersonation.reason,
                "ends_at": impersonation.ends_at,
                "ended_at": impersonation.ended_at,
            }))
            .collect::<Vec<_>>()
    ))
}

/// Each impersonation in one of `sessions`, ended on the record, for whatever ended them.
pub(crate) async fn ending_in_sessions(
    read: &Read,
    sessions: &[Uuid],
    now: Instant,
) -> Result<Vec<Impersonation>> {
    if sessions.is_empty() {
        return Ok(Vec::new());
    }
    let mut open = Impersonation::query()
        .where_(Impersonation::SESSION.in_(sessions.to_vec()))
        .where_(Impersonation::ENDED_AT.is_null())
        .all(read)
        .await?;
    for impersonation in &mut open {
        impersonation.end(now)?;
    }
    Ok(open)
}

pub(crate) async fn sessions_started_by(read: &Read, impersonator: AccountId) -> Result<Vec<Uuid>> {
    let open = Impersonation::query()
        .where_(Impersonation::IMPERSONATOR.eq(impersonator))
        .where_(Impersonation::ENDED_AT.is_null())
        .all(read)
        .await?;
    Ok(open
        .into_iter()
        .map(|impersonation| impersonation.session)
        .collect())
}

/// One recorded without the session it started from may have started from one of them.
pub(crate) async fn sessions_started_from(
    read: &Read,
    impersonator: AccountId,
    from: &[Uuid],
) -> Result<Vec<Uuid>> {
    let open = Impersonation::query()
        .where_(Impersonation::IMPERSONATOR.eq(impersonator))
        .where_(Impersonation::ENDED_AT.is_null())
        .all(read)
        .await?;
    Ok(open
        .into_iter()
        .filter(|impersonation| {
            impersonation
                .started_from
                .is_none_or(|started| from.contains(&started))
        })
        .map(|impersonation| impersonation.session)
        .collect())
}

async fn of_session(read: &Read, session: Option<Uuid>) -> Result<Option<Impersonation>> {
    let Some(session) = session else {
        return Ok(None);
    };
    let found = Impersonation::query()
        .where_(Impersonation::SESSION.eq(session))
        .first(read)
        .await?;
    Ok(found.filter(|impersonation| impersonation.ended_at.is_none()))
}

pub(crate) enum Checked {
    Absent,
    By(Uuid),
    Beyond(Box<Impersonation>),
}

/// Read on each request of an impersonated session only, so no other pays for it.
pub(crate) async fn checked(read: &Read, session: Uuid) -> Result<Checked> {
    let Some(impersonation) = of_session(read, Some(session)).await? else {
        return Ok(Checked::Absent);
    };
    let member_holds =
        membership::roles::granted(read, &impersonation.area, impersonation.account).await?;
    let impersonator_holds = membership::roles::granted(
        read,
        &impersonation.impersonator_area,
        impersonation.impersonator,
    )
    .await?;
    if impersonation.still_covers(&member_holds, &impersonator_holds) {
        return Ok(Checked::By(impersonation.impersonator.uuid()));
    }
    Ok(Checked::Beyond(Box::new(impersonation)))
}

/// For a member area's screens: who is impersonating its member, if anyone, so a page can
/// show it and offer `/impersonation/stop`.
pub struct Impersonating(Option<String>);

impl Impersonating {
    #[must_use]
    pub fn by(&self) -> Option<&str> {
        self.0.as_deref()
    }
}

impl<A: Area, M> Resolve<RequestContext<A, M>> for Impersonating {
    async fn resolve(context: &mut RequestContext<A, M>) -> Result<Self> {
        let current = CurrentSession::resolve(context).await?;
        let read = Read::resolve(context).await?;
        let impersonation = of_session(&read, current.id()).await?;
        Ok(Self(impersonation.map(|impersonation| {
            impersonation.impersonator_email.as_str().to_owned()
        })))
    }
}