Compare commits
1 commit
bea38bc445
...
639b5cf89f
Author | SHA1 | Date | |
---|---|---|---|
639b5cf89f |
2 changed files with 9 additions and 9 deletions
16
src/auth.rs
16
src/auth.rs
|
@ -5,21 +5,21 @@ use tracing::{trace, warn};
|
||||||
|
|
||||||
use crate::password;
|
use crate::password;
|
||||||
|
|
||||||
pub fn layer<'a, ResBody>(
|
pub fn auth_layer<'a, ResBody>(
|
||||||
user_pass_hash: &'a [u8],
|
user_pass_hash: &'a [u8],
|
||||||
salt: &'a str,
|
salt: &'a str,
|
||||||
) -> ValidateRequestHeaderLayer<Basic<'a, ResBody>> {
|
) -> ValidateRequestHeaderLayer<BasicAuth<'a, ResBody>> {
|
||||||
ValidateRequestHeaderLayer::custom(Basic::new(user_pass_hash, salt))
|
ValidateRequestHeaderLayer::custom(BasicAuth::new(user_pass_hash, salt))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy)]
|
#[derive(Copy)]
|
||||||
pub struct Basic<'a, ResBody> {
|
pub struct BasicAuth<'a, ResBody> {
|
||||||
pass: &'a [u8],
|
pass: &'a [u8],
|
||||||
salt: &'a str,
|
salt: &'a str,
|
||||||
_ty: std::marker::PhantomData<fn() -> ResBody>,
|
_ty: std::marker::PhantomData<fn() -> ResBody>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<ResBody> std::fmt::Debug for Basic<'_, ResBody> {
|
impl<ResBody> std::fmt::Debug for BasicAuth<'_, ResBody> {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
f.debug_struct("BasicAuth")
|
f.debug_struct("BasicAuth")
|
||||||
.field("pass", &self.pass)
|
.field("pass", &self.pass)
|
||||||
|
@ -29,7 +29,7 @@ impl<ResBody> std::fmt::Debug for Basic<'_, ResBody> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<ResBody> Clone for Basic<'_, ResBody> {
|
impl<ResBody> Clone for BasicAuth<'_, ResBody> {
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
Self {
|
Self {
|
||||||
pass: self.pass,
|
pass: self.pass,
|
||||||
|
@ -39,7 +39,7 @@ impl<ResBody> Clone for Basic<'_, ResBody> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, ResBody> Basic<'a, ResBody> {
|
impl<'a, ResBody> BasicAuth<'a, ResBody> {
|
||||||
pub fn new(pass: &'a [u8], salt: &'a str) -> Self {
|
pub fn new(pass: &'a [u8], salt: &'a str) -> Self {
|
||||||
Self {
|
Self {
|
||||||
pass,
|
pass,
|
||||||
|
@ -81,7 +81,7 @@ impl<'a, ResBody> Basic<'a, ResBody> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<B, ResBody> tower_http::validate_request::ValidateRequest<B> for Basic<'_, ResBody>
|
impl<B, ResBody> tower_http::validate_request::ValidateRequest<B> for BasicAuth<'_, ResBody>
|
||||||
where
|
where
|
||||||
ResBody: Default,
|
ResBody: Default,
|
||||||
{
|
{
|
||||||
|
|
|
@ -290,7 +290,7 @@ fn main() -> Result<()> {
|
||||||
let app = Router::new().route("/update", get(update_records));
|
let app = Router::new().route("/update", get(update_records));
|
||||||
// if a password is provided, validate it
|
// if a password is provided, validate it
|
||||||
let app = if let Some(pass) = password_hash {
|
let app = if let Some(pass) = password_hash {
|
||||||
app.layer(auth::layer(Box::leak(pass), String::leak(salt)))
|
app.layer(auth::auth_layer(Box::leak(pass), String::leak(salt)))
|
||||||
} else {
|
} else {
|
||||||
app
|
app
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue