mirror of
https://github.com/windmill-labs/windmill.git
synced 2026-12-03 08:10:27 +00:00
* feat: make new tokens workspace-bound and rights-free unless opted in Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: honour withheld token rights in devops, instance db and webhook rotation Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * chore: bump ee ref Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: name token admin explicitly in every token insert Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: carry withheld token rights through caller JWTs and keep nonmember superadmin mints working Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * chore: bump ee ref Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * chore: update ee-repo-ref to 74c3a4ce446d8d9abd69c69da5be8bfdb6d775f4 This commit updates the EE repository reference after PR #1018 was merged in windmill-ee-private. Previous ee-repo-ref: 28e9391cff06b84fff9bd420a8b244c35588523a New ee-repo-ref: 74c3a4ce446d8d9abd69c69da5be8bfdb6d775f4 Automated by sync-ee-ref workflow. * feat: make the instance-wide token choice explicit in the token form Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * feat: rename the token scope switch to restrict to specific scopes Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: refuse mcp oauth approval from a token withholding admin, warn on empty scope restriction Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * feat: workspace choice on the cli login page and hints for workspace-bound cli tokens Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: bind a cli login token only to the workspace the cli names Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: windmill-internal-app[bot] <windmill-internal-app[bot]@users.noreply.github.com>
154 lines
6.7 KiB
TypeScript
154 lines
6.7 KiB
TypeScript
import { colors } from "@cliffy/ansi/colors";
|
|
import * as log from "./log.ts";
|
|
import { setClient } from "./client.ts";
|
|
import * as wmill from "../../gen/services.gen.ts";
|
|
import { GlobalUserInfo, User } from "../../gen/types.gen.ts";
|
|
|
|
import { loginInteractive, tryGetLoginInfo } from "./login.ts";
|
|
import { GlobalOptions } from "../types.ts";
|
|
|
|
|
|
// Workspace-scoped tokens (tokens bound to a workspace via token.workspace_id)
|
|
// cannot call /api/users/whoami because the backend rejects any token with a
|
|
// workspace binding when the request path has no workspace in it. Fall back to
|
|
// the workspace-scoped whoami endpoint in that case so the CLI still works.
|
|
// Uses a name-based ApiError check rather than `instanceof` to match the
|
|
// pattern in cli/src/main.ts: bundling (bun build for npm, JSR dev path) can
|
|
// produce multiple module instances of gen/core/ApiError.ts, making
|
|
// `instanceof` silently return false and reintroducing the bug this fixes.
|
|
async function fetchWhoami(workspaceId: string): Promise<GlobalUserInfo> {
|
|
try {
|
|
return await wmill.globalWhoami();
|
|
} catch (error) {
|
|
if (
|
|
error && typeof error === "object" &&
|
|
"name" in error && (error as { name: unknown }).name === "ApiError" &&
|
|
(error as { status?: number }).status === 401
|
|
) {
|
|
const user = await wmill.whoami({ workspace: workspaceId });
|
|
return workspaceUserToGlobalUserInfo(user);
|
|
}
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
// Adapter for the 401 fallback path. `login_type`, `verified`, `first_time_user`
|
|
// and `role_source` are NOT derivable from the workspace-scoped User response
|
|
// and are filled with best-effort defaults — do not trust them downstream after
|
|
// a fallback whoami. Today only cli/src/commands/hub/hub.ts reads this return
|
|
// value, and only `.email`.
|
|
function workspaceUserToGlobalUserInfo(user: User): GlobalUserInfo {
|
|
return {
|
|
email: user.email,
|
|
login_type: "password",
|
|
super_admin: user.is_super_admin,
|
|
verified: true,
|
|
name: user.name,
|
|
username: user.username,
|
|
operator_only: user.operator,
|
|
first_time_user: false,
|
|
role_source: "manual",
|
|
disabled: user.disabled,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Main authentication function - moved from context.ts to break circular dependencies
|
|
* This function maintains the original API signature from context.ts
|
|
*/
|
|
export async function requireLogin(
|
|
opts: GlobalOptions
|
|
): Promise<GlobalUserInfo> {
|
|
// Import resolveWorkspace to avoid circular dependency at module level
|
|
const { resolveWorkspace } = await import("./context.ts");
|
|
const workspace = await resolveWorkspace(opts);
|
|
|
|
let token = await tryGetLoginInfo(opts);
|
|
|
|
if (!token) {
|
|
token = workspace.token;
|
|
}
|
|
|
|
setClient(token, workspace.remote.substring(0, workspace.remote.length - 1));
|
|
|
|
try {
|
|
return await fetchWhoami(workspace.workspaceId);
|
|
} catch (error) {
|
|
// Check for network errors and provide clearer messages
|
|
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
if (errorMsg.includes('fetch') || errorMsg.includes('connection') || errorMsg.includes('ECONNREFUSED') || errorMsg.includes('refused')) {
|
|
throw new Error(`Network error: Could not connect to Windmill server at ${workspace.remote}`);
|
|
}
|
|
|
|
// If the user explicitly provided credentials via flags, fail immediately
|
|
// rather than falling back to interactive login — they expect their explicit
|
|
// credentials to work and should fix them if they don't.
|
|
if (opts.token || opts.baseUrl) {
|
|
const isApiError = error && typeof error === "object" &&
|
|
"name" in error && (error as { name: unknown }).name === "ApiError";
|
|
if (isApiError) {
|
|
const status = (error as { status?: number }).status;
|
|
const body = (error as { body?: unknown }).body;
|
|
let bodyStr = typeof body === "object" && body !== null
|
|
? JSON.stringify(body)
|
|
: String(body ?? "").trim();
|
|
// Strip backend source-file refs like "(flows.rs:1400)" or
|
|
// "@scopes.rs:509" from the surfaced message — same pattern used in
|
|
// main.ts for ApiError display.
|
|
bodyStr = bodyStr.replace(/\s*[@(]\w+\.rs:\d+[:\d]*\)?/g, "");
|
|
// The backend's `Error::PermissionDenied` formats with a
|
|
// "Permission denied: " prefix; the new CLI message also leads with
|
|
// that phrase, so strip it from the body to avoid duplication.
|
|
bodyStr = bodyStr.replace(/^(Permission denied|Not authorized): /, "");
|
|
if (status === 403) {
|
|
// 403 means the token authenticated but lacks scope — re-issuing
|
|
// won't help. Keep this distinct from the 401 message so the user
|
|
// doesn't waste time reproducing the token.
|
|
log.infoStderr(colors.red(
|
|
`Permission denied: the token is valid but lacks the required scope.${bodyStr ? `\n${bodyStr}` : ""}`
|
|
));
|
|
} else if (status === 401) {
|
|
log.infoStderr(colors.red(
|
|
`Could not authenticate with the provided credentials. Please check your --token and --base-url and try again.${bodyStr ? `\n${bodyStr}` : ""}`
|
|
));
|
|
} else {
|
|
log.infoStderr(colors.red(
|
|
`Request failed (${status ?? "unknown"}): ${bodyStr}`
|
|
));
|
|
}
|
|
return process.exit(1);
|
|
}
|
|
log.infoStderr(colors.red("Could not authenticate with the provided credentials. Please check your --token and --base-url and try again."));
|
|
return process.exit(1);
|
|
}
|
|
|
|
const isUnauthorized = error && typeof error === "object" &&
|
|
"name" in error && (error as { name: unknown }).name === "ApiError" &&
|
|
(error as { status?: number }).status === 401;
|
|
log.infoStderr(colors.yellow(
|
|
isUnauthorized
|
|
? `! The token of workspace profile '${workspace.name}' has expired or was revoked. Sign in again to continue.`
|
|
: "! Could not reach API given existing credentials. Attempting to reauth..."
|
|
));
|
|
const newToken = await loginInteractive(workspace.remote, workspace.workspaceId);
|
|
if (!newToken) {
|
|
throw new Error(
|
|
`Unauthorized: Could not authenticate with the provided credentials. Run 'wmill workspace login ${workspace.name}' in a terminal to sign in again.`
|
|
);
|
|
}
|
|
|
|
// Removed silently first: `addWorkspace` logs the update to stdout, which would land in the
|
|
// output of the command being run (e.g. `wmill variable get … > file`).
|
|
const { removeWorkspace, addWorkspace } = await import("../commands/workspace/workspace.ts");
|
|
await removeWorkspace(workspace.name, true, opts);
|
|
workspace.token = newToken;
|
|
await addWorkspace(workspace, opts);
|
|
|
|
setClient(
|
|
newToken,
|
|
workspace.remote.substring(0, workspace.remote.length - 1)
|
|
);
|
|
return await fetchWhoami(workspace.workspaceId);
|
|
}
|
|
}
|