mirror of
https://github.com/n08i40k/schedule-parser-rusted.git
synced 2025-12-07 02:07:48 +03:00
0.5.0
Возвращёна реализация сериализации в json для IResponse Добавлены типы для экстракции данных из запросов средствами actix-web Добавлен экстрактор для получения пользователя по токену доступа передаваемому в запросе Добавлен макрос для автоматической реализации ResponseError для ошибок экстракторов Добавлен эндпоинт users/me Из главного проекта исключена зависимость actix-http посредством переноса части тестового функционала в отдельный crate
This commit is contained in:
69
src/extractors/authorized_user.rs
Normal file
69
src/extractors/authorized_user.rs
Normal file
@@ -0,0 +1,69 @@
|
||||
use crate::app_state::AppState;
|
||||
use crate::database::driver;
|
||||
use crate::database::models::User;
|
||||
use crate::extractors::base::FromRequestSync;
|
||||
use crate::utility::jwt;
|
||||
use actix_macros::ResponseErrorMessage;
|
||||
use actix_web::body::BoxBody;
|
||||
use actix_web::dev::Payload;
|
||||
use actix_web::{HttpRequest, web};
|
||||
use derive_more::Display;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt::Debug;
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, Display, ResponseErrorMessage)]
|
||||
#[status_code = "actix_web::http::StatusCode::UNAUTHORIZED"]
|
||||
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
||||
pub enum Error {
|
||||
#[display("No authorization header found")]
|
||||
NoHeader,
|
||||
|
||||
#[display("Bearer token is required")]
|
||||
UnknownAuthorizationType,
|
||||
|
||||
#[display("No bearer token provided")]
|
||||
NoBearerToken,
|
||||
|
||||
#[display("Invalid or expired access token")]
|
||||
InvalidAccessToken,
|
||||
|
||||
#[display("No user associated with access token")]
|
||||
NoUser,
|
||||
}
|
||||
|
||||
impl Error {
|
||||
pub fn into_err(self) -> actix_web::Error {
|
||||
actix_web::Error::from(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl FromRequestSync for User {
|
||||
type Error = actix_web::Error;
|
||||
|
||||
fn from_request_sync(req: &HttpRequest, _: &mut Payload) -> Result<Self, Self::Error> {
|
||||
let authorization = req
|
||||
.headers()
|
||||
.get("Authorization")
|
||||
.ok_or(Error::NoHeader.into_err())?
|
||||
.to_str()
|
||||
.map_err(|_| Error::NoHeader.into_err())?
|
||||
.to_string();
|
||||
|
||||
let parts: Vec<&str> = authorization.split(' ').collect();
|
||||
|
||||
if parts.len() == 0 || parts[0] != "Bearer" {
|
||||
return Err(Error::UnknownAuthorizationType.into_err());
|
||||
}
|
||||
|
||||
if parts.len() < 2 {
|
||||
return Err(Error::NoBearerToken.into());
|
||||
}
|
||||
|
||||
let user_id = jwt::verify_and_decode(&parts[1].to_string())
|
||||
.map_err(|_| Error::InvalidAccessToken.into_err())?;
|
||||
|
||||
let app_state = req.app_data::<web::Data<AppState>>().unwrap();
|
||||
|
||||
driver::users::get(&app_state.database, &user_id).map_err(|_| Error::NoUser.into())
|
||||
}
|
||||
}
|
||||
56
src/extractors/base.rs
Normal file
56
src/extractors/base.rs
Normal file
@@ -0,0 +1,56 @@
|
||||
use actix_web::dev::Payload;
|
||||
use actix_web::{FromRequest, HttpRequest};
|
||||
use futures_util::future::LocalBoxFuture;
|
||||
use std::future::{Ready, ready};
|
||||
|
||||
pub trait FromRequestAsync: Sized {
|
||||
type Error: Into<actix_web::Error>;
|
||||
|
||||
async fn from_request_async(req: HttpRequest, payload: Payload) -> Result<Self, Self::Error>;
|
||||
}
|
||||
|
||||
pub struct AsyncExtractor<T>(T);
|
||||
|
||||
impl<T> AsyncExtractor<T> {
|
||||
pub fn into_inner(self) -> T {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: FromRequestAsync> FromRequest for AsyncExtractor<T> {
|
||||
type Error = T::Error;
|
||||
type Future = LocalBoxFuture<'static, Result<Self, Self::Error>>;
|
||||
|
||||
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
|
||||
let req = req.clone();
|
||||
let payload = payload.take();
|
||||
Box::pin(async move {
|
||||
T::from_request_async(req, payload)
|
||||
.await
|
||||
.map(|res| Self(res))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub trait FromRequestSync: Sized {
|
||||
type Error: Into<actix_web::Error>;
|
||||
|
||||
fn from_request_sync(req: &HttpRequest, payload: &mut Payload) -> Result<Self, Self::Error>;
|
||||
}
|
||||
|
||||
pub struct SyncExtractor<T>(T);
|
||||
|
||||
impl<T> SyncExtractor<T> {
|
||||
pub fn into_inner(self) -> T {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: FromRequestSync> FromRequest for SyncExtractor<T> {
|
||||
type Error = T::Error;
|
||||
type Future = Ready<Result<Self, Self::Error>>;
|
||||
|
||||
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
|
||||
ready(T::from_request_sync(req, payload).map(|res| Self(res)))
|
||||
}
|
||||
}
|
||||
2
src/extractors/mod.rs
Normal file
2
src/extractors/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
pub mod authorized_user;
|
||||
pub mod base;
|
||||
Reference in New Issue
Block a user