From cc5528f1700ad0915f8d472d5234898f967b8d4d Mon Sep 17 00:00:00 2001 From: Akbar Rahman Date: Thu, 11 Sep 2025 23:52:17 +0100 Subject: [PATCH] add password option for auth --- src/main.rs | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index dc3b494..717c5d5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,12 +2,14 @@ use std::{env, error::Error}; use poem::{Route, Server, listener::TcpListener}; use poem_openapi::{ - ApiResponse, Object, OpenApi, OpenApiService, SecurityScheme, auth::Bearer, payload::Json, - payload::PlainText, + ApiResponse, Object, OpenApi, OpenApiService, SecurityScheme, + auth::{Basic, Bearer}, + payload::{Json, PlainText}, }; use tokio::task::spawn_blocking; struct Bookmark { + #[allow(unused)] id: i32, title: String, url: String, @@ -24,6 +26,31 @@ impl BearerAuth { } } +#[derive(SecurityScheme)] +#[oai(ty = "basic")] +struct BasicAuth(Basic); + +impl BasicAuth { + fn check(&self) -> bool { + self.0.password == std::env::var("MINIBOOKFLUXMARK_ACCESS_TOKEN").unwrap() + } +} + +#[derive(SecurityScheme)] +enum Auth { + BearerAuth(BearerAuth), + BasicAuth(BasicAuth), +} + +impl Auth { + fn check(&self) -> bool { + match self { + Self::BasicAuth(a) => a.check(), + Self::BearerAuth(a) => a.check(), + } + } +} + #[derive(Object, Clone)] struct BookmarkRequest { title: String, @@ -56,11 +83,7 @@ struct Api; #[OpenApi] impl Api { #[oai(path = "/add", method = "post")] - async fn post_bookmark( - &self, - body: Json, - auth: BearerAuth, - ) -> AddBookmarkResponse { + async fn post_bookmark(&self, body: Json, auth: Auth) -> AddBookmarkResponse { if !auth.check() { return AddBookmarkResponse::NotAuthorized; } @@ -92,7 +115,7 @@ impl Api { } #[oai(path = "/feed", method = "get")] - async fn get_feed(&self, auth: BearerAuth) -> FeedResponse { + async fn get_feed(&self, auth: Auth) -> FeedResponse { if !auth.check() { return FeedResponse::NotAuthorized; }