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:
parent
65763a2ad2
commit
d2202a07ab
|
|
@ -1,8 +1,12 @@
|
||||||
use radicle::crypto::PublicKey;
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use time::serde::timestamp;
|
use time::serde::timestamp;
|
||||||
use time::{Duration, OffsetDateTime};
|
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 UNAUTHORIZED_SESSIONS_EXPIRATION: Duration = Duration::seconds(60);
|
||||||
pub const AUTHORIZED_SESSIONS_EXPIRATION: Duration = Duration::weeks(1);
|
pub const AUTHORIZED_SESSIONS_EXPIRATION: Duration = Duration::weeks(1);
|
||||||
|
|
||||||
|
|
@ -23,3 +27,16 @@ pub struct Session {
|
||||||
#[serde(with = "timestamp")]
|
#[serde(with = "timestamp")]
|
||||||
pub expires_at: OffsetDateTime,
|
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(
|
.route(
|
||||||
"/projects/:project/patches/:id",
|
"/projects/:project/patches/:id",
|
||||||
get(patch_handler).patch(patch_update_handler),
|
patch(patch_update_handler).get(patch_handler),
|
||||||
)
|
)
|
||||||
.with_state(ctx)
|
.with_state(ctx)
|
||||||
}
|
}
|
||||||
|
|
@ -481,8 +481,7 @@ async fn issue_create_handler(
|
||||||
Path(project): Path<Id>,
|
Path(project): Path<Id>,
|
||||||
Json(issue): Json<IssueCreate>,
|
Json(issue): Json<IssueCreate>,
|
||||||
) -> impl IntoResponse {
|
) -> impl IntoResponse {
|
||||||
let sessions = ctx.sessions.read().await;
|
api::auth::validate(&ctx, &token).await?;
|
||||||
sessions.get(&token).ok_or(Error::Auth("Unauthorized"))?;
|
|
||||||
let storage = &ctx.profile.storage;
|
let storage = &ctx.profile.storage;
|
||||||
let signer = ctx
|
let signer = ctx
|
||||||
.profile
|
.profile
|
||||||
|
|
@ -514,12 +513,7 @@ async fn issue_update_handler(
|
||||||
Path((project, issue_id)): Path<(Id, Oid)>,
|
Path((project, issue_id)): Path<(Id, Oid)>,
|
||||||
Json(action): Json<issue::Action>,
|
Json(action): Json<issue::Action>,
|
||||||
) -> impl IntoResponse {
|
) -> impl IntoResponse {
|
||||||
ctx.sessions
|
api::auth::validate(&ctx, &token).await?;
|
||||||
.write()
|
|
||||||
.await
|
|
||||||
.get(&token)
|
|
||||||
.ok_or(Error::Auth("Unauthorized"))?;
|
|
||||||
|
|
||||||
let storage = &ctx.profile.storage;
|
let storage = &ctx.profile.storage;
|
||||||
let signer = ctx.profile.signer().unwrap();
|
let signer = ctx.profile.signer().unwrap();
|
||||||
let repo = storage.repository(project)?;
|
let repo = storage.repository(project)?;
|
||||||
|
|
@ -596,11 +590,7 @@ async fn patch_create_handler(
|
||||||
Path(project): Path<Id>,
|
Path(project): Path<Id>,
|
||||||
Json(patch): Json<PatchCreate>,
|
Json(patch): Json<PatchCreate>,
|
||||||
) -> impl IntoResponse {
|
) -> impl IntoResponse {
|
||||||
ctx.sessions
|
api::auth::validate(&ctx, &token).await?;
|
||||||
.read()
|
|
||||||
.await
|
|
||||||
.get(&token)
|
|
||||||
.ok_or(Error::Auth("Unauthorized"))?;
|
|
||||||
let storage = &ctx.profile.storage;
|
let storage = &ctx.profile.storage;
|
||||||
let signer = ctx
|
let signer = ctx
|
||||||
.profile
|
.profile
|
||||||
|
|
@ -635,11 +625,7 @@ async fn patch_update_handler(
|
||||||
Path((project, patch_id)): Path<(Id, Oid)>,
|
Path((project, patch_id)): Path<(Id, Oid)>,
|
||||||
Json(action): Json<patch::Action>,
|
Json(action): Json<patch::Action>,
|
||||||
) -> impl IntoResponse {
|
) -> impl IntoResponse {
|
||||||
ctx.sessions
|
api::auth::validate(&ctx, &token).await?;
|
||||||
.write()
|
|
||||||
.await
|
|
||||||
.get(&token)
|
|
||||||
.ok_or(Error::Auth("Unauthorized"))?;
|
|
||||||
let storage = &ctx.profile.storage;
|
let storage = &ctx.profile.storage;
|
||||||
let signer = ctx
|
let signer = ctx
|
||||||
.profile
|
.profile
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue