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