use std::path::Path;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use super::{Login, store};
use crate::claude::seats::{Seat, SeatId};
use crate::core::harness::Harness;
use crate::core::ids::LoginId;
use crate::errors::VaultError;
const INDEX_VERSION: u32 = 4;
#[derive(Debug, Serialize, Deserialize)]
pub(super) struct Index {
#[serde(default = "version_1")]
v: u32,
pub(super) logins: Vec<Login>,
#[serde(default)]
pub(super) seats: std::collections::BTreeMap<SeatId, SeatSeen>,
#[serde(default, skip_serializing_if = "std::collections::BTreeMap::is_empty")]
legacy_seats: std::collections::BTreeMap<String, SeatSeen>,
}
fn version_1() -> u32 {
1
}
impl Default for Index {
fn default() -> Self {
Index {
v: INDEX_VERSION,
logins: Vec::new(),
seats: Default::default(),
legacy_seats: Default::default(),
}
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
pub(super) struct SeatSeen {
#[serde(default, skip_serializing_if = "String::is_empty")]
pub(super) label: String,
pub(super) fingerprint: String,
pub(super) account_uuid: Option<String>,
pub(super) profile_at: Option<DateTime<Utc>>,
pub(super) unverified_since: Option<DateTime<Utc>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(super) switched_at: Option<DateTime<Utc>>,
}
pub(super) fn read_index(dir: &Path) -> anyhow::Result<(Index, Option<String>)> {
let text = store::read_optional(&dir.join("logins.json"))?;
let index = match &text {
Some(t) => {
let mut i: Index = serde_json::from_str(t)
.map_err(|e| anyhow::Error::new(e).context(VaultError::Damaged("logins.json")))?;
i.upgrade()?;
i
}
None => Index::default(),
};
Ok((index, text))
}
impl Index {
pub(super) fn upgrade(&mut self) -> anyhow::Result<()> {
if self.v > INDEX_VERSION {
return Err(VaultError::NewerFormat(self.v).into());
}
if self.v < 2 {
for l in &mut self.logins {
if let Some(label) = std::mem::take(&mut l.mounted).into_iter().next() {
l.mounted_label = Some(label.as_str().to_string());
}
}
for (label, mut seen) in std::mem::take(&mut self.seats) {
seen.label = label.as_str().to_string();
self.legacy_seats.insert(label.as_str().to_string(), seen);
}
}
if self.v < 3 {
for l in &mut self.logins {
if let Some(raw) = &l.oauth_account
&& let Some(mut acct) = crate::claude::config_json::Account::from_value(raw)
{
acct.apply_credentials(&l.meta);
let id = acct.id();
if id.has_org() || l.account_uuid.is_none() {
l.account_uuid = Some(id.to_string());
}
if let Some(tier) = acct.effective_tier() {
l.plan = Some(tier);
}
}
}
}
self.v = INDEX_VERSION;
Ok(())
}
pub(super) fn adopt_seats(&mut self, seats: &[Seat]) {
for seat in seats {
if let Some(mut seen) = self.legacy_seats.remove(&seat.label) {
seen.label = seat.label.clone();
self.seats.entry(seat.id.clone()).or_insert(seen);
}
for l in &mut self.logins {
if l.mounted_label.as_deref() == Some(seat.label.as_str()) {
if !l.is_in(&seat.id) {
l.mounted.push(seat.id.clone());
}
l.mounted_label = None;
}
}
}
}
pub(super) fn seat_name(&self, id: &SeatId) -> String {
self.seats
.get(id)
.map(|s| s.label.clone())
.filter(|l| !l.is_empty())
.unwrap_or_else(|| id.to_string())
}
pub(super) fn new_login_id(&self, fingerprint: &str) -> LoginId {
let base = format!("l{}", fingerprint.chars().take(10).collect::<String>());
let taken = |id: &str| self.logins.iter().any(|l| l.id.as_str() == id);
let mut id = base.clone();
let mut n = 2u32;
while taken(&id) {
id = format!("{base}-{n}");
n += 1;
}
LoginId::from_stored(id)
}
pub(super) fn violations(&self, has_secret: impl Fn(&LoginId) -> bool) -> Vec<String> {
let mut out = Vec::new();
let mut ids = std::collections::BTreeSet::new();
let mut grants = std::collections::BTreeSet::new();
let mut seats = std::collections::BTreeMap::new();
for l in &self.logins {
if !ids.insert(&l.id) {
out.push(format!("two logins have id '{}'", l.id));
}
if !grants.insert((l.harness, l.meta.fingerprint.as_str())) {
out.push(format!(
"the grant of '{}' ({}) is saved twice",
l.label, l.meta.fingerprint
));
}
if !has_secret(&l.id) {
out.push(format!("login '{}' has no secret", l.label));
}
if !l.harness.shares_logins() && l.mounted.len() > 1 {
out.push(format!(
"{} login '{}' is in {} seats",
l.harness,
l.label,
l.mounted.len()
));
}
for seat in &l.mounted {
if let Some(other) = seats.insert((l.harness, seat), l.label.as_str()) {
out.push(format!(
"seat {} holds both '{other}' and '{}'",
self.seat_name(seat),
l.label
));
}
}
}
out
}
pub(super) fn find(&self, key: &str) -> Result<&Login, VaultError> {
self.find_in(key, None)
}
pub(super) fn find_in(
&self,
key: &str,
harness: Option<Harness>,
) -> Result<&Login, VaultError> {
let hits: Vec<&Login> = self
.logins
.iter()
.filter(|l| harness.is_none_or(|h| l.harness == h))
.filter(|l| l.label == key || l.id.as_str() == key || l.email.as_deref() == Some(key))
.collect();
match hits.as_slice() {
[one] => Ok(one),
[] => Err(VaultError::NoLogin(key.to_string())),
many => Err(VaultError::AmbiguousLogin {
key: key.to_string(),
logins: many
.iter()
.map(|l| format!("{} ({})", l.label, l.harness.display_name()))
.collect(),
}),
}
}
pub(super) fn find_mut(&mut self, id: &LoginId) -> Option<&mut Login> {
self.logins.iter_mut().find(|l| l.id == *id)
}
pub(super) fn label_of(&self, id: &LoginId) -> Option<String> {
self.logins
.iter()
.find(|l| l.id == *id)
.map(|l| l.label.clone())
}
pub(super) fn check_label(
&self,
label: &str,
id: Option<&LoginId>,
harness: Harness,
) -> anyhow::Result<()> {
if label.trim().is_empty() {
return Err(VaultError::LabelEmpty.into());
}
if self.logins.iter().any(|l| {
Some(&l.id) != id
&& ((l.harness == harness && l.label == label)
|| l.id.as_str() == label
|| l.email.as_deref() == Some(label))
}) {
return Err(VaultError::LabelTaken {
label: label.to_string(),
harness: harness.display_name().to_string(),
}
.into());
}
Ok(())
}
pub(super) fn unique_label(&self, base: &str, harness: Harness) -> String {
let taken = |l: &str| {
self.logins
.iter()
.any(|x| x.harness == harness && x.label == l)
};
let mut label = base.to_string();
let mut n = 2;
while taken(&label) {
label = format!("{base}-{n}");
n += 1;
}
label
}
pub(super) fn dedupe_labels(&mut self) -> Vec<(String, String)> {
let mut renamed = Vec::new();
for i in 0..self.logins.len() {
let label = self.logins[i].label.clone();
let h = self.logins[i].harness;
if self.logins[..i]
.iter()
.any(|l| l.harness == h && l.label == label)
{
let new = self.unique_label(&label, h);
self.logins[i].label = new.clone();
renamed.push((label, new));
}
}
renamed
}
}