azure-devops 0.1.0

Idiomatic Rust client for Azure DevOps REST APIs
Documentation
use secrecy::{ExposeSecret, SecretString};
use std::fmt;

/// Authentication credentials for Azure DevOps.
///
/// Currently supports Personal Access Token (PAT) authentication.
/// Additional mechanisms (Basic, Bearer, etc.) can be added as needed.
pub enum Credentials {
    /// Authenticate using a Personal Access Token.
    Pat(SecretString),
}

impl Credentials {
    /// Create credentials from a PAT string.
    ///
    /// The token is stored in a [`SecretString`] to prevent accidental
    /// exposure in logs or debug output.
    pub fn from_pat(token: impl Into<String>) -> Self {
        Credentials::Pat(SecretString::new(token.into().into()))
    }

    /// Apply the credentials to an HTTP request.
    ///
    /// For PAT: uses HTTP Basic auth with an empty username and the PAT as password,
    /// which is how Azure DevOps expects PATs to be sent.
    pub(crate) fn apply(&self, request: reqwest::RequestBuilder) -> reqwest::RequestBuilder {
        match self {
            Credentials::Pat(token) => request.basic_auth("", Some(token.expose_secret())),
        }
    }
}

impl fmt::Debug for Credentials {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Credentials::Pat(_) => write!(f, "Pat(<redacted>)"),
        }
    }
}

impl Clone for Credentials {
    fn clone(&self) -> Self {
        match self {
            Credentials::Pat(t) => Credentials::Pat(t.clone()),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn from_pat_creates_pat_variant() {
        let creds = Credentials::from_pat("my-token");
        match creds {
            Credentials::Pat(_) => {}
        }
    }

    #[test]
    fn debug_does_not_expose_token() {
        let creds = Credentials::from_pat("super-secret-token");
        let debug_str = format!("{:?}", creds);
        assert!(!debug_str.contains("super-secret-token"));
        assert!(debug_str.contains("redacted"));
    }

    #[test]
    fn clone_preserves_token() {
        let creds = Credentials::from_pat("my-token");
        let cloned = creds.clone();
        // Verify the token is the same by checking Debug output is identical
        assert_eq!(format!("{:?}", creds), format!("{:?}", cloned));
    }
}