-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING CHANGE: msrv is now nightly 1.62
- Loading branch information
Tomio
committed
May 4, 2022
1 parent
22283d2
commit 39514ad
Showing
9 changed files
with
116 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use std::env; | ||
use std::future::{ready, Ready}; | ||
|
||
use actix_web::body::EitherBody; | ||
use actix_web::dev::{self, Service, ServiceRequest, ServiceResponse, Transform}; | ||
use actix_web::http::header; | ||
use actix_web::{Error, HttpResponse}; | ||
use futures_util::future::LocalBoxFuture; | ||
|
||
use crate::error::Error as Errors; | ||
|
||
pub struct Auth; | ||
|
||
impl<S, B> Transform<S, ServiceRequest> for Auth | ||
where | ||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>, | ||
S::Future: 'static, | ||
B: 'static, | ||
{ | ||
type Response = ServiceResponse<EitherBody<B>>; | ||
type Error = Error; | ||
type InitError = (); | ||
type Transform = AuthMiddleware<S>; | ||
type Future = Ready<Result<Self::Transform, Self::InitError>>; | ||
|
||
fn new_transform(&self, service: S) -> Self::Future { | ||
ready(Ok(AuthMiddleware { | ||
service, | ||
})) | ||
} | ||
} | ||
|
||
pub struct AuthMiddleware<S> { | ||
service: S, | ||
} | ||
|
||
impl<S, B> Service<ServiceRequest> for AuthMiddleware<S> | ||
where | ||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>, | ||
S::Future: 'static, | ||
B: 'static, | ||
{ | ||
type Response = ServiceResponse<EitherBody<B>>; | ||
type Error = Error; | ||
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>; | ||
|
||
dev::forward_ready!(service); | ||
|
||
fn call(&self, req: ServiceRequest) -> Self::Future { | ||
let auth_token = env::var("AUTH_TOKEN").ok(); | ||
let headers = req.headers().clone(); | ||
let auth_header = headers.get(header::AUTHORIZATION); | ||
let (req, pl) = req.into_parts(); | ||
|
||
if let Some(auth_token) = auth_token && req.path() == "/screenshot" { | ||
match auth_header { | ||
Some(auth) => { | ||
let auth = auth.to_str().expect("Failed converting to str").to_owned(); | ||
|
||
if auth_token != auth { | ||
let res = HttpResponse::from_error(Errors::Unauthorized) | ||
.map_into_right_body::<B>(); | ||
|
||
return Box::pin(async { Ok(ServiceResponse::new(req, res)) }); | ||
} | ||
}, | ||
None => { | ||
let res = HttpResponse::from_error(Errors::MissingAuthToken) | ||
.map_into_right_body::<B>(); | ||
|
||
return Box::pin(async { Ok(ServiceResponse::new(req, res)) }); | ||
}, | ||
}; | ||
} | ||
|
||
let res = self.service.call(ServiceRequest::from_parts(req, pl)); | ||
|
||
Box::pin(async move { res.await.map(ServiceResponse::map_into_left_body) }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mod auth; | ||
|
||
pub use auth::*; |