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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// SPDX-FileCopyrightText: 2022 Profian Inc. <opensource@profian.com>
// SPDX-License-Identifier: AGPL-3.0-only

#![warn(rust_2018_idioms, unused_lifetimes, unused_qualifications, clippy::all)]
#![forbid(unsafe_code)]

mod entity;
mod repo;
mod tag;
mod tree;
mod user;

pub use entity::*;
pub use repo::*;
pub use tag::*;
pub use tree::*;
pub use user::*;

pub use drawbridge_type as types;

pub use anyhow::{Context, Result};
pub use mime;
pub use url::Url;

use std::sync::Arc;

use drawbridge_type::{RepositoryContext, TagContext, TreeContext, UserContext};

use rustls::cipher_suite::{
    TLS13_AES_128_GCM_SHA256, TLS13_AES_256_GCM_SHA384, TLS13_CHACHA20_POLY1305_SHA256,
};
use rustls::kx_group::{SECP256R1, SECP384R1, X25519};
use rustls::version::TLS13;
use rustls::{Certificate, OwnedTrustAnchor, PrivateKey, RootCertStore};

#[derive(Clone, Debug)]
pub struct Client {
    inner: ureq::Agent,
    root: Url,
    token: Option<String>,
}

impl Client {
    pub fn builder(url: Url) -> ClientBuilder {
        ClientBuilder::new(url)
    }

    pub fn new(url: Url) -> Result<Self> {
        Self::builder(url).build()
    }

    fn url(&self, path: &str) -> Result<Url> {
        format!("{}{path}", self.root)
            .parse()
            .context("failed to construct URL")
    }

    pub fn user(&self, UserContext { name }: &UserContext) -> User<'_> {
        User::new(Entity::new(self), name)
    }

    pub fn repository<'a>(
        &'a self,
        RepositoryContext { owner, name }: &'a RepositoryContext,
    ) -> Repository<'_> {
        self.user(owner).repository(name)
    }

    pub fn tag<'a>(&'a self, TagContext { repository, name }: &'a TagContext) -> Tag<'_> {
        self.repository(repository).tag(name)
    }

    pub fn tree<'a>(&'a self, TreeContext { tag, path }: &'a TreeContext) -> Node<'_> {
        self.tag(tag).path(path)
    }
}

#[derive(Clone, Debug)]
pub struct ClientBuilder {
    url: Url,
    credentials: Option<(Vec<Certificate>, PrivateKey)>,
    roots: Option<RootCertStore>,
    token: Option<String>,
}

impl ClientBuilder {
    pub fn new(url: Url) -> Self {
        Self {
            url,
            credentials: None,
            roots: None,
            token: None,
        }
    }

    pub fn credentials(self, cert: Vec<Certificate>, key: PrivateKey) -> Self {
        Self {
            credentials: Some((cert, key)),
            ..self
        }
    }

    pub fn roots(self, roots: RootCertStore) -> Self {
        Self {
            roots: Some(roots),
            ..self
        }
    }

    pub fn token(self, token: impl Into<String>) -> Self {
        Self {
            token: Some(token.into()),
            ..self
        }
    }

    pub fn build(self) -> Result<Client> {
        let root = self
            .url
            .join(&format!("api/v{}", env!("CARGO_PKG_VERSION")))
            .context("failed to contruct URL")?;

        let tls = rustls::ClientConfig::builder()
            .with_cipher_suites(&[
                TLS13_AES_256_GCM_SHA384,
                TLS13_AES_128_GCM_SHA256,
                TLS13_CHACHA20_POLY1305_SHA256,
            ])
            .with_kx_groups(&[&X25519, &SECP384R1, &SECP256R1])
            .with_protocol_versions(&[&TLS13])?
            .with_root_certificates(if let Some(roots) = self.roots {
                roots
            } else {
                let mut root_store = RootCertStore::empty();
                root_store.add_server_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.0.iter().map(
                    |ta| {
                        OwnedTrustAnchor::from_subject_spki_name_constraints(
                            ta.subject,
                            ta.spki,
                            ta.name_constraints,
                        )
                    },
                ));
                root_store
            });
        let tls = if let Some((cert, key)) = self.credentials {
            tls.with_single_cert(cert, key)?
        } else {
            tls.with_no_client_auth()
        };

        Ok(Client {
            inner: ureq::AgentBuilder::new().tls_config(Arc::new(tls)).build(),
            root,
            token: self.token,
        })
    }
}