1#![deny(missing_debug_implementations)]
35
36#[macro_use]
37extern crate serde;
38#[macro_use]
39extern crate log;
40#[macro_use]
41extern crate strum_macros;
42
43pub mod errors;
44pub mod mediatypes;
45pub mod reference;
46pub mod render;
47pub mod v2;
48
49use errors::{Result, Error};
50use std::collections::HashMap;
51use std::io::Read;
52
53
54pub static USER_AGENT: &str = "camallo-dkregistry/0.0";
56
57pub fn get_credentials<T: Read>(
62 reader: T,
63 index: &str,
64) -> Result<(Option<String>, Option<String>)> {
65 let map: Auths = serde_json::from_reader(reader)?;
66 let real_index = match index {
67 "docker.io" | "registry-1.docker.io" => "https://index.docker.io/v1/",
69 other => other,
70 };
71 let auth = match map.auths.get(real_index) {
72 Some(x) => base64::decode(x.auth.as_str())?,
73 None => return Err(Error::AuthInfoMissing(real_index.to_string())),
74 };
75 let s = String::from_utf8(auth)?;
76 let creds: Vec<&str> = s.splitn(2, ':').collect();
77 let up = match (creds.get(0), creds.get(1)) {
78 (Some(&""), Some(p)) => (None, Some(p.to_string())),
79 (Some(u), Some(&"")) => (Some(u.to_string()), None),
80 (Some(u), Some(p)) => (Some(u.to_string()), Some(p.to_string())),
81 (_, _) => (None, None),
82 };
83 trace!("Found credentials for user={:?} on {}", up.0, index);
84 Ok(up)
85}
86
87#[derive(Debug, Deserialize, Serialize)]
88struct Auths {
89 auths: HashMap<String, AuthObj>,
90}
91
92#[derive(Debug, Default, Deserialize, Serialize)]
93struct AuthObj {
94 auth: String,
95}