use axum::{extract, Json}; use chartered_db::users::UserSession; use chartered_db::{users::User, ConnectionPool}; use serde::Serialize; use std::sync::Arc; use thiserror::Error; pub async fn handle_get( extract::Extension(db): extract::Extension, extract::Extension(user): extract::Extension>, ) -> Result, Error> { let sessions = UserSession::list(db.clone(), user.id).await?; Ok(Json(Response { sessions: sessions .into_iter() .map(|(session, ssh_key)| ResponseSession { uuid: session.uuid.0, expires_at: session.expires_at, user_agent: session.user_agent, ip: session.ip, ssh_key_fingerprint: ssh_key .map(|v| v.fingerprint().unwrap_or_else(|_| "INVALID".to_string())), }) .collect(), })) } #[derive(Serialize)] pub struct Response { sessions: Vec, } #[derive(Serialize)] pub struct ResponseSession { uuid: chartered_db::uuid::Uuid, expires_at: Option, user_agent: Option, ip: Option, ssh_key_fingerprint: Option, } #[derive(Error, Debug)] pub enum Error { #[error("{0}")] Database(#[from] chartered_db::Error), } impl Error { pub fn status_code(&self) -> axum::http::StatusCode { match self { Self::Database(e) => e.status_code(), } } } define_error_response!(Error);