plexus-core 0.8.0

Core infrastructure for Plexus RPC: Activation trait, DynamicHub, schemas
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
//! AUTH-5 — **the `auth` activation**: one auth surface in every gated
//! Plexus hub. Sign in, sign up, who am I, what does this need, and the
//! admin's roles and memberships.
//!
//! A service turns login on with [`DynamicHub::with_auth`], handing it an
//! [`AuthSetup`]: what only the service knows —
//!
//! - an [`AuthBackend`]: its providers, its login flows (the browser start
//!   and finish, a password, a session minted on a key-proved or signed-in
//!   connection), signup and the waiting list, logout;
//! - an editable [`RoleStore`] (role → grants: defaults from the
//!   activations' `roles(...)`, an admin's edits win) and
//!   [`MembershipStore`] (who holds which role where);
//! - its scope [`Resolve`]r.
//!
//! The hub builds the [`Gate`] from those same stores (so an edit reaches
//! the next call and the next view), mounts this activation, and the rest
//! the activation answers itself from the gate and the stores:
//!
//! | method | who | answers |
//! |---|---|---|
//! | `providers`, `login`, `signup` | anyone (`public`) | the door |
//! | `whoami`, `logout`, `requires` | any signed-in caller (whatever its roles or ceiling) | `whoami`, `logged_out`, `requires` |
//! | `roles` | whoever holds `auth.roles` (admin) | one `role` per role |
//! | `role_set` | admin on `*` | the role as it now stands |
//! | `grant`, `revoke` | whoever holds them on the scope (`target = "scope"`) | `granted` / `revoked` |
//! | `waiting` | admin on `*` | one `waiting` per principal asking |
//!
//! A refusal the gate makes is AUTH-2's typed error item; one this
//! activation makes (`role_set`, `waiting` short of `*`) is an in-band
//! `{type: "error", error_kind: "forbidden", grant, scope, roles,
//! held_on, message}` event — the same fields.
//!
//! [`super::auth_memory`] has a reference in-memory implementation of every
//! trait here, for tests and as a starting point.
//!
//! [`DynamicHub::with_auth`]: super::DynamicHub::with_auth

use std::collections::{BTreeMap, BTreeSet};
use std::fmt;
use std::sync::{Arc, RwLock};

use async_stream::stream;
use async_trait::async_trait;
use futures::Stream;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use plexus_auth_core::grants::{
    Ceiling, Gate, Grant, GrantSet, GrantsGeneration, GrantsMove, Liveness, Membership, Memberships, Reach, Resolve, Roles, ScopeRef, ADMIN,
};
use plexus_auth_core::identity::Principal;
use plexus_auth_core::RoleName;

use super::auth::AuthContext;
use super::auth_params as param;
use super::schema::{MethodRole, PluginSchema};

/// The grants every signed-in caller reaches, whatever its roles or its
/// session's ceiling: asking who it is, signing out, and asking what a
/// method needs. Never anonymously.
pub const SIGNED_IN_GRANTS: [&str; 3] = ["auth.whoami", "auth.logout", "auth.requires"];

// ===========================================================================
// Wire types
// ===========================================================================

/// How a provider signs someone in.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum ProviderKind {
    /// A person, through the browser (OIDC, WorkOS, …): `login {flow:
    /// {browser}}` then `{code}`.
    Browser,
    /// A username and password (`login {flow: {password}}`).
    Password,
    /// An agent's key: a proof at the upgrade, then `login {flow: {session}}`.
    Key,
}

/// One way in this server offers.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct Provider {
    /// Its name (`workos`, `plexus-idp`, `key`).
    pub name: String,
    pub kind: ProviderKind,
    /// A human label, when it has one.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub label: Option<String>,
}

/// How a caller signs in (`auth.login {flow: …}`). On the wire, externally
/// tagged: `{"browser": {"redirect": …}}`, `{"code": {"code", "state"}}`,
/// `{"password": {"username", "password"}}`, `{"session": {"ttl_secs",
/// "ceiling"}}`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum LoginFlow {
    /// A person, from a client with a browser: answered with the URL to
    /// open (`login_url {url, state}`); the browser comes back to
    /// `redirect` (a loopback address the client listens on) with a
    /// one-time code.
    Browser { redirect: String },
    /// The browser came back: trade the code for a session. Single-use,
    /// bound to the state.
    Code { code: String, state: String },
    /// Where a provider takes a password.
    Password { username: String, password: String },
    /// A session on a connection that is already someone — a key proof at
    /// the upgrade (IDENTITY-40), or a session — bounded by `ceiling`
    /// (`role@SCOPE` / `grant@SCOPE` entries) and lasting `ttl_secs`.
    Session {
        #[serde(default)]
        ttl_secs: Option<u64>,
        #[serde(default)]
        ceiling: Option<Vec<String>>,
    },
}

/// A session a backend minted.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct Session {
    /// The bearer token (`Authorization: Bearer …`, or the service's cookie).
    pub token: String,
    /// Who it is: `key:…`, `oidc:workos/user_…`, `idp:…`.
    pub principal: String,
    /// The agent id, for an agent's session.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub agent: Option<String>,
    /// A display name, when the provider gives one.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// Unix seconds.
    pub expires_at: i64,
    /// The session's ceiling, `role@SCOPE` / `grant@SCOPE`; empty: none.
    #[serde(default)]
    pub ceiling: Vec<String>,
}

/// Someone asking for access.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct Waiting {
    pub principal: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// The way they came in (`key`, `workos`, …).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub provider: Option<String>,
    /// Unix seconds: when they first asked.
    pub since: i64,
}

/// What `login` answers.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LoginAnswer {
    /// Open this URL; the browser comes back with a code for this state.
    LoginUrl { url: String, state: String },
    Session(Session),
}

/// What `signup` answers when the backend decides it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SignupAnswer {
    Waiting(Waiting),
    LoginUrl { url: String, state: String },
    Session(Session),
}

/// Why a backend would not: said to the caller as an in-band error.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AuthFailure(pub String);

impl AuthFailure {
    pub fn new(message: impl Into<String>) -> Self {
        Self(message.into())
    }
}

impl fmt::Display for AuthFailure {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

impl std::error::Error for AuthFailure {}

/// Why a store would not write.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StoreError(pub String);

impl StoreError {
    pub fn new(message: impl Into<String>) -> Self {
        Self(message.into())
    }
}

impl fmt::Display for StoreError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

impl std::error::Error for StoreError {}

/// A membership as `whoami` says it.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct MembershipEntry {
    pub role: String,
    pub scope: String,
}

/// Every event the `auth` activation sends (`{"type": …}`).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum AuthEvent {
    /// `providers`: one way in.
    Provider {
        name: String,
        kind: ProviderKind,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        label: Option<String>,
    },
    /// `login {browser}` / `signup`: open this URL.
    LoginUrl { url: String, state: String },
    /// `login`: signed in.
    Session {
        token: String,
        principal: String,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        agent: Option<String>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        name: Option<String>,
        expires_at: i64,
        #[serde(default)]
        ceiling: Vec<String>,
    },
    /// `signup` (asking for access) / `waiting` (an admin's list).
    Waiting {
        principal: String,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        name: Option<String>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        provider: Option<String>,
        since: i64,
    },
    /// `whoami`.
    Whoami {
        principal: String,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        name: Option<String>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        agent: Option<String>,
        memberships: Vec<MembershipEntry>,
        ceiling: Vec<String>,
    },
    /// `logout`.
    LoggedOut { principal: String },
    /// `requires {method}`: its grant, its target, the roles that list it.
    Requires {
        method: String,
        grant: String,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        target: Option<String>,
        roles: Vec<String>,
        #[serde(default, skip_serializing_if = "std::ops::Not::not")]
        public: bool,
    },
    /// `roles` / `role_set`: a role and its grants (`all`: every grant —
    /// admin).
    Role {
        role: String,
        grants: Vec<String>,
        #[serde(default, skip_serializing_if = "std::ops::Not::not")]
        all: bool,
    },
    /// `grant`.
    Granted { principal: String, role: String, scope: String },
    /// `revoke`: the roles it removed there.
    Revoked { principal: String, scope: String, roles: Vec<String> },
    /// Anything refused or failed. A refusal (`error_kind: "forbidden"`)
    /// carries AUTH-2's fields.
    Error {
        message: String,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        error_kind: Option<String>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        grant: Option<String>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        scope: Option<String>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        roles: Option<Vec<String>>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        held_on: Option<Vec<String>>,
    },
}

impl AuthEvent {
    fn error(message: impl Into<String>) -> Self {
        Self::Error { message: message.into(), error_kind: None, grant: None, scope: None, roles: None, held_on: None }
    }
}

/// MACRO-13: `AuthClient` implements the `Auth` trait; a client failure is
/// the in-band error, the last item of its stream.
impl plexus_client::FromClientError for AuthEvent {
    fn from_client_error(error: plexus_client::ClientError) -> Self {
        Self::error(error.to_string())
    }
}

impl From<Session> for AuthEvent {
    fn from(s: Session) -> Self {
        Self::Session {
            token: s.token,
            principal: s.principal,
            agent: s.agent,
            name: s.name,
            expires_at: s.expires_at,
            ceiling: s.ceiling,
        }
    }
}

impl From<Waiting> for AuthEvent {
    fn from(w: Waiting) -> Self {
        Self::Waiting { principal: w.principal, name: w.name, provider: w.provider, since: w.since }
    }
}

// ===========================================================================
// What a service supplies
// ===========================================================================

/// What only the service knows about signing in. Async: a backend may talk
/// to its identity provider. (The gate's own questions — [`Memberships`],
/// [`Roles`], [`Resolve`] — stay synchronous and from memory.)
#[async_trait]
pub trait AuthBackend: Send + Sync + 'static {
    /// The ways in this server offers (`auth.providers`).
    fn providers(&self) -> Vec<Provider>;

    /// Sign in by `flow` (`auth.login`). `caller` is the connection's
    /// identity, if any: a [`LoginFlow::Session`] needs one (a key proof at
    /// the upgrade, or a session) and must not widen it — the new
    /// session's ceiling lies within the caller's (layer it). The browser
    /// flow's start and finish, the code's single use and expiry, and the
    /// session's minting are the backend's.
    async fn login(&self, caller: Option<&AuthContext>, flow: LoginFlow) -> Result<LoginAnswer, AuthFailure>;

    /// `auth.signup` where the activation's own rule does not decide it:
    /// an anonymous caller (create an account — or refuse: sign in first),
    /// or one who already holds a membership. (A signed-in caller holding
    /// none is put on the waiting list by [`AuthBackend::ask_access`].)
    async fn signup(&self, caller: Option<&AuthContext>, via: Option<String>) -> Result<SignupAnswer, AuthFailure>;

    /// Put a signed-in caller who holds no membership on the waiting list
    /// (asking again keeps the first `since`).
    async fn ask_access(&self, caller: &AuthContext, via: Option<String>) -> Result<Waiting, AuthFailure>;

    /// Everyone who has asked for access. The activation shows an admin
    /// only those still holding no membership.
    fn waiting(&self) -> Vec<Waiting>;

    /// End the caller's session (`auth.logout`). A connection with no
    /// session to end (a bare key proof) is not an error.
    async fn logout(&self, caller: &AuthContext) -> Result<(), AuthFailure>;

    /// AUTH-11: whether a signed-in caller's sign-in still stands — its
    /// session not signed out, revoked or expired. The gate asks it before
    /// every call it decides (`auth.*` included), so a sign-in that ended
    /// holds nothing from its next call on. `Err`: why not, in words for
    /// the caller. Memory only: it is asked on every call. (The gate
    /// refuses a caller past its `expires_at` stamp before asking.)
    fn alive(&self, caller: &AuthContext) -> Result<(), String>;
}

/// AUTH-11: the backend as the gate's [`Liveness`].
struct BackendLiveness(Arc<dyn AuthBackend>);

impl Liveness for BackendLiveness {
    fn alive(&self, caller: &AuthContext) -> Result<(), String> {
        self.0.alive(caller)
    }
}

/// [`Roles`] an admin can edit (`auth.role_set`). The gate reads the same
/// store, so an edit reaches the next call and the next view.
#[async_trait]
pub trait RoleStore: Roles + 'static {
    /// Replace `role`'s grants. Never called for [`ADMIN`].
    async fn set(&self, role: &RoleName, grants: GrantSet) -> Result<(), StoreError>;
}

/// [`Memberships`] an admin can edit (`auth.grant`, `auth.revoke`).
#[async_trait]
pub trait MembershipStore: Memberships + 'static {
    /// Add a membership (holding it already is not an error).
    async fn grant(&self, membership: Membership) -> Result<(), StoreError>;

    /// Remove `principal`'s memberships on `scope` — the one role named, or
    /// every role there. Answers the roles removed.
    async fn revoke(
        &self,
        principal: &Principal,
        role: Option<&RoleName>,
        scope: &ScopeRef,
    ) -> Result<Vec<RoleName>, StoreError>;
}

/// What [`DynamicHub::with_auth`](super::DynamicHub::with_auth) takes.
#[derive(Clone)]
pub struct AuthSetup {
    pub backend: Arc<dyn AuthBackend>,
    pub roles: Arc<dyn RoleStore>,
    pub memberships: Arc<dyn MembershipStore>,
    pub resolve: Arc<dyn Resolve>,
    /// BIDIR-8: the service's grants generation, when it shares one between
    /// this service's call gate and gates of its own (menger's access
    /// layer); else the call gate counts alone. `auth.grant`, `auth.revoke`,
    /// `auth.role_set` and `auth.logout` move it.
    pub generation: Option<GrantsGeneration>,
}

impl AuthSetup {
    pub fn new(
        backend: Arc<dyn AuthBackend>,
        roles: Arc<dyn RoleStore>,
        memberships: Arc<dyn MembershipStore>,
        resolve: Arc<dyn Resolve>,
    ) -> Self {
        Self { backend, roles, memberships, resolve, generation: None }
    }

    /// BIDIR-8: the service's call gate counts its moves in `generation`, shared with
    /// whatever else the service decides by it.
    pub fn with_generation(mut self, generation: GrantsGeneration) -> Self {
        self.generation = Some(generation);
        self
    }
}

// ===========================================================================
// The catalog: what each method requires
// ===========================================================================

/// What one method requires, as `auth.requires` says it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Requirement {
    pub grant: String,
    pub target: Option<String>,
    pub public: bool,
}

/// Every method of the hub by grant — fed as activations register.
#[derive(Clone, Default)]
pub struct Catalog(Arc<RwLock<BTreeMap<String, Requirement>>>);

impl Catalog {
    /// Add an activation's methods (not its auto-generated `schema`, which
    /// is introspection, nor its child routes).
    pub(crate) fn add(&self, schema: &PluginSchema) {
        let mut map = self.0.write().unwrap_or_else(|p| p.into_inner());
        for m in &schema.methods {
            if m.is_auto_schema() || !matches!(m.role, MethodRole::Rpc) {
                continue;
            }
            let grant = m.grant_or(&schema.namespace);
            map.insert(grant.clone(), Requirement { grant, target: m.target.clone(), public: m.public });
        }
    }

    /// The requirement of `method` (`tickets.set_status`).
    pub fn get(&self, method: &str) -> Option<Requirement> {
        self.0.read().unwrap_or_else(|p| p.into_inner()).get(method).cloned()
    }

    /// Whether `grant` names a method of this hub.
    pub fn knows(&self, grant: &str) -> bool {
        self.0.read().unwrap_or_else(|p| p.into_inner()).contains_key(grant)
    }
}

// ===========================================================================
// The activation
// ===========================================================================

/// The `auth` activation. Mounted by
/// [`DynamicHub::with_auth`](super::DynamicHub::with_auth); see the module
/// docs.
#[derive(Clone)]
pub struct AuthActivation {
    inner: Arc<AuthInner>,
}

struct AuthInner {
    backend: Arc<dyn AuthBackend>,
    roles: Arc<dyn RoleStore>,
    memberships: Arc<dyn MembershipStore>,
    gate: Gate,
    catalog: Catalog,
}

/// The service's resolver, and `*` → `[*]` (a membership on everywhere is
/// granted and revoked like any other scope).
struct WithEverywhere(Arc<dyn Resolve>);

impl Resolve for WithEverywhere {
    fn chain(&self, target: &str) -> Option<Vec<ScopeRef>> {
        if target.trim() == "*" {
            return Some(vec![ScopeRef::Everywhere]);
        }
        self.0.chain(target)
    }
}

impl AuthActivation {
    /// The activation and the gate over the same stores.
    pub(crate) fn mount(setup: AuthSetup, catalog: Catalog) -> (Self, Gate) {
        let memberships: Arc<dyn Memberships> = setup.memberships.clone();
        let roles: Arc<dyn Roles> = setup.roles.clone();
        let mut gate = Gate::new(memberships, roles, Arc::new(WithEverywhere(setup.resolve)))
            .with_liveness(Arc::new(BackendLiveness(setup.backend.clone())));
        if let Some(generation) = setup.generation {
            gate = gate.with_generation(generation);
        }
        let auth = Self {
            inner: Arc::new(AuthInner {
                backend: setup.backend,
                roles: setup.roles,
                memberships: setup.memberships,
                gate: gate.clone(),
                catalog,
            }),
        };
        (auth, gate)
    }
}

fn principal_of(ctx: &AuthContext) -> Option<Principal> {
    ctx.principal()
}

fn meta(ctx: &AuthContext, key: &str) -> Option<String> {
    ctx.get_metadata_string(key).filter(|s| !s.is_empty())
}

/// An in-band refusal: `grant` is needed on `*`.
fn needs_everywhere(gate: &Gate, grant: &str, reach: &Reach) -> AuthEvent {
    let g = Grant::new(grant);
    AuthEvent::Error {
        message: format!("needs {grant} on *"),
        error_kind: Some("forbidden".into()),
        grant: Some(grant.into()),
        scope: Some("*".into()),
        roles: Some(gate.roles_listing(&g).iter().map(|r| r.as_str().to_string()).collect()),
        held_on: Some(reach.held_on().iter().map(|s| s.as_str().to_string()).collect()),
    }
}

fn role_event(role: &RoleName, grants: Option<GrantSet>) -> AuthEvent {
    match grants {
        Some(GrantSet::All) => AuthEvent::Role { role: role.as_str().into(), grants: Vec::new(), all: true },
        Some(GrantSet::Listed(set)) => AuthEvent::Role {
            role: role.as_str().into(),
            grants: set.iter().map(|g| g.as_str().to_string()).collect(),
            all: false,
        },
        None => AuthEvent::Role { role: role.as_str().into(), grants: Vec::new(), all: false },
    }
}

#[plexus_macros::activation(
    version = "1.0.0",
    description = "Sign in, who am I, what does this need",
    roles(member = ["whoami", "logout", "requires"]),
    // MACRO-12: every parameter is a type (`super::auth_params`); a bare
    // string parameter does not compile here.
    typed_params,
)]
impl AuthActivation {
    /// The ways in this server offers.
    #[plexus_macros::method(public, group = "sign in", description = "The ways in this server offers")]
    async fn providers(&self) -> impl Stream<Item = AuthEvent> + Send + 'static {
        let providers = self.inner.backend.providers();
        stream! {
            for p in providers {
                yield AuthEvent::Provider { name: p.name, kind: p.kind, label: p.label };
            }
        }
    }

    /// Sign in — a person through the browser (`{browser: {redirect}}`,
    /// then `{code: {code, state}}`), a password, or an agent's session on
    /// a key-proved connection (`{session: {ttl_secs, ceiling}}`).
    #[plexus_macros::method(
        public,
        group = "sign in",
        description = "Sign in — a person through the browser, an agent with its key",
        params(flow = "browser {redirect} | code {code, state} | password {username, password} | session {ttl_secs?, ceiling?}")
    )]
    async fn login(&self, auth: Option<&AuthContext>, flow: LoginFlow) -> impl Stream<Item = AuthEvent> + Send + 'static {
        let inner = self.inner.clone();
        let caller = auth.cloned();
        stream! {
            if let LoginFlow::Session { ceiling: Some(entries), .. } = &flow {
                if let Err(e) = Ceiling::parse(entries) {
                    yield AuthEvent::error(format!("ceiling: {e}"));
                    return;
                }
            }
            if matches!(flow, LoginFlow::Session { .. }) && caller.as_ref().and_then(principal_of).is_none() {
                yield AuthEvent::error("a session needs a connection that is someone: a key proof at the upgrade, or a session");
                return;
            }
            match inner.backend.login(caller.as_ref(), flow).await {
                Ok(LoginAnswer::LoginUrl { url, state }) => yield AuthEvent::LoginUrl { url, state },
                Ok(LoginAnswer::Session(s)) => yield s.into(),
                Err(e) => yield AuthEvent::error(e.0),
            }
        }
    }

    /// Create an account, or ask for access: a signed-in caller holding no
    /// membership of its own ([`Memberships::own_held`]) joins the waiting
    /// list for an admin.
    #[plexus_macros::method(
        public,
        group = "sign in",
        description = "Create an account, or ask for access",
        params(via = "The provider to sign up through, where the server offers several")
    )]
    async fn signup(&self, auth: Option<&AuthContext>, via: Option<param::ProviderName>) -> impl Stream<Item = AuthEvent> + Send + 'static {
        let inner = self.inner.clone();
        let caller = auth.cloned();
        let via = via.map(param::ProviderName::into_string);
        stream! {
            let holds_none = caller
                .as_ref()
                .and_then(principal_of)
                .map(|p| inner.memberships.own_held(&p).is_empty());
            let answer = match (holds_none, caller.as_ref()) {
                (Some(true), Some(ctx)) => inner.backend.ask_access(ctx, via).await.map(SignupAnswer::Waiting),
                _ => inner.backend.signup(caller.as_ref(), via).await,
            };
            match answer {
                Ok(SignupAnswer::Waiting(w)) => yield w.into(),
                Ok(SignupAnswer::LoginUrl { url, state }) => yield AuthEvent::LoginUrl { url, state },
                Ok(SignupAnswer::Session(s)) => yield s.into(),
                Err(e) => yield AuthEvent::error(e.0),
            }
        }
    }

    /// Who this connection is: principal, name, the roles it holds where,
    /// and its session's ceiling.
    #[plexus_macros::method(group = "you", description = "Who you are: principal, roles held where, ceiling")]
    async fn whoami(&self, auth: &AuthContext) -> impl Stream<Item = AuthEvent> + Send + 'static {
        let inner = self.inner.clone();
        let ctx = auth.clone();
        stream! {
            let Some(p) = principal_of(&ctx) else {
                yield AuthEvent::error("sign in first");
                return;
            };
            let memberships = inner
                .memberships
                .held(&p)
                .into_iter()
                .map(|m| MembershipEntry { role: m.role.as_str().into(), scope: m.scope.as_str().into() })
                .collect();
            let ceiling = Ceiling::layers_of(&ctx)
                .map(|layers| layers.iter().flat_map(|l| l.text()).collect())
                .unwrap_or_default();
            yield AuthEvent::Whoami {
                principal: p.to_string(),
                name: meta(&ctx, "name"),
                agent: meta(&ctx, "agent"),
                memberships,
                ceiling,
            };
        }
    }

    /// End this session.
    #[plexus_macros::method(group = "you", description = "Sign out: end this session")]
    async fn logout(&self, auth: &AuthContext) -> impl Stream<Item = AuthEvent> + Send + 'static {
        let inner = self.inner.clone();
        let ctx = auth.clone();
        stream! {
            let principal = principal_of(&ctx).map(|p| p.to_string()).unwrap_or_default();
            match inner.backend.logout(&ctx).await {
                Ok(()) => {
                    // BIDIR-8: a sign-in ended; whatever holds it verifies again.
                    inner.gate.moved(GrantsMove::SignOut);
                    yield AuthEvent::LoggedOut { principal }
                }
                Err(e) => yield AuthEvent::error(e.0),
            }
        }
    }

    /// What a method needs: its grant, its target and the roles that list
    /// it.
    #[plexus_macros::method(
        group = "you",
        description = "What a method needs: its grant, target, and the roles that list it",
        params(method = "The method, as <activation>.<method>: tickets.set_status"),
        example(note = "what changing a ticket's status needs", params = r#"{"method": "tickets.set_status"}"#)
    )]
    async fn requires(&self, auth: &AuthContext, method: param::MethodPath) -> impl Stream<Item = AuthEvent> + Send + 'static {
        let _ = auth;
        let inner = self.inner.clone();
        stream! {
            match inner.catalog.get(method.as_str()) {
                None => yield AuthEvent::error(format!("no method {method} here")),
                Some(req) => {
                    let roles = inner.gate.roles_listing(&Grant::new(req.grant.clone()));
                    yield AuthEvent::Requires {
                        method: method.into_string(),
                        grant: req.grant,
                        target: req.target,
                        roles: roles.iter().map(|r| r.as_str().to_string()).collect(),
                        public: req.public,
                    };
                }
            }
        }
    }

    /// Every role and the grants it lists (admin: every grant).
    #[plexus_macros::method(group = "admin", description = "Every role and the grants it lists")]
    async fn roles(&self, auth: &AuthContext) -> impl Stream<Item = AuthEvent> + Send + 'static {
        let _ = auth;
        let inner = self.inner.clone();
        stream! {
            let admin = RoleName::new(ADMIN);
            yield role_event(&admin, Some(GrantSet::All));
            let names: BTreeSet<RoleName> = inner.roles.names().into_iter().filter(|r| r.as_str() != ADMIN).collect();
            for r in names {
                let grants = inner.roles.grants(&r);
                yield role_event(&r, grants);
            }
        }
    }

    /// Set a role's grants (admin on `*`). Each entry a grant
    /// (`tickets.get`) or an existing role whose grants it includes.
    #[plexus_macros::method(
        group = "admin",
        description = "Set a role's grants (admin on *)",
        params(role = "The role (not admin)", grants = "Grants (tickets.get) or roles to include")
    )]
    async fn role_set(&self, auth: &AuthContext, reach: Reach, role: param::RoleName, grants: Vec<param::GrantName>) -> impl Stream<Item = AuthEvent> + Send + 'static {
        let _ = auth;
        let inner = self.inner.clone();
        stream! {
            if !reach.is_everywhere() {
                yield needs_everywhere(&inner.gate, "auth.role_set", &reach);
                return;
            }
            // The name's shape (one word, no `.` or `@`) is `param::RoleName`'s,
            // refused as InvalidParams before this runs.
            if role.as_str() == ADMIN {
                yield AuthEvent::error("admin holds every grant; it is not edited");
                return;
            }
            let mut set: BTreeSet<Grant> = BTreeSet::new();
            for entry in grants.iter() {
                let entry = entry.as_str();
                if entry.contains('.') {
                    if !inner.catalog.knows(entry) {
                        yield AuthEvent::error(format!("no method {entry} here"));
                        return;
                    }
                    set.insert(Grant::new(entry));
                } else {
                    match inner.roles.grants(&RoleName::new(entry)) {
                        Some(GrantSet::Listed(included)) => set.extend(included),
                        Some(GrantSet::All) => {
                            yield AuthEvent::error(format!("{entry} holds every grant; a role cannot include it"));
                            return;
                        }
                        None => {
                            yield AuthEvent::error(format!("no role {entry} to include"));
                            return;
                        }
                    }
                }
            }
            let name = role.role();
            if let Err(e) = inner.roles.set(&name, GrantSet::Listed(set)).await {
                yield AuthEvent::error(e.0);
                return;
            }
            inner.gate.moved(GrantsMove::RoleEdit);
            let now = inner.roles.grants(&name);
            yield role_event(&name, now);
        }
    }

    /// Give `principal` `role` on `scope` (whoever holds `auth.grant` on
    /// the scope: admin there).
    #[plexus_macros::method(
        target = "scope",
        group = "admin",
        description = "Give a principal a role on a scope",
        params(principal = "key:…, oidc:…/…, idp:…", role = "The role", scope = "Where: a scope name, or *"),
        example(note = "make a person an editor on MENGER", params = r#"{"principal": "oidc:workos/user_01M4", "role": "editor", "scope": "MENGER"}"#),
        example(note = "make an agent's key an admin everywhere", params = r#"{"principal": "key:ac79e0d1f2a3b4c5d6e7f8091a2b3c4d5e6f708192a3b4c5d6e7f8091a2b3c4d", "role": "admin", "scope": "*"}"#)
    )]
    async fn grant(&self, auth: &AuthContext, principal: param::Principal, role: param::RoleName, scope: param::ScopeName) -> impl Stream<Item = AuthEvent> + Send + 'static {
        let _ = auth;
        let inner = self.inner.clone();
        stream! {
            let who: Principal = principal.principal();
            let role_name = role.role();
            if role_name.as_str() != ADMIN && inner.roles.grants(&role_name).is_none() {
                yield AuthEvent::error(format!("no role {role} here"));
                return;
            }
            let scope_ref = scope.scope();
            let m = Membership { principal: who.clone(), role: role_name.clone(), scope: scope_ref.clone() };
            match inner.memberships.grant(m).await {
                Ok(()) => {
                    inner.gate.moved(GrantsMove::Grant);
                    yield AuthEvent::Granted {
                        principal: who.to_string(),
                        role: role_name.as_str().into(),
                        scope: scope_ref.as_str().into(),
                    }
                }
                Err(e) => yield AuthEvent::error(e.0),
            }
        }
    }

    /// Take `principal`'s role (or every role) on `scope` away.
    #[plexus_macros::method(
        target = "scope",
        group = "admin",
        description = "Take a principal's role on a scope away",
        params(principal = "key:…, oidc:…/…, idp:…", role = "The role; absent: every role there", scope = "Where: a scope name, or *")
    )]
    async fn revoke(&self, auth: &AuthContext, principal: param::Principal, role: Option<param::RoleName>, scope: param::ScopeName) -> impl Stream<Item = AuthEvent> + Send + 'static {
        let _ = auth;
        let inner = self.inner.clone();
        stream! {
            let who: Principal = principal.principal();
            let scope_ref = scope.scope();
            let role_name = role.as_ref().map(param::RoleName::role);
            match inner.memberships.revoke(&who, role_name.as_ref(), &scope_ref).await {
                Ok(removed) => {
                    if !removed.is_empty() {
                        inner.gate.moved(GrantsMove::Revoke);
                    }
                    yield AuthEvent::Revoked {
                        principal: who.to_string(),
                        scope: scope_ref.as_str().into(),
                        roles: removed.iter().map(|r| r.as_str().to_string()).collect(),
                    }
                }
                Err(e) => yield AuthEvent::error(e.0),
            }
        }
    }

    /// Who signed up or asked for access and holds nothing of its own yet
    /// ([`Memberships::own_held`]; admin on `*`).
    #[plexus_macros::method(group = "admin", description = "Who asked for access and holds nothing yet (admin on *)")]
    async fn waiting(&self, auth: &AuthContext, reach: Reach) -> impl Stream<Item = AuthEvent> + Send + 'static {
        let _ = auth;
        let inner = self.inner.clone();
        stream! {
            if !reach.is_everywhere() {
                yield needs_everywhere(&inner.gate, "auth.waiting", &reach);
                return;
            }
            for w in inner.backend.waiting() {
                let still = w
                    .principal
                    .parse::<Principal>()
                    .map(|p| inner.memberships.own_held(&p).is_empty())
                    .unwrap_or(true);
                if still {
                    yield w.into();
                }
            }
        }
    }
}