mirror of
https://codeberg.org/selfsigned-ash/antifed
synced 2026-07-21 18:49:34 +02:00
48 lines
1.4 KiB
Rust
48 lines
1.4 KiB
Rust
use rocket::fairing::{Fairing, Info, Kind};
|
|
use rocket::http::{Header, Status};
|
|
use rocket::request::{FromRequest, Outcome};
|
|
use rocket::{Request, Response};
|
|
use std::env;
|
|
|
|
pub struct Authenticated;
|
|
|
|
#[rocket::async_trait]
|
|
impl<'r> FromRequest<'r> for Authenticated {
|
|
type Error = ();
|
|
|
|
async fn from_request(req: &'r Request<'_>) -> Outcome<Self, Self::Error> {
|
|
let token = req.headers().get_one("Authorization");
|
|
if let Some(token) = token {
|
|
if token == env::var("API_KEY").unwrap().as_str() {
|
|
Outcome::Success(Authenticated)
|
|
} else {
|
|
Outcome::Error((Status::Unauthorized, ()))
|
|
}
|
|
} else {
|
|
Outcome::Error((Status::Unauthorized, ()))
|
|
}
|
|
}
|
|
}
|
|
|
|
pub struct CORS;
|
|
|
|
#[rocket::async_trait]
|
|
impl Fairing for CORS {
|
|
fn info(&self) -> Info {
|
|
Info {
|
|
name: "Add CORS headers to responses",
|
|
kind: Kind::Response,
|
|
}
|
|
}
|
|
|
|
async fn on_response<'r>(&self, _request: &'r Request<'_>, response: &mut Response<'r>) {
|
|
response.set_header(Header::new("Access-Control-Allow-Origin", "*"));
|
|
response.set_header(Header::new(
|
|
"Access-Control-Allow-Methods",
|
|
"GET, POST, PUT, PATCH, DELETE, OPTIONS",
|
|
));
|
|
response.set_header(Header::new("Access-Control-Allow-Headers", "*"));
|
|
response.set_header(Header::new("Access-Control-Allow-Credentials", "true"));
|
|
}
|
|
}
|