-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathauth.rs
More file actions
195 lines (175 loc) · 5.74 KB
/
auth.rs
File metadata and controls
195 lines (175 loc) · 5.74 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
use std::time::Duration;
use crate::api_client::{CodSpeedAPIClient, GetRepositoryVars};
use crate::cli::run::helpers::{
ParsedRepository, find_repository_root, parse_repository_from_remote,
};
use crate::config::CodSpeedConfig;
use crate::prelude::*;
use clap::{Args, Subcommand};
use console::style;
use git2::Repository;
use tokio::time::{Instant, sleep};
use super::status::{check_mark, cross_mark};
#[derive(Debug, Args)]
pub struct AuthArgs {
#[command(subcommand)]
command: AuthCommands,
}
#[derive(Debug, Subcommand)]
enum AuthCommands {
/// Login to CodSpeed
Login,
/// Show the authentication status
Status,
}
pub async fn run(
args: AuthArgs,
api_client: &CodSpeedAPIClient,
config_name: Option<&str>,
) -> Result<()> {
match args.command {
AuthCommands::Login => login(api_client, config_name).await?,
AuthCommands::Status => status(api_client).await?,
}
Ok(())
}
const LOGIN_SESSION_MAX_DURATION: Duration = Duration::from_secs(60 * 5); // 5 minutes
async fn login(api_client: &CodSpeedAPIClient, config_name: Option<&str>) -> Result<()> {
debug!("Login to CodSpeed");
start_group!("Creating login session");
let login_session_payload = api_client.create_login_session().await?;
end_group!();
if open::that(&login_session_payload.callback_url).is_ok() {
info!("Your browser has been opened to complete the login process");
} else {
warn!("Failed to open the browser automatically, please open the URL manually");
}
info!(
"Authentication URL: {}\n",
style(login_session_payload.callback_url)
.blue()
.bold()
.underlined()
);
start_group!("Waiting for the login to be completed");
let token;
let start = Instant::now();
loop {
if start.elapsed() > LOGIN_SESSION_MAX_DURATION {
bail!("Login session expired, please try again");
}
match api_client
.consume_login_session(&login_session_payload.session_id)
.await?
.token
{
Some(token_from_api) => {
token = token_from_api;
break;
}
None => sleep(Duration::from_secs(5)).await,
}
}
end_group!();
let mut config = CodSpeedConfig::load_with_override(config_name, None)?;
config.auth.token = Some(token);
config.persist(config_name)?;
debug!("Token saved to configuration file");
info!("Login successful, your are now authenticated on CodSpeed");
Ok(())
}
/// Detect the repository from the git remote of the current directory
fn detect_repository() -> Option<ParsedRepository> {
let current_dir = std::env::current_dir().ok()?;
let root_path = find_repository_root(¤t_dir)?;
let git_repository = Repository::open(&root_path).ok()?;
let remote = git_repository.find_remote("origin").ok()?;
let url = remote.url()?;
parse_repository_from_remote(url).ok()
}
pub async fn status(api_client: &CodSpeedAPIClient) -> Result<()> {
let config = CodSpeedConfig::load_with_override(None, None)?;
let has_token = config.auth.token.is_some();
let detected_repo = detect_repository();
// 1. Check token validity
let current_user = if has_token {
api_client.get_current_user().await.ok().flatten()
} else {
None
};
let token_valid = current_user.is_some();
info!("{}", style("Authentication").bold());
if let Some(user) = current_user {
info!(
" {} Logged in as {} ({})",
check_mark(),
style(&user.login).bold(),
user.provider
);
} else if has_token {
info!(
" {} Token expired (run {} to re-authenticate)",
cross_mark(),
style("codspeed auth login").cyan()
);
} else {
info!(
" {} Not logged in (run {} to authenticate)",
cross_mark(),
style("codspeed auth login").cyan()
);
}
info!("");
// 2. If token is valid and we detected a repo, check repository existence
info!("{}", style("Repository").bold());
let local_runs_fallback = match detected_repo {
Some(parsed) => {
let label = parsed.provider.to_string();
if token_valid {
let repo_exists = api_client
.get_repository(GetRepositoryVars {
owner: parsed.owner.clone(),
name: parsed.name.clone(),
provider: parsed.provider.clone(),
})
.await
.ok()
.flatten()
.is_some();
if repo_exists {
info!(
" {} {}/{} ({})",
check_mark(),
parsed.owner,
parsed.name,
label
);
false
} else {
info!(
" {} {}/{} ({}, not enabled on CodSpeed)",
cross_mark(),
parsed.owner,
parsed.name,
label
);
true
}
} else {
info!(" {}/{} ({})", parsed.owner, parsed.name, label);
false
}
}
None => {
info!(" Not inside a git repository");
true
}
};
if local_runs_fallback {
warn!(
"Runs will be uploaded to a {} CodSpeed project not associated with any repository.",
crate::cli::exec::DEFAULT_REPOSITORY_NAME
);
}
Ok(())
}