π€― LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
import { describeTokenLookup, getValidToken } from '../auth/refresh';
import { CLI_API_KEY_ENV, readCliApiKeyEnv } from '../constants/auth';
import { CLI_PRIMARY_BIN } from '../constants/identity';
import { resolveServerUrl } from '../settings';
import { log } from '../utils/logger';
import { withWorkspaceHeader } from './workspace';
export interface AuthInfo {
accessToken: string;
/** Headers required for /webapi/* endpoints (Oidc-Auth for authentication) */
headers: Record<string, string>;
serverUrl: string;
}
export async function getAuthInfo(workspaceId?: string): Promise<AuthInfo> {
const serverUrl = resolveServerUrl();
const envJwt = process.env.LOBEHUB_JWT;
if (envJwt) {
return {
accessToken: envJwt,
headers: withWorkspaceHeader(
{
'Content-Type': 'application/json',
'Oidc-Auth': envJwt,
},
workspaceId,
),
serverUrl,
};
}
const result = await getValidToken();
if (result.status !== 'ok') {
if (readCliApiKeyEnv()) {
log.error(
`API key auth from ${CLI_API_KEY_ENV} is not supported for /webapi/* routes. Run OIDC login instead.`,
);
process.exit(1);
}
const report = describeTokenLookup(result);
log.error(
report
? `${report.detail} ${report.fix}`
: `No authentication found. Run '${CLI_PRIMARY_BIN} login' first.`,
);
process.exit(1);
}
const accessToken = result.credentials.accessToken;
return {
accessToken,
headers: withWorkspaceHeader(
{
'Content-Type': 'application/json',
'Oidc-Auth': accessToken,
},
workspaceId,
),
serverUrl,
};
}
export type AgentStreamTokenType = 'jwt' | 'apiKey';
export interface AgentStreamAuthInfo {
headers: Record<string, string>;
serverUrl: string;
/**
* Raw token value (without header prefix). Used for WebSocket auth messages
* where header-based auth is not available.
*/
token: string;
/**
* How the token should be verified by downstream services (agent gateway WS).
* jwt β validate with JWKS
* apiKey β validate by calling /api/v1/users/me
*/
tokenType: AgentStreamTokenType;
}
export interface AgentStreamAuthOptions {
/**
* `false` for the auth resolver a socket reconnect calls.
*
* Exiting is right before a command has produced anything: there is no
* outcome to report and no channel to report it on. It is wrong for a
* reconnect in the middle of a run β the stored access token may still
* authenticate the journal and the status endpoint, and the caller has its
* own polling fallback. Throwing lets the transport fail as a Gateway
* problem and hand control back instead of killing the process.
*/
exitOnMissing?: boolean;
}
export async function getAgentStreamAuthInfo(
workspaceId?: string,
options: AgentStreamAuthOptions = {},
): Promise<AgentStreamAuthInfo> {
const { exitOnMissing = true } = options;
const serverUrl = resolveServerUrl();
const envJwt = process.env.LOBEHUB_JWT;
if (envJwt) {
return {
headers: withWorkspaceHeader({ 'Oidc-Auth': envJwt }, workspaceId),
serverUrl,
token: envJwt,
tokenType: 'jwt',
};
}
const envApiKey = readCliApiKeyEnv();
if (envApiKey) {
return {
headers: withWorkspaceHeader({ 'X-API-Key': envApiKey }, workspaceId),
serverUrl,
token: envApiKey,
tokenType: 'apiKey',
};
}
const result = await getValidToken();
if (result.status !== 'ok') {
const report = describeTokenLookup(result);
const message = report
? `${report.detail} ${report.fix}`
: `No authentication found. Run '${CLI_PRIMARY_BIN} login' first, or set ${CLI_API_KEY_ENV}.`;
if (!exitOnMissing) throw new Error(message);
log.error(message);
process.exit(1);
return {
headers: {},
serverUrl,
token: '',
tokenType: 'jwt',
};
}
return {
headers: withWorkspaceHeader({ 'Oidc-Auth': result.credentials.accessToken }, workspaceId),
serverUrl,
token: result.credentials.accessToken,
tokenType: 'jwt',
};
}
/**
* Auth for a stream that may reconnect mid-run: same resolution as
* {@link getAgentStreamAuthInfo}, but a missing or unrefreshable credential
* throws instead of ending the process.
*/
export const resolveAgentStreamAuthInfo = (workspaceId?: string) =>
getAgentStreamAuthInfo(workspaceId, { exitOnMissing: false });