Skip to main content

tablo_core/declaration/
auth.rs

1//! The declaration mistakes of a panel's auth: its sign-up, profile, password-reset,
2//! email-verification, tenant registration and tenant settings pages, and the mail they send.
3
4use std::fmt;
5
6/// Why a panel's sign-up page cannot be served:
7/// [`DeclarationErrorKind::SignUp`](super::DeclarationErrorKind::SignUp).
8#[derive(Debug, Clone, PartialEq, Eq)]
9#[non_exhaustive]
10pub enum SignUpFault {
11    /// `Auth::registration` on `Auth::disabled`, which serves no sign-in to land on.
12    ///
13    /// Turn authentication on.
14    AuthDisabled,
15    /// A field named like a key the sign-up POST carries itself: `csrf_token` or `next`.
16    ///
17    /// Rename the field.
18    ReservedField(String),
19    /// A file field, whose upload the sign-up POST does not read.
20    ///
21    /// Ask for the file once the account signs in.
22    FileField(String),
23    /// A relationship choice, whose options would list records to a visitor nobody signed in.
24    ///
25    /// Offer static options.
26    RelationshipField(String),
27}
28
29/// Why a panel's profile page cannot be served:
30/// [`DeclarationErrorKind::Profile`](super::DeclarationErrorKind::Profile).
31#[derive(Debug, Clone, PartialEq, Eq)]
32#[non_exhaustive]
33pub enum ProfileFault {
34    /// `Auth::profile` on `Auth::disabled`, which signs nobody in to edit.
35    ///
36    /// Turn authentication on.
37    AuthDisabled,
38    /// A field named like a key the profile POST carries itself: `csrf_token`.
39    ///
40    /// Rename the field.
41    ReservedField(String),
42    /// A file field, whose upload the profile POST does not read.
43    ///
44    /// Edit the file on a resource's form.
45    FileField(String),
46    /// A relationship choice, whose options no route serves the profile page.
47    ///
48    /// Offer static options.
49    RelationshipField(String),
50}
51
52/// Why a panel's password reset cannot be served:
53/// [`DeclarationErrorKind::PasswordReset`](super::DeclarationErrorKind::PasswordReset).
54#[derive(Debug, Clone, PartialEq, Eq)]
55#[non_exhaustive]
56pub enum ResetFault {
57    /// `Auth::password_reset` on `Auth::disabled`, which signs nobody in after a reset.
58    ///
59    /// Turn authentication on.
60    AuthDisabled,
61}
62
63impl fmt::Display for ResetFault {
64    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65        match self {
66            Self::AuthDisabled => f.write_str(
67                "a password reset needs auth: `Auth::disabled()` serves no sign-in to reset",
68            ),
69        }
70    }
71}
72
73impl fmt::Display for ProfileFault {
74    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75        match self {
76            Self::AuthDisabled => {
77                f.write_str("a profile page needs auth: `Auth::disabled()` signs nobody in to edit")
78            }
79            Self::ReservedField(field) => write!(
80                f,
81                "the profile form declares a field named '{field}', which its POST carries \
82             itself: rename the field"
83            ),
84            Self::FileField(field) => write!(
85                f,
86                "the profile form declares a file field '{field}', which its POST does not \
87             upload: edit the file on a resource's form"
88            ),
89            Self::RelationshipField(field) => write!(
90                f,
91                "the profile form declares a relationship choice '{field}', whose options no \
92             route serves the profile page: offer static options"
93            ),
94        }
95    }
96}
97
98impl fmt::Display for SignUpFault {
99    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
100        match self {
101            Self::AuthDisabled => f.write_str(
102                "registration needs auth: `Auth::disabled()` serves no sign-in for a new \
103             account to land on",
104            ),
105            Self::ReservedField(field) => write!(
106                f,
107                "the sign-up form declares a field named '{field}', which its POST carries \
108             itself: rename the field"
109            ),
110            Self::FileField(field) => write!(
111                f,
112                "the sign-up form declares a file field '{field}', which its POST does not \
113             upload: ask for the file once the account signs in"
114            ),
115            Self::RelationshipField(field) => write!(
116                f,
117                "the sign-up form declares a relationship choice '{field}', which would list \
118             records to a visitor nobody signed in: offer static options"
119            ),
120        }
121    }
122}
123
124/// Why a panel's auth mail cannot be sent:
125/// [`DeclarationErrorKind::AuthMail`](super::DeclarationErrorKind::AuthMail).
126#[derive(Debug, Clone, PartialEq, Eq)]
127#[non_exhaustive]
128pub enum AuthMailFault {
129    /// A feature that mails links or codes, named as its `Auth` method, on an `Auth` without
130    /// `Auth::mail`.
131    ///
132    /// Install a sender with `Auth::mail(AuthMail::new(..))`.
133    Unset {
134        /// The `Auth` method that installs the feature, such as `password_reset`.
135        feature: &'static str,
136    },
137    /// The base URL links start with is not an absolute `http` or `https` URL.
138    ///
139    /// Pass the scheme and host, such as `https://admin.example.com`.
140    BaseUrl(String),
141    /// The router registers no `Mailer` to send the mail through.
142    ///
143    /// Add one with `.with(Mailer::new(..))` before `.panel(..)`, or hand the mail to
144    /// [`AuthMail::delivery`](crate::AuthMail::delivery).
145    MissingMail,
146}
147
148impl fmt::Display for AuthMailFault {
149    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
150        match self {
151            Self::Unset { feature } => write!(
152                f,
153                "`Auth::{feature}` sends mail, but the auth sets no sender: install one with \
154             `Auth::mail(AuthMail::new(..))`"
155            ),
156            Self::BaseUrl(url) => write!(
157                f,
158                "the auth mail's base URL '{url}' is not an absolute http or https URL: pass the \
159             scheme and host the app answers on, such as `https://admin.example.com`"
160            ),
161            Self::MissingMail => f.write_str(
162                "the panel's auth mails links, but the router registers no `Mailer`: add \
163             one with `.with(Mailer::new(..))` before mounting the panel",
164            ),
165        }
166    }
167}
168
169/// Why a panel's email verification cannot be served:
170/// [`DeclarationErrorKind::EmailVerification`](super::DeclarationErrorKind::EmailVerification).
171#[derive(Debug, Clone, PartialEq, Eq)]
172#[non_exhaustive]
173pub enum VerificationFault {
174    /// `Auth::email_verification` on `Auth::disabled`, which signs nobody in to verify.
175    ///
176    /// Turn authentication on.
177    AuthDisabled,
178}
179
180impl fmt::Display for VerificationFault {
181    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
182        match self {
183            Self::AuthDisabled => f.write_str(
184                "email verification needs auth: `Auth::disabled()` signs nobody in to verify",
185            ),
186        }
187    }
188}
189
190/// Why a panel's tenant registration page cannot be served:
191/// [`DeclarationErrorKind::TenantRegistration`](super::DeclarationErrorKind::TenantRegistration).
192#[derive(Debug, Clone, PartialEq, Eq)]
193#[non_exhaustive]
194pub enum TenantRegistrationFault {
195    /// `Auth::tenant_registration` on `Auth::disabled`, which signs nobody in to register one.
196    ///
197    /// Turn authentication on.
198    AuthDisabled,
199    /// A field named like a key the registration POST carries itself: `csrf_token`.
200    ///
201    /// Rename the field.
202    ReservedField(String),
203    /// A file field, whose upload the registration POST does not read.
204    ///
205    /// Ask for the file on a resource's form.
206    FileField(String),
207    /// A relationship choice, whose options no route serves the registration page.
208    ///
209    /// Offer static options.
210    RelationshipField(String),
211}
212
213impl fmt::Display for TenantRegistrationFault {
214    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
215        match self {
216            Self::AuthDisabled => f.write_str(
217                "tenant registration needs auth: `Auth::disabled()` signs nobody in to register \
218             a tenant",
219            ),
220            Self::ReservedField(field) => write!(
221                f,
222                "the tenant registration form declares a field named '{field}', which its POST \
223             carries itself: rename the field"
224            ),
225            Self::FileField(field) => write!(
226                f,
227                "the tenant registration form declares a file field '{field}', which its POST \
228             does not upload: ask for the file on a resource's form"
229            ),
230            Self::RelationshipField(field) => write!(
231                f,
232                "the tenant registration form declares a relationship choice '{field}', whose \
233             options no route serves the registration page: offer static options"
234            ),
235        }
236    }
237}
238
239/// Why a panel's tenant settings page cannot be served:
240/// [`DeclarationErrorKind::TenantProfile`](super::DeclarationErrorKind::TenantProfile).
241#[derive(Debug, Clone, PartialEq, Eq)]
242#[non_exhaustive]
243pub enum TenantProfileFault {
244    /// `Auth::tenant_profile` on `Auth::disabled`, which signs nobody in to edit a tenant.
245    ///
246    /// Turn authentication on.
247    AuthDisabled,
248    /// A field named like a key the settings POST carries itself: `csrf_token`.
249    ///
250    /// Rename the field.
251    ReservedField(String),
252    /// A file field, whose upload the settings POST does not read.
253    ///
254    /// Edit the file on a resource's form.
255    FileField(String),
256    /// A relationship choice, whose options no route serves the settings page.
257    ///
258    /// Offer static options.
259    RelationshipField(String),
260}
261
262impl fmt::Display for TenantProfileFault {
263    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
264        match self {
265            Self::AuthDisabled => f.write_str(
266                "a tenant settings page needs auth: `Auth::disabled()` signs nobody in to edit a \
267             tenant",
268            ),
269            Self::ReservedField(field) => write!(
270                f,
271                "the tenant settings form declares a field named '{field}', which its POST \
272             carries itself: rename the field"
273            ),
274            Self::FileField(field) => write!(
275                f,
276                "the tenant settings form declares a file field '{field}', which its POST does \
277             not upload: edit the file on a resource's form"
278            ),
279            Self::RelationshipField(field) => write!(
280                f,
281                "the tenant settings form declares a relationship choice '{field}', whose \
282             options no route serves the settings page: offer static options"
283            ),
284        }
285    }
286}
287
288/// Why the panel's two-factor sign-in cannot be served:
289/// [`DeclarationErrorKind::TwoFactor`](super::DeclarationErrorKind::TwoFactor).
290#[derive(Debug, Clone, PartialEq, Eq)]
291#[non_exhaustive]
292pub enum TwoFactorFault {
293    /// `Auth::two_factor` on `Auth::disabled`, which signs nobody in.
294    ///
295    /// Turn authentication on.
296    AuthDisabled,
297    /// The router registers no cookie `Key`, which seals the authenticator
298    /// secrets.
299    ///
300    /// Call `.with(Key::from(..))` before `.panel(..)`.
301    MissingKey,
302}
303
304impl fmt::Display for TwoFactorFault {
305    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
306        match self {
307            Self::AuthDisabled => f.write_str(
308                "two-factor sign-in needs auth: `Auth::disabled()` signs nobody in to ask a \
309             second factor of",
310            ),
311            Self::MissingKey => f.write_str(
312                "two-factor sign-in seals each authenticator secret under the router's cookie \
313             key, and the router has none: register a `topcoat::cookie::Key` with `.with(..)`",
314            ),
315        }
316    }
317}
318
319/// Why the panel's provider sign-in cannot be served:
320/// [`DeclarationErrorKind::Oidc`](super::DeclarationErrorKind::Oidc).
321#[derive(Debug, Clone, PartialEq, Eq)]
322#[non_exhaustive]
323pub enum OidcFault {
324    /// `Auth::oidc` on `Auth::disabled`, which signs nobody in.
325    ///
326    /// Turn authentication on.
327    AuthDisabled,
328    /// `Auth::oidc` on `Auth::external`, whose app signs every user in itself.
329    ///
330    /// Sign in through the provider in the app, or use `Auth::custom`.
331    ExternalAuth,
332    /// The router registers no cookie `Key`, which seals a sign-in's secrets while the browser
333    /// is at the provider.
334    ///
335    /// Call `.with(Key::from(..))` before `.panel(..)`.
336    MissingKey,
337    /// `Oidc::new`'s base URL is not an absolute `https` origin.
338    ///
339    /// Pass the app's public origin, such as `https://app.example.com`.
340    BaseUrl(String),
341    /// `Oidc` without a provider.
342    ///
343    /// Add one with `Oidc::provider`.
344    NoProvider,
345    /// A provider key that is not lowercase ASCII letters, digits and `-`.
346    ///
347    /// Rename the provider.
348    ProviderKey(String),
349    /// Two providers with one key.
350    ///
351    /// Give each a distinct key.
352    DuplicateProvider(String),
353    /// A provider whose issuer is not an `https` URL, nor an `http` one on a loopback host.
354    ///
355    /// Fix the issuer.
356    Issuer(String),
357    /// A provider's `param` names an authorization parameter the panel sets: the provider key,
358    /// then the parameter.
359    ///
360    /// Drop the parameter.
361    ReservedParam(String, String),
362}
363
364impl fmt::Display for OidcFault {
365    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
366        match self {
367            Self::AuthDisabled => f.write_str(
368                "provider sign-in needs auth: `Auth::disabled()` signs nobody in to link an \
369                 identity to",
370            ),
371            Self::ExternalAuth => f.write_str(
372                "provider sign-in cannot sit beside `Auth::external`: the app signs every user in \
373                 itself, so sign in through the provider there",
374            ),
375            Self::MissingKey => f.write_str(
376                "provider sign-in seals each pending sign-in under the router's cookie key, and \
377                 the router has none: register a `topcoat::cookie::Key` with `.with(..)`",
378            ),
379            Self::BaseUrl(url) => write!(
380                f,
381                "provider sign-in's base URL '{url}' is not an absolute `https` origin: pass the \
382                 app's public origin, such as `https://app.example.com`"
383            ),
384            Self::NoProvider => {
385                f.write_str("provider sign-in has no provider: add one with `Oidc::provider`")
386            }
387            Self::ProviderKey(key) => write!(
388                f,
389                "provider key '{key}' cannot name a route: use lowercase ASCII letters, digits \
390                 and `-`"
391            ),
392            Self::DuplicateProvider(key) => write!(
393                f,
394                "two providers have the key '{key}': give each a distinct key"
395            ),
396            Self::Issuer(key) => write!(
397                f,
398                "provider '{key}' has an issuer that is not an `https` URL, nor an `http` one on \
399                 a loopback host"
400            ),
401            Self::ReservedParam(key, name) => write!(
402                f,
403                "provider '{key}' sets the authorization parameter '{name}', which the panel \
404                 sets itself: drop it"
405            ),
406        }
407    }
408}