MCPcopy Create free account
hub / github.com/CodSpeedHQ/codspeed / login

Function login

src/cli/auth.rs:55–136  ·  view source on GitHub ↗
(
    api_client: &CodSpeedAPIClient,
    config_name: Option<&str>,
    mut config: CodSpeedConfig,
    with_token: bool,
)

Source from the content-addressed store, hash-verified

53const LOGIN_SESSION_MAX_DURATION: Duration = Duration::from_secs(60 * 5); // 5 minutes
54
55async fn login(
56 api_client: &CodSpeedAPIClient,
57 config_name: Option<&str>,
58 mut config: CodSpeedConfig,
59 with_token: bool,
60) -> Result<()> {
61 debug!("Login to CodSpeed");
62
63 let token = if with_token {
64 let mut buf = String::new();
65 std::io::stdin().read_to_string(&mut buf)?;
66 let token = buf.trim().to_owned();
67 if token.is_empty() {
68 bail!("No token provided on stdin");
69 }
70 token
71 } else {
72 start_group!("Creating login session");
73 let login_session_payload = api_client.create_login_session().await?;
74 end_group!();
75
76 if open::that(&login_session_payload.callback_url).is_ok() {
77 info!("Your browser has been opened to complete the login process");
78 } else {
79 warn!("Failed to open the browser automatically, please open the URL manually");
80 }
81 info!(
82 "Authentication URL: {}\n",
83 style(login_session_payload.callback_url)
84 .blue()
85 .bold()
86 .underlined()
87 );
88
89 start_group!("Waiting for the login to be completed");
90 let token;
91 let start = Instant::now();
92 loop {
93 if start.elapsed() > LOGIN_SESSION_MAX_DURATION {
94 bail!("Login session expired, please try again");
95 }
96
97 match api_client
98 .consume_login_session(&login_session_payload.session_id)
99 .await?
100 .token
101 {
102 Some(token_from_api) => {
103 token = token_from_api;
104 break;
105 }
106 None => sleep(Duration::from_secs(5)).await,
107 }
108 }
109 end_group!();
110 token
111 };
112

Callers 1

runFunction · 0.85

Calls 8

create_login_sessionMethod · 0.80
consume_login_sessionMethod · 0.80
with_tokenMethod · 0.80
sessionMethod · 0.80
selected_profile_nameMethod · 0.80
profile_mutMethod · 0.80
persistMethod · 0.80
is_emptyMethod · 0.45

Tested by

no test coverage detected