httpd: Add DELETE route to remove sessions

Signed-off-by: Sebastian Martinez <me@sebastinez.dev>
This commit is contained in:
Sebastian Martinez 2023-02-06 11:30:10 -03:00 committed by Alexis Sellier
parent ed9900b5ac
commit 1128842457
No known key found for this signature in database
1 changed files with 20 additions and 1 deletions

View File

@ -4,6 +4,7 @@ use axum::extract::State;
use axum::response::IntoResponse;
use axum::routing::{post, put};
use axum::{Json, Router};
use axum_auth::AuthBearer;
use hyper::StatusCode;
use radicle::crypto::{PublicKey, Signature};
use serde::{Deserialize, Serialize};
@ -23,7 +24,9 @@ pub fn router(ctx: Context) -> Router {
.route("/sessions", post(session_create_handler))
.route(
"/sessions/:id",
put(session_signin_handler).get(session_handler),
put(session_signin_handler)
.get(session_handler)
.delete(session_delete_handler),
)
.with_state(ctx)
}
@ -103,6 +106,22 @@ async fn session_signin_handler(
Err(Error::Auth("Session already authorized"))
}
/// Delete session.
/// `DELETE /sessions/:id`
async fn session_delete_handler(
State(ctx): State<Context>,
AuthBearer(token): AuthBearer,
Path(session_id): Path<String>,
) -> impl IntoResponse {
if token != session_id {
return Err(Error::Auth("Not authorized to delete this session"));
}
let mut sessions = ctx.sessions.write().await;
sessions.remove_entry(&token).ok_or(Error::NotFound)?;
Ok::<_, Error>(Json(json!({ "success": true })))
}
#[cfg(test)]
mod routes {
use axum::body::Body;