mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-09 13:40:28 +08:00
httpd: Add validate fn to auth module
Check for AuthState and expiry. Signed-off-by: Sebastian Martinez <me@sebastinez.dev>
This commit is contained in:
committed by
Alexis Sellier
parent
b1169319c3
commit
7e681e7d7c
@@ -1,8 +1,12 @@
|
||||
use radicle::crypto::PublicKey;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use time::serde::timestamp;
|
||||
use time::{Duration, OffsetDateTime};
|
||||
|
||||
use radicle::crypto::PublicKey;
|
||||
|
||||
use crate::api::error::Error;
|
||||
use crate::api::Context;
|
||||
|
||||
pub const UNAUTHORIZED_SESSIONS_EXPIRATION: Duration = Duration::seconds(60);
|
||||
pub const AUTHORIZED_SESSIONS_EXPIRATION: Duration = Duration::weeks(1);
|
||||
|
||||
@@ -23,3 +27,16 @@ pub struct Session {
|
||||
#[serde(with = "timestamp")]
|
||||
pub expires_at: OffsetDateTime,
|
||||
}
|
||||
|
||||
pub async fn validate(ctx: &Context, token: &str) -> Result<(), Error> {
|
||||
let sessions_store = ctx.sessions.read().await;
|
||||
let session = sessions_store
|
||||
.get(token)
|
||||
.ok_or(Error::Auth("Unauthorized"))?;
|
||||
|
||||
if session.status != AuthState::Authorized || session.expires_at <= OffsetDateTime::now_utc() {
|
||||
return Err(Error::Auth("Unauthorized"));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ pub fn router(ctx: Context) -> Router {
|
||||
)
|
||||
.route(
|
||||
"/projects/:project/patches/:id",
|
||||
get(patch_handler).patch(patch_update_handler),
|
||||
patch(patch_update_handler).get(patch_handler),
|
||||
)
|
||||
.with_state(ctx)
|
||||
}
|
||||
@@ -481,8 +481,7 @@ async fn issue_create_handler(
|
||||
Path(project): Path<Id>,
|
||||
Json(issue): Json<IssueCreate>,
|
||||
) -> impl IntoResponse {
|
||||
let sessions = ctx.sessions.read().await;
|
||||
sessions.get(&token).ok_or(Error::Auth("Unauthorized"))?;
|
||||
api::auth::validate(&ctx, &token).await?;
|
||||
let storage = &ctx.profile.storage;
|
||||
let signer = ctx
|
||||
.profile
|
||||
@@ -514,12 +513,7 @@ async fn issue_update_handler(
|
||||
Path((project, issue_id)): Path<(Id, Oid)>,
|
||||
Json(action): Json<issue::Action>,
|
||||
) -> impl IntoResponse {
|
||||
ctx.sessions
|
||||
.write()
|
||||
.await
|
||||
.get(&token)
|
||||
.ok_or(Error::Auth("Unauthorized"))?;
|
||||
|
||||
api::auth::validate(&ctx, &token).await?;
|
||||
let storage = &ctx.profile.storage;
|
||||
let signer = ctx.profile.signer().unwrap();
|
||||
let repo = storage.repository(project)?;
|
||||
@@ -596,11 +590,7 @@ async fn patch_create_handler(
|
||||
Path(project): Path<Id>,
|
||||
Json(patch): Json<PatchCreate>,
|
||||
) -> impl IntoResponse {
|
||||
ctx.sessions
|
||||
.read()
|
||||
.await
|
||||
.get(&token)
|
||||
.ok_or(Error::Auth("Unauthorized"))?;
|
||||
api::auth::validate(&ctx, &token).await?;
|
||||
let storage = &ctx.profile.storage;
|
||||
let signer = ctx
|
||||
.profile
|
||||
@@ -635,11 +625,7 @@ async fn patch_update_handler(
|
||||
Path((project, patch_id)): Path<(Id, Oid)>,
|
||||
Json(action): Json<patch::Action>,
|
||||
) -> impl IntoResponse {
|
||||
ctx.sessions
|
||||
.write()
|
||||
.await
|
||||
.get(&token)
|
||||
.ok_or(Error::Auth("Unauthorized"))?;
|
||||
api::auth::validate(&ctx, &token).await?;
|
||||
let storage = &ctx.profile.storage;
|
||||
let signer = ctx
|
||||
.profile
|
||||
|
||||
Reference in New Issue
Block a user