11import fs from "fs"
22import path from "path"
3- import { rm } from "node:fs/promises"
3+ import fsp , { rm } from "node:fs/promises"
44import { createHash } from "crypto"
55import { Global } from "../../global"
66import { Filesystem } from "../../util/filesystem"
@@ -38,11 +38,6 @@ export function cachePath(): string {
3838 return path . join ( Global . Path . home , ".altimate" , "telemetry-identity.json" )
3939}
4040
41- /** True when a credential is in use (file or auth store), whatever its validity. */
42- export async function hasCredential ( ) : Promise < boolean > {
43- return ( await loadCredential ( ) ) !== undefined
44- }
45-
4641/** The credential the Altimate provider would use, or undefined when signed out. */
4742export async function loadCredential ( ) : Promise < Credential | undefined > {
4843 try {
@@ -69,7 +64,8 @@ export async function loadCredential(): Promise<Credential | undefined> {
6964/**
7065 * A cheap stamp of the two credential files (mtime and size), so a flush can notice that a
7166 * sign-in, sign-out or key change happened on disk, in this thread or another, without
72- * reading or parsing anything.
67+ * reading or parsing anything. Synchronous on purpose: two `stat` calls on small local files,
68+ * once per flush (every 5 s at most), cost less than the async round trips would.
7369 */
7470export function credentialStamp ( ) : string {
7571 const stamp = ( p : string ) => {
@@ -83,6 +79,9 @@ export function credentialStamp(): string {
8379 return `${ stamp ( AltimateApi . credentialsPath ( ) ) } |${ stamp ( Auth . filepath ( ) ) } `
8480}
8581
82+ /** A cache key, not a secret: 64 bits of SHA-256 over a high-entropy API key leave no realistic
83+ * collision between the credentials one machine sees, and the cache holds nothing the key's
84+ * owner could not already read. */
8685function fingerprint ( c : Credential ) : string {
8786 return createHash ( "sha256" )
8887 . update ( `${ c . altimateUrl } \n${ c . altimateInstanceName } \n${ c . altimateApiKey } ` )
@@ -124,36 +123,53 @@ export async function cached(): Promise<Identity | undefined> {
124123 * keeps it. The answer is written and returned only if the same credential is still in use.
125124 */
126125export async function resolve ( opts : { refresh ?: boolean } = { } ) : Promise < Identity | undefined > {
126+ return ( await resolveWithOutcome ( opts ) ) . identity
127+ }
128+
129+ /**
130+ * How a resolution ended: `ok` with an identity; `none` when no credential is stored (or it
131+ * changed during the request); `rejected` when the backend refused the credential (401/403),
132+ * which only a new credential can change; `error` for a transient failure worth retrying.
133+ */
134+ export type Outcome = "ok" | "none" | "rejected" | "error"
135+
136+ /** {@link resolve }, also saying how it ended, so a caller retries only a transient failure. */
137+ export async function resolveWithOutcome (
138+ opts : { refresh ?: boolean } = { } ,
139+ ) : Promise < { identity ?: Identity ; outcome : Outcome } > {
127140 try {
128141 const creds = await loadCredential ( )
129142 if ( ! creds ) {
130143 await clear ( )
131- return undefined
144+ return { outcome : "none" }
132145 }
133146 const fp = fingerprint ( creds )
134147 if ( ! opts . refresh ) {
135148 const hit = await readCache ( )
136- if ( hit && hit . fingerprint === fp ) return { user_id : hit . user_id , tenant : hit . tenant }
149+ if ( hit && hit . fingerprint === fp )
150+ return { identity : { user_id : hit . user_id , tenant : hit . tenant } , outcome : "ok" }
137151 }
138152 const answer = await fetchMe ( creds )
139153 if ( answer . kind === "rejected" ) {
140154 const hit = await readCache ( )
141155 if ( hit && hit . fingerprint === fp ) await clear ( )
142- return undefined
156+ return { outcome : "rejected" }
143157 }
144- if ( answer . kind !== "ok" ) return undefined
158+ if ( answer . kind !== "ok" ) return { outcome : "error" }
145159 const now = await loadCredential ( )
146- if ( ! now || fingerprint ( now ) !== fp ) return undefined
147- const identity : Identity = { user_id : answer . uuid , tenant : creds . altimateInstanceName }
160+ if ( ! now || fingerprint ( now ) !== fp ) return { outcome : "none" }
161+ const identity : Identity = { user_id : answer . uuid , tenant : now . altimateInstanceName }
148162 try {
163+ // The directory holds the machine id and this cache: owner-only when this creates it.
164+ await fsp . mkdir ( path . dirname ( cachePath ( ) ) , { recursive : true , mode : 0o700 } )
149165 await Filesystem . writeJson ( cachePath ( ) , { ...identity , fingerprint : fp } satisfies Cached , 0o600 )
150166 } catch ( err ) {
151167 log . warn ( "telemetry identity cache not written" , { err : String ( err ) } )
152168 }
153- return identity
169+ return { identity, outcome : "ok" }
154170 } catch ( err ) {
155171 log . warn ( "telemetry identity unresolved" , { err : String ( err ) } )
156- return undefined
172+ return { outcome : "error" }
157173 }
158174}
159175
0 commit comments