add password option for auth
This commit is contained in:
39
src/main.rs
39
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<BookmarkRequest>,
|
||||
auth: BearerAuth,
|
||||
) -> AddBookmarkResponse {
|
||||
async fn post_bookmark(&self, body: Json<BookmarkRequest>, 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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user