From b2450419eb744854ad5d51d92b3ec9957c62813e Mon Sep 17 00:00:00 2001 From: Fu Zi Xiang Date: Tue, 10 Oct 2023 14:37:12 +0800 Subject: [PATCH] extractor for access token --- admin_frontend/src/access_token.rs | 48 +++++++++++++++++++++++++++++ admin_frontend/src/main.rs | 1 + admin_frontend/src/templates.rs | 4 +++ admin_frontend/src/web_app.rs | 24 +++++++++------ admin_frontend/templates/admin.html | 16 ++++++++++ admin_frontend/templates/home.html | 12 ++++++-- admin_frontend/templates/login.html | 4 +-- admin_frontend/templates/users.html | 36 ++++++++++++++++++++++ 8 files changed, 131 insertions(+), 14 deletions(-) create mode 100644 admin_frontend/src/access_token.rs create mode 100644 admin_frontend/templates/admin.html create mode 100644 admin_frontend/templates/users.html diff --git a/admin_frontend/src/access_token.rs b/admin_frontend/src/access_token.rs new file mode 100644 index 00000000..d7267c2a --- /dev/null +++ b/admin_frontend/src/access_token.rs @@ -0,0 +1,48 @@ +use axum::{ + async_trait, + extract::FromRequestParts, + http::request::Parts, + response::{IntoResponse, Redirect}, +}; +use axum_extra::extract::CookieJar; + +pub struct WebAccessToken(pub String); + +#[async_trait] +impl FromRequestParts for WebAccessToken +where + S: Send + Sync, +{ + type Rejection = AccessTokenRejection; + + async fn from_request_parts(parts: &mut Parts, state: &S) -> Result { + let jar = CookieJar::from_request_parts(parts, state) + .await + .map_err(|e| AccessTokenRejection::CookieError(e.to_string()))?; + + let token = jar + .get("access_token") + .ok_or(AccessTokenRejection::NoAccessToken)? + .to_string(); + + Ok(WebAccessToken(token)) + } +} + +#[derive(Clone, Debug)] +pub enum AccessTokenRejection { + NoAccessToken, + CookieError(String), +} + +impl IntoResponse for AccessTokenRejection { + fn into_response(self) -> axum::response::Response { + match self { + AccessTokenRejection::NoAccessToken => Redirect::permanent("/web/login").into_response(), + AccessTokenRejection::CookieError(err) => { + println!("cookie error: {}", err); + Redirect::permanent("/web/login").into_response() + }, + } + } +} diff --git a/admin_frontend/src/main.rs b/admin_frontend/src/main.rs index c7136eca..d99140c3 100644 --- a/admin_frontend/src/main.rs +++ b/admin_frontend/src/main.rs @@ -1,3 +1,4 @@ +mod access_token; mod error; mod models; mod response; diff --git a/admin_frontend/src/templates.rs b/admin_frontend/src/templates.rs index b905d8a7..184563ac 100644 --- a/admin_frontend/src/templates.rs +++ b/admin_frontend/src/templates.rs @@ -7,3 +7,7 @@ pub struct Login; #[derive(Template)] #[template(path = "home.html")] pub struct Home; + +#[derive(Template)] +#[template(path = "admin.html")] +pub struct Admin; diff --git a/admin_frontend/src/web_app.rs b/admin_frontend/src/web_app.rs index 9b0f210d..75bdcc5a 100644 --- a/admin_frontend/src/web_app.rs +++ b/admin_frontend/src/web_app.rs @@ -1,3 +1,4 @@ +use crate::access_token::WebAccessToken; use crate::error::RenderError; use askama::Template; use axum::response::Result; @@ -11,17 +12,22 @@ pub fn router() -> Router { .route("/", get(home_handler)) .route("/home", get(home_handler)) .route("/login", get(login_handler)) + .route("/admin", get(admin_handler)) + .route("/admin/users", get(admin_users_handler)) } -pub async fn home_handler(cookies: CookieJar) -> Result, RenderError> { - let access_token = cookies.get("access_token"); - match access_token { - Some(access_token) => { - let s = templates::Home {}.render()?; - Ok(Html(s)) - }, - None => login_handler().await, - } +pub async fn home_handler(access_token: WebAccessToken) -> Result, RenderError> { + let s = templates::Home {}.render()?; + Ok(Html(s)) +} + +pub async fn admin_handler(access_token: WebAccessToken) -> Result, RenderError> { + let s = templates::Admin {}.render()?; + Ok(Html(s)) +} + +pub async fn admin_users_handler(cookies: CookieJar) -> Result, RenderError> { + todo!() } pub async fn login_handler() -> Result, RenderError> { diff --git a/admin_frontend/templates/admin.html b/admin_frontend/templates/admin.html new file mode 100644 index 00000000..4a124845 --- /dev/null +++ b/admin_frontend/templates/admin.html @@ -0,0 +1,16 @@ + + + +

Welcome to the Admin Page

+ + + + + diff --git a/admin_frontend/templates/home.html b/admin_frontend/templates/home.html index 9239ee1f..cac8b8be 100644 --- a/admin_frontend/templates/home.html +++ b/admin_frontend/templates/home.html @@ -3,18 +3,24 @@

Welcome to the Home Page

- + diff --git a/admin_frontend/templates/login.html b/admin_frontend/templates/login.html index ddb433c7..40d97c7c 100644 --- a/admin_frontend/templates/login.html +++ b/admin_frontend/templates/login.html @@ -1,5 +1,5 @@ - +

Admin Login

@@ -52,7 +52,7 @@ // Set the token as a cookie document.cookie = "access_token=" + data.data.access_token + "; path=/"; - window.location.href = "/"; + window.location.href = "/web/home"; }) .catch((error) => { console.error("Error:", error); diff --git a/admin_frontend/templates/users.html b/admin_frontend/templates/users.html new file mode 100644 index 00000000..09773c27 --- /dev/null +++ b/admin_frontend/templates/users.html @@ -0,0 +1,36 @@ + + + + +

User List

+
    + + + + +