diff --git a/api/src/guard/candidate_jwt.rs b/api/src/guard/candidate_jwt.rs index 576f111..977f021 100644 --- a/api/src/guard/candidate_jwt.rs +++ b/api/src/guard/candidate_jwt.rs @@ -1,40 +1,25 @@ -use config::DbConn; use rocket::http::Status; use rocket::outcome::Outcome; -use rocket::request::{self, FromRequest, Request}; -use rocket::response::status; -use rocket_contrib::json::Json; +use rocket::request::{FromRequest, Request}; use portfolio_core::token::candidate_token::CandidateToken; use portfolio_core::token::decode_candidate_token; -impl<'a, 'r> FromRequest<'a, 'r> for UserToken { - type Error = status::Custom>; - fn from_request( - request: &'a Request<'r>, - ) -> request::Outcome>> { - let conn = request.guard::().unwrap(); - if let Some(authen_header) = request.headers().get_one("Authorization") { - let authen_str = authen_header.to_string(); - if authen_str.starts_with("Bearer") { - let token = authen_str[6..authen_str.len()].trim(); +pub struct Token(CandidateToken); + +#[rocket::async_trait] +impl<'r> FromRequest<'r> for Token { + type Error = Status; + async fn from_request(req: &'r Request<'_>) -> Outcome { + 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(); if let Ok(token_data) = decode_candidate_token(token.to_string()) { - // if verify_token(&token_data, &conn) { - return Outcome::Success(token_data.claims); - // } + return Outcome::Success(Token(token_data.claims)); } } } - - Outcome::Failure(( - Status::BadRequest, - status::Custom( - Status::Unauthorized, - Json(Response { - message: String::from("Invalid token"), - data: serde_json::to_value("").unwrap(), - }), - ), - )) + return Outcome::Failure((Status::Unauthorized, Status::Unauthorized)); } } diff --git a/api/src/lib.rs b/api/src/lib.rs index a601693..9b245e2 100644 --- a/api/src/lib.rs +++ b/api/src/lib.rs @@ -1,5 +1,6 @@ #[macro_use] extern crate rocket; + use rocket::{Rocket, Build}; use rocket::serde::json::Json; use rocket::fairing::{self, AdHoc};