forked from ideal-world/spacegate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/dev' into update/k8s
- Loading branch information
Showing
75 changed files
with
736 additions
and
1,438 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,8 @@ | ||
[env] | ||
TS_RS_EXPORT_DIR = { value = "./sdk/admin-client/src/model", relative = true } | ||
TS_RS_EXPORT_DIR = { value = "./sdk/admin-client/src/model", relative = true } | ||
CONFIG = "file:./resource/local-example" | ||
PLUGINS = "./target/debug" | ||
RUST_LOG = "trace" | ||
FORMAT = "json" | ||
KEY = "moCZihByqvXt4dfMYjOz75fzBi0eul6Ffg2EoUzWyqA=" | ||
SK = "password" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod authentication; | ||
pub mod version_control; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use axum::{ | ||
extract::{self, State}, | ||
http::StatusCode, | ||
middleware::Next, | ||
response::Response, | ||
}; | ||
use axum_extra::extract::cookie::CookieJar; | ||
use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::state::AppState; | ||
|
||
/// Our claims struct, it needs to derive `Serialize` and/or `Deserialize` | ||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct Claims { | ||
pub sub: String, | ||
pub exp: u64, | ||
pub username: String, | ||
} | ||
|
||
pub struct Authentication { | ||
pub secret: String, | ||
} | ||
|
||
pub async fn authentication<B>(State(state): State<AppState<B>>, cookie: CookieJar, request: extract::Request, next: Next) -> Response { | ||
use axum::http::header::AUTHORIZATION; | ||
if let Some(secret) = state.secret { | ||
let Some(jwt) = request | ||
.headers() | ||
.get(AUTHORIZATION) | ||
.and_then(|header| header.to_str().ok()) | ||
.and_then(|header| header.strip_prefix("Bearer ")) | ||
.or(cookie.get("jwt").map(|cookie| cookie.value())) | ||
else { | ||
return Response::builder().status(StatusCode::UNAUTHORIZED).body("expect jwt token".into()).unwrap(); | ||
}; | ||
let Ok(_jwt) = decode::<Claims>(jwt, &DecodingKey::from_secret(secret.as_ref()), &Validation::new(Algorithm::HS256)) else { | ||
return Response::builder().status(StatusCode::UNAUTHORIZED).body("invalid jwt token".into()).unwrap(); | ||
}; | ||
} | ||
|
||
next.run(request).await | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use std::time::SystemTime; | ||
|
||
use crate::{ | ||
error::InternalError, | ||
mw::authentication::Claims, | ||
state::{self, AppState}, | ||
}; | ||
use axum::{ | ||
extract::State, | ||
http::{header::SET_COOKIE, HeaderValue}, | ||
routing::post, | ||
Json, Router, | ||
}; | ||
use jsonwebtoken::{encode, EncodingKey, Header}; | ||
use serde::{Deserialize, Serialize}; | ||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct Login { | ||
pub ak: String, | ||
pub sk: String, | ||
} | ||
const EXPIRE: u64 = 3600; | ||
async fn login<B>(State(AppState { secret, sk_digest, .. }): State<AppState<B>>, login: Json<Login>) -> Result<axum::response::Response, InternalError> { | ||
let mut response = axum::response::Response::new(axum::body::Body::empty()); | ||
if let Some(sk_digest) = sk_digest { | ||
let out: [u8; 32] = <sha2::Sha256 as digest::Digest>::digest(&login.sk).into(); | ||
if &out != sk_digest.as_ref() { | ||
*response.status_mut() = axum::http::StatusCode::UNAUTHORIZED; | ||
return Ok(response); | ||
} | ||
} | ||
if let Some(sec) = secret { | ||
let jwt = encode( | ||
&Header::default(), | ||
&Claims { | ||
sub: "admin".to_string(), | ||
exp: SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs() + EXPIRE, | ||
username: login.ak.to_string(), | ||
}, | ||
&EncodingKey::from_secret(sec.as_ref()), | ||
) | ||
.map_err(InternalError::boxed)?; | ||
response.headers_mut().insert( | ||
SET_COOKIE, | ||
HeaderValue::from_str(&format!("jwt={jwt}; path=/; HttpOnly; Max-Age=3600")).expect("invalid jwt"), | ||
); | ||
} | ||
Ok(response) | ||
} | ||
pub fn router<B: Send + Sync + 'static>() -> axum::Router<state::AppState<B>> { | ||
Router::new().route("/login", post(login::<B>)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.