Skip to main content

coqui_access/
kind.rs

1use guatiao::library::ProviderError;
2
3use crate::model::{
4    AccountInfo, AccountPut, Attempt, AuthorizationDecision, AuthorizationRequest, Challenge,
5    ChallengeRequest, Description, Lookup, Person, PutOutcome, SignInContext, SignInDecision,
6    Verdict, Waiting,
7};
8
9/// Checks credentials and returns only a [`Lookup`] reference.
10#[guatiao::kind(name = "coqui_authenticator")]
11pub trait Authenticator: Send + Sync {
12    /// Checks one attempt without returning person details or credential material.
13    ///
14    /// An error means the authenticator could not find out; it is never a verdict.
15    fn authenticate(&self, attempt: Attempt) -> Result<Verdict, ProviderError>;
16
17    /// What this authenticator is and what it takes. Defaults to nothing said.
18    fn describe(&self) -> Result<Description, ProviderError> {
19        Ok(Description::default())
20    }
21}
22
23/// Resolves an authenticated reference into the authoritative [`Person`] record.
24#[guatiao::kind(name = "coqui_directory")]
25pub trait Directory: Send + Sync {
26    /// Returns the person for `lookup`, or `None` when the reference is unknown or the
27    /// account is disabled.
28    fn lookup(&self, lookup: Lookup) -> Result<Option<Person>, ProviderError>;
29
30    /// What this directory is. Defaults to nothing said.
31    fn describe(&self) -> Result<Description, ProviderError> {
32        Ok(Description::default())
33    }
34}
35
36/// Authorizes one action on one resource for a [`Person`] a directory returned.
37#[guatiao::kind(name = "coqui_authorizer")]
38pub trait Authorizer: Send + Sync {
39    /// Evaluates the request using the supplied authoritative person record.
40    fn authorize(
41        &self,
42        request: AuthorizationRequest,
43    ) -> Result<AuthorizationDecision, ProviderError>;
44}
45
46/// Account management for a provider that keeps its own accounts.
47///
48/// A separate kind, so authentication providers that keep nothing (an LDAP client, a
49/// reverse-proxy header reader) do not implement it. A provider offering this kind is
50/// usually also an [`Authenticator`] and a [`Directory`] over the same store.
51#[guatiao::kind(name = "coqui_accounts")]
52pub trait Accounts: Send + Sync {
53    /// The accounts kept, without anything that proves one.
54    fn accounts(&self) -> Result<Vec<AccountInfo>, ProviderError>;
55
56    /// Makes an account, or changes the one of that name; a field not said is left as it
57    /// is. A provider that makes secrets itself answers with the one it made.
58    fn put(&self, account: AccountPut) -> Result<PutOutcome, ProviderError>;
59
60    /// Takes an account away. `GUATIAO_ERR_NOT_FOUND` when none has that name.
61    fn remove(&self, name: String) -> Result<(), ProviderError>;
62
63    /// What was shown lately and is nobody's yet, and could be accepted as somebody's with
64    /// [`put`](Accounts::put): a peer certificate nobody has accepted. Empty for a
65    /// provider that keeps no such thing.
66    fn waiting(&self) -> Result<Vec<Waiting>, ProviderError> {
67        Ok(Vec::new())
68    }
69}
70
71/// Decides which methods a sign-in may use and what it still needs, from where and how it
72/// is made.
73///
74/// The consumer asks before any credential is checked (what to offer), again for each
75/// method presented, and again once a step has named someone. `Err` means the provider
76/// could not find out; a consumer then offers nothing and refuses, never allowing.
77#[guatiao::kind(name = "coqui_sign_in_policy")]
78pub trait SignInPolicy: Send + Sync {
79    /// The methods that may be presented next, or that the sign-in is complete.
80    fn decide(&self, context: SignInContext) -> Result<SignInDecision, ProviderError>;
81}
82
83/// Issues what a method needs before it can be answered: for a passkey, a challenge the signed
84/// answer is bound to.
85///
86/// A separate kind because a challenged sign-in is two calls and
87/// [`Authenticator::authenticate`] is one: appending a method to that kind would refuse every
88/// provider already built. The answer goes through `authenticate` with
89/// [`Attempt::challenge`](crate::Attempt::challenge) set to what [`Challenge::challenge`] said.
90/// A consumer builds one instance per kind, so a challenge issued by one instance must be
91/// found by the other: a provider keeps it in a table both read, never in memory.
92#[guatiao::kind(name = "coqui_challenger")]
93pub trait Challenger: Send + Sync {
94    /// Makes a challenge for one sign-in. `GUATIAO_ERR_UNSUPPORTED` for a method the provider
95    /// does not challenge, `GUATIAO_ERR_NOT_FOUND` for a named account it cannot challenge, and
96    /// [`error::busy`](crate::error::busy) when the source holds too many already.
97    fn challenge(&self, request: ChallengeRequest) -> Result<Challenge, ProviderError>;
98}