summaryrefslogtreecommitdiff
path: root/src/login/mod.rs
diff options
context:
space:
mode:
authorOwen Jacobson <owen@grimoire.ca>2024-10-29 20:26:47 -0400
committerOwen Jacobson <owen@grimoire.ca>2024-10-29 20:33:42 -0400
commit9ae0faf4f027caaaf3bc4a42738d4ed31e67852d (patch)
tree69c61f71f38a1e13012f0e7fbd789c6f7bd013ca /src/login/mod.rs
parentda485e523913df28def6335be0836b1fc437617f (diff)
Create a dedicated workflow type for creating logins.
Nasty design corner. Logins need to be created in three places: 1. In tests, using app.logins().create(…); 2. On initial setup, using app.setup().initial(…); and 3. When accepting invites, using app.invites().accept(…). These three places do the same thing with respect to logins, but also do a varying mix of other things. Testing is the simplest and _only_ creates a login. Initial setup and invite acceptance both issue a token for the newly-created login. Accepting an invite also invalidates the invite. Previously, those three functions have been copy-pasted variations on a theme. Now that we have validation, the copy-paste approach is no longer tenable; it will become increasingly hard to ensure that the three functions (plus any future functions) remain in synch. To accommodate the variations while consolidating login creation, I've added a typestate-based state machine, which is driven by method calls: * A creation attempt begins with `let create = Create::begin()`. This always succeeds; it packages up arguments used in later steps, but does nothing else. * A creation attempt can be validated using `let validated = create.validate()?`. This may fail. Input validation and password hashing are carried out at this stage, making it potentially expensive. * A validated attempt can be stored in the DB, using `let stored = validated.store(&mut tx).await?`. This may fail. The login will be written to the DB; the caller is responsible for transaction demarcation, to allow other things to take place in the same transaction. * A fully-stored attempt can be used to publish events, using `let login = stored.publish(self.events)`. This always succeeds, and unwraps the state machine to its final product (a `login::History`).
Diffstat (limited to 'src/login/mod.rs')
-rw-r--r--src/login/mod.rs3
1 files changed, 2 insertions, 1 deletions
diff --git a/src/login/mod.rs b/src/login/mod.rs
index 6d10e17..5a6d715 100644
--- a/src/login/mod.rs
+++ b/src/login/mod.rs
@@ -1,4 +1,5 @@
pub mod app;
+pub mod create;
pub mod event;
mod history;
mod id;
@@ -6,7 +7,7 @@ pub mod password;
pub mod repo;
mod routes;
mod snapshot;
-pub mod validate;
+mod validate;
pub use self::{
event::Event, history::History, id::Id, password::Password, routes::router, snapshot::Login,