Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flow-client: Make sure to remove any potentially expired JWT from the client used to exchange a refresh token for an access token in refresh_authorizations() #1725

Merged
merged 2 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/dekaf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl Authenticated {
self.client = self
.client
.clone()
.with_creds(Some(access))
.with_user_access_token(Some(access))
.with_fresh_gazette_client();
}

Expand All @@ -98,7 +98,7 @@ impl App {
let client = self
.client_base
.clone()
.with_creds(Some(access.clone()))
.with_user_access_token(Some(access.clone()))
.with_fresh_gazette_client();

let claims = flow_client::client::client_claims(&client)?;
Expand Down
13 changes: 9 additions & 4 deletions crates/flow-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ impl Client {
}
}

pub fn with_creds(self, user_access_token: Option<String>) -> Self {
pub fn with_user_access_token(self, user_access_token: Option<String>) -> Self {
Self {
user_access_token: user_access_token.or(self.user_access_token),
user_access_token,
..self
}
}
Expand Down Expand Up @@ -312,7 +312,7 @@ pub async fn refresh_authorizations(
(Some(access), None) => {
// We have an access token but no refresh token. Create one.
let refresh_token = api_exec::<RefreshToken>(
client.clone().with_creds(Some(access.to_owned())).rpc(
client.clone().with_user_access_token(Some(access.to_owned())).rpc(
"create_refresh_token",
serde_json::json!({"multi_use": true, "valid_for": "90d", "detail": "Created by flowctl"})
.to_string(),
Expand All @@ -331,10 +331,15 @@ pub async fn refresh_authorizations(
access_token: String,
refresh_token: Option<RefreshToken>, // Set iff the token was single-use.
}
// We either never had an access token, or we had one and it expired,
// in which case the client may have an invalid access token configured.
// The `generate_access_token` RPC only needs the provided refresh token
// for authentication, so we should use an unauthenticated client to make
// the request.
let Response {
access_token,
refresh_token: next_refresh_token,
} = api_exec::<Response>(client.rpc(
} = api_exec::<Response>(client.clone().with_user_access_token(None).rpc(
"generate_access_token",
serde_json::json!({"refresh_token_id": id, "secret": secret}).to_string(),
))
Expand Down
2 changes: 1 addition & 1 deletion crates/flowctl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ impl Cli {
config.user_access_token = Some(access.to_owned());
config.user_refresh_token = Some(refresh.to_owned());

anon_client.with_creds(Some(access))
anon_client.with_user_access_token(Some(access))
}
Err(err) => {
tracing::debug!(?err, "Error refreshing credentials");
Expand Down
Loading