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
//! A pure-Rust asynchronous library for Docker Registry API.
//!
//! This library provides support for asynchronous interaction with
//! container registries conformant to the Docker Registry HTTP API V2.
//!
//! ## Example
//!
//! ```rust,no_run
//! # use tokio;
//!
//! # #[tokio::main]
//! # async fn main() {
//! # async fn run() -> docker_registry::errors::Result<()> {
//! #
//! use docker_registry::v2::Client;
//!
//! // Check whether a registry supports API v2.
//! let host = "quay.io";
//! let client = Client::configure()
//!   .insecure_registry(false)
//!   .registry(host)
//!   .build()?;
//! match client.is_v2_supported().await? {
//!   false => println!("{} does NOT support v2", host),
//!   true => println!("{} supports v2", host),
//! };
//! #
//! # Ok(())
//! # };
//! # run().await.unwrap();
//! # }
//! ```

#![deny(missing_debug_implementations)]

use log::trace;
use serde::{Deserialize, Serialize};

pub mod errors;
pub mod mediatypes;
pub mod reference;
pub mod render;
pub mod v2;

use std::{collections::HashMap, io::Read};

use base64::prelude::*;
use errors::{Error, Result};

/// Default User-Agent client identity.
pub static USER_AGENT: &str = "clowdhaus/docker-registry/0.0";

/// Get registry credentials from a JSON config reader.
///
/// This is a convenience decoder for docker-client credentials
/// typically stored under `~/.docker/config.json`.
pub fn get_credentials<T: Read>(reader: T, index: &str) -> Result<(Option<String>, Option<String>)> {
  let map: Auths = serde_json::from_reader(reader)?;
  let real_index = match index {
    // docker.io has some special casing in config.json
    "docker.io" | "registry-1.docker.io" => "https://index.docker.io/v1/",
    other => other,
  };
  let auth = match map.auths.get(real_index) {
    Some(x) => BASE64_STANDARD.decode(x.auth.as_str())?,
    None => return Err(Error::AuthInfoMissing(real_index.to_string())),
  };
  let s = String::from_utf8(auth)?;
  let creds: Vec<&str> = s.splitn(2, ':').collect();
  let up = match (creds.first(), creds.get(1)) {
    (Some(&""), Some(p)) => (None, Some(p.to_string())),
    (Some(u), Some(&"")) => (Some(u.to_string()), None),
    (Some(u), Some(p)) => (Some(u.to_string()), Some(p.to_string())),
    (_, _) => (None, None),
  };
  trace!("Found credentials for user={:?} on {}", up.0, index);
  Ok(up)
}

#[derive(Debug, Deserialize, Serialize)]
struct Auths {
  auths: HashMap<String, AuthObj>,
}

#[derive(Debug, Default, Deserialize, Serialize)]
struct AuthObj {
  auth: String,
}