omics-os 0.0.1 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +664 -0
- package/README.md +384 -4
- package/package.json +34 -12
- package/packages/cli/dist/index.js +154 -0
- package/packages/cloud-client/dist/agents.d.ts +20 -0
- package/packages/cloud-client/dist/agents.js +69 -0
- package/packages/cloud-client/dist/apiClient.d.ts +44 -0
- package/packages/cloud-client/dist/apiClient.js +368 -0
- package/packages/cloud-client/dist/auth.d.ts +76 -0
- package/packages/cloud-client/dist/auth.js +462 -0
- package/packages/cloud-client/dist/biovault/approval.d.ts +40 -0
- package/packages/cloud-client/dist/biovault/approval.js +38 -0
- package/packages/cloud-client/dist/biovault/assurance.d.ts +10 -0
- package/packages/cloud-client/dist/biovault/assurance.js +14 -0
- package/packages/cloud-client/dist/biovault/client.d.ts +76 -0
- package/packages/cloud-client/dist/biovault/client.js +212 -0
- package/packages/cloud-client/dist/biovault/compat.d.ts +29 -0
- package/packages/cloud-client/dist/biovault/compat.js +90 -0
- package/packages/cloud-client/dist/biovault/entries.d.ts +143 -0
- package/packages/cloud-client/dist/biovault/entries.js +359 -0
- package/packages/cloud-client/dist/biovault/errors.d.ts +18 -0
- package/packages/cloud-client/dist/biovault/errors.js +90 -0
- package/packages/cloud-client/dist/biovault/fields.d.ts +7 -0
- package/packages/cloud-client/dist/biovault/fields.js +42 -0
- package/packages/cloud-client/dist/biovault/identity.d.ts +10 -0
- package/packages/cloud-client/dist/biovault/identity.js +43 -0
- package/packages/cloud-client/dist/biovault/index.d.ts +15 -0
- package/packages/cloud-client/dist/biovault/index.js +16 -0
- package/packages/cloud-client/dist/biovault/instructionMatch.d.ts +32 -0
- package/packages/cloud-client/dist/biovault/instructionMatch.js +118 -0
- package/packages/cloud-client/dist/biovault/pending.d.ts +41 -0
- package/packages/cloud-client/dist/biovault/pending.js +208 -0
- package/packages/cloud-client/dist/biovault/preCommit.d.ts +10 -0
- package/packages/cloud-client/dist/biovault/preCommit.js +10 -0
- package/packages/cloud-client/dist/biovault/provenance.d.ts +45 -0
- package/packages/cloud-client/dist/biovault/provenance.js +55 -0
- package/packages/cloud-client/dist/biovault/vendor/packageDeliveryV1.generated.d.ts +3981 -0
- package/packages/cloud-client/dist/biovault/vendor/packageDeliveryV1.generated.js +3632 -0
- package/packages/cloud-client/dist/biovault/vendor/runV1.generated.d.ts +2326 -0
- package/packages/cloud-client/dist/biovault/vendor/runV1.generated.js +491 -0
- package/packages/cloud-client/dist/biovault/wireShape.d.ts +25 -0
- package/packages/cloud-client/dist/biovault/wireShape.js +88 -0
- package/packages/cloud-client/dist/bootstrap.d.ts +4 -0
- package/packages/cloud-client/dist/bootstrap.js +19 -0
- package/packages/cloud-client/dist/config.d.ts +44 -0
- package/packages/cloud-client/dist/config.js +309 -0
- package/packages/cloud-client/dist/credentials.d.ts +96 -0
- package/packages/cloud-client/dist/credentials.js +395 -0
- package/packages/cloud-client/dist/editor.d.ts +77 -0
- package/packages/cloud-client/dist/editor.js +278 -0
- package/packages/cloud-client/dist/errors.d.ts +27 -0
- package/packages/cloud-client/dist/errors.js +102 -0
- package/packages/cloud-client/dist/exports.d.ts +19 -0
- package/packages/cloud-client/dist/exports.js +85 -0
- package/packages/cloud-client/dist/featureFlags.d.ts +10 -0
- package/packages/cloud-client/dist/featureFlags.js +36 -0
- package/packages/cloud-client/dist/files.d.ts +35 -0
- package/packages/cloud-client/dist/files.js +109 -0
- package/packages/cloud-client/dist/index.d.ts +29 -0
- package/packages/cloud-client/dist/index.js +22 -0
- package/packages/cloud-client/dist/multipartUpload.d.ts +99 -0
- package/packages/cloud-client/dist/multipartUpload.js +624 -0
- package/packages/cloud-client/dist/projects.d.ts +42 -0
- package/packages/cloud-client/dist/projects.js +119 -0
- package/packages/cloud-client/dist/resources.d.ts +8 -0
- package/packages/cloud-client/dist/resources.js +26 -0
- package/packages/cloud-client/dist/sessions.d.ts +67 -0
- package/packages/cloud-client/dist/sessions.js +187 -0
- package/packages/cloud-client/dist/templates.d.ts +4 -0
- package/packages/cloud-client/dist/templates.js +12 -0
- package/packages/cloud-client/dist/types.d.ts +431 -0
- package/packages/cloud-client/dist/types.js +94 -0
- package/packages/cloud-client/dist/uploads.d.ts +93 -0
- package/packages/cloud-client/dist/uploads.js +61 -0
- package/packages/cloud-client/dist/vault.d.ts +20 -0
- package/packages/cloud-client/dist/vault.js +43 -0
- package/packages/cloud-client/dist/vectorSearch.d.ts +3 -0
- package/packages/cloud-client/dist/vectorSearch.js +12 -0
- package/packages/tui/dist/lobster.mjs +273 -0
- package/skills/omics-os-cloud/SKILL.md +24 -0
- package/skills/omics-os-lobster/SKILL.md +24 -0
- package/bin/omics-os.js +0 -5
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/** Configuration resolution, credential management, and auth header construction. */
|
|
2
|
+
import { type AppConfig, type AuthType } from "./types.js";
|
|
3
|
+
export { validateApiOrigin } from "./credentials.js";
|
|
4
|
+
/** Read stored credentials with auto-refresh for expired OAuth tokens.
|
|
5
|
+
* Reads from `profile` when given, otherwise the active profile. */
|
|
6
|
+
export declare function readStoredCredentialsAsync(profile?: string): Promise<{
|
|
7
|
+
token: string;
|
|
8
|
+
authType: AuthType;
|
|
9
|
+
} | undefined>;
|
|
10
|
+
/** Read stored credentials synchronously (no refresh).
|
|
11
|
+
* Reads from `profile` when given, otherwise the active profile. */
|
|
12
|
+
export declare function readStoredCredentials(profile?: string): {
|
|
13
|
+
token: string;
|
|
14
|
+
authType: AuthType;
|
|
15
|
+
} | undefined;
|
|
16
|
+
/** Resolve a full AppConfig from CLI/env arguments. */
|
|
17
|
+
export declare function resolveConfig(args: {
|
|
18
|
+
apiUrl?: string;
|
|
19
|
+
streamApiUrl?: string;
|
|
20
|
+
sessionId?: string;
|
|
21
|
+
projectId?: string;
|
|
22
|
+
token?: string;
|
|
23
|
+
cloud?: boolean;
|
|
24
|
+
isResume?: boolean;
|
|
25
|
+
profile?: string;
|
|
26
|
+
}): AppConfig;
|
|
27
|
+
/**
|
|
28
|
+
* Environment variables that carry an API key, in the order read. OMICS_OS_API_KEY first: the documented name,
|
|
29
|
+
* the one the Python engine reads, so `status` and a local engine never use different keys. OMICSOS_API_KEY is the
|
|
30
|
+
* older npm name, kept as a compat alias (only this client reads it). LOBSTER_TOKEN is the legacy fallback.
|
|
31
|
+
*/
|
|
32
|
+
export declare const API_KEY_ENV: readonly ["OMICS_OS_API_KEY", "OMICSOS_API_KEY", "LOBSTER_TOKEN"];
|
|
33
|
+
/** The API key from the environment and the variable it came from (callers may print the name, never the value). */
|
|
34
|
+
export declare function envApiKey(env?: NodeJS.ProcessEnv): {
|
|
35
|
+
name: (typeof API_KEY_ENV)[number];
|
|
36
|
+
value: string;
|
|
37
|
+
} | undefined;
|
|
38
|
+
/** Build auth headers from config (sync — uses current token). */
|
|
39
|
+
export declare function authHeaders(config: AppConfig): Record<string, string>;
|
|
40
|
+
/** Build fresh auth headers, refreshing expired OAuth tokens if needed.
|
|
41
|
+
* Refuses to return stored credentials when apiUrl targets an untrusted origin. */
|
|
42
|
+
export declare function freshAuthHeaders(config: AppConfig): Promise<Record<string, string>>;
|
|
43
|
+
/** Build the SSE stream endpoint URL for a session. */
|
|
44
|
+
export declare function streamUrl(config: AppConfig, sessionId: string): string;
|
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
/** Configuration resolution, credential management, and auth header construction. */
|
|
2
|
+
import { AuthError, StoredCredentialOriginError } from "./types.js";
|
|
3
|
+
import { CLOUD_API_BASE, CLOUD_ENDPOINT, CLOUD_STREAM_BASE, LOCAL_API_BASE, endpointToApiUrl, parseOAuthCredentialResponse, readProfileCredential, saveProfileCredential, getActiveProfileName, resolveAuthBrokerApiUrl, syncRefreshedCredentialAcrossProfiles, validateApiOrigin, } from "./credentials.js";
|
|
4
|
+
export { validateApiOrigin } from "./credentials.js";
|
|
5
|
+
function isTokenExpired(creds) {
|
|
6
|
+
const expiry = creds.token_expiry;
|
|
7
|
+
if (!expiry)
|
|
8
|
+
return true;
|
|
9
|
+
try {
|
|
10
|
+
return new Date(expiry).getTime() < Date.now() + 60_000;
|
|
11
|
+
}
|
|
12
|
+
catch {
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
async function refreshOAuthToken(creds, profile) {
|
|
17
|
+
const refreshToken = creds.refresh_token;
|
|
18
|
+
const credentialId = creds.credential_id;
|
|
19
|
+
if (!refreshToken || !credentialId)
|
|
20
|
+
return undefined;
|
|
21
|
+
if (!refreshToken.startsWith("omr_"))
|
|
22
|
+
return undefined;
|
|
23
|
+
// The credential's own endpoint (e.g. a tenant subdomain) is preserved on the
|
|
24
|
+
// saved credential, but the refresh POST itself must go to the PLATFORM broker —
|
|
25
|
+
// tenant hosts gate the unauthenticated refresh path before it runs.
|
|
26
|
+
const profileEndpoint = (creds.endpoint ?? CLOUD_ENDPOINT).replace(/\/+$/, "");
|
|
27
|
+
const brokerUrl = `${resolveAuthBrokerApiUrl(endpointToApiUrl(profileEndpoint))}/gateway/oauth/cli/token`;
|
|
28
|
+
try {
|
|
29
|
+
const resp = await fetch(brokerUrl, {
|
|
30
|
+
redirect: "error",
|
|
31
|
+
method: "POST",
|
|
32
|
+
headers: { "Content-Type": "application/json" },
|
|
33
|
+
body: JSON.stringify({
|
|
34
|
+
grant_type: "refresh_token",
|
|
35
|
+
credential_id: credentialId,
|
|
36
|
+
refresh_token: refreshToken,
|
|
37
|
+
}),
|
|
38
|
+
signal: AbortSignal.timeout(15_000),
|
|
39
|
+
});
|
|
40
|
+
if (!resp.ok)
|
|
41
|
+
return undefined;
|
|
42
|
+
const data = await resp.json();
|
|
43
|
+
// Preserve the profile's endpoint — refresh must not relocate a tenant credential to the platform.
|
|
44
|
+
const refreshed = parseOAuthCredentialResponse(data, profileEndpoint);
|
|
45
|
+
if (!refreshed)
|
|
46
|
+
return undefined;
|
|
47
|
+
// Persist back to the profile the token was read from, keeping ITS endpoint.
|
|
48
|
+
const targetProfile = profile ?? getActiveProfileName();
|
|
49
|
+
const label = targetProfile === "default" ? "Platform" : targetProfile;
|
|
50
|
+
saveProfileCredential(targetProfile, { ...refreshed, endpoint: profileEndpoint }, label);
|
|
51
|
+
// The same credential may be stored under multiple profiles (tenant login
|
|
52
|
+
// writes both "default" and the tenant profile). Refresh rotates the shared
|
|
53
|
+
// refresh token, so every other profile holding this credential_id must be
|
|
54
|
+
// updated too — otherwise they keep a burned refresh token and fail next time.
|
|
55
|
+
syncRefreshedCredentialAcrossProfiles(credentialId, refreshed, targetProfile);
|
|
56
|
+
return refreshed.cli_token;
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/** One refresh in flight per credential: the server accepts a refresh token once, so a second refresh of it fails. */
|
|
63
|
+
const refreshing = new Map();
|
|
64
|
+
/** Refresh once for every concurrent caller; if that fails, use the token another process may have saved meanwhile. */
|
|
65
|
+
async function refreshShared(creds, profile) {
|
|
66
|
+
const id = creds.credential_id;
|
|
67
|
+
let pending = refreshing.get(id);
|
|
68
|
+
if (!pending) {
|
|
69
|
+
pending = refreshOAuthToken(creds, profile).finally(() => refreshing.delete(id));
|
|
70
|
+
refreshing.set(id, pending);
|
|
71
|
+
}
|
|
72
|
+
const refreshed = await pending;
|
|
73
|
+
if (refreshed)
|
|
74
|
+
return refreshed;
|
|
75
|
+
const now = readProfileCredential(profile);
|
|
76
|
+
return now?.auth_mode === "oauth" && now.credential_id === id && !isTokenExpired(now) ? now.cli_token : undefined;
|
|
77
|
+
}
|
|
78
|
+
/** Read stored credentials with auto-refresh for expired OAuth tokens.
|
|
79
|
+
* Reads from `profile` when given, otherwise the active profile. */
|
|
80
|
+
export async function readStoredCredentialsAsync(profile) {
|
|
81
|
+
return tokenFromCredential(readProfileCredential(profile), profile);
|
|
82
|
+
}
|
|
83
|
+
async function tokenFromCredential(creds, profile) {
|
|
84
|
+
if (!creds)
|
|
85
|
+
return undefined;
|
|
86
|
+
if (creds.auth_mode === "api_key" && creds.api_key.startsWith("omk_")) {
|
|
87
|
+
return { token: creds.api_key, authType: "api-key" };
|
|
88
|
+
}
|
|
89
|
+
if (creds.auth_mode === "oauth") {
|
|
90
|
+
if (isTokenExpired(creds)) {
|
|
91
|
+
// Known-expired: a fresh token is required. If refresh fails, do NOT fall
|
|
92
|
+
// back to the stale token — that just turns a clear refresh failure into a
|
|
93
|
+
// confusing 401 from the data API later. Surface "no usable credential".
|
|
94
|
+
const refreshed = await refreshShared(creds, profile);
|
|
95
|
+
if (refreshed)
|
|
96
|
+
return { token: refreshed, authType: "bearer" };
|
|
97
|
+
return undefined;
|
|
98
|
+
}
|
|
99
|
+
if (creds.cli_token)
|
|
100
|
+
return { token: creds.cli_token, authType: "bearer" };
|
|
101
|
+
return undefined;
|
|
102
|
+
}
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
105
|
+
/** Read stored credentials synchronously (no refresh).
|
|
106
|
+
* Reads from `profile` when given, otherwise the active profile. */
|
|
107
|
+
export function readStoredCredentials(profile) {
|
|
108
|
+
const creds = readProfileCredential(profile);
|
|
109
|
+
if (!creds)
|
|
110
|
+
return undefined;
|
|
111
|
+
if (creds.auth_mode === "api_key" && creds.api_key.startsWith("omk_")) {
|
|
112
|
+
return { token: creds.api_key, authType: "api-key" };
|
|
113
|
+
}
|
|
114
|
+
if (creds.auth_mode === "oauth" && creds.cli_token.startsWith("omc_")) {
|
|
115
|
+
return { token: creds.cli_token, authType: "bearer" };
|
|
116
|
+
}
|
|
117
|
+
return undefined;
|
|
118
|
+
}
|
|
119
|
+
function detectAuthType(token) {
|
|
120
|
+
if (token.startsWith("omk_"))
|
|
121
|
+
return "api-key";
|
|
122
|
+
return "bearer";
|
|
123
|
+
}
|
|
124
|
+
function deriveStreamBase(apiUrl, streamOverride) {
|
|
125
|
+
if (streamOverride)
|
|
126
|
+
return streamOverride;
|
|
127
|
+
if (apiUrl === CLOUD_API_BASE)
|
|
128
|
+
return CLOUD_STREAM_BASE;
|
|
129
|
+
return apiUrl;
|
|
130
|
+
}
|
|
131
|
+
/** Resolve a full AppConfig from CLI/env arguments. */
|
|
132
|
+
export function resolveConfig(args) {
|
|
133
|
+
const isCloud = args.cloud ?? false;
|
|
134
|
+
const storedCred = isCloud && !args.apiUrl ? readProfileCredential(args.profile) : undefined;
|
|
135
|
+
const storedEndpoint = storedCred?.endpoint;
|
|
136
|
+
const apiUrl = args.apiUrl
|
|
137
|
+
?? (storedEndpoint ? endpointToApiUrl(storedEndpoint) : (isCloud ? CLOUD_API_BASE : LOCAL_API_BASE));
|
|
138
|
+
const streamApiUrl = deriveStreamBase(apiUrl, args.streamApiUrl);
|
|
139
|
+
const isResume = args.isResume ?? false;
|
|
140
|
+
const envToken = envApiKey()?.value;
|
|
141
|
+
if (envToken) {
|
|
142
|
+
return {
|
|
143
|
+
apiUrl,
|
|
144
|
+
streamApiUrl,
|
|
145
|
+
sessionId: args.sessionId,
|
|
146
|
+
projectId: args.projectId,
|
|
147
|
+
token: envToken,
|
|
148
|
+
authType: detectAuthType(envToken),
|
|
149
|
+
tokenSource: "env",
|
|
150
|
+
isCloud,
|
|
151
|
+
isResume,
|
|
152
|
+
profile: args.profile,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
if (args.token) {
|
|
156
|
+
return {
|
|
157
|
+
apiUrl,
|
|
158
|
+
streamApiUrl,
|
|
159
|
+
sessionId: args.sessionId,
|
|
160
|
+
projectId: args.projectId,
|
|
161
|
+
token: args.token,
|
|
162
|
+
authType: detectAuthType(args.token),
|
|
163
|
+
tokenSource: "cli",
|
|
164
|
+
isCloud,
|
|
165
|
+
isResume,
|
|
166
|
+
profile: args.profile,
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
if (isCloud) {
|
|
170
|
+
const stored = readStoredCredentials(args.profile);
|
|
171
|
+
if (stored) {
|
|
172
|
+
return {
|
|
173
|
+
apiUrl,
|
|
174
|
+
streamApiUrl,
|
|
175
|
+
sessionId: args.sessionId,
|
|
176
|
+
projectId: args.projectId,
|
|
177
|
+
token: stored.token,
|
|
178
|
+
authType: stored.authType,
|
|
179
|
+
tokenSource: "stored",
|
|
180
|
+
isCloud,
|
|
181
|
+
isResume,
|
|
182
|
+
profile: args.profile,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return {
|
|
187
|
+
apiUrl,
|
|
188
|
+
streamApiUrl,
|
|
189
|
+
sessionId: args.sessionId,
|
|
190
|
+
projectId: args.projectId,
|
|
191
|
+
authType: "none",
|
|
192
|
+
tokenSource: "none",
|
|
193
|
+
isCloud,
|
|
194
|
+
isResume,
|
|
195
|
+
profile: args.profile,
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Environment variables that carry an API key, in the order read. OMICS_OS_API_KEY first: the documented name,
|
|
200
|
+
* the one the Python engine reads, so `status` and a local engine never use different keys. OMICSOS_API_KEY is the
|
|
201
|
+
* older npm name, kept as a compat alias (only this client reads it). LOBSTER_TOKEN is the legacy fallback.
|
|
202
|
+
*/
|
|
203
|
+
export const API_KEY_ENV = ["OMICS_OS_API_KEY", "OMICSOS_API_KEY", "LOBSTER_TOKEN"];
|
|
204
|
+
let warnedBothKeys = false;
|
|
205
|
+
/** The API key from the environment and the variable it came from (callers may print the name, never the value). */
|
|
206
|
+
export function envApiKey(env = process.env) {
|
|
207
|
+
const [old, documented] = [env.OMICSOS_API_KEY, env.OMICS_OS_API_KEY];
|
|
208
|
+
if (old && documented && old !== documented && !warnedBothKeys) {
|
|
209
|
+
warnedBothKeys = true;
|
|
210
|
+
process.stderr.write("OMICSOS_API_KEY and OMICS_OS_API_KEY are both set, with different values; using OMICS_OS_API_KEY.\n");
|
|
211
|
+
}
|
|
212
|
+
const name = API_KEY_ENV.find((candidate) => env[candidate]);
|
|
213
|
+
return name ? { name, value: env[name] } : undefined;
|
|
214
|
+
}
|
|
215
|
+
/** Build auth headers from config (sync — uses current token). */
|
|
216
|
+
export function authHeaders(config) {
|
|
217
|
+
if (!config.token)
|
|
218
|
+
return {};
|
|
219
|
+
switch (config.authType) {
|
|
220
|
+
case "api-key":
|
|
221
|
+
return {
|
|
222
|
+
Authorization: `Bearer ${config.token}`,
|
|
223
|
+
"X-API-Key": config.token,
|
|
224
|
+
};
|
|
225
|
+
case "bearer":
|
|
226
|
+
return { Authorization: `Bearer ${config.token}` };
|
|
227
|
+
default:
|
|
228
|
+
return {};
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
/** Build fresh auth headers, refreshing expired OAuth tokens if needed.
|
|
232
|
+
* Refuses to return stored credentials when apiUrl targets an untrusted origin. */
|
|
233
|
+
export async function freshAuthHeaders(config) {
|
|
234
|
+
if (config.tokenSource === "env" || config.tokenSource === "cli") {
|
|
235
|
+
// Explicit user-provided token (env/flag) — user accepts responsibility
|
|
236
|
+
return authHeaders(config);
|
|
237
|
+
}
|
|
238
|
+
if (!config.isCloud) {
|
|
239
|
+
return authHeaders(config);
|
|
240
|
+
}
|
|
241
|
+
// Guard: never send stored credentials to untrusted origins
|
|
242
|
+
const { safe, origin } = validateApiOrigin(config.apiUrl);
|
|
243
|
+
if (!safe) {
|
|
244
|
+
// Nothing stored: the true message is "not logged in", not a refusal to send a credential.
|
|
245
|
+
if (!config.token)
|
|
246
|
+
throw new AuthError(config.apiUrl);
|
|
247
|
+
throw new Error(`Refusing to send stored credentials to untrusted origin: ${origin}. ` +
|
|
248
|
+
`Use OMICS_OS_API_KEY env var with --endpoint, or remove --endpoint.`);
|
|
249
|
+
}
|
|
250
|
+
if (config.streamApiUrl) {
|
|
251
|
+
const stream = validateApiOrigin(config.streamApiUrl);
|
|
252
|
+
if (!stream.safe) {
|
|
253
|
+
throw new Error(`Refusing to send stored credentials to untrusted stream origin: ${stream.origin}. ` +
|
|
254
|
+
`Remove --stream-endpoint or use an explicit API key.`);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
// One read: the credential whose origin is checked is the one whose token is sent.
|
|
258
|
+
const credential = readProfileCredential(config.profile);
|
|
259
|
+
assertStoredCredentialOrigin(config, credential);
|
|
260
|
+
const stored = await tokenFromCredential(credential, config.profile);
|
|
261
|
+
if (stored) {
|
|
262
|
+
const refreshedConfig = { ...config, token: stored.token, authType: stored.authType };
|
|
263
|
+
return authHeaders(refreshedConfig);
|
|
264
|
+
}
|
|
265
|
+
// No usable stored token now (e.g. a failed refresh): never fall back to the one read when the
|
|
266
|
+
// config was resolved; it may belong to another profile than the one just checked.
|
|
267
|
+
if (config.tokenSource === "stored")
|
|
268
|
+
throw new AuthError(config.apiUrl);
|
|
269
|
+
return authHeaders(config);
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Where a stored credential may be sent: the origin it was stored for (a credential with no endpoint
|
|
273
|
+
* recorded is the platform's), and the API that issues CLI credentials (resolveAuthBrokerApiUrl: the
|
|
274
|
+
* platform with its stream host, or the local backend). Login always stores a tenant credential under
|
|
275
|
+
* the platform profile too, so the issuer sees nothing new.
|
|
276
|
+
*/
|
|
277
|
+
function storedCredentialOrigins(endpoint) {
|
|
278
|
+
const own = new URL(endpoint || CLOUD_ENDPOINT).origin;
|
|
279
|
+
const issuer = new URL(resolveAuthBrokerApiUrl(endpointToApiUrl(own))).origin;
|
|
280
|
+
return [own, issuer, ...(issuer === CLOUD_ENDPOINT ? [new URL(CLOUD_STREAM_BASE).origin] : [])];
|
|
281
|
+
}
|
|
282
|
+
const TENANT_HOST = /^([a-z0-9-]+)\.omics-os\.com$/;
|
|
283
|
+
/** "Not logged in for <host>", with the login that would cover it. */
|
|
284
|
+
function notLoggedInFor(url) {
|
|
285
|
+
const { host } = new URL(url);
|
|
286
|
+
const tenant = TENANT_HOST.exec(host)?.[1];
|
|
287
|
+
const login = host === new URL(CLOUD_ENDPOINT).host ? "omics-os login"
|
|
288
|
+
: tenant && tenant !== "stream" ? `omics-os login --tenant ${tenant}`
|
|
289
|
+
: `omics-os --endpoint ${url} login`;
|
|
290
|
+
return `Not logged in for ${host}. Run: ${login}, or set OMICS_OS_API_KEY.`;
|
|
291
|
+
}
|
|
292
|
+
/** A stored credential never goes to another origin than the one it was stored for (see storedCredentialOrigins). */
|
|
293
|
+
function assertStoredCredentialOrigin(config, credential) {
|
|
294
|
+
if (!credential) {
|
|
295
|
+
// The token in config came from a profile that is gone now (e.g. logout elsewhere): send nothing.
|
|
296
|
+
if (config.tokenSource === "stored")
|
|
297
|
+
throw new AuthError(config.apiUrl);
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
const allowed = storedCredentialOrigins(credential.endpoint);
|
|
301
|
+
for (const url of [config.apiUrl, config.streamApiUrl]) {
|
|
302
|
+
if (url && !allowed.includes(new URL(url).origin))
|
|
303
|
+
throw new StoredCredentialOriginError(url, notLoggedInFor(url));
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
/** Build the SSE stream endpoint URL for a session. */
|
|
307
|
+
export function streamUrl(config, sessionId) {
|
|
308
|
+
return `${config.streamApiUrl}/sessions/${sessionId}/chat/stream`;
|
|
309
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
export declare const CLOUD_ENDPOINT = "https://app.omics-os.com";
|
|
2
|
+
export declare const CLOUD_API_BASE = "https://app.omics-os.com/api/v1";
|
|
3
|
+
export declare const CLOUD_STREAM_BASE = "https://stream.omics-os.com/api/v1";
|
|
4
|
+
export declare const LOCAL_API_BASE = "http://localhost:8000";
|
|
5
|
+
export declare const CREDENTIALS_PATH: string;
|
|
6
|
+
export interface ApiKeyCredential {
|
|
7
|
+
auth_mode: "api_key";
|
|
8
|
+
api_key: string;
|
|
9
|
+
endpoint: string;
|
|
10
|
+
user_id?: string;
|
|
11
|
+
email?: string;
|
|
12
|
+
org_id?: string;
|
|
13
|
+
tier?: string;
|
|
14
|
+
}
|
|
15
|
+
export interface OAuthCredential {
|
|
16
|
+
auth_mode: "oauth";
|
|
17
|
+
credential_type: "omics_cli";
|
|
18
|
+
credential_id: string;
|
|
19
|
+
cli_token: string;
|
|
20
|
+
refresh_token: string;
|
|
21
|
+
token_expiry: string;
|
|
22
|
+
refresh_expiry: string;
|
|
23
|
+
endpoint: string;
|
|
24
|
+
user_id?: string;
|
|
25
|
+
email?: string;
|
|
26
|
+
org_id?: string;
|
|
27
|
+
tier?: string;
|
|
28
|
+
}
|
|
29
|
+
export type OmicsCredential = ApiKeyCredential | OAuthCredential;
|
|
30
|
+
export declare function apiUrlToEndpoint(apiUrl: string): string;
|
|
31
|
+
/** Resolve the API base for CLI OAuth broker calls (start, token, refresh,
|
|
32
|
+
* device, revoke). These are PLATFORM concerns — credentials are platform-wide
|
|
33
|
+
* (single shared Cognito pool) and tenant subdomains gate every non-public path,
|
|
34
|
+
* so an unauthenticated refresh POST to a tenant host is rejected by the tenant
|
|
35
|
+
* gate before it reaches the handler. Always route broker calls to the canonical
|
|
36
|
+
* platform API. Localhost is preserved for local backend development. */
|
|
37
|
+
export declare function resolveAuthBrokerApiUrl(apiUrl?: string): string;
|
|
38
|
+
export declare function endpointToApiUrl(endpoint: string): string;
|
|
39
|
+
export declare function isAllowedEndpoint(endpoint: string): boolean;
|
|
40
|
+
export declare function validateApiOrigin(apiUrl: string): {
|
|
41
|
+
safe: boolean;
|
|
42
|
+
origin: string;
|
|
43
|
+
};
|
|
44
|
+
export declare function readCredentialFile(): OmicsCredential | undefined;
|
|
45
|
+
export declare function saveStoredCredentials(creds: OmicsCredential): void;
|
|
46
|
+
export declare function removeStoredCredentials(): boolean;
|
|
47
|
+
export declare function parseStoredCredential(data: unknown): OmicsCredential | undefined;
|
|
48
|
+
export declare function parseOAuthCredentialResponse(data: unknown, endpoint: string): OAuthCredential | undefined;
|
|
49
|
+
export interface CredentialProfile {
|
|
50
|
+
endpoint: string;
|
|
51
|
+
label: string;
|
|
52
|
+
auth_mode: "oauth" | "api_key";
|
|
53
|
+
credential_type?: "omics_cli";
|
|
54
|
+
credential_id?: string;
|
|
55
|
+
cli_token?: string;
|
|
56
|
+
refresh_token?: string;
|
|
57
|
+
token_expiry?: string;
|
|
58
|
+
refresh_expiry?: string;
|
|
59
|
+
api_key?: string;
|
|
60
|
+
user_id?: string;
|
|
61
|
+
email?: string;
|
|
62
|
+
org_id?: string;
|
|
63
|
+
tier?: string;
|
|
64
|
+
}
|
|
65
|
+
export interface CredentialFileV2 {
|
|
66
|
+
version: 2;
|
|
67
|
+
active_profile: string;
|
|
68
|
+
profiles: Record<string, CredentialProfile>;
|
|
69
|
+
}
|
|
70
|
+
export declare function readCredentialFileV2(): CredentialFileV2 | undefined;
|
|
71
|
+
export declare function saveCredentialFileV2(file: CredentialFileV2): void;
|
|
72
|
+
export declare function getActiveProfileName(): string;
|
|
73
|
+
export declare function setActiveProfile(name: string): void;
|
|
74
|
+
export declare function listProfiles(): Array<{
|
|
75
|
+
name: string;
|
|
76
|
+
endpoint: string;
|
|
77
|
+
label: string;
|
|
78
|
+
email?: string;
|
|
79
|
+
active: boolean;
|
|
80
|
+
}>;
|
|
81
|
+
export declare function removeProfile(name: string): boolean;
|
|
82
|
+
/** Log out of a single profile, preserving siblings — mirrors Python's
|
|
83
|
+
* `_clear_active_profile`. Removes `profiles[target]` (target defaults to the
|
|
84
|
+
* active profile); if survivors remain, repoints `active_profile` to one and
|
|
85
|
+
* writes back; if it was the last profile — or the file is V1/absent — deletes
|
|
86
|
+
* the whole file. A whole-file wipe is reserved for an explicit logout --all. */
|
|
87
|
+
export declare function clearActiveProfile(profile?: string): boolean;
|
|
88
|
+
export declare function saveProfileCredential(profileName: string, creds: OmicsCredential, label: string): void;
|
|
89
|
+
/** Propagate a rotated OAuth credential to EVERY profile that shares the same
|
|
90
|
+
* credential_id, except `sourceProfile` (already saved by the caller).
|
|
91
|
+
* Tenant login stores one platform-wide credential under multiple profiles
|
|
92
|
+
* (e.g. "default" + the tenant). Refresh rotates the shared refresh token, so
|
|
93
|
+
* the other profiles must be updated or they keep a burned token. Each profile
|
|
94
|
+
* keeps its OWN endpoint and label — only the rotating token material changes. */
|
|
95
|
+
export declare function syncRefreshedCredentialAcrossProfiles(credentialId: string, refreshed: OAuthCredential, sourceProfile: string): void;
|
|
96
|
+
export declare function readProfileCredential(profileName?: string): OmicsCredential | undefined;
|