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

Function login

src/cli/auth.rs:56–138  ·  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

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

Callers 1

runFunction · 0.85

Calls 8

create_login_sessionMethod · 0.80
consume_login_sessionMethod · 0.80
with_authenticationMethod · 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