mirror of
https://codeberg.org/selfsigned-ash/antifed
synced 2026-07-21 18:49:34 +02:00
💥 Nukes rust and replaces with kotlin
This commit is contained in:
@@ -1,56 +0,0 @@
|
||||
use rocket::Response;
|
||||
use rocket::http::Status;
|
||||
use rocket::request::{FromRequest, Outcome, Request};
|
||||
use serde::Serialize;
|
||||
use serde_json::json;
|
||||
use std::io::Cursor;
|
||||
|
||||
pub struct ApiKey {}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct GenericResponse {
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
#[rocket::async_trait]
|
||||
impl<'r> FromRequest<'r> for ApiKey {
|
||||
type Error = Response<'r>;
|
||||
|
||||
async fn from_request(req: &'r Request<'_>) -> Outcome<Self, Response<'r>> {
|
||||
fn is_valid(key: &str) -> bool {
|
||||
key == dotenv::var("API_KEY").unwrap()
|
||||
}
|
||||
|
||||
match req.headers().get_one("Authorization") {
|
||||
None => {
|
||||
let body = json!(GenericResponse {
|
||||
message: "auth token not found".to_string()
|
||||
})
|
||||
.to_string();
|
||||
|
||||
Outcome::Error((
|
||||
Status::Unauthorized,
|
||||
Response::build()
|
||||
.status(Status::Unauthorized)
|
||||
.sized_body(body.len(), Cursor::new(body))
|
||||
.finalize(),
|
||||
))
|
||||
}
|
||||
Some(key) if is_valid(key) => Outcome::Success(ApiKey {}),
|
||||
Some(_) => {
|
||||
let body = json!(GenericResponse {
|
||||
message: "invalid auth token".to_string()
|
||||
})
|
||||
.to_string();
|
||||
|
||||
Outcome::Error((
|
||||
Status::Unauthorized,
|
||||
Response::build()
|
||||
.status(Status::Unauthorized)
|
||||
.sized_body(body.len(), Cursor::new(body))
|
||||
.finalize(),
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user