Skip to content

Lightpanda: the headless browser designed for AI and automation

auth: simplify token refresh and json store helpers

Adrià Arrufat27b37e6c9374
diff --git a/src/agent/Agent.zig b/src/agent/Agent.zig
index 649eb2791..d4646909d 100644
--- a/src/agent/Agent.zig
+++ b/src/agent/Agent.zig
@@ -1041,21 +1041,17 @@ fn setProvider(self: *Agent, credential: settings.Credential) !void {
/// Keep a subscription (bearer) token current before a model request: when the
/// active session's token is near expiry, re-import from the source and repoint
/// the client at the fresh token (`keySlice` readers see it automatically).
-/// A no-op for API-key auth. Runs between turns on the single agent thread,
-/// never during a request, so the displaced token is never dereferenced after
-/// it's freed here.
+/// A no-op for API-key auth. Runs between turns on the single agent thread, so
+/// the client is repointed before the next request borrows the token.
fn refreshAuthIfNeeded(self: *Agent) void {
const cred = if (self.credential) |*c| c else return;
if (cred.key != .session) return;
const session = &cred.key.session;
- const displaced = session.ensureFresh() catch |err| {
+ const refreshed = session.ensureFresh() catch |err| {
self.terminal.printError("could not refresh the subscription token: {s}", .{@errorName(err)});
return;
};
- if (displaced) |old| {
- if (self.ai_client) |client| client.setApiKey(session.tokens.access_token);
- old.deinit(session.allocator);
- }
+ if (refreshed) if (self.ai_client) |client| client.setApiKey(session.tokens.access_token);
}
const PathAndMode = struct { path: []const u8, mode: save.Mode };
@@ -1618,8 +1614,9 @@ fn recordSlashToolCall(
fn formatApiError(self: *Agent, client: zenai.provider.Client, err: anyerror) []const u8 {
const e = client.lastError();
const status = e.status orelse return @errorName(err);
+ const owned_key = if (self.credential) |c| c.key == .owned else false;
const hint = if (status == 401 and client == .vertex)
- if (self.credential != null and self.credential.?.key == .owned)
+ if (owned_key)
" (Vertex token may have expired; run /provider vertex to refresh)"
else
" (Vertex express mode needs an express API key — a Gemini Developer key won't work)"
@@ -1972,10 +1969,7 @@ fn completionProviders(context: *anyopaque, arena: std.mem.Allocator) []const []
// one is what starts the login.
for (auth.registry) |desc| {
const name = @tagName(desc.provider);
- const detected = for (self.available_providers) |p| {
- if (std.mem.eql(u8, p, name)) break true;
- } else false;
- if (!detected) {
+ if (!string.isOneOf(name, self.available_providers)) {
names[n] = name;
n += 1;
}
diff --git a/src/agent/auth/auth.zig b/src/agent/auth/auth.zig
index 502f582c4..bdc654b75 100644
--- a/src/agent/auth/auth.zig
+++ b/src/agent/auth/auth.zig
@@ -79,11 +79,6 @@ pub const Descriptor = struct {
loginFn: *const fn (std.mem.Allocator, interrupt: ?*zenai.http.Interrupt) anyerror!TokenSet,
/// Exchange a refresh token for a new `TokenSet` (with re-derived account id).
refreshFn: *const fn (std.mem.Allocator, refresh_token: []const u8) anyerror!TokenSet,
-
- /// Key in the on-disk store (`auth.json`).
- pub fn storeKey(self: *const Descriptor) []const u8 {
- return @tagName(self.provider);
- }
};
pub const registry = [_]*const Descriptor{&codex.descriptor};
@@ -93,7 +88,7 @@ pub fn descriptorFor(provider: Config.AiProvider) ?*const Descriptor {
return null;
}
-// --- On-disk token store: <data_dir>/auth.json, a JSON map keyed by storeKey ---
+// --- On-disk token store: <data_dir>/auth.json, a JSON map keyed by provider tag ---
// The `*At` variants take a scratch arena and an explicit dir (unit-testable);
// the public wrappers build the arena and resolve the app data dir themselves.
@@ -108,54 +103,44 @@ fn storePath(arena: std.mem.Allocator, dir: []const u8) ![]const u8 {
return std.fs.path.join(arena, &.{ dir, "auth.json" });
}
+/// The parsed store; empty on any failure (absent, unreadable, malformed).
+fn readStoreFile(arena: std.mem.Allocator, path: []const u8) std.json.ArrayHashMap(StoredToken) {
+ const data = std.Io.Dir.cwd().readFileAlloc(lp.io, path, arena, .limited(64 * 1024)) catch return .{};
+ return std.json.parseFromSliceLeaky(std.json.ArrayHashMap(StoredToken), arena, data, .{ .ignore_unknown_fields = true }) catch .{};
+}
+
+/// Serialize `value` as JSON to `path` via temp+rename, so a failed write
+/// can't corrupt an existing file.
+pub fn writeJsonAtomic(path: []const u8, value: anytype, permissions: std.Io.File.Permissions) !void {
+ var af = try std.Io.Dir.cwd().createFileAtomic(lp.io, path, .{ .permissions = permissions, .replace = true });
+ defer af.deinit(lp.io);
+ var buf: [1024]u8 = undefined;
+ var w = af.file.writer(lp.io, &buf);
+ try std.json.Stringify.value(value, .{}, &w.interface);
+ try w.end();
+ try af.replace(lp.io);
+}
+
fn storeLoadAt(allocator: std.mem.Allocator, dir: []const u8, id: []const u8) !?TokenSet {
var arena: std.heap.ArenaAllocator = .init(allocator);
defer arena.deinit();
const a = arena.allocator();
const path = try storePath(a, dir);
- const data = std.Io.Dir.cwd().readFileAlloc(lp.io, path, a, .limited(64 * 1024)) catch return null;
- const parsed = std.json.parseFromSliceLeaky(std.json.ArrayHashMap(StoredToken), a, data, .{ .ignore_unknown_fields = true }) catch return null;
- const t = parsed.map.get(id) orelse return null;
+ const t = readStoreFile(a, path).map.get(id) orelse return null;
return try TokenSet.dup(allocator, t.access, t.refresh, t.expires_at_ms, t.account_id);
}
fn storeSaveAt(arena: std.mem.Allocator, dir: []const u8, id: []const u8, tokens: TokenSet) !void {
const path = try storePath(arena, dir);
-
- var map: std.json.ArrayHashMap(StoredToken) = .{};
- if (std.Io.Dir.cwd().readFileAlloc(lp.io, path, arena, .limited(64 * 1024))) |data| {
- map = std.json.parseFromSliceLeaky(std.json.ArrayHashMap(StoredToken), arena, data, .{ .ignore_unknown_fields = true }) catch .{};
- } else |_| {}
+ var map = readStoreFile(arena, path);
try map.map.put(arena, id, .{
.access = tokens.access_token,
.refresh = tokens.refresh_token,
.expires_at_ms = tokens.expires_at_ms,
.account_id = tokens.account_id,
});
-
- // Secrets file: owner-only perms; temp+rename so a failed write can't
- // corrupt the store.
- var af = try std.Io.Dir.cwd().createFileAtomic(lp.io, path, .{ .permissions = .fromMode(0o600), .replace = true });
- defer af.deinit(lp.io);
- var buf: [1024]u8 = undefined;
- var w = af.file.writer(lp.io, &buf);
- try std.json.Stringify.value(map, .{}, &w.interface);
- try w.end();
- try af.replace(lp.io);
-}
-
-fn storeDeleteAt(arena: std.mem.Allocator, dir: []const u8, id: []const u8) void {
- const path = storePath(arena, dir) catch return;
- const data = std.Io.Dir.cwd().readFileAlloc(lp.io, path, arena, .limited(64 * 1024)) catch return;
- var parsed = std.json.parseFromSliceLeaky(std.json.ArrayHashMap(StoredToken), arena, data, .{ .ignore_unknown_fields = true }) catch return;
- _ = parsed.map.swapRemove(id);
- var af = std.Io.Dir.cwd().createFileAtomic(lp.io, path, .{ .permissions = .fromMode(0o600), .replace = true }) catch return;
- defer af.deinit(lp.io);
- var buf: [1024]u8 = undefined;
- var w = af.file.writer(lp.io, &buf);
- std.json.Stringify.value(parsed, .{}, &w.interface) catch return;
- w.end() catch return;
- af.replace(lp.io) catch return;
+ // Secrets file: owner-only perms.
+ try writeJsonAtomic(path, map, .fromMode(0o600));
}
/// Load the stored token for `id`, or null when absent/unreadable/no data dir.
@@ -184,30 +169,35 @@ pub const Session = struct {
allocator: std.mem.Allocator,
descriptor: *const Descriptor,
tokens: TokenSet,
-
- /// When the access token is within `refresh_skew_ms` of expiry, exchange the
- /// refresh token for a new one, persist it, and return the displaced set.
- /// Null when still fresh. The caller must repoint anything borrowing from
- /// the displaced set (`tokens` holds the fresh one) before freeing it.
- pub fn ensureFresh(self: *Session) !?TokenSet {
+ /// The set displaced by the last refresh, kept until the next one (or
+ /// deinit) because the AI client borrows the access token until the
+ /// caller repoints it.
+ displaced: ?TokenSet = null,
+
+ /// When the access token is within `refresh_skew_ms` of expiry, exchange
+ /// the refresh token for a new one and persist it. True when `tokens` was
+ /// replaced — repoint anything borrowing the old access token.
+ pub fn ensureFresh(self: *Session) !bool {
const now = nowMs();
- if (self.tokens.expires_at_ms - now > refresh_skew_ms) return null;
+ if (self.tokens.expires_at_ms - now > refresh_skew_ms) return false;
const fresh = self.descriptor.refreshFn(self.allocator, self.tokens.refresh_token) catch |err| {
// A transient refresh failure while the token is still valid is not fatal.
- if (self.tokens.expires_at_ms > now) return null;
+ if (self.tokens.expires_at_ms > now) return false;
return err;
};
- storeSave(self.allocator, self.descriptor.storeKey(), fresh) catch |err| {
- log.warn(.app, "auth token persist failed", .{ .provider = self.descriptor.storeKey(), .err = err });
+ storeSave(self.allocator, @tagName(self.descriptor.provider), fresh) catch |err| {
+ log.warn(.app, "auth token persist failed", .{ .provider = @tagName(self.descriptor.provider), .err = err });
};
- const displaced = self.tokens;
+ if (self.displaced) |old| old.deinit(self.allocator);
+ self.displaced = self.tokens;
self.tokens = fresh;
- return displaced;
+ return true;
}
pub fn deinit(self: *Session) void {
+ if (self.displaced) |old| old.deinit(self.allocator);
self.tokens.deinit(self.allocator);
}
};
@@ -215,15 +205,15 @@ pub const Session = struct {
/// Load a stored session for `provider`, or null when the user hasn't logged in.
pub fn sessionFor(allocator: std.mem.Allocator, provider: Config.AiProvider) !?Session {
const desc = descriptorFor(provider) orelse return null;
- const tokens = (try storeLoad(allocator, desc.storeKey())) orelse return null;
+ const tokens = (try storeLoad(allocator, @tagName(provider))) orelse return null;
return .{ .allocator = allocator, .descriptor = desc, .tokens = tokens };
}
/// Run the interactive login and persist the result, returning a live session.
pub fn login(allocator: std.mem.Allocator, desc: *const Descriptor, interrupt: ?*zenai.http.Interrupt) !Session {
const tokens = try desc.loginFn(allocator, interrupt);
- storeSave(allocator, desc.storeKey(), tokens) catch |err| {
- log.warn(.app, "auth token persist failed", .{ .provider = desc.storeKey(), .err = err });
+ storeSave(allocator, @tagName(desc.provider), tokens) catch |err| {
+ log.warn(.app, "auth token persist failed", .{ .provider = @tagName(desc.provider), .err = err });
};
return .{ .allocator = allocator, .descriptor = desc, .tokens = tokens };
}
@@ -232,10 +222,14 @@ pub fn login(allocator: std.mem.Allocator, desc: *const Descriptor, interrupt: ?
/// (`ensureFresh` runs before the first request)? Lets the picker offer the
/// subscription without an API-key env var.
pub fn subscriptionAvailable(provider: Config.AiProvider) bool {
- const desc = descriptorFor(provider) orelse return false;
- const tokens = (storeLoad(std.heap.page_allocator, desc.storeKey()) catch return false) orelse return false;
- defer tokens.deinit(std.heap.page_allocator);
- return tokens.refresh_token.len > 0 or tokens.expires_at_ms > nowMs();
+ if (descriptorFor(provider) == null) return false;
+ var arena: std.heap.ArenaAllocator = .init(std.heap.page_allocator);
+ defer arena.deinit();
+ const a = arena.allocator();
+ const dir = dataDir(a) orelse return false;
+ const path = storePath(a, dir) catch return false;
+ const t = readStoreFile(a, path).map.get(@tagName(provider)) orelse return false;
+ return t.refresh.len > 0 or t.expires_at_ms > nowMs();
}
test "TokenSet dup/deinit round-trips and is leak-free" {
@@ -253,7 +247,7 @@ test "descriptorFor resolves codex, null for a non-OAuth provider" {
try std.testing.expectEqual(@as(?*const Descriptor, null), descriptorFor(.openai));
}
-test "token store save/load/delete round-trips" {
+test "token store save/load round-trips" {
const a = std.testing.allocator;
var arena: std.heap.ArenaAllocator = .init(a);
defer arena.deinit();
@@ -275,7 +269,4 @@ test "token store save/load/delete round-trips" {
try std.testing.expectEqualStrings("ref-1", loaded.refresh_token);
try std.testing.expectEqualStrings("acct-9", loaded.account_id.?);
try std.testing.expectEqual(@as(i64, 999), loaded.expires_at_ms);
-
- storeDeleteAt(scratch, dir, "codex");
- try std.testing.expectEqual(@as(?TokenSet, null), try storeLoadAt(a, dir, "codex"));
}
diff --git a/src/agent/auth/codex.zig b/src/agent/auth/codex.zig
index 00e361caa..01b0cff06 100644
--- a/src/agent/auth/codex.zig
+++ b/src/agent/auth/codex.zig
@@ -136,6 +136,18 @@ const DeviceToken = struct {
code_verifier: []const u8,
};
+/// Sleep in short slices, polling `interrupt` between them: the REPL's Ctrl-C
+/// only fires the interrupt (it never kills the process).
+fn waitCancellable(ms: u64, interrupt: ?*zenai.http.Interrupt) error{LoginCancelled}!void {
+ var remaining_ms = ms;
+ while (remaining_ms > 0) {
+ if (interrupt) |it| if (it.isFired()) return error.LoginCancelled;
+ const slice_ms = @min(remaining_ms, cancel_slice_ms);
+ lp.io.sleep(.fromMilliseconds(@intCast(slice_ms)), .awake) catch {};
+ remaining_ms -= slice_ms;
+ }
+}
+
fn deviceLogin(allocator: std.mem.Allocator, interrupt: ?*zenai.http.Interrupt) !auth.TokenSet {
var arena: std.heap.ArenaAllocator = .init(allocator);
defer arena.deinit();
@@ -159,15 +171,7 @@ fn deviceLogin(allocator: std.mem.Allocator, interrupt: ?*zenai.http.Interrupt)
.{},
)});
const dt: DeviceToken = while (true) {
- // The REPL's Ctrl-C only fires the interrupt (it never kills the
- // process), so the wait must poll it.
- var remaining_ms: u64 = interval_ms + poll_margin_ms;
- while (remaining_ms > 0) {
- if (interrupt) |it| if (it.isFired()) return error.LoginCancelled;
- const slice_ms = @min(remaining_ms, cancel_slice_ms);
- lp.io.sleep(.fromMilliseconds(@intCast(slice_ms)), .awake) catch {};
- remaining_ms -= slice_ms;
- }
+ try waitCancellable(interval_ms + poll_margin_ms, interrupt);
const res = try post(a, interrupt, device_token_url, "application/json", poll_body);
switch (res.status) {
.ok => break try std.json.parseFromSliceLeaky(DeviceToken, a, res.body, .{ .ignore_unknown_fields = true }),
diff --git a/src/agent/auth/models_dev.zig b/src/agent/auth/models_dev.zig
index 3f4f5ea8d..9e3221b96 100644
--- a/src/agent/auth/models_dev.zig
+++ b/src/agent/auth/models_dev.zig
@@ -65,13 +65,7 @@ fn readCache(arena: std.mem.Allocator, app_dir: []const u8, provider_id: []const
fn writeCache(arena: std.mem.Allocator, app_dir: []const u8, provider_id: []const u8, ids: []const []const u8) !void {
const path = try cachePath(arena, app_dir, provider_id);
- var af = try std.Io.Dir.cwd().createFileAtomic(lp.io, path, .{ .replace = true });
- defer af.deinit(lp.io);
- var buf: [1024]u8 = undefined;
- var w = af.file.writer(lp.io, &buf);
- try std.json.Stringify.value(Cache{ .fetched_ms = auth.nowMs(), .ids = ids }, .{}, &w.interface);
- try w.end();
- try af.replace(lp.io);
+ try auth.writeJsonAtomic(path, Cache{ .fetched_ms = auth.nowMs(), .ids = ids }, .default_file);
}
/// Model-id keys of `catalog[provider_id].models`, filtered to tool-call-capable