Skip to content

Commit 69765a6

Browse files
committed
flowctl: Fix auth login not properly exchanging access token for refresh token
1 parent 8a24ab8 commit 69765a6

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

crates/flow-client/src/client.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ pub async fn refresh_authorizations(
312312
(Some(access), None) => {
313313
// We have an access token but no refresh token. Create one.
314314
let refresh_token = api_exec::<RefreshToken>(
315-
client.rpc(
315+
client.clone().with_creds(Some(access.to_owned())).rpc(
316316
"create_refresh_token",
317317
serde_json::json!({"multi_use": true, "valid_for": "90d", "detail": "Created by flowctl"})
318318
.to_string(),

crates/flowctl/src/auth/mod.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
mod roles;
22

33
use anyhow::Context;
4+
use flow_client::client::refresh_authorizations;
45

56
#[derive(Debug, clap::Args)]
67
#[clap(rename_all = "kebab-case")]
@@ -64,7 +65,20 @@ impl Auth {
6465
Ok(())
6566
}
6667
Command::Roles(roles) => roles.run(ctx).await,
67-
}
68+
}?;
69+
70+
// Ensure that any changes to the credentials fully propagate
71+
// i.e if an access token is changed, we also need to make sure
72+
// to generate and store an updated refresh token.
73+
let (access_token, refresh_token) = refresh_authorizations(
74+
&ctx.client,
75+
ctx.config.user_access_token.to_owned(),
76+
ctx.config.user_refresh_token.to_owned(),
77+
)
78+
.await?;
79+
ctx.config.user_access_token = Some(access_token);
80+
ctx.config.user_refresh_token = Some(refresh_token);
81+
Ok(())
6882
}
6983
}
7084

crates/flowctl/src/lib.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -136,21 +136,25 @@ impl Cli {
136136

137137
let anon_client: flow_client::Client = config.build_anon_client();
138138

139-
let client = if let Ok((access, refresh)) = refresh_authorizations(
139+
let client = match refresh_authorizations(
140140
&anon_client,
141141
config.user_access_token.to_owned(),
142142
config.user_refresh_token.to_owned(),
143143
)
144144
.await
145145
{
146-
// Make sure to store refreshed tokens back in Config so they get written back to disk
147-
config.user_access_token = Some(access.to_owned());
148-
config.user_refresh_token = Some(refresh.to_owned());
146+
Ok((access, refresh)) => {
147+
// Make sure to store refreshed tokens back in Config so they get written back to disk
148+
config.user_access_token = Some(access.to_owned());
149+
config.user_refresh_token = Some(refresh.to_owned());
149150

150-
anon_client.with_creds(Some(access))
151-
} else {
152-
tracing::warn!("You are not authenticated. Run `auth login` to login to Flow.");
153-
anon_client
151+
anon_client.with_creds(Some(access))
152+
}
153+
Err(err) => {
154+
tracing::debug!(?err, "Error refreshing credentials");
155+
tracing::warn!("You are not authenticated. Run `auth login` to login to Flow.");
156+
anon_client
157+
}
154158
};
155159

156160
let mut context = CliContext {

0 commit comments

Comments
 (0)