2024-11-22 13:13:04 +08:00
|
|
|
pub mod auth;
|
2024-12-18 21:54:37 +08:00
|
|
|
pub mod fields;
|
|
|
|
pub mod page;
|
|
|
|
pub mod post;
|
2024-11-26 12:19:57 +08:00
|
|
|
pub mod setup;
|
|
|
|
pub mod users;
|
2024-12-18 21:54:37 +08:00
|
|
|
|
2024-11-25 03:36:24 +08:00
|
|
|
use rocket::request::{FromRequest, Outcome, Request};
|
2024-11-22 13:13:04 +08:00
|
|
|
use rocket::routes;
|
2024-12-18 21:54:37 +08:00
|
|
|
use crate::api::users::Role;
|
|
|
|
use rocket::http::Status;
|
|
|
|
use crate::security::jwt;
|
2024-11-21 01:04:59 +08:00
|
|
|
|
2024-11-25 03:36:24 +08:00
|
|
|
pub struct Token(String);
|
|
|
|
|
|
|
|
#[rocket::async_trait]
|
|
|
|
impl<'r> FromRequest<'r> for Token {
|
|
|
|
type Error = ();
|
|
|
|
|
|
|
|
async fn from_request(request: &'r Request<'_>) -> Outcome<Self, Self::Error> {
|
|
|
|
let token = request
|
|
|
|
.headers()
|
|
|
|
.get_one("Authorization")
|
|
|
|
.map(|value| value.replace("Bearer ", ""));
|
|
|
|
|
|
|
|
match token {
|
|
|
|
Some(token) => Outcome::Success(Token(token)),
|
|
|
|
None => Outcome::Success(Token("".to_string())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-18 21:54:37 +08:00
|
|
|
|
2024-11-25 03:36:24 +08:00
|
|
|
pub struct SystemToken(String);
|
|
|
|
|
|
|
|
#[rocket::async_trait]
|
|
|
|
impl<'r> FromRequest<'r> for SystemToken {
|
|
|
|
type Error = ();
|
|
|
|
async fn from_request(request: &'r Request<'_>) -> Outcome<Self, Self::Error> {
|
|
|
|
let token = request
|
|
|
|
.headers()
|
|
|
|
.get_one("Authorization")
|
|
|
|
.map(|value| value.replace("Bearer ", ""));
|
2024-11-25 17:59:18 +08:00
|
|
|
match token.and_then(|t| jwt::validate_jwt(&t).ok()) {
|
|
|
|
Some(claims) if claims.name == "system" => Outcome::Success(SystemToken(claims.name)),
|
|
|
|
_ => Outcome::Error((Status::Unauthorized, ())),
|
2024-11-25 03:36:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-18 21:54:37 +08:00
|
|
|
|
2024-11-22 13:13:04 +08:00
|
|
|
pub fn jwt_routes() -> Vec<rocket::Route> {
|
2024-12-20 00:34:54 +08:00
|
|
|
routes![auth::token::token_system,auth::token::test_token]
|
2024-11-21 19:07:42 +08:00
|
|
|
}
|
2024-11-25 17:59:18 +08:00
|
|
|
|
2024-12-18 21:54:37 +08:00
|
|
|
pub fn fields_routes() -> Vec<rocket::Route> {
|
2024-12-20 00:34:54 +08:00
|
|
|
routes![fields::get_field_handler,fields::insert_field_handler,fields::delete_field_handler,fields::delete_all_fields_handler,fields::update_field_handler]
|
2024-11-25 17:59:18 +08:00
|
|
|
}
|