mirror of
https://github.com/danbulant/Portfolio
synced 2026-07-07 03:50:36 +00:00
fix: fix build
This commit is contained in:
parent
32c266e366
commit
7a9d59a60c
2 changed files with 14 additions and 28 deletions
|
|
@ -1,40 +1,25 @@
|
||||||
use config::DbConn;
|
|
||||||
use rocket::http::Status;
|
use rocket::http::Status;
|
||||||
use rocket::outcome::Outcome;
|
use rocket::outcome::Outcome;
|
||||||
use rocket::request::{self, FromRequest, Request};
|
use rocket::request::{FromRequest, Request};
|
||||||
use rocket::response::status;
|
|
||||||
use rocket_contrib::json::Json;
|
|
||||||
|
|
||||||
use portfolio_core::token::candidate_token::CandidateToken;
|
use portfolio_core::token::candidate_token::CandidateToken;
|
||||||
use portfolio_core::token::decode_candidate_token;
|
use portfolio_core::token::decode_candidate_token;
|
||||||
|
|
||||||
impl<'a, 'r> FromRequest<'a, 'r> for UserToken {
|
pub struct Token(CandidateToken);
|
||||||
type Error = status::Custom<Json<Response>>;
|
|
||||||
fn from_request(
|
#[rocket::async_trait]
|
||||||
request: &'a Request<'r>,
|
impl<'r> FromRequest<'r> for Token {
|
||||||
) -> request::Outcome<Self, status::Custom<Json<Response>>> {
|
type Error = Status;
|
||||||
let conn = request.guard::<DbConn>().unwrap();
|
async fn from_request(req: &'r Request<'_>) -> Outcome<Token, (Status, Status), ()> {
|
||||||
if let Some(authen_header) = request.headers().get_one("Authorization") {
|
if let Some(auth) = req.headers().get_one("Authorization") {
|
||||||
let authen_str = authen_header.to_string();
|
let auth_string = auth.to_string();
|
||||||
if authen_str.starts_with("Bearer") {
|
if auth_string.starts_with("Bearer") {
|
||||||
let token = authen_str[6..authen_str.len()].trim();
|
let token = auth_string[6..auth_string.len()].trim();
|
||||||
if let Ok(token_data) = decode_candidate_token(token.to_string()) {
|
if let Ok(token_data) = decode_candidate_token(token.to_string()) {
|
||||||
// if verify_token(&token_data, &conn) {
|
return Outcome::Success(Token(token_data.claims));
|
||||||
return Outcome::Success(token_data.claims);
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return Outcome::Failure((Status::Unauthorized, Status::Unauthorized));
|
||||||
Outcome::Failure((
|
|
||||||
Status::BadRequest,
|
|
||||||
status::Custom(
|
|
||||||
Status::Unauthorized,
|
|
||||||
Json(Response {
|
|
||||||
message: String::from("Invalid token"),
|
|
||||||
data: serde_json::to_value("").unwrap(),
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate rocket;
|
extern crate rocket;
|
||||||
|
|
||||||
use rocket::{Rocket, Build};
|
use rocket::{Rocket, Build};
|
||||||
use rocket::serde::json::Json;
|
use rocket::serde::json::Json;
|
||||||
use rocket::fairing::{self, AdHoc};
|
use rocket::fairing::{self, AdHoc};
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue