Something went wrong. Try again.
Sign git commits and tags with an OpenPGP key kept in Bitwarden.
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! Reading fields out of Bitwarden's JSON without caring how they are cased.//!//! The official server answers in camelCase. Vaultwarden has answered in//! PascalCase, camelCase, or both depending on the endpoint and the release://! the token response still carries `Key` and `KdfIterations` beside//! `access_token`. So every lookup here tries the exact name and then any//! name that differs only in case.
const std = @import("std");const Value = std.json.Value;const testing = std.testing;
/// The field `name` of `v`, or null when `v` is not an object, has no such/// field, or has it set to JSON null.pub fn get(v: Value, name: []const u8) ?Value { const obj = switch (v) { .object => |o| o, else => return null, }; const found = obj.get(name) orelse blk: { var it = obj.iterator(); while (it.next()) |entry| { if (std.ascii.eqlIgnoreCase(entry.key_ptr.*, name)) break :blk entry.value_ptr.*; } return null; }; return if (found == .null) null else found;}
pub fn getString(v: Value, name: []const u8) ?[]const u8 { return switch (get(v, name) orelse return null) { .string => |s| s, else => null, };}
pub fn getInt(v: Value, name: []const u8) ?i64 { return switch (get(v, name) orelse return null) { .integer => |i| i, else => null, };}
pub fn getArray(v: Value, name: []const u8) ?[]const Value { return switch (get(v, name) orelse return null) { .array => |a| a.items, else => null, };}
test "fields are found whatever their case, and null is absent" { const gpa = testing.allocator; const parsed = try std.json.parseFromSlice(Value, gpa, \\{"Key":"2.a|b|c","kdfIterations":600000,"Profile":{"Email":"x@y"},"notes":null,"Ciphers":[1,2]} , .{}); defer parsed.deinit(); const v = parsed.value;
try testing.expectEqualStrings("2.a|b|c", getString(v, "key").?); try testing.expectEqual(@as(?i64, 600000), getInt(v, "KdfIterations")); try testing.expectEqualStrings("x@y", getString(get(v, "profile").?, "email").?); try testing.expectEqual(@as(?[]const u8, null), getString(v, "notes")); try testing.expectEqual(@as(?[]const u8, null), getString(v, "missing")); try testing.expectEqual(@as(usize, 2), getArray(v, "ciphers").?.len); // The wrong type is as good as missing. try testing.expectEqual(@as(?i64, null), getInt(v, "key"));}