alloy_transport/
common.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use base64::{engine::general_purpose, Engine};
use std::{fmt, net::SocketAddr};

/// Basic, bearer or raw authentication in http or websocket transport.
///
/// Use to inject username and password or an auth token into requests.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Authorization {
    /// [RFC7617](https://datatracker.ietf.org/doc/html/rfc7617) HTTP Basic Auth.
    Basic(String),
    /// [RFC6750](https://datatracker.ietf.org/doc/html/rfc6750) Bearer Auth.
    Bearer(String),
    /// Raw auth string.
    Raw(String),
}

impl Authorization {
    /// Extract the auth info from a URL.
    pub fn extract_from_url(url: &url::Url) -> Option<Self> {
        let username = url.username();
        let password = url.password().unwrap_or_default();

        // eliminates false positives on the authority
        if username.contains("localhost") || username.parse::<SocketAddr>().is_ok() {
            return None;
        }

        (!username.is_empty() || !password.is_empty()).then(|| Self::basic(username, password))
    }

    /// Instantiate a new basic auth from an authority string.
    pub fn authority(auth: impl AsRef<str>) -> Self {
        let auth_secret = general_purpose::STANDARD.encode(auth.as_ref());
        Self::Basic(auth_secret)
    }

    /// Instantiate a new basic auth from a username and password.
    pub fn basic(username: impl AsRef<str>, password: impl AsRef<str>) -> Self {
        let username = username.as_ref();
        let password = password.as_ref();
        Self::authority(format!("{username}:{password}"))
    }

    /// Instantiate a new bearer auth from the given token.
    pub fn bearer(token: impl Into<String>) -> Self {
        Self::Bearer(token.into())
    }

    /// Instantiate a new raw auth from the given token.
    pub fn raw(token: impl Into<String>) -> Self {
        Self::Raw(token.into())
    }
}

impl fmt::Display for Authorization {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Basic(auth) => write!(f, "Basic {auth}"),
            Self::Bearer(auth) => write!(f, "Bearer {auth}"),
            Self::Raw(auth) => write!(f, "{auth}"),
        }
    }
}

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

    #[test]
    fn test_extract_from_url_with_basic_auth() {
        let url = Url::parse("http://username:password@domain.com").unwrap();
        let auth = Authorization::extract_from_url(&url).unwrap();

        // Expected Basic auth encoded in base64
        assert_eq!(
            auth,
            Authorization::Basic(general_purpose::STANDARD.encode("username:password"))
        );
    }

    #[test]
    fn test_extract_from_url_no_auth() {
        let url = Url::parse("http://domain.com").unwrap();
        assert!(Authorization::extract_from_url(&url).is_none());
    }

    #[test]
    fn test_extract_from_url_with_localhost() {
        let url = Url::parse("http://localhost:password@domain.com").unwrap();
        assert!(Authorization::extract_from_url(&url).is_none());
    }

    #[test]
    fn test_extract_from_url_with_socket_address() {
        let url = Url::parse("http://127.0.0.1:8080").unwrap();
        assert!(Authorization::extract_from_url(&url).is_none());
    }

    #[test]
    fn test_authority() {
        let auth = Authorization::authority("user:pass");
        assert_eq!(auth, Authorization::Basic(general_purpose::STANDARD.encode("user:pass")));
    }

    #[test]
    fn test_basic() {
        let auth = Authorization::basic("user", "pass");
        assert_eq!(auth, Authorization::Basic(general_purpose::STANDARD.encode("user:pass")));
    }

    #[test]
    fn test_raw() {
        let auth = Authorization::raw("raw_token");
        assert_eq!(auth, Authorization::Raw("raw_token".to_string()));
    }
}