summaryrefslogtreecommitdiff
path: root/src/user/routes/logout/post.rs
blob: bb09b9f49ce4a49a17ef3b1fb0e9a14cc3f3f3db (plain)
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
use axum::{
    extract::{Json, State},
    http::StatusCode,
    response::{IntoResponse, Response},
};

use crate::{
    app::App,
    clock::RequestedAt,
    error::{Internal, Unauthorized},
    token::{app, extract::IdentityCookie},
};

pub async fn handler(
    State(app): State<App>,
    RequestedAt(now): RequestedAt,
    identity: IdentityCookie,
    Json(_): Json<Request>,
) -> Result<(IdentityCookie, 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(),
        }
    }
}