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
//! [`ProviderIdentity`], who a provider says signed in, and the [`IdentityLinker`] an app turns
//! it into its user with.
use ;
use Cx;
use ;
use crateBoxFuture;
/// Who an OpenID Connect provider says signed in, once the panel has checked its ID token: the
/// token came from the provider's token endpoint, names the panel's client, has not expired and
/// carries the nonce this sign-in sent.
/// How a panel turns a [`ProviderIdentity`] into one of the app's users, installed with
/// [`Auth::oidc`](crate::Auth::oidc) through an [`Oidc`](super::Oidc).
///
/// The app stores which of its users each identity is, as a row keyed by
/// [`issuer`](ProviderIdentity::issuer) and [`subject`](ProviderIdentity::subject). A sign-in
/// whose identity no row names may link it to an existing account, or create one, as the app
/// decides; linking by email alone hands that account to whoever controls the address at the
/// provider, so link only an address the provider [verified](ProviderIdentity::email_verified).
///
/// ```rust
/// # use tablo_core::{IdentityLinker, PanelUser, ProviderIdentity};
/// # use topcoat::context::Cx;
/// # #[derive(Debug, Clone, toasty::Model)]
/// # struct Staff {
/// # #[key] #[auto] id: uuid::Uuid,
/// # #[unique] email: String,
/// # name: String,
/// # }
/// # impl PanelUser for Staff {
/// # fn user_id(&self) -> String { self.id.to_string() }
/// # fn display_name(&self) -> &str { &self.name }
/// # fn can_access_panel(&self, _panel: &str) -> bool { true }
/// # }
/// #[derive(Debug, Clone, toasty::Model)]
/// struct StaffIdentity {
/// #[key]
/// #[auto]
/// id: uuid::Uuid,
/// #[index]
/// staff_id: uuid::Uuid,
/// issuer: String,
/// subject: String,
/// }
///
/// struct StaffLinker;
///
/// impl IdentityLinker for StaffLinker {
/// type User = Staff;
///
/// async fn sign_in(
/// &self,
/// _cx: &Cx,
/// identity: &ProviderIdentity,
/// ex: &mut dyn toasty::Executor,
/// ) -> topcoat::Result<Option<Staff>> {
/// let linked = StaffIdentity::filter(
/// StaffIdentity::fields()
/// .issuer()
/// .eq(identity.issuer.clone())
/// .and(
/// StaffIdentity::fields()
/// .subject()
/// .eq(identity.subject.clone()),
/// ),
/// )
/// .first()
/// .exec(&mut *ex)
/// .await?;
/// if let Some(linked) = linked {
/// return Ok(Staff::filter_by_id(linked.staff_id)
/// .first()
/// .exec(ex)
/// .await?);
/// }
/// // Link an existing account only by an address the provider verified.
/// let (Some(email), true) = (&identity.email, identity.email_verified) else {
/// return Ok(None);
/// };
/// let Some(staff) = Staff::filter_by_email(email).first().exec(&mut *ex).await? else {
/// return Ok(None);
/// };
/// toasty::create!(StaffIdentity {
/// staff_id: staff.id,
/// issuer: identity.issuer.clone(),
/// subject: identity.subject.clone(),
/// })
/// .exec(ex)
/// .await?;
/// Ok(Some(staff))
/// }
/// }
/// ```
/// [`IdentityLinker`] with its user type erased, as the panel keeps it.
pub