From 1b68d73399c487edbadb36c437beed78bed4edaf Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Fri, 3 Jul 2020 12:21:54 +0700 Subject: [PATCH] Using Options for basic auth --- src/config.rs | 12 ++++++------ src/gst.rs | 36 +++++++++++++++++++----------------- src/main.rs | 8 +++++++- 3 files changed, 32 insertions(+), 24 deletions(-) diff --git a/src/config.rs b/src/config.rs index a96550d..199d91d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -27,10 +27,10 @@ pub struct Config { pub certificate: String, #[serde(default = "default_username")] - pub username: String, + pub username: Option, #[serde(default = "default_password")] - pub password: String, + pub password: Option, #[validate(regex(path = "RE_TLS_CLIENT_AUTH", message = "Incorrect stream format", code = "format"))] #[serde(default = "default_tls_client_auth")] @@ -82,10 +82,10 @@ fn default_tls_client_auth() -> String { "none".to_string() } -fn default_username() -> String { - "".to_string() +fn default_username() -> Option { + None } -fn default_password() -> String { - "".to_string() +fn default_password() -> Option { + None } diff --git a/src/gst.rs b/src/gst.rs index d8cd28f..0c20234 100644 --- a/src/gst.rs +++ b/src/gst.rs @@ -12,7 +12,6 @@ use gstreamer_rtsp_server::{RTSPAuth, RTSPToken, RTSPMediaFactory, RTSP_TOKEN_ME use log::debug; use std::io; use std::io::Write; -use std::fs; use gio::{TlsCertificate,TlsAuthenticationMode}; type Result = std::result::Result; @@ -98,23 +97,26 @@ impl RtspServer { Ok(maybe_app_src) } - pub fn set_credentials(&self, user :&str, pass :&str) -> Result<()> { - let auth = self.server.get_auth().unwrap_or_else(RTSPAuth::new()); + pub fn set_credentials(&self, user_pass: Option<(&str, &str)>) -> Result<()> { + let auth = self.server.get_auth().unwrap_or_else(|| RTSPAuth::new()); - if ! user.is_empty() && ! pass.is_empty() { - debug!("Setting credentials for user {}", user); - debug!("Password is {}", pass); - let token = RTSPToken::new(&[(*RTSP_TOKEN_MEDIA_FACTORY_ROLE, &"watcher")]); - let basic = RTSPAuth::make_basic(user, pass); - auth.set_supported_methods(RTSPAuthMethod::Basic); - auth.add_basic(basic.as_str(), &token); + match user_pass { + Some((user, pass)) => { + debug!("Setting credentials for user {}", user); + debug!("Password is {}", pass); + let token = RTSPToken::new(&[(*RTSP_TOKEN_MEDIA_FACTORY_ROLE, &"watcher")]); + let basic = RTSPAuth::make_basic(user, pass); + auth.set_supported_methods(RTSPAuthMethod::Basic); + auth.add_basic(basic.as_str(), &token); - // Now we have basic auth we stop others from watching - let mut token = RTSPToken::new_empty(); - auth.set_default_token(Some(&mut token)); - } else { - let mut token = RTSPToken::new(&[(*RTSP_TOKEN_MEDIA_FACTORY_ROLE, &"watcher")]); - auth.set_default_token(Some(&mut token)); //By default anyone can watch if we don't turn on basic + // Now we have basic auth we stop others from watching + let mut token = RTSPToken::new_empty(); + auth.set_default_token(Some(&mut token)); + }, + None => { + let mut token = RTSPToken::new(&[(*RTSP_TOKEN_MEDIA_FACTORY_ROLE, &"watcher")]); + auth.set_default_token(Some(&mut token)); //By default anyone can watch if we don't turn on basic + } } self.server.set_auth(Some(&auth)); @@ -124,7 +126,7 @@ impl RtspServer { pub fn set_tls(&self, cert_file: &str, client_auth: TlsAuthenticationMode) -> Result<()> { if ! cert_file.is_empty() { debug!("Setting up TLS using {}", cert_file); - let auth = self.server.get_auth().unwrap_or_else(RTSPAuth::new()); + let auth = self.server.get_auth().unwrap_or_else(|| RTSPAuth::new()); let cert = TlsCertificate::new_from_file(cert_file).expect("Not a valid TLS certificate or not found."); auth.set_tls_certificate(Some(&cert)); diff --git a/src/main.rs b/src/main.rs index 8242617..a6f447f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -60,7 +60,13 @@ fn main() -> Result<(), Error> { }; rtsp.set_tls(&config.certificate, tls_client_auth).expect("Failed to set up TLS"); - rtsp.set_credentials(&config.username, &config.password).expect("Failed to set up users."); + let name = &config.username; + let pass = &config.password; + let user_pass = match (name, pass) { + (Some(name), Some(pass)) => Some((&name as &str, &pass as &str)), + _ => None, + }; + rtsp.set_credentials(user_pass).expect("Failed to set up users."); crossbeam::scope(|s| {