Skip to content

Commit 9ef4b9f

Browse files
committed
fix: save device registration when launching chat
1 parent 577a3ed commit 9ef4b9f

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

crates/fig_auth/src/consts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use aws_types::region::Region;
22

33
pub(crate) const CLIENT_NAME: &str = "Amazon Q Developer for command line";
44

5-
pub(crate) const OIDC_BUILDER_ID_REGION: Region = Region::from_static("us-east-1");
5+
pub const OIDC_BUILDER_ID_REGION: Region = Region::from_static("us-east-1");
66

77
/// The scopes requested for OIDC
88
///

crates/fig_auth/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
pub mod builder_id;
2-
mod consts;
2+
pub mod consts;
33
mod error;
44
pub mod pkce;
55
mod scope;

crates/q_cli/src/cli/mod.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,13 @@ use eyre::{
4646
bail,
4747
};
4848
use feed::Feed;
49-
use fig_auth::builder_id::BuilderIdToken;
49+
use fig_auth::builder_id::{
50+
BuilderIdToken,
51+
DeviceRegistration,
52+
};
53+
use fig_auth::consts::OIDC_BUILDER_ID_REGION;
5054
use fig_auth::is_logged_in;
55+
use fig_auth::pkce::Region;
5156
use fig_auth::secret_store::SecretStore;
5257
use fig_ipc::local::open_ui_element;
5358
use fig_log::{
@@ -71,6 +76,7 @@ use tracing::{
7176
Level,
7277
debug,
7378
error,
79+
warn,
7480
};
7581

7682
use self::integrations::IntegrationsSubcommands;
@@ -380,10 +386,35 @@ impl Cli {
380386
assert_logged_in().await?;
381387
}
382388

389+
// Save credentials from the macOS keychain to sqlite.
390+
// On Linux, this essentially just rewrites to the database.
383391
let secret_store = SecretStore::new().await.ok();
384392
if let Some(secret_store) = secret_store {
385393
if let Ok(database) = database().map_err(|err| error!(?err, "failed to open database")) {
386394
if let Ok(token) = BuilderIdToken::load(&secret_store, false).await {
395+
// Save the device registration. This is required for token refresh to succeed.
396+
if let Some(token) = token.as_ref() {
397+
let region = token.region.clone().map_or(OIDC_BUILDER_ID_REGION, Region::new);
398+
match DeviceRegistration::load_from_secret_store(&secret_store, &region).await {
399+
Ok(Some(reg)) => match serde_json::to_string(&reg) {
400+
Ok(reg) => {
401+
database
402+
.set_auth_value("codewhisperer:odic:device-registration", reg)
403+
.map_err(|err| error!(?err, "failed to write device registration to auth db"))
404+
.ok();
405+
},
406+
Err(err) => error!(?err, "failed to serialize the device registration"),
407+
},
408+
Ok(None) => {
409+
warn!(?token, "no device registration found for token");
410+
},
411+
Err(err) => {
412+
error!(?err, "failed to load device registration");
413+
},
414+
}
415+
}
416+
417+
// Next, save the token.
387418
if let Ok(token) = serde_json::to_string(&token) {
388419
database
389420
.set_auth_value("codewhisperer:odic:token", token)

0 commit comments

Comments
 (0)