Using Options for basic auth
This commit is contained in:
3 files changed
+32
-24
No files matched your search
+6
-6
@@ -27,10 +27,10 @@ pub struct Config {
|
||||
pub certificate: String,
|
||||
|
||||
#[serde(default = "default_username")]
|
||||
pub username: String,
|
||||
pub username: Option<String>,
|
||||
|
||||
#[serde(default = "default_password")]
|
||||
pub password: String,
|
||||
pub password: Option<String>,
|
||||
|
||||
#[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<String> {
|
||||
None
|
||||
}
|
||||
|
||||
fn default_password() -> String {
|
||||
"".to_string()
|
||||
fn default_password() -> Option<String> {
|
||||
None
|
||||
}
|
||||
+19
-17
@@ -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<T> = std::result::Result<T, ()>;
|
||||
@@ -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));
|
||||
|
||||
+7
-1
@@ -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| {
|
||||
|
||||
Reference in new issue
Block a user