mirror of
https://github.com/mimblewimble/grin.git
synced 2025-05-11 19:51:18 +03:00
BasicAuthMiddleware: Add option to ignore authentication for a particular URI (#3037)
* api::BasicAuthMiddleware: Add option to ignore authentication for a particular URI * rustfmt
This commit is contained in:
parent
32286ccd7a
commit
95004a4b96
2 changed files with 17 additions and 3 deletions
api/src
|
@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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(),
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue