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
10 changes: 10 additions & 0 deletions crates/fctl-client/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2085,6 +2085,16 @@ impl Api {
.await
}

/// Names one of your Claude connections, or, with `None`, takes its
/// name away, so that it goes by its account or its kind.
pub async fn rename_claude(&self, id: &str, label: Option<String>) -> Result<Me> {
self.patch(
&format!("/me/claude/{}", enc(id)),
&UpdateClaudeCredentialRequest { label: Some(label) },
)
.await
}

pub async fn start_claude_login(&self) -> Result<ClaudeLoginJob> {
self.post("/me/claude/login", &serde_json::json!({})).await
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- A Claude connection's label is the name its person gave it, which goes
-- before its account wherever it is named. Logins were labelled "Claude
-- login" by fctl itself, which would hide their account: they go without.
UPDATE claude_credentials SET label = NULL WHERE kind = 'oauth' AND label = 'Claude login';
51 changes: 51 additions & 0 deletions crates/fctl-control/src/api/claude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,57 @@ async fn stop_running(ctx: &Ctx, running: &[(&str, Option<&str>)]) {
}
}

/// `PATCH /api/v1/me/claude/{id}`: names one of the caller's connections,
/// or takes its name away.
pub async fn update(ctx: &mut Ctx, req: &mut Request, id: &str) -> AppResult<Response> {
let body: UpdateClaudeCredentialRequest = http::body(req).await?;
let user = ctx.user()?.clone();
if let Some(label) = body.label {
Comment thread
fctl-review[bot] marked this conversation as resolved.
Resolved
let label = label
.as_deref()
.map(str::trim)
.filter(|l| !l.is_empty())
.map(|l| fctl_domain::validate::name("label", l))
.transpose()?;
#[derive(serde::Deserialize)]
struct Owned {
#[serde(deserialize_with = "crate::db::int_bool", default)]
provisioned: bool,
}
let owned: Option<Owned> = q(
&ctx.db,
"SELECT provisioned FROM claude_credentials WHERE id = ? AND user_id = ?",
&[s(id), s(&user.id)],
)?
.first()
.await?;
let Some(owned) = owned else {
return Err(not_found("Credential"));
};
// Its admins named it, read its name (`api::provision::read`) and
// name the next one: a name of the person's would reach them.
if owned.provisioned {
return Err(validation(
"label",
"Your admins set this token, and its name: they rename it",
));
}
q(
&ctx.db,
"UPDATE claude_credentials SET label = ?, updated_at = ? WHERE id = ? AND user_id = ?",
&[os(label.as_deref()), n(ctx.now), s(id), s(&user.id)],
)?
.run()
.await?;
// Not the name: admins read the log, and never see it.
ctx.audit("claude.renamed", Some(id), None);
}
let user = auth::user_by_id(ctx, &user.id)
.await?
.ok_or_else(|| not_found("User"))?;
http::ok(&auth::me(ctx, &user).await?)
}

pub async fn set_default(ctx: &mut Ctx, id: &str) -> AppResult<Response> {
let user = ctx.user()?.clone();
let owned: Option<crate::db::Count> = q(
Expand Down
5 changes: 2 additions & 3 deletions crates/fctl-control/src/do_fleet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4456,12 +4456,11 @@ impl FleetObject {
let email = bundle.account.as_ref().and_then(|a| a.email.clone());
crate::db::q(
db,
"INSERT INTO claude_credentials (id, user_id, kind, label, secret_enc, scopes, account_email, subscription_type, rate_limit_tier, account, status, expires_at, created_at, updated_at) \
VALUES (?, ?, 'oauth', ?, ?, ?, ?, ?, ?, ?, 'active', ?, ?, ?)",
"INSERT INTO claude_credentials (id, user_id, kind, secret_enc, scopes, account_email, subscription_type, rate_limit_tier, account, status, expires_at, created_at, updated_at) \
VALUES (?, ?, 'oauth', ?, ?, ?, ?, ?, ?, 'active', ?, ?, ?)",
&[
crate::db::s(id.as_str()),
crate::db::s(user_id),
crate::db::s("Claude login"),
crate::db::s(&sealed),
crate::db::s(&bundle.scopes.join(" ")),
crate::db::os(email.as_deref()),
Expand Down
1 change: 1 addition & 0 deletions crates/fctl-control/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,7 @@ async fn dispatch_rest(
// Linear accounts
(Get, ["me", "linear"]) => api::linear::mine(ctx).await,
(Delete, ["me", "linear", organization]) => api::linear::unlink(ctx, organization).await,
(Patch, ["me", "claude", id]) => api::claude::update(ctx, req, id).await,
(Delete, ["me", "claude", id]) => api::claude::remove(ctx, id).await,
// Notifications
(Get, ["me", "notifications"]) => api::notifications::get(ctx).await,
Expand Down
19 changes: 19 additions & 0 deletions crates/fctl-proto/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5495,6 +5495,9 @@ pub enum CredentialStatus {
pub struct ClaudeCredentialInfo {
pub id: CredentialId,
pub kind: ClaudeCredentialKind,
/// The name its person gave it, which says what it is for ("Work
/// seat"): it goes before its account and its kind wherever it is
/// named. Absent until they name it.
pub label: Option<String>,
pub account_email: Option<String>,
pub subscription_type: Option<String>,
Expand Down Expand Up @@ -5829,6 +5832,22 @@ pub struct AddClaudeTokenRequest {
pub made_at: Option<Millis>,
}

/// `PATCH /api/v1/me/claude/{id}`: changes one of the caller's Claude
/// connections.
#[skip_serializing_none]
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "ts", derive(ts_rs::TS), ts(optional_fields))]
pub struct UpdateClaudeCredentialRequest {
/// Its name, which says what it is for ("Work seat"): `null` or `""`
/// for none, so that it goes by its account or its kind. Left out, it
/// stays. Its person sees it, never their workspaces' admins. A
/// connection an admin token set (`provisioned`) keeps the name its
/// admins gave it: renaming it is refused.
#[serde(default, with = "::serde_with::rust::double_option")]
#[cfg_attr(feature = "ts", ts(optional = nullable, as = "Option<String>"))]
pub label: Option<Option<String>>,
}

/// `POST /api/v1/me/claude/on-runners`: connects the Claude token the
/// caller keeps on their runners ([`ClaudeCredentialInfo::on_runners`]),
/// which they never give to fctl, and makes it their default. Connecting it
Expand Down
1 change: 1 addition & 0 deletions crates/fctl-proto/src/ts_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ pub fn export_typescript(dir: &Path) -> Result<(), ExportError> {
api::PlanLimit,
api::AddClaudeTokenRequest,
api::AddClaudeOnRunnersRequest,
api::UpdateClaudeCredentialRequest,
api::ClaudeLoginJob,
api::ClaudeLoginState,
api::ClaudeLoginCodeRequest,
Expand Down
Loading
Loading