mirror of
https://github.com/danbulant/Portfolio
synced 2026-06-18 22:11:14 +00:00
33 lines
1.1 KiB
Rust
33 lines
1.1 KiB
Rust
use rocket::http::Status;
|
|
use rocket::outcome::Outcome;
|
|
use rocket::request::{FromRequest, Request};
|
|
|
|
use portfolio_core::token::candidate_token::CandidateToken;
|
|
use portfolio_core::token::decode_candidate_token;
|
|
|
|
pub struct TokenRequest(CandidateToken);
|
|
|
|
impl TokenRequest {
|
|
pub fn to_token(self) -> CandidateToken {
|
|
self.0
|
|
}
|
|
}
|
|
|
|
#[rocket::async_trait]
|
|
impl<'r> FromRequest<'r> for TokenRequest {
|
|
type Error = Status;
|
|
async fn from_request(req: &'r Request<'_>) -> Outcome<TokenRequest, (Status, Status), ()> {
|
|
if let Some(auth) = req.headers().get_one("Authorization") {
|
|
let auth_string = auth.to_string();
|
|
if auth_string.starts_with("Bearer") {
|
|
let token = auth_string[6..auth_string.len()].trim();
|
|
let token_data = decode_candidate_token(token.to_string());
|
|
|
|
if token_data.is_ok() {
|
|
return Outcome::Success(TokenRequest(token_data.ok().unwrap().claims));
|
|
}
|
|
}
|
|
}
|
|
return Outcome::Failure((Status::Unauthorized, Status::Unauthorized));
|
|
}
|
|
}
|