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::{Route, Server, listener::TcpListener};
|
||||||
use poem_openapi::{
|
use poem_openapi::{
|
||||||
ApiResponse, Object, OpenApi, OpenApiService, SecurityScheme, auth::Bearer, payload::Json,
|
ApiResponse, Object, OpenApi, OpenApiService, SecurityScheme,
|
||||||
payload::PlainText,
|
auth::{Basic, Bearer},
|
||||||
|
payload::{Json, PlainText},
|
||||||
};
|
};
|
||||||
use tokio::task::spawn_blocking;
|
use tokio::task::spawn_blocking;
|
||||||
|
|
||||||
struct Bookmark {
|
struct Bookmark {
|
||||||
|
#[allow(unused)]
|
||||||
id: i32,
|
id: i32,
|
||||||
title: String,
|
title: String,
|
||||||
url: 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)]
|
#[derive(Object, Clone)]
|
||||||
struct BookmarkRequest {
|
struct BookmarkRequest {
|
||||||
title: String,
|
title: String,
|
||||||
@@ -56,11 +83,7 @@ struct Api;
|
|||||||
#[OpenApi]
|
#[OpenApi]
|
||||||
impl Api {
|
impl Api {
|
||||||
#[oai(path = "/add", method = "post")]
|
#[oai(path = "/add", method = "post")]
|
||||||
async fn post_bookmark(
|
async fn post_bookmark(&self, body: Json<BookmarkRequest>, auth: Auth) -> AddBookmarkResponse {
|
||||||
&self,
|
|
||||||
body: Json<BookmarkRequest>,
|
|
||||||
auth: BearerAuth,
|
|
||||||
) -> AddBookmarkResponse {
|
|
||||||
if !auth.check() {
|
if !auth.check() {
|
||||||
return AddBookmarkResponse::NotAuthorized;
|
return AddBookmarkResponse::NotAuthorized;
|
||||||
}
|
}
|
||||||
@@ -92,7 +115,7 @@ impl Api {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[oai(path = "/feed", method = "get")]
|
#[oai(path = "/feed", method = "get")]
|
||||||
async fn get_feed(&self, auth: BearerAuth) -> FeedResponse {
|
async fn get_feed(&self, auth: Auth) -> FeedResponse {
|
||||||
if !auth.check() {
|
if !auth.check() {
|
||||||
return FeedResponse::NotAuthorized;
|
return FeedResponse::NotAuthorized;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user