Skip to content

Commit

Permalink
BasicAuthMiddleware: Add option to ignore authentication for a partic…
Browse files Browse the repository at this point in the history
…ular URI (mimblewimble#3037)

* api::BasicAuthMiddleware: Add option to ignore authentication for a particular URI

* rustfmt
  • Loading branch information
yeastplume authored and antiochp committed Sep 19, 2019
1 parent e22bd6d commit 6f745d1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
13 changes: 12 additions & 1 deletion api/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,19 @@ lazy_static! {
pub struct BasicAuthMiddleware {
api_basic_auth: String,
basic_realm: &'static HeaderValue,
ignore_uri: Option<String>,
}

impl BasicAuthMiddleware {
pub fn new(api_basic_auth: String, basic_realm: &'static HeaderValue) -> BasicAuthMiddleware {
pub fn new(
api_basic_auth: String,
basic_realm: &'static HeaderValue,
ignore_uri: Option<String>,
) -> BasicAuthMiddleware {
BasicAuthMiddleware {
api_basic_auth,
basic_realm,
ignore_uri,
}
}
}
Expand All @@ -52,6 +58,11 @@ impl Handler for BasicAuthMiddleware {
if req.method().as_str() == "OPTIONS" {
return next_handler.call(req, handlers);
}
if let Some(u) = self.ignore_uri.as_ref() {
if req.uri().path() == u {
return next_handler.call(req, handlers);
}
}
if req.headers().contains_key(AUTHORIZATION)
&& verify_slices_are_equal(
req.headers()[AUTHORIZATION].as_bytes(),
Expand Down
7 changes: 5 additions & 2 deletions api/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,11 @@ pub fn start_rest_apis(
let mut router = build_router(chain, tx_pool, peers).expect("unable to build API router");
if let Some(api_secret) = api_secret {
let api_basic_auth = format!("Basic {}", util::to_base64(&format!("grin:{}", api_secret)));
let basic_auth_middleware =
Arc::new(BasicAuthMiddleware::new(api_basic_auth, &GRIN_BASIC_REALM));
let basic_auth_middleware = Arc::new(BasicAuthMiddleware::new(
api_basic_auth,
&GRIN_BASIC_REALM,
None,
));
router.add_middleware(basic_auth_middleware);
}

Expand Down

0 comments on commit 6f745d1

Please sign in to comment.