Skip to content

Commit

Permalink
Add optional push_password for simple authentication
Browse files Browse the repository at this point in the history
When this optional password is given then it is required when pushing
media to the server. If it isn't given in the configuration then we
fall back to the normal password.
  • Loading branch information
Ceron257 committed Jun 4, 2024
1 parent 2be56a7 commit a37a63b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
1 change: 1 addition & 0 deletions application/xiu/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ pub struct HttpNotifierConfig {
pub struct AuthSecretConfig {
pub key: String,
pub password: String,
pub push_password: Option<String>
}

#[derive(Debug, Deserialize, Clone, Default)]
Expand Down
1 change: 1 addition & 0 deletions application/xiu/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ impl Service {
Some(Auth::new(
authsecret.key.clone(),
authsecret.password.clone(),
authsecret.push_password.clone(),
cfg.algorithm.clone(),
auth_type,
))
Expand Down
17 changes: 12 additions & 5 deletions library/common/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,23 @@ pub struct Auth {
algorithm: AuthAlgorithm,
key: String,
password: String,
push_password: Option<String>,
pub auth_type: AuthType,
}

impl Auth {
pub fn new(
key: String,
password: String,
push_password: Option<String>,
algorithm: AuthAlgorithm,
auth_type: AuthType,
) -> Self {
Self {
algorithm,
key,
password,
push_password,
auth_type,
}
}
Expand Down Expand Up @@ -70,7 +73,7 @@ impl Auth {
}

if let Some(token) = query_pairs.get("token") {
if self.check(stream_name, token) {
if self.check(stream_name, token, is_pull) {
return Ok(());
}
auth_err_reason = format!("token is not correct: {}", token);
Expand All @@ -90,11 +93,15 @@ impl Auth {
Ok(())
}

fn check(&self, stream_name: &String, auth_str: &str) -> bool {
fn check(&self, stream_name: &String, auth_str: &str, is_pull: bool) -> bool {
let password = if is_pull {
&self.password
} else {
self.push_password.as_ref().unwrap_or(&self.password)
};

match self.algorithm {
AuthAlgorithm::Simple => {
self.password == auth_str
}
AuthAlgorithm::Simple => password == auth_str,
AuthAlgorithm::Md5 => {
let raw_data = format!("{}{}", self.key, stream_name);
let digest_str = format!("{:x}", md5::compute(raw_data));
Expand Down

0 comments on commit a37a63b

Please sign in to comment.