mirror of
https://github.com/n08i40k/schedule-parser-rusted.git
synced 2025-12-06 17:57:47 +03:00
feat!: add telegram auth and async refactor
- Removed "/schedule/update-download-url" endpoint, this mechanism was replaced by Yandex Cloud FaaS. Ура :) - Improved schedule caching mechanism. - Added Telegram WebApp authentication support. - Reworked endpoints responses and errors mechanism. - Refactored application state management. - Make synchronous database operations, middlewares and extractors to asynchronous. - Made user password field optional to support multiple auth methods. - Renamed users table column "version" to "android_version" and made it nullable.
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
use crate::database::models::User;
|
||||
use crate::extractors::authorized_user;
|
||||
use crate::extractors::base::FromRequestSync;
|
||||
use crate::extractors::base::FromRequestAsync;
|
||||
use actix_web::body::{BoxBody, EitherBody};
|
||||
use actix_web::dev::{Payload, Service, ServiceRequest, ServiceResponse, Transform, forward_ready};
|
||||
use actix_web::{Error, HttpRequest, ResponseError};
|
||||
use futures_util::future::LocalBoxFuture;
|
||||
use std::future::{Ready, ready};
|
||||
use std::rc::Rc;
|
||||
|
||||
/// Middleware guard working with JWT tokens.
|
||||
pub struct JWTAuthorization {
|
||||
@@ -21,7 +22,7 @@ impl Default for JWTAuthorization {
|
||||
|
||||
impl<S, B> Transform<S, ServiceRequest> for JWTAuthorization
|
||||
where
|
||||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
|
||||
S::Future: 'static,
|
||||
B: 'static,
|
||||
{
|
||||
@@ -33,14 +34,14 @@ where
|
||||
|
||||
fn new_transform(&self, service: S) -> Self::Future {
|
||||
ready(Ok(JWTAuthorizationMiddleware {
|
||||
service,
|
||||
service: Rc::new(service),
|
||||
ignore: self.ignore,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
pub struct JWTAuthorizationMiddleware<S> {
|
||||
service: S,
|
||||
service: Rc<S>,
|
||||
/// List of ignored endpoints.
|
||||
ignore: &'static [&'static str],
|
||||
}
|
||||
@@ -52,12 +53,11 @@ where
|
||||
B: 'static,
|
||||
{
|
||||
/// Checking the validity of the token.
|
||||
fn check_authorization(
|
||||
&self,
|
||||
req: &HttpRequest,
|
||||
payload: &mut Payload,
|
||||
) -> Result<(), authorized_user::Error> {
|
||||
User::from_request_sync(req, payload)
|
||||
async fn check_authorization(req: &HttpRequest) -> Result<(), authorized_user::Error> {
|
||||
let mut payload = Payload::None;
|
||||
|
||||
User::from_request_async(req, &mut payload)
|
||||
.await
|
||||
.map(|_| ())
|
||||
.map_err(|e| e.as_error::<authorized_user::Error>().unwrap().clone())
|
||||
}
|
||||
@@ -79,9 +79,9 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, S, B> Service<ServiceRequest> for JWTAuthorizationMiddleware<S>
|
||||
impl<S, B> Service<ServiceRequest> for JWTAuthorizationMiddleware<S>
|
||||
where
|
||||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
|
||||
S::Future: 'static,
|
||||
B: 'static,
|
||||
{
|
||||
@@ -97,20 +97,19 @@ where
|
||||
return Box::pin(async move { Ok(fut.await?.map_into_left_body()) });
|
||||
}
|
||||
|
||||
let (http_req, mut payload) = req.into_parts();
|
||||
let service = Rc::clone(&self.service);
|
||||
|
||||
if let Err(err) = self.check_authorization(&http_req, &mut payload) {
|
||||
return Box::pin(async move {
|
||||
Ok(ServiceResponse::new(
|
||||
http_req,
|
||||
Box::pin(async move {
|
||||
match Self::check_authorization(req.request()).await {
|
||||
Ok(_) => {
|
||||
let fut = service.call(req).await?;
|
||||
Ok(fut.map_into_left_body())
|
||||
}
|
||||
Err(err) => Ok(ServiceResponse::new(
|
||||
req.into_parts().0,
|
||||
err.error_response().map_into_right_body(),
|
||||
))
|
||||
});
|
||||
}
|
||||
|
||||
let req = ServiceRequest::from_parts(http_req, payload);
|
||||
let fut = self.service.call(req);
|
||||
|
||||
Box::pin(async move { Ok(fut.await?.map_into_left_body()) })
|
||||
)),
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user