🗝
summary refs log tree commit diff
diff options
context:
space:
mode:
authormia <mia@mia.jetzt>2024-05-03 18:25:03 -0700
committermia <mia@mia.jetzt>2024-05-03 18:25:03 -0700
commitc630a3d95fcba117eeeeb03a0c656ef26bea3072 (patch)
tree6e10801f20716bcd45d3bbc113a02908f05129a2
parentcb74c3346428d5e352b66d1f7982f77e0e8b8621 (diff)
downloaddissociate-c630a3d95fcba117eeeeb03a0c656ef26bea3072.tar.gz
dissociate-c630a3d95fcba117eeeeb03a0c656ef26bea3072.zip
refactor scope access endpoints v0.3.0
-rw-r--r--Cargo.lock2
-rw-r--r--Cargo.toml2
-rw-r--r--src/server/account.rs (renamed from src/server/falx.rs)58
-rw-r--r--src/server/mod.rs11
-rw-r--r--src/server/panel.rs30
5 files changed, 41 insertions, 62 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 1fa23b2..1914f79 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -488,7 +488,7 @@ dependencies = [
 
 [[package]]
 name = "dissociate"
-version = "0.2.3"
+version = "0.3.0"
 dependencies = [
  "argon2",
  "axum",
diff --git a/Cargo.toml b/Cargo.toml
index 794e6d9..0298a01 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
 [package]
 name = "dissociate"
-version = "0.2.3"
+version = "0.3.0"
 edition = "2021"
 
 [dependencies]
diff --git a/src/server/falx.rs b/src/server/account.rs
index 9783cef..b2c294f 100644
--- a/src/server/falx.rs
+++ b/src/server/account.rs
@@ -2,43 +2,59 @@ use std::time::SystemTime;
 
 use axum::{
     body::Body,
-    extract::{Path, State},
+    extract::State,
     http::{HeaderMap, StatusCode, Uri},
     response::{IntoResponse, Response},
     routing::get,
     Router,
 };
 use axum_extra::extract::CookieJar;
+use maud::html;
+use tap::Pipe;
 
-use crate::server::store::Store;
+use crate::server::{store::Store, Handoffs, Nevermind};
 
-use super::{ApiState, Handoffs};
+use super::{account_auth, render_html, ApiState};
 
 pub fn bind(app: Router<ApiState>) -> Router<ApiState> {
-    app.route("/check/:token/:scope", get(check_))
-        .route("/handoff", get(handoff))
+    app.route("/", get(get_panel))
+        .route("/scopes", get(get_scopes))
+        .route("/handoff", get(get_handoff))
 }
 
 #[axum::debug_handler(state = ApiState)]
-async fn check_(
-    Path((token, scope)): Path<(String, String)>,
-    State(store): State<Store>,
-) -> Response {
-    let Some((name, _)) = store.check_token(&token).await else {
-        return StatusCode::UNAUTHORIZED.into_response();
-    };
-    let Some(account) = store.get_account(&name).await else {
-        return StatusCode::UNAUTHORIZED.into_response();
-    };
-    if account.scopes.contains(&scope) {
-        StatusCode::OK.into_response()
-    } else {
-        StatusCode::FORBIDDEN.into_response()
-    }
+async fn get_panel(jar: CookieJar, State(store): State<Store>) -> Result<Response, Response> {
+    Ok(account_auth(&jar, &store)
+        .await
+        .prompt_login()?
+        .pipe(render_normal_panel))
+}
+
+fn render_normal_panel(name: String) -> Response {
+    render_html(
+        html!(title { "dissociate" }),
+        html! {
+            p { "currently logged in as " (name) }
+            a href="/logout" { button { "log out" } }
+        },
+    )
+}
+
+#[axum::debug_handler(state = ApiState)]
+async fn get_scopes(jar: CookieJar, State(store): State<Store>) -> Result<Response, Response> {
+    let name = account_auth(&jar, &store).await.prompt_login()?;
+    let account = store.get_account(&name).await.prompt_logout()?;
+    let body = account.scopes.join(" ");
+
+    Ok(Response::builder()
+        .status(StatusCode::OK)
+        .header("Content-Type", "text/plain")
+        .body(body.into())
+        .unwrap())
 }
 
 #[axum::debug_handler(state = ApiState)]
-async fn handoff(
+async fn get_handoff(
     jar: CookieJar,
     State(Handoffs(handoffs)): State<Handoffs>,
     State(store): State<Store>,
diff --git a/src/server/mod.rs b/src/server/mod.rs
index a583f85..b6dd451 100644
--- a/src/server/mod.rs
+++ b/src/server/mod.rs
@@ -1,8 +1,7 @@
+mod account;
 mod admin;
 mod config;
-mod falx;
 mod login;
-mod panel;
 mod store;
 
 use std::{future::IntoFuture, path::PathBuf, sync::Arc};
@@ -42,12 +41,10 @@ pub async fn serve() -> eyre::Result<()> {
 
     let app = Router::new()
         .pipe(login::bind)
-        .pipe(falx::bind)
-        .pipe(panel::bind)
+        .pipe(account::bind)
         .with_state(ApiState {
             store,
             cookie_domain: CookieDomain(config.cookie_domain),
-            web_base: WebBase(config.web_base),
             handoffs: Handoffs(Arc::new(config.handoffs)),
         })
         .fallback(get(|| async {
@@ -73,7 +70,6 @@ pub async fn serve() -> eyre::Result<()> {
 struct ApiState {
     pub store: Store,
     pub cookie_domain: CookieDomain,
-    pub web_base: WebBase,
     pub handoffs: Handoffs,
 }
 
@@ -81,9 +77,6 @@ struct ApiState {
 struct CookieDomain(Option<String>);
 
 #[derive(Clone)]
-struct WebBase(String);
-
-#[derive(Clone)]
 struct Handoffs(Arc<HashSet<String>>);
 
 fn render_html(head: PreEscaped<impl AsRef<str>>, body: PreEscaped<impl AsRef<str>>) -> Response {
diff --git a/src/server/panel.rs b/src/server/panel.rs
deleted file mode 100644
index addb0d8..0000000
--- a/src/server/panel.rs
+++ /dev/null
@@ -1,30 +0,0 @@
-use axum::{extract::State, response::Response, routing::get, Router};
-use axum_extra::extract::CookieJar;
-use maud::html;
-use tap::Pipe;
-
-use crate::server::{store::Store, Nevermind};
-
-use super::{account_auth, render_html, ApiState};
-
-pub fn bind(app: Router<ApiState>) -> Router<ApiState> {
-    app.route("/", get(get_panel))
-}
-
-#[axum::debug_handler(state = ApiState)]
-async fn get_panel(jar: CookieJar, State(store): State<Store>) -> Result<Response, Response> {
-    Ok(account_auth(&jar, &store)
-        .await
-        .prompt_login()?
-        .pipe(render_normal_panel))
-}
-
-fn render_normal_panel(name: String) -> Response {
-    render_html(
-        html!(title { "dissociate" }),
-        html! {
-            p { "currently logged in as " (name) }
-            a href="/logout" { button { "log out" } }
-        },
-    )
-}