-
-
Notifications
You must be signed in to change notification settings - Fork 153
Refactor auth flow #425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor auth flow #425
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,21 +22,22 @@ use std::future::{ready, Ready}; | |
use actix_web::{ | ||
dev::{forward_ready, Service, ServiceRequest, ServiceResponse, Transform}, | ||
error::{ErrorBadRequest, ErrorUnauthorized}, | ||
Error, HttpMessage, | ||
Error, | ||
}; | ||
use actix_web_httpauth::extractors::basic::BasicAuth; | ||
use futures_util::future::LocalBoxFuture; | ||
|
||
use crate::{ | ||
option::CONFIG, | ||
rbac::{role::Action, Users}, | ||
}; | ||
|
||
pub struct Authorization { | ||
pub struct Auth { | ||
pub action: Action, | ||
pub stream: bool, | ||
} | ||
|
||
impl<S, B> Transform<S, ServiceRequest> for Authorization | ||
impl<S, B> Transform<S, ServiceRequest> for Auth | ||
where | ||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>, | ||
S::Future: 'static, | ||
|
@@ -45,25 +46,25 @@ where | |
type Response = ServiceResponse<B>; | ||
type Error = Error; | ||
type InitError = (); | ||
type Transform = AuthorizationMiddleware<S>; | ||
type Transform = AuthMiddleware<S>; | ||
type Future = Ready<Result<Self::Transform, Self::InitError>>; | ||
|
||
fn new_transform(&self, service: S) -> Self::Future { | ||
ready(Ok(AuthorizationMiddleware { | ||
ready(Ok(AuthMiddleware { | ||
action: self.action, | ||
match_stream: self.stream, | ||
service, | ||
})) | ||
} | ||
} | ||
|
||
pub struct AuthorizationMiddleware<S> { | ||
pub struct AuthMiddleware<S> { | ||
action: Action, | ||
match_stream: bool, | ||
service: S, | ||
} | ||
|
||
impl<S, B> Service<ServiceRequest> for AuthorizationMiddleware<S> | ||
impl<S, B> Service<ServiceRequest> for AuthMiddleware<S> | ||
where | ||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>, | ||
S::Future: 'static, | ||
|
@@ -75,25 +76,34 @@ where | |
|
||
forward_ready!(service); | ||
|
||
fn call(&self, req: ServiceRequest) -> Self::Future { | ||
fn call(&self, mut req: ServiceRequest) -> Self::Future { | ||
// Extract username and password using basic auth extractor. | ||
let creds = req.extract::<BasicAuth>().into_inner(); | ||
let creds = creds.map_err(Into::into).map(|creds| { | ||
let username = creds.user_id().trim().to_owned(); | ||
// password is not mandatory by basic auth standard. | ||
// If not provided then treat as empty string | ||
let password = creds.password().unwrap_or("").trim().to_owned(); | ||
(username, password) | ||
}); | ||
|
||
let stream = if self.match_stream { | ||
req.match_info().get("logstream") | ||
} else { | ||
None | ||
}; | ||
let extensions = req.extensions(); | ||
let username = extensions | ||
.get::<String>() | ||
.expect("authentication layer verified username"); | ||
let is_auth = Users.check_permission(username, self.action, stream); | ||
drop(extensions); | ||
|
||
let auth_result: Result<bool, Error> = creds.map(|(username, password)| { | ||
Users.authenticate(username, password, self.action, stream) | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The type is result of authentication and authorization. Type of creds is Result<> which i can only return in the async block down below. So have to
|
||
|
||
let fut = self.service.call(req); | ||
|
||
Box::pin(async move { | ||
if !is_auth { | ||
if !auth_result? { | ||
return Err(ErrorUnauthorized("Not authorized")); | ||
} | ||
|
||
fut.await | ||
}) | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.