parent
86469d6b35
commit
201bfdc1fd
|
@ -44,7 +44,7 @@ repos:
|
|||
- id: cargo-test
|
||||
name: cargo test
|
||||
description: unit test for the project
|
||||
entry: bash -c 'cargo nextest run --all-features'
|
||||
entry: bash -c 'cargo nextest run --workspace --all-features'
|
||||
language: rust
|
||||
files: \.rs$
|
||||
pass_filenames: false
|
||||
|
|
|
@ -914,15 +914,6 @@ dependencies = [
|
|||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "deranged"
|
||||
version = "0.3.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b42b6fa04a440b495c8b04d0e71b707c585f83cb9cb28cf8cd0d976c315e31b4"
|
||||
dependencies = [
|
||||
"powerfmt",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "digest"
|
||||
version = "0.10.7"
|
||||
|
@ -1493,21 +1484,6 @@ dependencies = [
|
|||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "jsonwebtoken"
|
||||
version = "9.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b9ae10193d25051e74945f1ea2d0b42e03cc3b890f7e4cc5faa44997d808193f"
|
||||
dependencies = [
|
||||
"base64 0.21.7",
|
||||
"js-sys",
|
||||
"pem",
|
||||
"ring",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"simple_asn1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.5.0"
|
||||
|
@ -1690,16 +1666,6 @@ dependencies = [
|
|||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-bigint"
|
||||
version = "0.4.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9"
|
||||
dependencies = [
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-bigint-dig"
|
||||
version = "0.8.4"
|
||||
|
@ -1717,12 +1683,6 @@ dependencies = [
|
|||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-conv"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9"
|
||||
|
||||
[[package]]
|
||||
name = "num-integer"
|
||||
version = "0.1.46"
|
||||
|
@ -1825,16 +1785,6 @@ version = "0.2.1"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8835116a5c179084a830efb3adc117ab007512b535bc1a21c991d3b32a6b44dd"
|
||||
|
||||
[[package]]
|
||||
name = "pem"
|
||||
version = "3.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8e459365e590736a54c3fa561947c84837534b8e9af6fc5bf781307e82658fae"
|
||||
dependencies = [
|
||||
"base64 0.22.1",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pem-rfc7468"
|
||||
version = "0.7.0"
|
||||
|
@ -1964,12 +1914,6 @@ version = "0.3.30"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec"
|
||||
|
||||
[[package]]
|
||||
name = "powerfmt"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391"
|
||||
|
||||
[[package]]
|
||||
name = "ppv-lite86"
|
||||
version = "0.2.20"
|
||||
|
@ -2181,21 +2125,6 @@ version = "0.8.4"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b"
|
||||
|
||||
[[package]]
|
||||
name = "ring"
|
||||
version = "0.17.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c17fa4cb658e3583423e915b9f3acc01cceaee1860e33d59ebae66adc3a2dc0d"
|
||||
dependencies = [
|
||||
"cc",
|
||||
"cfg-if",
|
||||
"getrandom",
|
||||
"libc",
|
||||
"spin",
|
||||
"untrusted",
|
||||
"windows-sys 0.52.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ron"
|
||||
version = "0.8.1"
|
||||
|
@ -2240,7 +2169,6 @@ dependencies = [
|
|||
"base64 0.22.1",
|
||||
"bevy_ecs",
|
||||
"chrono",
|
||||
"jsonwebtoken",
|
||||
"rtss_db",
|
||||
"rtss_dto",
|
||||
"rtss_log",
|
||||
|
@ -2497,18 +2425,6 @@ dependencies = [
|
|||
"rand_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "simple_asn1"
|
||||
version = "0.6.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "adc4e5204eb1910f40f9cfa375f6f05b68c3abac4b6fd879c8ff5e7ae8a0a085"
|
||||
dependencies = [
|
||||
"num-bigint",
|
||||
"num-traits",
|
||||
"thiserror",
|
||||
"time",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "slab"
|
||||
version = "0.4.9"
|
||||
|
@ -2908,37 +2824,6 @@ dependencies = [
|
|||
"once_cell",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "time"
|
||||
version = "0.3.36"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5dfd88e563464686c916c7e46e623e520ddc6d79fa6641390f2e3fa86e83e885"
|
||||
dependencies = [
|
||||
"deranged",
|
||||
"itoa",
|
||||
"num-conv",
|
||||
"powerfmt",
|
||||
"serde",
|
||||
"time-core",
|
||||
"time-macros",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "time-core"
|
||||
version = "0.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3"
|
||||
|
||||
[[package]]
|
||||
name = "time-macros"
|
||||
version = "0.2.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3f252a68540fde3a3877aeea552b832b40ab9a69e318efd078774a01ddee1ccf"
|
||||
dependencies = [
|
||||
"num-conv",
|
||||
"time-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tiny-keccak"
|
||||
version = "2.0.2"
|
||||
|
@ -3265,12 +3150,6 @@ version = "0.1.1"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "39ec24b3121d976906ece63c9daad25b85969647682eee313cb5779fdd69e14e"
|
||||
|
||||
[[package]]
|
||||
name = "untrusted"
|
||||
version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1"
|
||||
|
||||
[[package]]
|
||||
name = "url"
|
||||
version = "2.5.2"
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
# 项目描述
|
||||
轨道交通信号系统仿真模块
|
||||
|
||||
## 三种架构(先按第一种实现)
|
||||
## 三种架构(现按第一种实现)
|
||||
|
||||
1. 服务端服务进程运行多个仿真,客户端处理显示(和之前的方式一致)
|
||||
2. 服务端通过网络启动多个运行单个仿真的进程(可以容器启动),客户端还是只处理显示
|
||||
|
|
|
@ -11,7 +11,7 @@ serde_json = "1.0.125"
|
|||
chrono = { version = "0.4.38", features = ["serde"] }
|
||||
axum = "0.7.5"
|
||||
axum-extra = { version = "0.9.3", features = ["typed-header"] }
|
||||
jsonwebtoken = "9.3.0"
|
||||
# jsonwebtoken = "9.3.0"
|
||||
tower-http = { version = "0.5.0", features = ["cors"] }
|
||||
async-graphql = { version = "7.0.7", features = ["chrono"] }
|
||||
async-graphql-axum = "7.0.6"
|
||||
|
|
|
@ -1,82 +1,82 @@
|
|||
use std::sync::LazyLock;
|
||||
// use std::sync::LazyLock;
|
||||
|
||||
use async_graphql::Result;
|
||||
use axum::http::HeaderMap;
|
||||
use jsonwebtoken::{decode, DecodingKey, Validation};
|
||||
use rtss_log::tracing::error;
|
||||
use serde::{Deserialize, Serialize};
|
||||
// use async_graphql::Result;
|
||||
// use axum::http::HeaderMap;
|
||||
// use jsonwebtoken::{decode, DecodingKey, Validation};
|
||||
// use rtss_log::tracing::error;
|
||||
// use serde::{Deserialize, Serialize};
|
||||
|
||||
static KEYS: LazyLock<Keys> = LazyLock::new(|| {
|
||||
// let secret = std::env::var("JWT_SECRET").expect("JWT_SECRET must be set");
|
||||
let secret = "joylink".to_string();
|
||||
Keys::new(secret.as_bytes())
|
||||
});
|
||||
// static KEYS: LazyLock<Keys> = LazyLock::new(|| {
|
||||
// // let secret = std::env::var("JWT_SECRET").expect("JWT_SECRET must be set");
|
||||
// let secret = "joylink".to_string();
|
||||
// Keys::new(secret.as_bytes())
|
||||
// });
|
||||
|
||||
struct Keys {
|
||||
// encoding: EncodingKey,
|
||||
decoding: DecodingKey,
|
||||
}
|
||||
// struct Keys {
|
||||
// // encoding: EncodingKey,
|
||||
// decoding: DecodingKey,
|
||||
// }
|
||||
|
||||
impl Keys {
|
||||
pub fn new(secret: &[u8]) -> Self {
|
||||
Self {
|
||||
// encoding: EncodingKey::from_secret(secret),
|
||||
decoding: DecodingKey::from_secret(secret),
|
||||
}
|
||||
}
|
||||
}
|
||||
// impl Keys {
|
||||
// pub fn new(secret: &[u8]) -> Self {
|
||||
// Self {
|
||||
// // encoding: EncodingKey::from_secret(secret),
|
||||
// decoding: DecodingKey::from_secret(secret),
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum AuthError {
|
||||
InvalidToken,
|
||||
}
|
||||
// #[derive(Debug)]
|
||||
// pub enum AuthError {
|
||||
// InvalidToken,
|
||||
// }
|
||||
|
||||
pub(crate) fn get_token_from_headers(headers: HeaderMap) -> Result<Option<Claims>, AuthError> {
|
||||
let option_token = headers.get("Token");
|
||||
if let Some(token) = option_token {
|
||||
let token_data = decode::<Claims>(
|
||||
token.to_str().unwrap(),
|
||||
&KEYS.decoding,
|
||||
&Validation::default(),
|
||||
)
|
||||
.map_err(|err| {
|
||||
error!("Error decoding token: {:?}", err);
|
||||
AuthError::InvalidToken
|
||||
})?;
|
||||
Ok(Some(token_data.claims))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
// pub(crate) fn get_token_from_headers(headers: HeaderMap) -> Result<Option<Claims>, AuthError> {
|
||||
// let option_token = headers.get("Token");
|
||||
// if let Some(token) = option_token {
|
||||
// let token_data = decode::<Claims>(
|
||||
// token.to_str().unwrap(),
|
||||
// &KEYS.decoding,
|
||||
// &Validation::default(),
|
||||
// )
|
||||
// .map_err(|err| {
|
||||
// error!("Error decoding token: {:?}", err);
|
||||
// AuthError::InvalidToken
|
||||
// })?;
|
||||
// Ok(Some(token_data.claims))
|
||||
// } else {
|
||||
// Ok(None)
|
||||
// }
|
||||
// }
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct Claims {
|
||||
pub id: u32,
|
||||
pub sub: String,
|
||||
}
|
||||
// #[derive(Debug, Serialize, Deserialize)]
|
||||
// pub struct Claims {
|
||||
// pub id: u32,
|
||||
// pub sub: String,
|
||||
// }
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
// #[cfg(test)]
|
||||
// mod tests {
|
||||
|
||||
use super::*;
|
||||
// use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_get_token_from_headers() {
|
||||
rtss_log::Logging::default().init();
|
||||
let mut headers: HeaderMap = HeaderMap::new();
|
||||
headers.insert("Token", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MjQ2NzAyMjcsImlkIjo2LCJvcmlnX2lhdCI6MTcyNDIzODIyNywic3ViIjoiNiJ9.sSfjdW7d3OqOE6G1p47c4dcCan4evRGoNjGPUyVfWLk".parse().unwrap());
|
||||
let result = get_token_from_headers(headers);
|
||||
match result {
|
||||
Ok(Some(claims)) => {
|
||||
assert_eq!(claims.id, 6);
|
||||
assert_eq!(claims.sub, "6");
|
||||
}
|
||||
Ok(None) => {
|
||||
panic!("Expected Some(claims), got None");
|
||||
}
|
||||
Err(e) => {
|
||||
panic!("Error: {:?}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// #[test]
|
||||
// fn test_get_token_from_headers() {
|
||||
// rtss_log::Logging::default().init();
|
||||
// let mut headers: HeaderMap = HeaderMap::new();
|
||||
// headers.insert("Token", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MjQ2NzAyMjcsImlkIjo2LCJvcmlnX2lhdCI6MTcyNDIzODIyNywic3ViIjoiNiJ9.sSfjdW7d3OqOE6G1p47c4dcCan4evRGoNjGPUyVfWLk".parse().unwrap());
|
||||
// let result = get_token_from_headers(headers);
|
||||
// match result {
|
||||
// Ok(Some(claims)) => {
|
||||
// assert_eq!(claims.id, 6);
|
||||
// assert_eq!(claims.sub, "6");
|
||||
// }
|
||||
// Ok(None) => {
|
||||
// panic!("Expected Some(claims), got None");
|
||||
// }
|
||||
// Err(e) => {
|
||||
// panic!("Error: {:?}", e);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
mod draft_data;
|
||||
mod jwt_auth;
|
||||
// mod jwt_auth;
|
||||
mod pagination;
|
||||
mod server;
|
||||
mod simulation;
|
||||
|
|
|
@ -10,12 +10,12 @@ use axum::{
|
|||
Router,
|
||||
};
|
||||
use http::{playground_source, GraphQLPlaygroundConfig};
|
||||
use rtss_log::tracing::{debug, error, info};
|
||||
use rtss_log::tracing::{debug, info};
|
||||
use tokio::net::TcpListener;
|
||||
use tower_http::cors::CorsLayer;
|
||||
|
||||
use crate::draft_data;
|
||||
use crate::simulation_definition::MutexSimulationManager;
|
||||
use crate::{draft_data, jwt_auth};
|
||||
|
||||
pub struct ServerConfig {
|
||||
pub database_url: String,
|
||||
|
@ -60,19 +60,20 @@ pub async fn serve(config: ServerConfig) -> anyhow::Result<()> {
|
|||
|
||||
async fn graphql_handler(
|
||||
State(schema): State<SimulationSchema>,
|
||||
headers: HeaderMap,
|
||||
_headers: HeaderMap,
|
||||
req: GraphQLRequest,
|
||||
) -> GraphQLResponse {
|
||||
let mut req = req.into_inner();
|
||||
let token = jwt_auth::get_token_from_headers(headers);
|
||||
match token {
|
||||
Ok(token) => {
|
||||
req = req.data(token);
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Error getting token from headers: {:?}", e);
|
||||
}
|
||||
}
|
||||
let req = req.into_inner();
|
||||
// let mut req = req.into_inner();
|
||||
// let token = jwt_auth::get_token_from_headers(headers);
|
||||
// match token {
|
||||
// Ok(token) => {
|
||||
// req = req.data(token);
|
||||
// }
|
||||
// Err(e) => {
|
||||
// error!("Error getting token from headers: {:?}", e);
|
||||
// }
|
||||
// }
|
||||
schema.execute(req).await.into()
|
||||
}
|
||||
|
||||
|
|
|
@ -1,11 +1,7 @@
|
|||
use async_graphql::{Context, InputObject, Object};
|
||||
use rtss_log::tracing::info;
|
||||
use rtss_sim_manage::{AvailablePlugins, SimulationBuilder};
|
||||
|
||||
use crate::{
|
||||
jwt_auth::Claims,
|
||||
simulation_definition::{MutexSimulationManager, SimulationOperation},
|
||||
};
|
||||
use crate::simulation_definition::{MutexSimulationManager, SimulationOperation};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct SimulationQuery;
|
||||
|
@ -28,13 +24,13 @@ impl SimulationMutation {
|
|||
ctx: &Context<'ctx>,
|
||||
req: StartSimulationRequest,
|
||||
) -> async_graphql::Result<String> {
|
||||
let claims = ctx.data::<Option<Claims>>().unwrap();
|
||||
match claims {
|
||||
Some(claims) => {
|
||||
info!("User {claims:?} started simulation");
|
||||
}
|
||||
_ => return Err("Unauthorized".into()),
|
||||
}
|
||||
// let claims = ctx.data::<Option<Claims>>().unwrap();
|
||||
// match claims {
|
||||
// Some(claims) => {
|
||||
// info!("User {claims:?} started simulation");
|
||||
// }
|
||||
// _ => return Err("Unauthorized".into()),
|
||||
// }
|
||||
let sim = ctx.data::<MutexSimulationManager>().unwrap();
|
||||
let id = sim.lock().await.start_simulation(
|
||||
SimulationBuilder::default()
|
||||
|
|
|
@ -386,10 +386,10 @@ mod tests {
|
|||
|
||||
// save as new draft测试
|
||||
let new_draft = accessor.save_as_new_draft(res.id, "new draft", 11).await?;
|
||||
println!("{:?}", new_draft);
|
||||
assert_eq!(new_draft.name, "new draft");
|
||||
assert_eq!(new_draft.user_id, 11);
|
||||
assert_eq!(new_draft.data.unwrap(), data);
|
||||
assert!(new_draft.updated_at > new_draft.created_at);
|
||||
|
||||
// delete测试
|
||||
accessor.delete_draft_data(&[res.id, new_draft.id]).await?;
|
||||
|
|
Loading…
Reference in New Issue