Skip to main content

oci_client/
secrets.rs

1//! Types for working with registry access secrets
2
3use std::fmt;
4
5/// A method for authenticating to a registry
6#[derive(Eq, PartialEq, Clone)]
7pub enum RegistryAuth {
8    /// Access the registry anonymously
9    Anonymous,
10    /// Access the registry using HTTP Basic authentication
11    Basic(String, String),
12    /// Access the registry using Bearer token authentication
13    Bearer(String),
14}
15
16impl fmt::Debug for RegistryAuth {
17    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18        match self {
19            RegistryAuth::Anonymous => write!(f, "Anonymous"),
20            RegistryAuth::Basic(username, _) => f
21                .debug_tuple("Basic")
22                .field(username)
23                .field(&"<redacted>")
24                .finish(),
25            RegistryAuth::Bearer(_) => f.debug_tuple("Bearer").field(&"<redacted>").finish(),
26        }
27    }
28}
29
30pub(crate) trait Authenticable {
31    fn apply_authentication(self, auth: &RegistryAuth) -> Self;
32}
33
34impl Authenticable for reqwest::RequestBuilder {
35    fn apply_authentication(self, auth: &RegistryAuth) -> Self {
36        match auth {
37            RegistryAuth::Anonymous => self,
38            RegistryAuth::Basic(username, password) => self.basic_auth(username, Some(password)),
39            RegistryAuth::Bearer(token) => self.bearer_auth(token),
40        }
41    }
42}