httpd: Remove session serialize and rework
Signed-off-by: Sebastian Martinez <me@sebastinez.dev>
This commit is contained in:
parent
34064ef8b5
commit
3d0a68eeb5
|
|
@ -11,21 +11,25 @@ impl Serialize for DateTime {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Serialize)]
|
#[derive(Clone)]
|
||||||
#[serde(tag = "type", rename_all = "camelCase")]
|
|
||||||
pub enum AuthState {
|
pub enum AuthState {
|
||||||
Authorized(Session),
|
Authorized(Session),
|
||||||
Unauthorized {
|
Unauthorized(Session),
|
||||||
public_key: PublicKey,
|
|
||||||
expires_at: DateTime,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// We copy the implementation of siwe::Message here to derive Serialization and Debug
|
#[derive(Clone)]
|
||||||
#[derive(Clone, Serialize)]
|
|
||||||
#[serde(rename_all = "camelCase")]
|
|
||||||
pub struct Session {
|
pub struct Session {
|
||||||
|
pub status: String,
|
||||||
pub public_key: PublicKey,
|
pub public_key: PublicKey,
|
||||||
pub issued_at: DateTime,
|
pub issued_at: DateTime,
|
||||||
pub expires_at: DateTime,
|
pub expires_at: DateTime,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<AuthState> for Session {
|
||||||
|
fn from(other: AuthState) -> Self {
|
||||||
|
match other {
|
||||||
|
AuthState::Authorized(s) => s,
|
||||||
|
AuthState::Unauthorized(s) => s,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
use std::convert::Into;
|
||||||
use std::iter::repeat_with;
|
use std::iter::repeat_with;
|
||||||
|
|
||||||
use axum::extract::State;
|
use axum::extract::State;
|
||||||
|
|
@ -44,12 +45,14 @@ async fn session_create_handler(State(ctx): State<Context>) -> impl IntoResponse
|
||||||
let expiration_time = OffsetDateTime::now_utc()
|
let expiration_time = OffsetDateTime::now_utc()
|
||||||
.checked_add(UNAUTHORIZED_SESSIONS_EXPIRATION)
|
.checked_add(UNAUTHORIZED_SESSIONS_EXPIRATION)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let auth_state = AuthState::Unauthorized {
|
let session = Session {
|
||||||
|
status: String::from("unauthorized"),
|
||||||
public_key: *signer.public_key(),
|
public_key: *signer.public_key(),
|
||||||
|
issued_at: DateTime(OffsetDateTime::now_utc()),
|
||||||
expires_at: DateTime(expiration_time),
|
expires_at: DateTime(expiration_time),
|
||||||
};
|
};
|
||||||
let mut sessions = ctx.sessions.write().await;
|
let mut sessions = ctx.sessions.write().await;
|
||||||
sessions.insert(session_id.clone(), auth_state);
|
sessions.insert(session_id.clone(), AuthState::Unauthorized(session));
|
||||||
|
|
||||||
Ok::<_, Error>(Json(
|
Ok::<_, Error>(Json(
|
||||||
json!({"sessionId": session_id, "publicKey": signer.public_key()}),
|
json!({"sessionId": session_id, "publicKey": signer.public_key()}),
|
||||||
|
|
@ -63,9 +66,12 @@ async fn session_handler(
|
||||||
Path(session_id): Path<String>,
|
Path(session_id): Path<String>,
|
||||||
) -> impl IntoResponse {
|
) -> impl IntoResponse {
|
||||||
let sessions = ctx.sessions.read().await;
|
let sessions = ctx.sessions.read().await;
|
||||||
let session = sessions.get(&session_id).ok_or(Error::NotFound)?.to_owned();
|
let auth_state = sessions.get(&session_id).ok_or(Error::NotFound)?;
|
||||||
|
let session = Session::from(auth_state.clone());
|
||||||
|
|
||||||
Ok::<_, Error>(Json(session))
|
Ok::<_, Error>(Json(
|
||||||
|
json!({"publicKey": session.public_key, "issuedAt": session.issued_at, "expiresAt": session.expires_at}),
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Update session.
|
/// Update session.
|
||||||
|
|
@ -77,15 +83,11 @@ async fn session_signin_handler(
|
||||||
) -> impl IntoResponse {
|
) -> impl IntoResponse {
|
||||||
let mut sessions = ctx.sessions.write().await;
|
let mut sessions = ctx.sessions.write().await;
|
||||||
let session = sessions.get(&session_id).ok_or(Error::NotFound)?;
|
let session = sessions.get(&session_id).ok_or(Error::NotFound)?;
|
||||||
if let AuthState::Unauthorized {
|
if let AuthState::Unauthorized(s) = session {
|
||||||
public_key,
|
if s.public_key != request.pk {
|
||||||
expires_at,
|
|
||||||
} = session
|
|
||||||
{
|
|
||||||
if public_key != &request.pk {
|
|
||||||
return Err(Error::Auth("Invalid public key"));
|
return Err(Error::Auth("Invalid public key"));
|
||||||
}
|
}
|
||||||
if expires_at <= &DateTime(OffsetDateTime::now_utc()) {
|
if s.expires_at <= DateTime(OffsetDateTime::now_utc()) {
|
||||||
return Err(Error::Auth("Session expired"));
|
return Err(Error::Auth("Session expired"));
|
||||||
}
|
}
|
||||||
let payload = format!("{}:{}", session_id, request.pk);
|
let payload = format!("{}:{}", session_id, request.pk);
|
||||||
|
|
@ -97,6 +99,7 @@ async fn session_signin_handler(
|
||||||
.checked_add(AUTHORIZED_SESSIONS_EXPIRATION)
|
.checked_add(AUTHORIZED_SESSIONS_EXPIRATION)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let session = Session {
|
let session = Session {
|
||||||
|
status: String::from("authorized"),
|
||||||
public_key: request.pk,
|
public_key: request.pk,
|
||||||
issued_at: DateTime(OffsetDateTime::now_utc()),
|
issued_at: DateTime(OffsetDateTime::now_utc()),
|
||||||
expires_at: DateTime(expiration_time),
|
expires_at: DateTime(expiration_time),
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue