use std::net::SocketAddr;
use crate::SupervisorError;
pub const SERVER_ENV: &str = "PRINTOBSERVER_SERVER";
pub const CREDENTIAL_ENV: &str = "PRINTOBSERVER_CREDENTIAL";
const REDACTED: &str = "<redacted>";
pub struct TurnPass {
server: Option<SocketAddr>,
credential: String,
}
impl TurnPass {
pub fn new(server: Option<SocketAddr>, credential: String) -> Result<Self, SupervisorError> {
if credential.is_empty() || !credential.bytes().all(|byte| byte.is_ascii_graphic()) {
return Err(SupervisorError::Unavailable {
detail: "the credential minted for this run is not one a request carries intact"
.to_owned(),
});
}
Ok(Self { server, credential })
}
#[must_use]
pub fn environment(&self) -> Vec<(&'static str, String)> {
let mut assigned = Vec::with_capacity(2);
if let Some(server) = self.server {
assigned.push((SERVER_ENV, format!("http://{server}")));
}
assigned.push((CREDENTIAL_ENV, self.credential.clone()));
assigned
}
}
impl core::fmt::Debug for TurnPass {
fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
formatter
.debug_struct("TurnPass")
.field("server", &self.server)
.field("credential", &REDACTED)
.finish()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TurnSession(String);
impl TurnSession {
pub fn new(name: impl Into<String>) -> Result<Self, SupervisorError> {
let name = name.into();
if name.trim().is_empty() {
return Err(SupervisorError::Unavailable {
detail: "a run in no session is issued no credential".to_owned(),
});
}
Ok(Self(name))
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl core::fmt::Display for TurnSession {
fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
formatter.write_str(&self.0)
}
}
pub trait TurnAccess: Send + Sync {
fn issue(&self, session: &TurnSession) -> Result<TurnPass, SupervisorError>;
}
#[cfg(test)]
mod tests {
use super::{CREDENTIAL_ENV, SERVER_ENV, TurnPass, TurnSession};
#[test]
fn a_pass_carries_both_variables_and_renders_no_credential() {
let pass = TurnPass::new(
Some("127.0.0.1:8420".parse().expect("an address")),
"qz7vk3xhw9mrbt2ycf5jdlgnps46auei".to_owned(),
)
.expect("a credential a header carries");
assert_eq!(
pass.environment(),
vec![
(SERVER_ENV, "http://127.0.0.1:8420".to_owned()),
(
CREDENTIAL_ENV,
"qz7vk3xhw9mrbt2ycf5jdlgnps46auei".to_owned()
),
]
);
assert!(!format!("{pass:?}").contains("qz7vk3xhw9mrbt2ycf5jdlgnps46auei"));
assert_eq!(
TurnPass::new(None, "c".to_owned())
.expect("one character is a credential")
.environment(),
vec![(CREDENTIAL_ENV, "c".to_owned())]
);
}
#[test]
fn a_blank_session_is_refused() {
for blank in ["", " ", "\t"] {
assert!(
TurnSession::new(blank).is_err(),
"{blank:?} was taken as a session"
);
}
let session = TurnSession::new("print-a-2").expect("a session");
assert_eq!(session.as_str(), "print-a-2");
assert_eq!(session.to_string(), "print-a-2");
}
#[test]
fn a_credential_no_header_carries_is_refused() {
for refused in ["", "qx-two words", "qx-tab\there", "qx-caf\u{e9}"] {
let error = TurnPass::new(None, refused.to_owned())
.err()
.unwrap_or_else(|| panic!("{refused:?} was taken as a credential"));
assert!(!error.to_string().contains("qx-"), "{error}");
}
}
}