diff options
| author | Owen Jacobson <owen@grimoire.ca> | 2024-10-16 20:14:33 -0400 |
|---|---|---|
| committer | Owen Jacobson <owen@grimoire.ca> | 2024-10-16 20:14:33 -0400 |
| commit | ea74daca4809e4008dd8d01039db9fff3be659d9 (patch) | |
| tree | 5972cabf646e8d5e635e9e2a176bff56c178461a /src/login/routes/logout/post.rs | |
| parent | 56e16e29db55dae84549229d24b971f8bcf7da21 (diff) | |
Organizational pass on endpoints and routes.
Diffstat (limited to 'src/login/routes/logout/post.rs')
| -rw-r--r-- | src/login/routes/logout/post.rs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/login/routes/logout/post.rs b/src/login/routes/logout/post.rs new file mode 100644 index 0000000..6b7a62a --- /dev/null +++ b/src/login/routes/logout/post.rs @@ -0,0 +1,47 @@ +use axum::{ + extract::{Json, State}, + http::StatusCode, + response::{IntoResponse, Response}, +}; + +use crate::{ + app::App, + clock::RequestedAt, + error::{Internal, Unauthorized}, + token::{app, extract::IdentityToken}, +}; + +pub async fn handler( + State(app): State<App>, + RequestedAt(now): RequestedAt, + identity: IdentityToken, + Json(_): Json<Request>, +) -> Result<(IdentityToken, StatusCode), Error> { + if let Some(secret) = identity.secret() { + let (token, _) = app.tokens().validate(&secret, &now).await?; + app.tokens().logout(&token).await?; + } + + let identity = identity.clear(); + Ok((identity, StatusCode::NO_CONTENT)) +} + +// This forces the only valid request to be `{}`, and not the infinite +// variation allowed when there's no body extractor. +#[derive(Default, serde::Deserialize)] +pub struct Request {} + +#[derive(Debug, thiserror::Error)] +#[error(transparent)] +pub struct Error(#[from] pub app::ValidateError); + +impl IntoResponse for Error { + fn into_response(self) -> Response { + let Self(error) = self; + #[allow(clippy::match_wildcard_for_single_variants)] + match error { + app::ValidateError::InvalidToken => Unauthorized.into_response(), + other => Internal::from(other).into_response(), + } + } +} |
