summaryrefslogtreecommitdiff
path: root/src/invite
diff options
context:
space:
mode:
Diffstat (limited to 'src/invite')
-rw-r--r--src/invite/app.rs27
-rw-r--r--src/invite/mod.rs8
-rw-r--r--src/invite/repo.rs66
-rw-r--r--src/invite/routes.rs97
-rw-r--r--src/invite/routes/invite/get.rs39
-rw-r--r--src/invite/routes/invite/mod.rs4
-rw-r--r--src/invite/routes/invite/post.rs52
-rw-r--r--src/invite/routes/mod.rs16
-rw-r--r--src/invite/routes/post.rs19
9 files changed, 174 insertions, 154 deletions
diff --git a/src/invite/app.rs b/src/invite/app.rs
index 6800d72..64ba753 100644
--- a/src/invite/app.rs
+++ b/src/invite/app.rs
@@ -7,6 +7,7 @@ use crate::{
db::{Duplicate as _, NotFound as _},
event::repo::Provider as _,
login::{repo::Provider as _, Login, Password},
+ name::Name,
token::{repo::Provider as _, Secret},
};
@@ -31,13 +32,9 @@ impl<'a> Invites<'a> {
Ok(invite)
}
- pub async fn get(&self, invite: &Id) -> Result<Summary, Error> {
+ pub async fn get(&self, invite: &Id) -> Result<Option<Summary>, sqlx::Error> {
let mut tx = self.db.begin().await?;
- let invite = tx
- .invites()
- .summary(invite)
- .await
- .not_found(|| Error::NotFound(invite.clone()))?;
+ let invite = tx.invites().summary(invite).await.optional()?;
tx.commit().await?;
Ok(invite)
@@ -46,10 +43,10 @@ impl<'a> Invites<'a> {
pub async fn accept(
&self,
invite: &Id,
- name: &str,
+ name: &Name,
password: &Password,
accepted_at: &DateTime,
- ) -> Result<Secret, AcceptError> {
+ ) -> Result<(Login, Secret), AcceptError> {
let mut tx = self.db.begin().await?;
let invite = tx
.invites()
@@ -72,11 +69,11 @@ impl<'a> Invites<'a> {
.logins()
.create(name, &password_hash, &created)
.await
- .duplicate(|| AcceptError::DuplicateLogin(name.into()))?;
+ .duplicate(|| AcceptError::DuplicateLogin(name.clone()))?;
let secret = tx.tokens().issue(&login, accepted_at).await?;
tx.commit().await?;
- Ok(secret)
+ Ok((login.as_created(), secret))
}
pub async fn expire(&self, relative_to: &DateTime) -> Result<(), sqlx::Error> {
@@ -92,19 +89,11 @@ impl<'a> Invites<'a> {
}
#[derive(Debug, thiserror::Error)]
-pub enum Error {
- #[error("invite not found: {0}")]
- NotFound(Id),
- #[error(transparent)]
- Database(#[from] sqlx::Error),
-}
-
-#[derive(Debug, thiserror::Error)]
pub enum AcceptError {
#[error("invite not found: {0}")]
NotFound(Id),
#[error("name in use: {0}")]
- DuplicateLogin(String),
+ DuplicateLogin(Name),
#[error(transparent)]
Database(#[from] sqlx::Error),
#[error(transparent)]
diff --git a/src/invite/mod.rs b/src/invite/mod.rs
index 5f9d490..d59fb9c 100644
--- a/src/invite/mod.rs
+++ b/src/invite/mod.rs
@@ -3,10 +3,7 @@ mod id;
mod repo;
mod routes;
-use crate::{
- clock::DateTime,
- login::{self, Login},
-};
+use crate::{clock::DateTime, login, normalize::nfc};
pub use self::{id::Id, routes::router};
@@ -19,6 +16,7 @@ pub struct Invite {
#[derive(serde::Serialize)]
pub struct Summary {
- pub issuer: Login,
+ pub id: Id,
+ pub issuer: nfc::String,
pub issued_at: DateTime,
}
diff --git a/src/invite/repo.rs b/src/invite/repo.rs
index 2ab993f..5f86e49 100644
--- a/src/invite/repo.rs
+++ b/src/invite/repo.rs
@@ -4,6 +4,7 @@ use super::{Id, Invite, Summary};
use crate::{
clock::DateTime,
login::{self, Login},
+ normalize::nfc,
};
pub trait Provider {
@@ -28,13 +29,13 @@ impl<'c> Invites<'c> {
let invite = sqlx::query_as!(
Invite,
r#"
- insert into invite (id, issuer, issued_at)
- values ($1, $2, $3)
- returning
- id as "id: Id",
- issuer as "issuer: login::Id",
- issued_at as "issued_at: DateTime"
- "#,
+ insert into invite (id, issuer, issued_at)
+ values ($1, $2, $3)
+ returning
+ id as "id: Id",
+ issuer as "issuer: login::Id",
+ issued_at as "issued_at: DateTime"
+ "#,
id,
issuer.id,
issued_at
@@ -49,13 +50,13 @@ impl<'c> Invites<'c> {
let invite = sqlx::query_as!(
Invite,
r#"
- select
- id as "id: Id",
- issuer as "issuer: login::Id",
- issued_at as "issued_at: DateTime"
- from invite
- where id = $1
- "#,
+ select
+ id as "id: Id",
+ issuer as "issuer: login::Id",
+ issued_at as "issued_at: DateTime"
+ from invite
+ where id = $1
+ "#,
invite,
)
.fetch_one(&mut *self.0)
@@ -67,21 +68,20 @@ impl<'c> Invites<'c> {
pub async fn summary(&mut self, invite: &Id) -> Result<Summary, sqlx::Error> {
let invite = sqlx::query!(
r#"
- select
- issuer.id as "issuer_id: login::Id",
- issuer.name as "issuer_name",
- invite.issued_at as "invite_issued_at: DateTime"
- from invite
- join login as issuer on (invite.issuer = issuer.id)
- where invite.id = $1
- "#,
+ select
+ invite.id as "invite_id: Id",
+ issuer.id as "issuer_id: login::Id",
+ issuer.display_name as "issuer_name: nfc::String",
+ invite.issued_at as "invite_issued_at: DateTime"
+ from invite
+ join login as issuer on (invite.issuer = issuer.id)
+ where invite.id = $1
+ "#,
invite,
)
.map(|row| Summary {
- issuer: Login {
- id: row.issuer_id,
- name: row.issuer_name,
- },
+ id: row.invite_id,
+ issuer: row.issuer_name,
issued_at: row.invite_issued_at,
})
.fetch_one(&mut *self.0)
@@ -93,10 +93,10 @@ impl<'c> Invites<'c> {
pub async fn accept(&mut self, invite: &Invite) -> Result<(), sqlx::Error> {
sqlx::query_scalar!(
r#"
- delete from invite
- where id = $1
- returning 1 as "deleted: bool"
- "#,
+ delete from invite
+ where id = $1
+ returning 1 as "deleted: bool"
+ "#,
invite.id,
)
.fetch_one(&mut *self.0)
@@ -108,9 +108,9 @@ impl<'c> Invites<'c> {
pub async fn expire(&mut self, expire_at: &DateTime) -> Result<(), sqlx::Error> {
sqlx::query!(
r#"
- delete from invite
- where issued_at < $1
- "#,
+ delete from invite
+ where issued_at < $1
+ "#,
expire_at,
)
.execute(&mut *self.0)
diff --git a/src/invite/routes.rs b/src/invite/routes.rs
deleted file mode 100644
index 977fe9b..0000000
--- a/src/invite/routes.rs
+++ /dev/null
@@ -1,97 +0,0 @@
-use axum::{
- extract::{Json, Path, State},
- http::StatusCode,
- response::{IntoResponse, Response},
- routing::{get, post},
- Router,
-};
-
-use super::{app, Id, Invite, Summary};
-use crate::{
- app::App,
- clock::RequestedAt,
- error::{Internal, NotFound},
- login::{Login, Password},
- token::extract::IdentityToken,
-};
-
-pub fn router() -> Router<App> {
- Router::new()
- .route("/api/invite", post(on_invite))
- .route("/api/invite/:invite", get(invite))
- .route("/api/invite/:invite", post(on_accept))
-}
-
-#[derive(serde::Deserialize)]
-struct InviteRequest {}
-
-async fn on_invite(
- State(app): State<App>,
- RequestedAt(issued_at): RequestedAt,
- login: Login,
- // Require `{}` as the only valid request for this endpoint.
- _: Json<InviteRequest>,
-) -> Result<Json<Invite>, Internal> {
- let invite = app.invites().create(&login, &issued_at).await?;
- Ok(Json(invite))
-}
-
-async fn invite(
- State(app): State<App>,
- Path(invite): Path<Id>,
-) -> Result<Json<Summary>, InviteError> {
- app.invites()
- .get(&invite)
- .await
- .map(Json)
- .map_err(InviteError)
-}
-
-struct InviteError(app::Error);
-
-impl IntoResponse for InviteError {
- fn into_response(self) -> Response {
- let Self(error) = self;
- match error {
- error @ app::Error::NotFound(_) => NotFound(error).into_response(),
- other => Internal::from(other).into_response(),
- }
- }
-}
-
-#[derive(serde::Deserialize)]
-struct AcceptRequest {
- name: String,
- password: Password,
-}
-
-async fn on_accept(
- State(app): State<App>,
- RequestedAt(accepted_at): RequestedAt,
- identity: IdentityToken,
- Path(invite): Path<Id>,
- Json(request): Json<AcceptRequest>,
-) -> Result<(IdentityToken, StatusCode), AcceptError> {
- let secret = app
- .invites()
- .accept(&invite, &request.name, &request.password, &accepted_at)
- .await
- .map_err(AcceptError)?;
- let identity = identity.set(secret);
- Ok((identity, StatusCode::NO_CONTENT))
-}
-
-struct AcceptError(app::AcceptError);
-
-impl IntoResponse for AcceptError {
- fn into_response(self) -> Response {
- let Self(error) = self;
- match error {
- error @ app::AcceptError::NotFound(_) => NotFound(error).into_response(),
- error @ app::AcceptError::DuplicateLogin(_) => {
- (StatusCode::CONFLICT, error.to_string()).into_response()
- }
- other => Internal::from(other).into_response(),
- }
- }
-}
diff --git a/src/invite/routes/invite/get.rs b/src/invite/routes/invite/get.rs
new file mode 100644
index 0000000..c8b52f1
--- /dev/null
+++ b/src/invite/routes/invite/get.rs
@@ -0,0 +1,39 @@
+use axum::{
+ extract::{Json, Path, State},
+ response::{IntoResponse, Response},
+};
+
+use crate::{
+ app::App,
+ error::{Internal, NotFound},
+ invite::{Id, Summary},
+};
+
+pub async fn handler(
+ State(app): State<App>,
+ Path(invite): Path<super::PathInfo>,
+) -> Result<Json<Summary>, Error> {
+ app.invites()
+ .get(&invite)
+ .await?
+ .map(Json)
+ .ok_or_else(move || Error::NotFound(invite))
+}
+
+#[derive(Debug, thiserror::Error)]
+pub enum Error {
+ #[error("invite not found: {0}")]
+ NotFound(Id),
+ #[error(transparent)]
+ Database(#[from] sqlx::Error),
+}
+
+impl IntoResponse for Error {
+ fn into_response(self) -> Response {
+ #[allow(clippy::match_wildcard_for_single_variants)]
+ match self {
+ Self::NotFound(_) => NotFound(self).into_response(),
+ other => Internal::from(other).into_response(),
+ }
+ }
+}
diff --git a/src/invite/routes/invite/mod.rs b/src/invite/routes/invite/mod.rs
new file mode 100644
index 0000000..04593fd
--- /dev/null
+++ b/src/invite/routes/invite/mod.rs
@@ -0,0 +1,4 @@
+pub mod get;
+pub mod post;
+
+type PathInfo = crate::invite::Id;
diff --git a/src/invite/routes/invite/post.rs b/src/invite/routes/invite/post.rs
new file mode 100644
index 0000000..3ca4e6b
--- /dev/null
+++ b/src/invite/routes/invite/post.rs
@@ -0,0 +1,52 @@
+use axum::{
+ extract::{Json, Path, State},
+ http::StatusCode,
+ response::{IntoResponse, Response},
+};
+
+use crate::{
+ app::App,
+ clock::RequestedAt,
+ error::{Internal, NotFound},
+ invite::app,
+ login::{Login, Password},
+ name::Name,
+ token::extract::IdentityCookie,
+};
+
+pub async fn handler(
+ State(app): State<App>,
+ RequestedAt(accepted_at): RequestedAt,
+ identity: IdentityCookie,
+ Path(invite): Path<super::PathInfo>,
+ Json(request): Json<Request>,
+) -> Result<(IdentityCookie, Json<Login>), Error> {
+ let (login, secret) = app
+ .invites()
+ .accept(&invite, &request.name, &request.password, &accepted_at)
+ .await
+ .map_err(Error)?;
+ let identity = identity.set(secret);
+ Ok((identity, Json(login)))
+}
+
+#[derive(serde::Deserialize)]
+pub struct Request {
+ pub name: Name,
+ pub password: Password,
+}
+
+pub struct Error(app::AcceptError);
+
+impl IntoResponse for Error {
+ fn into_response(self) -> Response {
+ let Self(error) = self;
+ match error {
+ app::AcceptError::NotFound(_) => NotFound(error).into_response(),
+ app::AcceptError::DuplicateLogin(_) => {
+ (StatusCode::CONFLICT, error.to_string()).into_response()
+ }
+ other => Internal::from(other).into_response(),
+ }
+ }
+}
diff --git a/src/invite/routes/mod.rs b/src/invite/routes/mod.rs
new file mode 100644
index 0000000..dae20ba
--- /dev/null
+++ b/src/invite/routes/mod.rs
@@ -0,0 +1,16 @@
+use axum::{
+ routing::{get, post},
+ Router,
+};
+
+use crate::app::App;
+
+mod invite;
+mod post;
+
+pub fn router() -> Router<App> {
+ Router::new()
+ .route("/api/invite", post(post::handler))
+ .route("/api/invite/:invite", get(invite::get::handler))
+ .route("/api/invite/:invite", post(invite::post::handler))
+}
diff --git a/src/invite/routes/post.rs b/src/invite/routes/post.rs
new file mode 100644
index 0000000..eb7d706
--- /dev/null
+++ b/src/invite/routes/post.rs
@@ -0,0 +1,19 @@
+use axum::extract::{Json, State};
+
+use crate::{
+ app::App, clock::RequestedAt, error::Internal, invite::Invite, token::extract::Identity,
+};
+
+pub async fn handler(
+ State(app): State<App>,
+ RequestedAt(issued_at): RequestedAt,
+ identity: Identity,
+ _: Json<Request>,
+) -> Result<Json<Invite>, Internal> {
+ let invite = app.invites().create(&identity.login, &issued_at).await?;
+ Ok(Json(invite))
+}
+
+// Require `{}` as the only valid request for this endpoint.
+#[derive(Default, serde::Deserialize)]
+pub struct Request {}