aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/util/github.rs
blob: c87a83fcb1421aa4e147a9bf6328d9ab9a681198 (plain)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
//! ref: composer/src/Composer/Util/GitHub.php

use crate::io::io_interface;
use indexmap::IndexMap;
use shirabe_external_packages::composer::pcre::{CaptureKey, Preg};
use shirabe_php_shim::{PhpMixed, date, stripos, strtolower};

use crate::config::Config;
use crate::downloader::TransportException;
use crate::factory::Factory;
use crate::io::IOInterface;
use crate::io::IOInterfaceImmutable;
use crate::util::HttpDownloader;
use crate::util::ProcessExecutor;

#[derive(Debug)]
pub struct GitHub {
    io: std::rc::Rc<std::cell::RefCell<dyn IOInterface>>,
    config: std::rc::Rc<std::cell::RefCell<Config>>,
    process: std::rc::Rc<std::cell::RefCell<ProcessExecutor>>,
    http_downloader: std::rc::Rc<std::cell::RefCell<HttpDownloader>>,
}

impl GitHub {
    pub const GITHUB_TOKEN_REGEX: &'static str =
        r"{^([a-f0-9]{12,}|gh[a-z]_[a-zA-Z0-9_]+|github_pat_[a-zA-Z0-9_]+)$}";

    pub fn new(
        io: std::rc::Rc<std::cell::RefCell<dyn IOInterface>>,
        config: std::rc::Rc<std::cell::RefCell<Config>>,
        process: Option<std::rc::Rc<std::cell::RefCell<ProcessExecutor>>>,
        http_downloader: Option<std::rc::Rc<std::cell::RefCell<HttpDownloader>>>,
    ) -> anyhow::Result<Self> {
        let process = process.unwrap_or_else(|| {
            std::rc::Rc::new(std::cell::RefCell::new(ProcessExecutor::new(Some(
                io.clone(),
            ))))
        });
        let http_downloader = match http_downloader {
            Some(h) => h,
            None => std::rc::Rc::new(std::cell::RefCell::new(Factory::create_http_downloader(
                io.clone(),
                &config,
                IndexMap::new(),
            )?)),
        };
        Ok(Self {
            io,
            config,
            process,
            http_downloader,
        })
    }

    pub fn authorize_oauth(&mut self, origin_url: &str) -> bool {
        let github_domains = self.config.borrow_mut().get("github-domains");
        let domains = match github_domains.as_array() {
            Some(arr) => arr.clone(),
            None => return false,
        };
        let origin_in_domains = domains.values().any(|v| v.as_string() == Some(origin_url));
        if !origin_in_domains {
            return false;
        }

        let mut output = String::new();
        if self.process.borrow_mut().execute_args(
            &[
                "git".to_string(),
                "config".to_string(),
                "github.accesstoken".to_string(),
            ],
            &mut output,
            (),
        ) == 0
        {
            self.io.borrow_mut().set_authentication(
                origin_url.to_string(),
                output.trim().to_string(),
                Some("x-oauth-basic".to_string()),
            );
            return true;
        }

        false
    }

    pub fn authorize_oauth_interactively(
        &mut self,
        origin_url: &str,
        message: Option<&str>,
    ) -> anyhow::Result<bool> {
        if let Some(msg) = message {
            self.io.write_error3(msg, true, io_interface::NORMAL);
        }

        let mut note = "Composer".to_string();
        let expose_hostname = self
            .config
            .borrow_mut()
            .get("github-expose-hostname")
            .as_bool()
            .unwrap_or(false);
        if expose_hostname {
            let mut output = String::new();
            if self
                .process
                .borrow_mut()
                .execute_args(&["hostname".to_string()], &mut output, ())
                == 0
            {
                note += &format!(" on {}", output.trim());
            }
        }
        note += &format!(" {}", date("Y-m-d Hi", None));

        // PHP: writeError(array) joins with newline. TODO(phase-b): writeError accepts array natively in Symfony.
        let (local_name, auth_name): (Option<String>, String) = {
            let cfg = self.config.borrow();
            (
                cfg.get_local_auth_config_source()
                    .map(|c| c.get_name().to_string()),
                cfg.get_auth_config_source().get_name().to_string(),
            )
        };
        let prefix = local_name
            .as_ref()
            .map(|n| format!("{} OR ", n))
            .unwrap_or_default();
        let lines = [
            "You need to provide a GitHub access token.".to_string(),
            format!(
                "Tokens will be stored in plain text in \"{}{}\" for future use by Composer.",
                prefix, auth_name
            ),
            "Due to the security risk of tokens being exfiltrated, use tokens with short expiration times and only the minimum permissions necessary.".to_string(),
            String::new(),
            "Carefully consider the following options in order:".to_string(),
            String::new(),
        ];
        self.io
            .write_error3(&lines.join("\n"), true, io_interface::NORMAL);

        let encoded_note = shirabe_php_shim::rawurlencode(&note).replace("%20", "+");
        let lines = [
            "1. When you don't use 'vcs'  type 'repositories'  in composer.json and do not need to clone source or download dist files".to_string(),
            "from private GitHub repositories over HTTPS, use a fine-grained token with read-only access to public information.".to_string(),
            "Use the following URL to create such a token:".to_string(),
            format!(
                "https://{}/settings/personal-access-tokens/new?name={}",
                origin_url, encoded_note
            ),
            String::new(),
        ];
        self.io
            .write_error3(&lines.join("\n"), true, io_interface::NORMAL);

        let lines = [
            "2. When all relevant _private_ GitHub repositories belong to a single user or organisation, use a fine-grained token with".to_string(),
            "repository \"content\" read-only permissions. You can start with the following URL, but you may need to change the resource owner".to_string(),
            "to the right user or organisation. Additionally, you can scope permissions down to apply only to selected repositories.".to_string(),
            format!(
                "https://{}/settings/personal-access-tokens/new?contents=read&name={}",
                origin_url, encoded_note
            ),
            String::new(),
        ];
        self.io
            .write_error3(&lines.join("\n"), true, io_interface::NORMAL);

        let mut lines3 = vec![
            "3. A \"classic\" token grants broad permissions on your behalf to all repositories accessible by you.".to_string(),
            "This may include write permissions, even though not needed by Composer. Use it only when you need to access".to_string(),
            "private repositories across multiple organisations at the same time and using directory-specific authentication sources".to_string(),
            "is not an option. You can generate a classic token here:".to_string(),
            format!(
                "https://{}/settings/tokens/new?scopes=repo&description={}",
                origin_url, encoded_note
            ),
            String::new(),
        ];
        let _ = &mut lines3;
        self.io
            .write_error3(&lines3.join("\n"), true, io_interface::NORMAL);

        self.io.write_error3(
            "For additional information, check https://getcomposer.org/doc/articles/authentication-for-private-packages.md#github-oauth",
            true,
            io_interface::NORMAL,
        );

        let mut store_in_local_auth_config = false;
        if local_name.is_some() {
            store_in_local_auth_config = self.io.ask_confirmation(
                "A local auth config source was found, do you want to store the token there?"
                    .to_string(),
                true,
            );
        }

        let token = self
            .io
            .ask_and_hide_answer("Token (hidden): ".to_string())
            .unwrap_or_default()
            .trim()
            .to_string();

        if token.is_empty() {
            self.io.write_error3(
                "<warning>No token given, aborting.</warning>",
                true,
                io_interface::NORMAL,
            );
            self.io.write_error3(
                "You can also add it manually later by using \"composer config --global --auth github-oauth.github.com <token>\"",
                true,
                io_interface::NORMAL,
            );
            return Ok(false);
        }

        self.io.borrow_mut().set_authentication(
            origin_url.to_string(),
            token.clone(),
            Some("x-oauth-basic".to_string()),
        );

        let api_url = if origin_url == "github.com" {
            "api.github.com/".to_string()
        } else {
            format!("{}/api/v3/", origin_url)
        };

        let mut http_options: indexmap::IndexMap<String, PhpMixed> = indexmap::IndexMap::new();
        http_options.insert("retry-auth-failure".to_string(), PhpMixed::Bool(false));

        match self
            .http_downloader
            .borrow_mut()
            .get(&format!("https://{}", api_url), http_options)
        {
            Ok(_) => {}
            Err(te) => {
                // TODO(phase-b): downcast anyhow::Error to TransportException for status code
                let code = te
                    .downcast_ref::<crate::downloader::TransportException>()
                    .and_then(|t| t.get_status_code())
                    .unwrap_or(0);
                if code == 403 || code == 401 {
                    self.io.write_error3(
                        "<error>Invalid token provided.</error>",
                        true,
                        io_interface::NORMAL,
                    );
                    self.io.write_error3(
                        "You can also add it manually later by using \"composer config --global --auth github-oauth.github.com <token>\"",
                        true,
                        io_interface::NORMAL,
                    );
                    return Ok(false);
                }
                return Err(te.into());
            }
        }

        // TODO(phase-b): Config getters return references; cross-borrow chains require
        // Rc<RefCell<dyn ConfigSourceInterface>> shape. For now use _mut variants.
        let use_local = store_in_local_auth_config
            && self
                .config
                .borrow()
                .get_local_auth_config_source()
                .is_some();
        let key = format!("github-oauth.{}", origin_url);
        {
            let mut cfg = self.config.borrow_mut();
            cfg.get_config_source_mut().remove_config_setting(&key)?;
        }
        if use_local {
            let mut cfg = self.config.borrow_mut();
            if let Some(local) = cfg.get_local_auth_config_source_mut() {
                local.add_config_setting(&key, PhpMixed::String(token))?;
            }
        } else {
            let mut cfg = self.config.borrow_mut();
            cfg.get_auth_config_source_mut()
                .add_config_setting(&key, PhpMixed::String(token))?;
        }

        self.io.write_error3(
            "<info>Token stored successfully.</info>",
            true,
            io_interface::NORMAL,
        );

        Ok(true)
    }

    pub fn get_rate_limit(&self, headers: &[String]) -> indexmap::IndexMap<String, PhpMixed> {
        let mut rate_limit = indexmap::IndexMap::new();
        rate_limit.insert("limit".to_string(), PhpMixed::String("?".to_string()));
        rate_limit.insert("reset".to_string(), PhpMixed::String("?".to_string()));

        for header in headers {
            let header = header.trim();
            if stripos(header, "x-ratelimit-").is_none() {
                continue;
            }
            let parts: Vec<&str> = header.splitn(2, ':').collect();
            if parts.len() < 2 {
                continue;
            }
            let (r#type, value) = (parts[0], parts[1]);
            match strtolower(r#type).as_str() {
                "x-ratelimit-limit" => {
                    let v: i64 = value.trim().parse().unwrap_or(0);
                    rate_limit.insert("limit".to_string(), PhpMixed::Int(v));
                }
                "x-ratelimit-reset" => {
                    let ts: i64 = value.trim().parse().unwrap_or(0);
                    rate_limit.insert(
                        "reset".to_string(),
                        PhpMixed::String(date("Y-m-d H:i:s", Some(ts))),
                    );
                }
                _ => {}
            }
        }

        rate_limit
    }

    pub fn get_sso_url(&self, headers: &[String]) -> Option<String> {
        for header in headers {
            let header = header.trim();
            if stripos(header, "x-github-sso: required").is_none() {
                continue;
            }
            let mut caps: IndexMap<CaptureKey, String> = IndexMap::new();
            if Preg::match_strict_groups3(r"{\burl=(?P<url>[^\s;]+)}", header, Some(&mut caps))
                .unwrap_or(false)
            {
                return caps.get(&CaptureKey::ByName("url".to_string())).cloned();
            }
        }

        None
    }

    pub fn is_rate_limited(&self, headers: &[String]) -> bool {
        for header in headers {
            if Preg::is_match(r"{^x-ratelimit-remaining: *0$}i", header.trim()).unwrap_or(false) {
                return true;
            }
        }

        false
    }

    pub fn requires_sso(&self, headers: &[String]) -> bool {
        for header in headers {
            if Preg::is_match(r"{^x-github-sso: required}i", header.trim()).unwrap_or(false) {
                return true;
            }
        }

        false
    }
}