From d889d275db113d6c255a3258a5f9c97954c9a3b4 Mon Sep 17 00:00:00 2001 From: puripuri2100 Date: Sat, 28 Oct 2023 04:24:24 +0900 Subject: [PATCH] =?UTF-8?q?=E8=AA=8D=E8=A8=BC=E3=83=88=E3=83=BC=E3=82=AF?= =?UTF-8?q?=E3=83=B3=E7=99=BA=E8=A1=8CAPI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 42 +++++++++++++++++++++++++++++++++++----- Cargo.toml | 2 +- logfile | 4 ++++ src/app.rs | 19 +++++++++++++++--- src/app/certification.rs | 32 ++++++++++++++++++++++++++++++ src/certification.rs | 19 ++++++++++++------ 6 files changed, 103 insertions(+), 15 deletions(-) create mode 100644 src/app/certification.rs diff --git a/Cargo.lock b/Cargo.lock index dce4868..286f085 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -118,6 +118,7 @@ dependencies = [ "bitflags 1.3.2", "bytes", "futures-util", + "headers", "http", "http-body", "hyper", @@ -171,6 +172,12 @@ dependencies = [ "rustc-demangle", ] +[[package]] +name = "base64" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" + [[package]] name = "base64" version = "0.21.2" @@ -735,6 +742,31 @@ dependencies = [ "hashbrown 0.14.0", ] +[[package]] +name = "headers" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3e372db8e5c0d213e0cd0b9be18be2aca3d44cf2fe30a9d46a65581cd454584" +dependencies = [ + "base64 0.13.1", + "bitflags 1.3.2", + "bytes", + "headers-core", + "http", + "httpdate", + "mime", + "sha1", +] + +[[package]] +name = "headers-core" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7f66481bfee273957b1f20485a4ff3362987f85b2c236580d81b4eb7a326429" +dependencies = [ + "http", +] + [[package]] name = "heck" version = "0.3.3" @@ -1037,7 +1069,7 @@ version = "8.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6971da4d9c3aa03c3d8f3ff0f4155b534aad021292003895a469716b2a230378" dependencies = [ - "base64", + "base64 0.21.2", "ring", "serde", "serde_json", @@ -1612,7 +1644,7 @@ version = "0.11.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cde824a14b7c14f85caff81225f411faacc04a2013f41670f41443742b1c1c55" dependencies = [ - "base64", + "base64 0.21.2", "bytes", "encoding_rs", "futures-core", @@ -1723,7 +1755,7 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2d3987094b1d07b653b7dfdc3f70ce9a1da9c51ac18c1b06b662e4f9a0e9f4b2" dependencies = [ - "base64", + "base64 0.21.2", ] [[package]] @@ -2085,7 +2117,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8264c59b28b6858796acfcedc660aa4c9075cc6e4ec8eb03cdca2a3e725726db" dependencies = [ "atoi", - "base64", + "base64 0.21.2", "bitflags 2.3.3", "byteorder", "bytes", @@ -2129,7 +2161,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1cab6147b81ca9213a7578f1b4c9d24c449a53953cd2222a7b5d7cd29a5c3139" dependencies = [ "atoi", - "base64", + "base64 0.21.2", "bitflags 2.3.3", "byteorder", "chrono", diff --git a/Cargo.toml b/Cargo.toml index bb0b522..b7a8910 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -axum = "0.6.20" +axum = { version = "0.6.20", features = ["json", "headers"] } chrono = { version = "0.4.26", features = ["serde"] } meilisearch-sdk = "0.24.2" rand = "0.8.5" diff --git a/logfile b/logfile index 102e21e..153995a 100644 --- a/logfile +++ b/logfile @@ -58,3 +58,7 @@ limit_day: int NOT NULL, ); +2023-10-27 03:06:56.693 JST [13295] LOG: received smart shutdown request +2023-10-27 03:06:56.696 JST [13295] LOG: background worker "logical replication launcher" (PID 13302) exited with exit code 1 +2023-10-27 03:06:56.696 JST [13297] LOG: shutting down +2023-10-27 03:06:56.710 JST [13295] LOG: database system is shut down diff --git a/src/app.rs b/src/app.rs index 54fd3a6..9ae939b 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,12 +1,13 @@ use crate::error_handling::{QrError, Result}; use axum::{ - extract::Query, + extract::{Query, TypedHeader}, + headers::authorization::{Authorization, Basic}, http::Method, routing::{delete, get, post}, Router, }; use chrono::Utc; -use reqwest::header::CONTENT_TYPE; +use reqwest::header::{AUTHORIZATION, CONTENT_TYPE}; use std::collections::HashMap; use std::net::SocketAddr; use std::sync::Arc; @@ -15,6 +16,8 @@ use tracing::*; use crate::search_engine; +/// 認証まわりのエンドポイントの定義 +pub mod certification; /// コンテナの管理を行うエンドポイントの定義 pub mod container; /// 物品情報の登録を行うエンドポイントの定義 @@ -208,10 +211,20 @@ pub async fn app(bind: SocketAddr) -> Result<()> { move |body| container::insert_container(body, conn) }), ) + .route( + "/gen_passtoken", + post({ + info!("POST /gen_passtoken"); + let conn = Arc::clone(&conn); + move |TypedHeader(Authorization(basic)): TypedHeader>| { + certification::api_gen_passtoken(basic, conn) + } + }), + ) .layer( CorsLayer::new() .allow_methods([Method::GET, Method::POST, Method::DELETE]) - .allow_headers([CONTENT_TYPE]) + .allow_headers([CONTENT_TYPE, AUTHORIZATION]) .allow_origin(Any), ); diff --git a/src/app/certification.rs b/src/app/certification.rs new file mode 100644 index 0000000..e82f9ad --- /dev/null +++ b/src/app/certification.rs @@ -0,0 +1,32 @@ +use crate::{ + certification::{self, str_to_role_opt}, + error_handling::{result_to_handler_with_log, QrError, Result, ReturnData}, +}; +use axum::headers::authorization::Basic; +use sqlx::{pool::Pool, postgres::Postgres}; +use std::sync::Arc; +use tracing::*; + +pub async fn api_gen_passtoken(token_info: Basic, conn: Arc>) -> ReturnData { + info!("Try gen passtoken"); + let res = gen_passtoken(token_info, conn).await; + result_to_handler_with_log( + |_| Some("Success gen passtoken".to_string()), + |e| Some(format!("Failed gen passtoken: {e}")), + &res, + ) + .await +} + +pub async fn gen_passtoken(token_info: Basic, conn: Arc>) -> Result { + let role_str = token_info.username(); + let key = token_info.password(); + match str_to_role_opt(role_str) { + Some(role) => { + let passtoken = certification::gen_passtoken(role, key)?; + certification::insert_passtoken(&*conn, &passtoken).await?; + Ok(passtoken.token) + } + None => Err(QrError::Authorized), + } +} diff --git a/src/certification.rs b/src/certification.rs index 74ed0bb..9b15c9c 100644 --- a/src/certification.rs +++ b/src/certification.rs @@ -40,6 +40,15 @@ pub enum Role { General, } +pub fn str_to_role_opt(item: &str) -> Option { + match item { + "administrator" => Some(Role::Administrator), + "equipment_manager" => Some(Role::EquipmentManager), + "general" => Some(Role::General), + _ => None, + } +} + impl std::fmt::Display for Role { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { match self { @@ -53,11 +62,9 @@ impl std::fmt::Display for Role { impl From for Role { fn from(item: String) -> Self { - match item.as_str() { - "administrator" => Role::Administrator, - "equipment_manager" => Role::EquipmentManager, - "general" => Role::General, - _ => panic!("Undefined role: {item}"), + match str_to_role_opt(&item) { + Some(r) => r, + None => panic!("unkown role: {item}"), } } } @@ -135,7 +142,7 @@ pub fn gen_passtoken(role: Role, key: &str) -> Result { } } -pub async fn insert_passtoken<'a, E>(conn: E, passtoken: Passtoken) -> Result<()> +pub async fn insert_passtoken<'a, E>(conn: E, passtoken: &Passtoken) -> Result<()> where E: sqlx::Executor<'a, Database = sqlx::Postgres>, {