Skip to content
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
33 changes: 33 additions & 0 deletions cli-engine/src/auth/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ use crate::{
};

/// Data rendered after a successful `auth login`.
///
/// Built by [`login_and_build`]/[`login_and_build_with_scopes`] from the
/// dispatcher's [`Credential`]. Consumer code receives it as a command
/// result and should not construct it directly — `#[non_exhaustive]` lets
/// this framework add fields (as it did for [`refreshable`](Self::refreshable))
/// without another breaking release.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct AuthLoginResult {
/// Provider used for login.
pub provider: String,
Expand All @@ -25,10 +32,22 @@ pub struct AuthLoginResult {
/// with a subsequent `auth status`.
#[serde(default)]
pub scopes: Vec<String>,
/// Whether a renewal mechanism was issued for the new credential (e.g.
/// an OAuth refresh token) — reflects presence, not verified validity.
/// Mirrors [`AuthStatusEntry::refreshable`].
#[serde(default)]
pub refreshable: bool,
}

/// Data rendered by `auth status`.
///
/// Built by [`status_result`]/[`to_status_entry`] from the dispatcher's
/// [`Credential`] (or its absence). Consumer code receives it as a command
/// result and should not construct it directly — `#[non_exhaustive]` lets
/// this framework add fields (as it did for [`refreshable`](Self::refreshable))
/// without another breaking release.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct AuthStatusEntry {
/// Provider name.
pub provider: String,
Expand All @@ -44,6 +63,16 @@ pub struct AuthStatusEntry {
pub scopes: Vec<String>,
/// Whether the cached credential is expired or unavailable.
pub expired: bool,
/// Whether a renewal mechanism (e.g. an OAuth refresh token) is on file
/// for this credential — reflects presence, not verified validity, since
/// that's only discoverable by attempting a renewal. Always `false` when
/// there is no cached credential, or the provider has no such mechanism
/// (e.g. PATs) — `expired && !refreshable` means the next command
/// definitely needs `auth login`, while `expired && refreshable` means it
/// will attempt a silent renewal first and only fall back to an
/// interactive login if that fails.
#[serde(default)]
pub refreshable: bool,
}

/// Builds the built-in runtime `auth` command group.
Expand Down Expand Up @@ -206,6 +235,7 @@ pub async fn login_and_build_with_scopes(
identity: credential.identity,
expires_at: credential.expires_at,
scopes: credential.scopes,
refreshable: credential.refreshable,
})
}

Expand All @@ -230,6 +260,7 @@ pub async fn status_result(dispatcher: &Dispatcher, provider: &str, env: &str) -
expires_at: String::new(),
scopes: Vec::new(),
expired: true,
refreshable: false,
}
} else {
to_status_entry(&entry.provider, &entry.env, entry.credential.as_ref())
Expand Down Expand Up @@ -264,6 +295,7 @@ pub fn to_status_entry(
expires_at: String::new(),
scopes: Vec::new(),
expired: true,
refreshable: false,
},
|credential| AuthStatusEntry {
provider: provider.to_owned(),
Expand All @@ -272,6 +304,7 @@ pub fn to_status_entry(
expires_at: credential.expires_at.clone(),
scopes: credential.scopes.clone(),
expired: credential.is_expired(),
refreshable: credential.refreshable,
},
)
}
10 changes: 10 additions & 0 deletions cli-engine/src/auth/credential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ pub struct Credential {
/// enforced server-side).
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub scopes: Vec<String>,
/// Whether a renewal mechanism (e.g. an OAuth refresh token) is present
/// for this credential, so an expired access token is *expected* to
/// renew without an interactive re-login. This reflects presence, not
/// verified validity — the mechanism itself may have been revoked or
/// expired server-side, which is only discoverable by attempting a
/// renewal. `false` for providers with no such mechanism (e.g. PATs) or
/// when the credential has none, even if the access token itself is
/// still valid.
#[serde(default)]
pub refreshable: bool,
}

impl Credential {
Expand Down
18 changes: 18 additions & 0 deletions cli-engine/src/auth/pkce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ impl PkceAuthProvider {
identity,
sub,
scopes: granted_scopes(token),
refreshable: token.refresh_token.is_some(),
..Credential::default()
Comment thread
jpage-godaddy marked this conversation as resolved.
Resolved
}
}
Expand Down Expand Up @@ -1691,6 +1692,23 @@ mod tests {
assert_eq!(credential.scopes, vec!["a", "b"]);
}

#[test]
fn build_credential_sets_refreshable_when_refresh_token_present() {
let provider = test_provider();
let mut token = valid_token(&make_jwt(&json!({"sub": "subject-1"})));
token.refresh_token = Some("a-refresh-token".to_owned());
let credential = provider.build_credential("prod", &token);
assert!(credential.refreshable);
}

#[test]
fn build_credential_leaves_refreshable_false_without_refresh_token() {
let provider = test_provider();
let token = valid_token(&make_jwt(&json!({"sub": "subject-1"})));
let credential = provider.build_credential("prod", &token);
assert!(!credential.refreshable);
}

#[test]
fn build_credential_leaves_identity_blank_for_opaque_token() {
let provider = test_provider();
Expand Down
66 changes: 35 additions & 31 deletions cli-engine/tests/foundation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2809,7 +2809,8 @@ async fn cli_runtime_auth_login_uses_registered_provider_default() {
"env": "prod",
"identity": "tester",
"expires_at": "2099-01-01T00:00:00Z",
"scopes": []
"scopes": [],
"refreshable": false
})
);

Expand Down Expand Up @@ -2852,7 +2853,8 @@ async fn cli_runtime_auth_login_uses_middleware_env_when_env_flag_omitted() {
"env": "dev",
"identity": "tester",
"expires_at": "2099-01-01T00:00:00Z",
"scopes": []
"scopes": [],
"refreshable": false
})
);
}
Expand Down Expand Up @@ -3014,7 +3016,8 @@ async fn cli_runtime_auth_status_and_logout_render_legacy_shapes() {
"identity": "tester",
"expires_at": "2099-01-01T00:00:00Z",
"scopes": [],
"expired": false
"expired": false,
"refreshable": false
})
);

Expand Down Expand Up @@ -4701,7 +4704,8 @@ async fn auth_command_helpers_match_login_status_and_logout_shapes() {
"identity": "tester",
"expires_at": "2099-01-01T00:00:00Z",
"scopes": [],
"expired": false
"expired": false,
"refreshable": false
})
);

Expand All @@ -4716,7 +4720,8 @@ async fn auth_command_helpers_match_login_status_and_logout_shapes() {
"identity": "tester",
"expires_at": "2099-01-01T00:00:00Z",
"scopes": [],
"expired": false
"expired": false,
"refreshable": false
}])
);

Expand Down Expand Up @@ -5080,8 +5085,8 @@ fn authn_request_serializes_compat_fields() {
);
}

#[test]
fn auth_module_reexports_primary_auth_port_surfaces() {
#[tokio::test]
async fn auth_module_reexports_primary_auth_port_surfaces() {
use cli_engine::auth::{
ACTION_AUTHENTICATE as REEXPORTED_AUTHENTICATE, AuthLoginResult,
AuthnRequest as ReexportedAuthnRequest, ExecProvider as ReexportedExecProvider,
Expand All @@ -5106,35 +5111,34 @@ fn auth_module_reexports_primary_auth_port_surfaces() {
let auth_group = reexported_auth_command_group("primary", &["primary".to_owned()]);
assert_eq!(auth_group.group.name, "auth");

let login = AuthLoginResult {
provider: "primary".to_owned(),
env: "prod".to_owned(),
identity: "tester".to_owned(),
expires_at: "2030-01-01T00:00:00Z".to_owned(),
scopes: Vec::new(),
};
// AuthLoginResult is #[non_exhaustive], so it's obtained through the real
// login flow (as any consumer must) rather than a struct literal.
let mut dispatcher = Dispatcher::new();
dispatcher.register(Arc::new(FakeProvider::new("primary", "tester")));
let login: AuthLoginResult = login_and_build(&dispatcher, "primary", "prod")
.await
.expect("login result should build");
assert_eq!(login.identity, "tester");
}

#[test]
fn crate_root_reexports_auth_command_result_surfaces() {
let login = cli_engine::AuthLoginResult {
provider: "primary".to_owned(),
env: "prod".to_owned(),
identity: "tester".to_owned(),
expires_at: "2030-01-01T00:00:00Z".to_owned(),
scopes: Vec::new(),
};
#[tokio::test]
async fn crate_root_reexports_auth_command_result_surfaces() {
// AuthLoginResult/AuthStatusEntry are #[non_exhaustive], so they're
// obtained through the real login/status flow (as any consumer must)
// rather than a struct literal.
let mut dispatcher = Dispatcher::new();
dispatcher.register(Arc::new(FakeProvider::new("primary", "tester")));

let login: cli_engine::AuthLoginResult = login_and_build(&dispatcher, "primary", "prod")
.await
.expect("login result should build");
assert_eq!(login.provider, "primary");

let status = cli_engine::AuthStatusEntry {
provider: "primary".to_owned(),
env: "prod".to_owned(),
identity: "tester".to_owned(),
expires_at: "2030-01-01T00:00:00Z".to_owned(),
scopes: vec!["domains.domain:read".to_owned()],
expired: false,
};
let credential = dispatcher
.status("primary", "prod")
.await
.expect("status should resolve");
let status: cli_engine::AuthStatusEntry = to_status_entry("primary", "prod", Some(&credential));
assert!(!status.expired);

let group = auth_command_group("primary", &["primary".to_owned()]);
Expand Down