diff --git a/api/src/guard/candidate_jwt.rs b/api/src/guard/candidate_jwt.rs deleted file mode 100644 index 25d9486..0000000 --- a/api/src/guard/candidate_jwt.rs +++ /dev/null @@ -1,33 +0,0 @@ -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 { - 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)); - } -} diff --git a/api/src/lib.rs b/api/src/lib.rs index 3f8745e..98821d4 100644 --- a/api/src/lib.rs +++ b/api/src/lib.rs @@ -1,7 +1,6 @@ #[macro_use] extern crate rocket; -use guard::candidate_jwt::TokenRequest; use portfolio_core::error::ServiceError; use portfolio_core::services::candidate_service::CandidateService; use requests::LoginRequest; @@ -47,17 +46,7 @@ async fn create(conn: Connection<'_, Db>, post_form: Json) -> Ok(plain_text_password) } -/* #[get("/refresh")] -async fn refresh_token(conn: Connection<'_, Db>, token_req: Result) -> Result> { - let db = conn.into_inner(); - let jwt = token_req.ok().unwrap().to_token(); - - let refresh_token = SessionService::login_user(db, jwt.application_id).await; - - Ok(refresh_token.ok().unwrap()) -} */ - -#[get("/validate_refresh")] +#[get("/whoami")] async fn validate(conn: Connection<'_, Db>, uuid_cookie: Result) -> Result> { let db = conn.into_inner(); let user = CandidateService::auth_user_session(db, uuid_cookie.ok().unwrap().value()).await; @@ -90,24 +79,6 @@ async fn login(conn: Connection<'_, Db>, login_form: Json) -> Resu } } -#[get("/whoami")] -async fn whoami(conn: Connection<'_, Db>, token_req: Result) -> Result> { - let db = conn.into_inner(); - let token = token_req.ok().unwrap().to_token(); - let user = CandidateService::authenticate_candidate(db, token).await; - - match user { - Ok(user) => Ok( - format!("{} {} {}", - user.application, - user.name.unwrap_or("".to_owned()), - user.surname.unwrap_or("".to_owned()) - ) - ), - Err(e) => Err(custom_err_from_service_err(e)), - } -} - #[get("/hello")] async fn hello() -> &'static str { "Hello, world!" @@ -125,7 +96,7 @@ async fn start() -> Result<(), rocket::Error> { .attach(Db::init()) .attach(AdHoc::try_on_ignite("Migrations", run_migrations)) //.mount("/", FileServer::from(relative!("/static"))) - .mount("/", routes![create, login, hello, whoami, validate]) + .mount("/", routes![create, login, hello, validate]) .register("/", catchers![]) .launch() .await