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
|
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(),
}
}
}
|