aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/util/gitlab.rs
blob: b245fb90c6f76f1ced5a537a41f68bd7e1d0cd3b (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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
//! ref: composer/src/Composer/Util/GitLab.php

use indexmap::IndexMap;
use shirabe_external_packages::composer::pcre::preg::Preg;
use shirabe_php_shim::{http_build_query, json_decode, time, PhpMixed, RuntimeException};

use crate::config::Config;
use crate::downloader::transport_exception::TransportException;
use crate::factory::Factory;
use crate::io::io_interface::IOInterface;
use crate::util::http_downloader::HttpDownloader;
use crate::util::process_executor::ProcessExecutor;

#[derive(Debug)]
pub struct GitLab {
    pub(crate) io: Box<dyn IOInterface>,
    pub(crate) config: Config,
    pub(crate) process: ProcessExecutor,
    pub(crate) http_downloader: HttpDownloader,
}

impl GitLab {
    pub fn new(
        io: Box<dyn IOInterface>,
        config: Config,
        process: Option<ProcessExecutor>,
        http_downloader: Option<HttpDownloader>,
    ) -> anyhow::Result<Self> {
        let process = process.unwrap_or_else(|| ProcessExecutor::new(&*io));
        let http_downloader = match http_downloader {
            Some(h) => h,
            None => Factory::create_http_downloader(&*io, &config)?,
        };
        Ok(Self {
            io,
            config,
            process,
            http_downloader,
        })
    }

    pub fn authorize_oauth(&mut self, origin_url: &str) -> bool {
        // before composer 1.9, origin URLs had no port number in them
        let bc_origin_url = Preg::replace("{:\\d+}", "", origin_url)
            .unwrap_or_else(|_| origin_url.to_string());

        let gitlab_domains = self.config.get("gitlab-domains");
        let domains = match gitlab_domains.as_array() {
            Some(arr) => arr.clone(),
            None => return false,
        };
        let origin_in_domains = domains.values().any(|v| v.as_string() == Some(origin_url));
        let bc_in_domains = domains.values().any(|v| v.as_string() == Some(bc_origin_url.as_str()));
        if !origin_in_domains && !bc_in_domains {
            return false;
        }

        // if available use token from git config
        let mut output = String::new();
        if self.process.execute(
            &[
                "git".to_string(),
                "config".to_string(),
                "gitlab.accesstoken".to_string(),
            ],
            &mut output,
            None,
        ) == 0
        {
            self.io.set_authentication(
                origin_url.to_string(),
                output.trim().to_string(),
                Some("oauth2".to_string()),
            );
            return true;
        }

        // if available use deploy token from git config
        let mut token_user = String::new();
        let mut token_password = String::new();
        if self.process.execute(
            &[
                "git".to_string(),
                "config".to_string(),
                "gitlab.deploytoken.user".to_string(),
            ],
            &mut token_user,
            None,
        ) == 0
            && self.process.execute(
                &[
                    "git".to_string(),
                    "config".to_string(),
                    "gitlab.deploytoken.token".to_string(),
                ],
                &mut token_password,
                None,
            ) == 0
        {
            self.io.set_authentication(
                origin_url.to_string(),
                token_user.trim().to_string(),
                Some(token_password.trim().to_string()),
            );
            return true;
        }

        // if available use token from composer config
        let auth_tokens = self.config.get("gitlab-token");

        let mut token: Option<PhpMixed> = None;

        if let Some(map) = auth_tokens.as_array() {
            if let Some(t) = map.get(origin_url) {
                token = Some(*t.clone());
            }
            if let Some(t) = map.get(bc_origin_url.as_str()) {
                token = Some(*t.clone());
            }
        }

        if let Some(token) = token {
            let (username, password) = match &token {
                PhpMixed::Array(arr) => {
                    let username = arr
                        .get("username")
                        .and_then(|v| v.as_string())
                        .unwrap_or("")
                        .to_string();
                    let password = arr
                        .get("token")
                        .and_then(|v| v.as_string())
                        .unwrap_or("private-token")
                        .to_string();
                    (username, password)
                }
                _ => {
                    let username = token.as_string().unwrap_or("").to_string();
                    let password = "private-token".to_string();
                    (username, password)
                }
            };

            // Composer expects the GitLab token to be stored as username and 'private-token' or
            // 'gitlab-ci-token' to be stored as password. Detect cases where this is reversed
            // and automatically resolve it.
            if ["private-token", "gitlab-ci-token", "oauth2"].contains(&username.as_str()) {
                self.io
                    .set_authentication(origin_url.to_string(), password, Some(username));
            } else {
                self.io
                    .set_authentication(origin_url.to_string(), username, Some(password));
            }

            return true;
        }

        false
    }

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

        let local_auth_config = self.config.get_local_auth_config_source();
        let personal_access_token_link = format!(
            "{}://{}/-/user_settings/personal_access_tokens",
            scheme, origin_url
        );
        let revoke_link = format!("{}://{}/-/user_settings/applications", scheme, origin_url);
        self.io.write_error(
            PhpMixed::String(format!(
                "A token will be created and stored in \"{}\", your password will never be stored",
                local_auth_config
                    .as_ref()
                    .map(|c| format!("{} OR ", c.get_name()))
                    .unwrap_or_default()
                    + &self.config.get_auth_config_source().get_name()
            )),
            true,
            IOInterface::NORMAL,
        );
        self.io.write_error(
            PhpMixed::String("To revoke access to this token you can visit:".to_string()),
            true,
            IOInterface::NORMAL,
        );
        self.io.write_error(
            PhpMixed::String(revoke_link.clone()),
            true,
            IOInterface::NORMAL,
        );
        self.io.write_error(
            PhpMixed::String(
                "Alternatively you can setup an personal access token on:".to_string(),
            ),
            true,
            IOInterface::NORMAL,
        );
        self.io.write_error(
            PhpMixed::String(personal_access_token_link.clone()),
            true,
            IOInterface::NORMAL,
        );
        self.io.write_error(
            PhpMixed::String("and store it under \"gitlab-token\" see https://getcomposer.org/doc/articles/authentication-for-private-packages.md#gitlab-token for more details.".to_string()),
            true,
            IOInterface::NORMAL,
        );
        self.io.write_error(
            PhpMixed::String("https://getcomposer.org/doc/articles/authentication-for-private-packages.md#gitlab-token".to_string()),
            true,
            IOInterface::NORMAL,
        );
        self.io.write_error(
            PhpMixed::String("for more details.".to_string()),
            true,
            IOInterface::NORMAL,
        );

        let mut store_in_local_auth_config = false;
        if local_auth_config.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 mut attempt_counter = 0;

        while attempt_counter < 5 {
            attempt_counter += 1;
            let response = match self.create_token(scheme, origin_url) {
                Ok(r) => r,
                Err(e) => {
                    // 401 is bad credentials,
                    // 403 is max login attempts exceeded
                    match e.downcast::<TransportException>() {
                        Ok(te) if te.code == 403 || te.code == 401 => {
                            if te.code == 401 {
                                let response = te
                                    .get_response()
                                    .and_then(|r| json_decode(r, true).ok());
                                let is_invalid_grant = response
                                    .as_ref()
                                    .and_then(|r| r.as_array())
                                    .and_then(|arr| arr.get("error"))
                                    .and_then(|v| v.as_string())
                                    == Some("invalid_grant");
                                if is_invalid_grant {
                                    self.io.write_error(
                                        PhpMixed::String("Bad credentials. If you have two factor authentication enabled you will have to manually create a personal access token".to_string()),
                                        true,
                                        IOInterface::NORMAL,
                                    );
                                } else {
                                    self.io.write_error(
                                        PhpMixed::String("Bad credentials.".to_string()),
                                        true,
                                        IOInterface::NORMAL,
                                    );
                                }
                            } else {
                                self.io.write_error(
                                    PhpMixed::String("Maximum number of login attempts exceeded. Please try again later.".to_string()),
                                    true,
                                    IOInterface::NORMAL,
                                );
                            }

                            self.io.write_error(
                                PhpMixed::String("You can also manually create a personal access token enabling the \"read_api\" scope at:".to_string()),
                                true,
                                IOInterface::NORMAL,
                            );
                            self.io.write_error(
                                PhpMixed::String(personal_access_token_link.clone()),
                                true,
                                IOInterface::NORMAL,
                            );
                            self.io.write_error(
                                PhpMixed::String(format!(
                                    "Add it using \"composer config --global --auth gitlab-token.{} <token>\"",
                                    origin_url
                                )),
                                true,
                                IOInterface::NORMAL,
                            );

                            continue;
                        }
                        Ok(te) => return Err(te.into()),
                        Err(e) => return Err(e),
                    }
                }
            };

            let access_token = response
                .as_array()
                .and_then(|arr| arr.get("access_token"))
                .and_then(|v| v.as_string())
                .unwrap_or("")
                .to_string();

            self.io.set_authentication(
                origin_url.to_string(),
                access_token.clone(),
                Some("oauth2".to_string()),
            );

            // store value in user config in auth file
            let use_local = store_in_local_auth_config && local_auth_config.is_some();
            let has_expires_in = response
                .as_array()
                .map(|arr| arr.contains_key("expires_in"))
                .unwrap_or(false);

            if use_local {
                let mut auth_config_source = local_auth_config.clone().unwrap();
                if has_expires_in {
                    auth_config_source.add_config_setting(
                        &format!("gitlab-oauth.{}", origin_url),
                        Self::build_oauth_config(&response, &access_token),
                    )?;
                } else {
                    auth_config_source.add_config_setting(
                        &format!("gitlab-oauth.{}", origin_url),
                        PhpMixed::String(access_token),
                    )?;
                }
            } else {
                let mut auth_config_source = self.config.get_auth_config_source();
                if has_expires_in {
                    auth_config_source.add_config_setting(
                        &format!("gitlab-oauth.{}", origin_url),
                        Self::build_oauth_config(&response, &access_token),
                    )?;
                } else {
                    auth_config_source.add_config_setting(
                        &format!("gitlab-oauth.{}", origin_url),
                        PhpMixed::String(access_token),
                    )?;
                }
            }

            return Ok(true);
        }

        Err(RuntimeException {
            message: "Invalid GitLab credentials 5 times in a row, aborting.".to_string(),
            code: 0,
        }
        .into())
    }

    pub fn authorize_oauth_refresh(
        &mut self,
        scheme: &str,
        origin_url: &str,
    ) -> anyhow::Result<bool> {
        let response = match self.refresh_token(scheme, origin_url) {
            Ok(r) => r,
            Err(e) => match e.downcast::<TransportException>() {
                Ok(te) => {
                    self.io.write_error(
                        PhpMixed::String(format!(
                            "Couldn't refresh access token: {}",
                            te.message
                        )),
                        true,
                        IOInterface::NORMAL,
                    );
                    return Ok(false);
                }
                Err(e) => return Err(e),
            },
        };

        let access_token = response
            .as_array()
            .and_then(|arr| arr.get("access_token"))
            .and_then(|v| v.as_string())
            .unwrap_or("")
            .to_string();

        self.io.set_authentication(
            origin_url.to_string(),
            access_token.clone(),
            Some("oauth2".to_string()),
        );

        // store value in user config in auth file
        self.config
            .get_auth_config_source()
            .add_config_setting(
                &format!("gitlab-oauth.{}", origin_url),
                Self::build_oauth_config(&response, &access_token),
            )?;

        Ok(true)
    }

    fn create_token(
        &mut self,
        scheme: &str,
        origin_url: &str,
    ) -> anyhow::Result<PhpMixed> {
        let username = match self.io.ask("Username: ".to_string(), PhpMixed::Null) {
            PhpMixed::String(s) => s,
            _ => String::new(),
        };
        let password = self
            .io
            .ask_and_hide_answer("Password: ".to_string())
            .unwrap_or_default();

        let headers = vec!["Content-Type: application/x-www-form-urlencoded".to_string()];

        let api_url = origin_url;
        let data = http_build_query(
            &[
                ("username", username.as_str()),
                ("password", password.as_str()),
                ("grant_type", "password"),
            ],
            "",
            "&",
        );
        let mut http_inner: IndexMap<String, Box<PhpMixed>> = IndexMap::new();
        http_inner.insert(
            "method".to_string(),
            Box::new(PhpMixed::String("POST".to_string())),
        );
        http_inner.insert(
            "header".to_string(),
            Box::new(PhpMixed::List(
                headers
                    .into_iter()
                    .map(|h| Box::new(PhpMixed::String(h)))
                    .collect(),
            )),
        );
        http_inner.insert(
            "content".to_string(),
            Box::new(PhpMixed::String(data)),
        );
        let mut options: IndexMap<String, Box<PhpMixed>> = IndexMap::new();
        options.insert(
            "retry-auth-failure".to_string(),
            Box::new(PhpMixed::Bool(false)),
        );
        options.insert("http".to_string(), Box::new(PhpMixed::Array(http_inner)));

        let token = self
            .http_downloader
            .get(
                &format!("{}://{}/oauth/token", scheme, api_url),
                &PhpMixed::Array(options),
            )?
            .decode_json()?;

        self.io.write_error(
            PhpMixed::String("Token successfully created".to_string()),
            true,
            IOInterface::NORMAL,
        );

        Ok(token)
    }

    pub fn is_oauth_expired(&self, origin_url: &str) -> bool {
        let auth_tokens = self.config.get("gitlab-oauth");
        if let Some(map) = auth_tokens.as_array() {
            if let Some(token_info) = map.get(origin_url) {
                if let Some(token_map) = token_info.as_array() {
                    if let Some(expires_at) = token_map.get("expires-at") {
                        if let Some(expires_at_int) = expires_at.as_int() {
                            if expires_at_int < time() {
                                return true;
                            }
                        }
                    }
                }
            }
        }

        false
    }

    fn refresh_token(
        &mut self,
        scheme: &str,
        origin_url: &str,
    ) -> anyhow::Result<PhpMixed> {
        let auth_tokens = self.config.get("gitlab-oauth");
        let refresh_token = auth_tokens
            .as_array()
            .and_then(|map| map.get(origin_url))
            .and_then(|v| v.as_array())
            .and_then(|token_map| token_map.get("refresh-token"))
            .and_then(|v| v.as_string())
            .map(|s| s.to_string());

        let refresh_token = match refresh_token {
            Some(t) => t,
            None => {
                return Err(RuntimeException {
                    message: format!(
                        "No GitLab refresh token present for {}.",
                        origin_url
                    ),
                    code: 0,
                }
                .into())
            }
        };

        let headers = vec!["Content-Type: application/x-www-form-urlencoded".to_string()];

        let data = http_build_query(
            &[
                ("refresh_token", refresh_token.as_str()),
                ("grant_type", "refresh_token"),
            ],
            "",
            "&",
        );
        let mut http_inner: IndexMap<String, Box<PhpMixed>> = IndexMap::new();
        http_inner.insert(
            "method".to_string(),
            Box::new(PhpMixed::String("POST".to_string())),
        );
        http_inner.insert(
            "header".to_string(),
            Box::new(PhpMixed::List(
                headers
                    .into_iter()
                    .map(|h| Box::new(PhpMixed::String(h)))
                    .collect(),
            )),
        );
        http_inner.insert(
            "content".to_string(),
            Box::new(PhpMixed::String(data)),
        );
        let mut options: IndexMap<String, Box<PhpMixed>> = IndexMap::new();
        options.insert(
            "retry-auth-failure".to_string(),
            Box::new(PhpMixed::Bool(false)),
        );
        options.insert("http".to_string(), Box::new(PhpMixed::Array(http_inner)));

        let token = self
            .http_downloader
            .get(
                &format!("{}://{}/oauth/token", scheme, origin_url),
                &PhpMixed::Array(options),
            )?
            .decode_json()?;

        self.io.write_error(
            PhpMixed::String("GitLab token successfully refreshed".to_string()),
            true,
            IOInterface::VERY_VERBOSE,
        );
        self.io.write_error(
            PhpMixed::String(format!(
                "To revoke access to this token you can visit {}://{}/-/user_settings/applications",
                scheme, origin_url
            )),
            true,
            IOInterface::VERY_VERBOSE,
        );

        Ok(token)
    }

    fn build_oauth_config(response: &PhpMixed, access_token: &str) -> PhpMixed {
        let created_at = response
            .as_array()
            .and_then(|arr| arr.get("created_at"))
            .and_then(|v| v.as_int())
            .unwrap_or(0);
        let expires_in = response
            .as_array()
            .and_then(|arr| arr.get("expires_in"))
            .and_then(|v| v.as_int())
            .unwrap_or(0);
        let refresh_token = response
            .as_array()
            .and_then(|arr| arr.get("refresh_token"))
            .and_then(|v| v.as_string())
            .unwrap_or("")
            .to_string();
        let mut setting: IndexMap<String, Box<PhpMixed>> = IndexMap::new();
        setting.insert(
            "expires-at".to_string(),
            Box::new(PhpMixed::Int(created_at + expires_in)),
        );
        setting.insert(
            "refresh-token".to_string(),
            Box::new(PhpMixed::String(refresh_token)),
        );
        setting.insert(
            "token".to_string(),
            Box::new(PhpMixed::String(access_token.to_string())),
        );
        PhpMixed::Array(setting)
    }
}