mirror of
https://github.com/danbulant/Portfolio
synced 2026-06-20 15:01:19 +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::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<Json<Response>>;
|
||||
fn from_request(
|
||||
request: &'a Request<'r>,
|
||||
) -> request::Outcome<Self, status::Custom<Json<Response>>> {
|
||||
let conn = request.guard::<DbConn>().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<Token, (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();
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
#[macro_use]
|
||||
extern crate rocket;
|
||||
|
||||
use rocket::{Rocket, Build};
|
||||
use rocket::serde::json::Json;
|
||||
use rocket::fairing::{self, AdHoc};
|
||||
|
|
|
|||
Loading…
Reference in a new issue