#![deny(missing_debug_implementations)]
#[macro_use]
extern crate serde;
#[macro_use]
extern crate log;
#[macro_use]
extern crate strum_macros;
pub mod errors;
pub mod mediatypes;
pub mod reference;
pub mod render;
pub mod v2;
use errors::{Result, Error};
use std::collections::HashMap;
use std::io::Read;
pub static USER_AGENT: &str = "camallo-dkregistry/0.0";
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" | "registry-1.docker.io" => "https://index.docker.io/v1/",
other => other,
};
let auth = match map.auths.get(real_index) {
Some(x) => base64::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.get(0), 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,
}