Skip to content

Commit

Permalink
Fix that auth is always activated regardless of the presence of auth_…
Browse files Browse the repository at this point in the history
…credentials
  • Loading branch information
Gowee committed Sep 25, 2019
1 parent 77ec9a6 commit 33cc83b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
42 changes: 22 additions & 20 deletions src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use futures::future::BoxFuture;

use base64::decode as base64_decode;
use tide::{
http::{response::Builder as ResponseBuilder, StatusCode},
http::{response::Builder as ResponseBuilder, StatusCode, HeaderValue},
middleware::{Middleware, Next},
Context, Response,
};
Expand Down Expand Up @@ -49,27 +49,29 @@ impl SimplisticHTTPBasicAuth {

impl<State: Send + Sync + 'static> Middleware<State> for SimplisticHTTPBasicAuth {
fn handle<'a>(&'a self, cx: Context<State>, next: Next<'a, State>) -> BoxFuture<'a, Response> {
let credentials = cx.headers().get("Authorization").and_then(|value| {
let (_type, credentials) = parse_authorization(value)?;
if _type.eq_ignore_ascii_case("Basic") {
Some(String::from_utf8(base64_decode(credentials).ok()?).ok()?)
} else {
None
}
});
Box::pin(async move {
let t = cx.headers().get("Authorization").and_then(|value| {
let authorization = value.to_str().ok()?;
let (_type, credentials) = {
// A trailing space is expected to be in `t`.
let (t, c) = authorization.split_at(authorization.find(' ')?);
(t.trim(), c.trim())
};
if _type.eq_ignore_ascii_case("Basic") {
Some(String::from_utf8(base64_decode(credentials).ok()?).ok()?)
} else {
None
}
});
match t {
Some(ref credentials) if self.authenticate(credentials) => {
trace!("An request is authenticated with {} .", credentials);
next.run(cx).await
}
_ => self.unauthorized(),
match credentials {
Some(ref credentials) if self.authenticate(credentials) => {
trace!("An request is authenticated with {} .", credentials);
next.run(cx).await
}
_ => self.unauthorized(),
}
})
}
}

fn parse_authorization(header_value: &HeaderValue) -> Option<(&str, &str)> {
let value = header_value.to_str().ok()?;
// A trailing space is expected to be in `t`.
let (_type, credentials) = value.split_at(value.find(' ')?);
Some((_type.trim(), credentials.trim()))
}
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ fn main() {
let expiration_task = app_state.expire();
let mut app = App::with_state(app_state);
app.middleware(RequestLogger::new());
app.middleware(HTTPBasicAuth::new());
if OPT.is_auth_enabled() {
app.middleware(HTTPBasicAuth::new());
}
app.at("/").get(handle_index);
app.at("/assets/*path").get(handle_assets);
app.at("/upload/start").post(handle_upload_start);
Expand Down
4 changes: 4 additions & 0 deletions src/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ impl Opt {
SocketAddr::new(self.ip_addr, self.port)
}

pub fn is_auth_enabled(&self) -> bool {
return !self.auth_credentials.is_empty()
}

pub fn credentials_match(&self, credentials: impl AsRef<str>) -> bool {
let credentials = credentials.as_ref();
self.auth_credentials.iter().any(|c| c == credentials)
Expand Down

0 comments on commit 33cc83b

Please sign in to comment.