aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/util/bitbucket.rs
blob: ca9aabd1e787a819fcac3c7844c2c08144085bef (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
//! ref: composer/src/Composer/Util/Bitbucket.php

use crate::io::io_interface;
use indexmap::IndexMap;
use shirabe_php_shim::{LogicException, PhpMixed, time};

use crate::config::Config;
use crate::config::config_source_interface::ConfigSourceInterface;
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 Bitbucket {
    io: Box<dyn IOInterface>,
    config: Config,
    process: ProcessExecutor,
    http_downloader: HttpDownloader,
    token: Option<IndexMap<String, PhpMixed>>,
    time: Option<i64>,
}

impl Bitbucket {
    pub const OAUTH2_ACCESS_TOKEN_URL: &'static str =
        "https://bitbucket.org/site/oauth2/access_token";

    pub fn new(
        io: Box<dyn IOInterface>,
        config: Config,
        process: Option<ProcessExecutor>,
        http_downloader: Option<HttpDownloader>,
        time: Option<i64>,
    ) -> 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, IndexMap::new())?,
        };
        Ok(Self {
            io,
            config,
            process,
            http_downloader,
            token: None,
            time,
        })
    }

    pub fn get_token(&self) -> String {
        match &self.token {
            Some(token) => token
                .get("access_token")
                .and_then(|v| v.as_string())
                .map(|s| s.to_string())
                .unwrap_or_default(),
            None => String::new(),
        }
    }

    pub fn authorize_oauth(&mut self, origin_url: &str) -> bool {
        if origin_url != "bitbucket.org" {
            return false;
        }

        let mut output = String::new();
        if self.process.execute(
            &[
                "git".to_string(),
                "config".to_string(),
                "bitbucket.accesstoken".to_string(),
            ],
            &mut output,
            None,
        ) == 0
        {
            self.io.set_authentication(
                origin_url.to_string(),
                "x-token-auth".to_string(),
                Some(output.trim().to_string()),
            );
            return true;
        }

        false
    }

    fn request_access_token(&mut self) -> anyhow::Result<bool> {
        let mut http = IndexMap::new();
        http.insert(
            "method".to_string(),
            Box::new(PhpMixed::String("POST".to_string())),
        );
        http.insert(
            "content".to_string(),
            Box::new(PhpMixed::String(
                "grant_type=client_credentials".to_string(),
            )),
        );
        let mut options = IndexMap::new();
        options.insert(
            "retry-auth-failure".to_string(),
            Box::new(PhpMixed::Bool(false)),
        );
        options.insert("http".to_string(), Box::new(PhpMixed::Array(http)));
        let options = PhpMixed::Array(options);

        let response = match self
            .http_downloader
            .get(Self::OAUTH2_ACCESS_TOKEN_URL, &options)
        {
            Ok(r) => r,
            Err(te) => {
                if te.code == 400 {
                    self.io.write_error(
                        PhpMixed::String(
                            "<error>Invalid OAuth consumer provided.</error>".to_string(),
                        ),
                        true,
                        io_interface::NORMAL,
                    );
                    self.io.write_error(
                        PhpMixed::String("This can have three reasons:".to_string()),
                        true,
                        io_interface::NORMAL,
                    );
                    self.io.write_error(
                            PhpMixed::String(
                                "1. You are authenticating with a bitbucket username/password combination".to_string(),
                            ),
                            true,
                            io_interface::NORMAL,
                        );
                    self.io.write_error(
                            PhpMixed::String(
                                "2. You are using an OAuth consumer, but didn't configure a (dummy) callback url".to_string(),
                            ),
                            true,
                            io_interface::NORMAL,
                        );
                    self.io.write_error(
                            PhpMixed::String(
                                "3. You are using an OAuth consumer, but didn't configure it as private consumer".to_string(),
                            ),
                            true,
                            io_interface::NORMAL,
                        );
                    return Ok(false);
                }
                if te.code == 403 || te.code == 401 {
                    self.io.write_error(
                        PhpMixed::String(
                            "<error>Invalid OAuth consumer provided.</error>".to_string(),
                        ),
                        true,
                        io_interface::NORMAL,
                    );
                    self.io.write_error(
                            PhpMixed::String(
                                "You can also add it manually later by using \"composer config --global --auth bitbucket-oauth.bitbucket.org <consumer-key> <consumer-secret>\"".to_string(),
                            ),
                            true,
                            io_interface::NORMAL,
                        );
                    return Ok(false);
                }
                return Err(te.into());
            }
        };

        let token = response.decode_json()?;
        let token_map = match token {
            PhpMixed::Array(ref m) => m.clone(),
            _ => {
                return Err(LogicException {
                    message: format!(
                        "Expected a token configured with expires_in and access_token present, got {}",
                        shirabe_php_shim::json_encode(&token).unwrap_or_default()
                    ),
                    code: 0,
                }
                .into());
            }
        };
        if !token_map.contains_key("expires_in") || !token_map.contains_key("access_token") {
            return Err(LogicException {
                message: format!(
                    "Expected a token configured with expires_in and access_token present, got {}",
                    shirabe_php_shim::json_encode(&token).unwrap_or_default()
                ),
                code: 0,
            }
            .into());
        }
        self.token = Some(token_map.into_iter().map(|(k, v)| (k, *v)).collect());

        Ok(true)
    }

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

        let local_auth_config = self.config.get_local_auth_config_source();
        let url =
            "https://support.atlassian.com/bitbucket-cloud/docs/use-oauth-on-bitbucket-cloud/";
        self.io.write_error(
            PhpMixed::String("Follow the instructions here:".to_string()),
            true,
            io_interface::NORMAL,
        );
        self.io.write_error(
            PhpMixed::String(url.to_string()),
            true,
            io_interface::NORMAL,
        );
        let auth_config_source_name = self.config.get_auth_config_source().get_name();
        let local_name_prefix = local_auth_config
            .as_ref()
            .map(|c| format!("{} OR ", c.get_name()))
            .unwrap_or_default();
        self.io.write_error(
            PhpMixed::String(format!(
                "to create a consumer. It will be stored in \"{}\" for future use by Composer.",
                local_name_prefix + &auth_config_source_name
            )),
            true,
            io_interface::NORMAL,
        );
        self.io.write_error(
            PhpMixed::String(
                "Ensure you enter a \"Callback URL\" (http://example.com is fine) or it will not be possible to create an Access Token (this callback url will not be used by composer)".to_string(),
            ),
            true,
            io_interface::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 consumer_key = self
            .io
            .ask_and_hide_answer("Consumer Key (hidden): ".to_string())
            .unwrap_or_default()
            .trim()
            .to_string();

        if consumer_key.is_empty() {
            self.io.write_error(
                PhpMixed::String("<warning>No consumer key given, aborting.</warning>".to_string()),
                true,
                io_interface::NORMAL,
            );
            self.io.write_error(
                PhpMixed::String(
                    "You can also add it manually later by using \"composer config --global --auth bitbucket-oauth.bitbucket.org <consumer-key> <consumer-secret>\"".to_string(),
                ),
                true,
                io_interface::NORMAL,
            );
            return Ok(false);
        }

        let consumer_secret = self
            .io
            .ask_and_hide_answer("Consumer Secret (hidden): ".to_string())
            .unwrap_or_default()
            .trim()
            .to_string();

        if consumer_secret.is_empty() {
            self.io.write_error(
                PhpMixed::String(
                    "<warning>No consumer secret given, aborting.</warning>".to_string(),
                ),
                true,
                io_interface::NORMAL,
            );
            self.io.write_error(
                PhpMixed::String(
                    "You can also add it manually later by using \"composer config --global --auth bitbucket-oauth.bitbucket.org <consumer-key> <consumer-secret>\"".to_string(),
                ),
                true,
                io_interface::NORMAL,
            );
            return Ok(false);
        }

        self.io.set_authentication(
            origin_url.to_string(),
            consumer_key.clone(),
            Some(consumer_secret.clone()),
        );

        if !self.request_access_token()? {
            return Ok(false);
        }

        let use_local =
            store_in_local_auth_config && self.config.get_local_auth_config_source().is_some();
        if use_local {
            let mut auth_config_source = self.config.get_local_auth_config_source().unwrap();
            self.store_in_auth_config(
                &mut *auth_config_source,
                origin_url,
                &consumer_key,
                &consumer_secret,
            )?;
        } else {
            let mut auth_config_source = self.config.get_auth_config_source();
            self.store_in_auth_config(
                &mut *auth_config_source,
                origin_url,
                &consumer_key,
                &consumer_secret,
            )?;
        }

        self.config
            .get_auth_config_source()
            .remove_config_setting(&format!("http-basic.{}", origin_url))?;

        self.io.write_error(
            PhpMixed::String("<info>Consumer stored successfully.</info>".to_string()),
            true,
            io_interface::NORMAL,
        );

        Ok(true)
    }

    pub fn request_token(
        &mut self,
        origin_url: &str,
        consumer_key: &str,
        consumer_secret: &str,
    ) -> anyhow::Result<String> {
        if self.token.is_some() || self.get_token_from_config(origin_url) {
            return Ok(self
                .token
                .as_ref()
                .unwrap()
                .get("access_token")
                .and_then(|v| v.as_string())
                .map(|s| s.to_string())
                .unwrap_or_default());
        }

        self.io.set_authentication(
            origin_url.to_string(),
            consumer_key.to_string(),
            Some(consumer_secret.to_string()),
        );
        if !self.request_access_token()? {
            return Ok(String::new());
        }

        let use_local = self.config.get_local_auth_config_source().is_some();
        if use_local {
            let mut auth_config_source = self.config.get_local_auth_config_source().unwrap();
            self.store_in_auth_config(
                &mut *auth_config_source,
                origin_url,
                consumer_key,
                consumer_secret,
            )?;
        } else {
            let mut auth_config_source = self.config.get_auth_config_source();
            self.store_in_auth_config(
                &mut *auth_config_source,
                origin_url,
                consumer_key,
                consumer_secret,
            )?;
        }

        let access_token = self
            .token
            .as_ref()
            .and_then(|t| t.get("access_token"))
            .and_then(|v| v.as_string())
            .map(|s| s.to_string());

        match access_token {
            Some(t) => Ok(t),
            None => Err(LogicException {
                message: "Failed to initialize token above".to_string(),
                code: 0,
            }
            .into()),
        }
    }

    fn store_in_auth_config(
        &mut self,
        auth_config_source: &mut dyn ConfigSourceInterface,
        origin_url: &str,
        consumer_key: &str,
        consumer_secret: &str,
    ) -> anyhow::Result<()> {
        self.config
            .get_config_source()
            .remove_config_setting(&format!("bitbucket-oauth.{}", origin_url))?;

        let token = self.token.as_ref().ok_or_else(|| LogicException {
            message: format!("Expected a token configured with expires_in present, got null",),
            code: 0,
        })?;
        let expires_in = token
            .get("expires_in")
            .and_then(|v| v.as_int())
            .ok_or_else(|| {
                let token_mixed = PhpMixed::Array(
                    token
                        .iter()
                        .map(|(k, v)| (k.clone(), Box::new(v.clone())))
                        .collect(),
                );
                LogicException {
                    message: format!(
                        "Expected a token configured with expires_in present, got {}",
                        shirabe_php_shim::json_encode(&token_mixed).unwrap_or_default()
                    ),
                    code: 0,
                }
            })?;

        let t = self.time.unwrap_or_else(time);
        let mut consumer: IndexMap<String, Box<PhpMixed>> = IndexMap::new();
        consumer.insert(
            "consumer-key".to_string(),
            Box::new(PhpMixed::String(consumer_key.to_string())),
        );
        consumer.insert(
            "consumer-secret".to_string(),
            Box::new(PhpMixed::String(consumer_secret.to_string())),
        );
        consumer.insert(
            "access-token".to_string(),
            Box::new(token.get("access_token").cloned().unwrap_or(PhpMixed::Null)),
        );
        consumer.insert(
            "access-token-expiration".to_string(),
            Box::new(PhpMixed::Int(t + expires_in)),
        );

        self.config.get_auth_config_source().add_config_setting(
            &format!("bitbucket-oauth.{}", origin_url),
            PhpMixed::Array(consumer),
        )?;

        Ok(())
    }

    fn get_token_from_config(&mut self, origin_url: &str) -> bool {
        let auth_config = self.config.get("bitbucket-oauth");

        let auth_map = match auth_config.as_array() {
            Some(m) => m.clone(),
            None => return false,
        };
        let origin_config = match auth_map.get(origin_url) {
            Some(v) => match v.as_array() {
                Some(m) => m.clone(),
                None => return false,
            },
            None => return false,
        };

        if !origin_config.contains_key("access-token")
            || !origin_config.contains_key("access-token-expiration")
        {
            return false;
        }
        if let Some(expiration) = origin_config
            .get("access-token-expiration")
            .and_then(|v| v.as_int())
        {
            if time() > expiration {
                return false;
            }
        } else {
            return false;
        }

        let access_token = match origin_config.get("access-token").map(|v| *v.clone()) {
            Some(t) => t,
            None => return false,
        };
        let mut token = IndexMap::new();
        token.insert("access_token".to_string(), access_token);
        self.token = Some(token);

        true
    }
}