-
Notifications
You must be signed in to change notification settings - Fork 427
Expand file tree
/
Copy pathuser.rs
More file actions
436 lines (386 loc) · 14.7 KB
/
user.rs
File metadata and controls
436 lines (386 loc) · 14.7 KB
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
use std::fmt;
use std::fmt::Display;
use std::process::{
ExitCode,
exit,
};
use std::time::Duration;
use anstream::{
eprintln,
println,
};
use clap::{
Args,
Subcommand,
};
use dialoguer::Select;
use eyre::{
Result,
bail,
};
use serde_json::json;
use tokio::signal::ctrl_c;
use tracing::{
error,
info,
};
use super::OutputFormat;
use crate::api_client::list_available_profiles;
use crate::auth::builder_id::{
BuilderIdToken,
PollCreateToken,
TokenType,
poll_create_token,
start_device_authorization,
};
use crate::auth::pkce::start_pkce_authorization;
use crate::constants::PRODUCT_NAME;
use crate::os::Os;
use crate::telemetry::{
QProfileSwitchIntent,
TelemetryResult,
};
use crate::theme::StyledText;
use crate::util::spinner::{
Spinner,
SpinnerComponent,
};
use crate::util::system_info::is_remote;
use crate::util::{
CLI_BINARY_NAME,
choose,
input,
};
#[derive(Args, Debug, PartialEq, Eq, Clone, Default)]
pub struct LoginArgs {
/// License type (pro for Identity Center, free for Builder ID)
#[arg(long, value_enum)]
pub license: Option<LicenseType>,
/// Identity provider URL (for Identity Center)
#[arg(long)]
pub identity_provider: Option<String>,
/// Region (for Identity Center)
#[arg(long)]
pub region: Option<String>,
/// Always use the OAuth device flow for authentication. Useful for instances where browser
/// redirects cannot be handled.
#[arg(long)]
pub use_device_flow: bool,
}
impl LoginArgs {
pub async fn execute(self, os: &mut Os) -> Result<ExitCode> {
if crate::auth::is_logged_in(&mut os.database).await {
eyre::bail!(
"Already logged in, please logout with {} first",
StyledText::command(&format!("{CLI_BINARY_NAME} logout"))
);
}
let login_method = match self.license {
Some(LicenseType::Free) => AuthMethod::BuilderId,
Some(LicenseType::Pro) => AuthMethod::IdentityCenter,
None => {
if self.identity_provider.is_some() && self.region.is_some() {
// If license is specified and --identity-provider and --region are specified,
// the license is determined to be pro
AuthMethod::IdentityCenter
} else {
// --license is not specified, prompt the user to choose
let options = [AuthMethod::BuilderId, AuthMethod::IdentityCenter];
let i = match choose("Select login method", &options)? {
Some(i) => i,
None => bail!("No login method selected"),
};
options[i]
}
},
};
match login_method {
AuthMethod::BuilderId | AuthMethod::IdentityCenter => {
let (start_url, region) = match login_method {
AuthMethod::BuilderId => (None, None),
AuthMethod::IdentityCenter => {
let default_start_url = match self.identity_provider {
Some(start_url) => Some(start_url),
None => os.database.get_start_url()?,
};
let default_region = match self.region {
Some(region) => Some(region),
None => os.database.get_idc_region()?,
};
let start_url = input("Enter Start URL", default_start_url.as_deref())?;
let region = input("Enter Region", default_region.as_deref())?.trim().to_string();
let _ = os.database.set_start_url(start_url.clone());
let _ = os.database.set_idc_region(region.clone());
(Some(start_url), Some(region))
},
};
// Remote machine won't be able to handle browser opening and redirects,
// hence always use device code flow.
if is_remote() || self.use_device_flow {
try_device_authorization(os, start_url.clone(), region.clone()).await?;
} else {
let (client, registration) = start_pkce_authorization(start_url.clone(), region.clone()).await?;
match crate::util::open::open_url_async(®istration.url).await {
// If it succeeded, finish PKCE.
Ok(()) => {
let mut spinner = Spinner::new(vec![
SpinnerComponent::Spinner,
SpinnerComponent::Text(" Logging in...".into()),
]);
let ctrl_c_stream = ctrl_c();
tokio::select! {
res = registration.finish(&client, Some(&mut os.database)) => {
if let Err(err) = res {
let auth_method = match crate::auth::builder_id::token_type_from_start_url(start_url.as_deref()) {
crate::auth::builder_id::TokenType::BuilderId => "BuilderId",
crate::auth::builder_id::TokenType::IamIdentityCenter => "IdentityCenter",
};
os.telemetry.send_auth_failed(auth_method, "PKCE", "NewLogin", None).ok();
return Err(err.into());
}
},
Ok(_) = ctrl_c_stream => {
#[allow(clippy::exit)]
exit(1);
},
}
os.telemetry.send_user_logged_in().ok();
spinner.stop_with_message("Logged in".into());
},
// If we are unable to open the link with the browser, then fallback to
// the device code flow.
Err(err) => {
error!(%err, "Failed to open URL with browser, falling back to device code flow");
// Try device code flow.
try_device_authorization(os, start_url.clone(), region.clone()).await?;
},
}
}
},
};
if login_method == AuthMethod::IdentityCenter {
select_profile_interactive(os, true).await?;
}
Ok(ExitCode::SUCCESS)
}
}
pub async fn logout(os: &mut Os) -> Result<ExitCode> {
let _ = crate::auth::logout(&mut os.database).await;
eprintln!("You are now logged out");
eprintln!(
"Run {} to log back in to {PRODUCT_NAME}",
StyledText::command(&format!("{CLI_BINARY_NAME} login"))
);
Ok(ExitCode::SUCCESS)
}
#[derive(Args, Debug, PartialEq, Eq, Clone, Default)]
pub struct WhoamiArgs {
/// Output format to use
#[arg(long, short, value_enum, default_value_t)]
format: OutputFormat,
}
impl WhoamiArgs {
pub async fn execute(self, os: &mut Os) -> Result<ExitCode> {
let builder_id = BuilderIdToken::load(&os.database, Some(&os.telemetry)).await;
match builder_id {
Ok(Some(token)) => {
self.format.print(
|| match token.token_type() {
TokenType::BuilderId => "Logged in with Builder ID".into(),
TokenType::IamIdentityCenter => {
format!(
"Logged in with IAM Identity Center ({})",
token.start_url.as_ref().unwrap()
)
},
},
|| {
json!({
"accountType": match token.token_type() {
TokenType::BuilderId => "BuilderId",
TokenType::IamIdentityCenter => "IamIdentityCenter",
},
"startUrl": token.start_url,
"region": token.region,
})
},
);
if matches!(token.token_type(), TokenType::IamIdentityCenter) {
if let Ok(Some(profile)) = os.database.get_auth_profile() {
color_print::cprintln!("\n<em>Profile:</em>\n{}\n{}\n", profile.profile_name, profile.arn);
}
}
Ok(ExitCode::SUCCESS)
},
_ => {
self.format.print(|| "Not logged in", || json!({ "account": null }));
Ok(ExitCode::FAILURE)
},
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
pub enum LicenseType {
/// Free license with Builder ID
Free,
/// Pro license with Identity Center
Pro,
}
pub async fn profile(os: &mut Os) -> Result<ExitCode> {
if let Ok(Some(token)) = BuilderIdToken::load(&os.database, Some(&os.telemetry)).await {
if matches!(token.token_type(), TokenType::BuilderId) {
bail!("This command is only available for Pro users");
}
}
select_profile_interactive(os, false).await?;
Ok(ExitCode::SUCCESS)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum AuthMethod {
/// Builder ID (free)
BuilderId,
/// IdC (enterprise)
IdentityCenter,
}
impl Display for AuthMethod {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
AuthMethod::BuilderId => write!(f, "Use for Free with Builder ID"),
AuthMethod::IdentityCenter => write!(f, "Use with Pro license"),
}
}
}
#[derive(Subcommand, Debug, PartialEq, Eq)]
pub enum UserSubcommand {
Profile,
}
async fn try_device_authorization(os: &mut Os, start_url: Option<String>, region: Option<String>) -> Result<()> {
let device_auth = start_device_authorization(&os.database, start_url.clone(), region.clone()).await?;
println!();
println!("Confirm the following code in the browser");
println!("Code: {}", StyledText::emphasis(&device_auth.user_code));
println!();
let print_open_url = || println!("Open this URL: {}", device_auth.verification_uri_complete);
if is_remote() {
print_open_url();
} else if let Err(err) = crate::util::open::open_url_async(&device_auth.verification_uri_complete).await {
error!(%err, "Failed to open URL with browser");
print_open_url();
}
let mut spinner = Spinner::new(vec![
SpinnerComponent::Spinner,
SpinnerComponent::Text(" Logging in...".into()),
]);
loop {
let ctrl_c_stream = ctrl_c();
tokio::select! {
_ = tokio::time::sleep(Duration::from_secs(device_auth.interval.try_into().unwrap_or(1))) => (),
Ok(_) = ctrl_c_stream => {
#[allow(clippy::exit)]
exit(1);
}
}
match poll_create_token(
&os.database,
device_auth.device_code.clone(),
start_url.clone(),
region.clone(),
&os.telemetry,
)
.await
{
PollCreateToken::Pending => {},
PollCreateToken::Complete => {
os.telemetry.send_user_logged_in().ok();
spinner.stop_with_message("Logged in".into());
break;
},
PollCreateToken::Error(err) => {
spinner.stop();
return Err(err.into());
},
};
}
Ok(())
}
async fn select_profile_interactive(os: &mut Os, whoami: bool) -> Result<()> {
let mut spinner = Spinner::new(vec![
SpinnerComponent::Spinner,
SpinnerComponent::Text(" Fetching profiles...".into()),
]);
let profiles = list_available_profiles(&os.env, &os.fs, &mut os.database).await?;
if profiles.is_empty() {
info!("Available profiles was empty");
return Ok(());
}
let sso_region = os.database.get_idc_region()?;
let total_profiles = profiles.len() as i64;
if whoami && profiles.len() == 1 {
if let Some(profile_region) = profiles[0].arn.split(':').nth(3) {
os.telemetry
.send_profile_state(
QProfileSwitchIntent::Update,
profile_region.to_string(),
TelemetryResult::Succeeded,
sso_region,
)
.ok();
}
spinner.stop_with_message(String::new());
os.database.set_auth_profile(&profiles[0])?;
return Ok(());
}
let mut items: Vec<String> = profiles
.iter()
.map(|p| format!("{} (arn: {})", p.profile_name, p.arn))
.collect();
let active_profile = os.database.get_auth_profile()?;
if let Some(default_idx) = active_profile
.as_ref()
.and_then(|active| profiles.iter().position(|p| p.arn == active.arn))
{
items[default_idx] = format!("{} (active)", items[default_idx].as_str());
}
spinner.stop_with_message(String::new());
let selected = Select::with_theme(&crate::util::dialoguer_theme())
.with_prompt("Select an IAM Identity Center profile")
.items(&items)
.default(0)
.interact_opt()?;
match selected {
Some(i) => {
let chosen = &profiles[i];
eprintln!("Profile set");
os.database.set_auth_profile(chosen)?;
if let Some(profile_region) = chosen.arn.split(':').nth(3) {
let intent = if whoami {
QProfileSwitchIntent::Auth
} else {
QProfileSwitchIntent::User
};
os.telemetry
.send_did_select_profile(
intent,
profile_region.to_string(),
TelemetryResult::Succeeded,
sso_region,
Some(total_profiles),
)
.ok();
}
},
None => {
os.telemetry
.send_did_select_profile(
QProfileSwitchIntent::User,
"not-set".to_string(),
TelemetryResult::Cancelled,
sso_region,
Some(total_profiles),
)
.ok();
bail!("No profile selected.\n");
},
}
Ok(())
}