Skip to content

Commit b2071bc

Browse files
committed
Feat: 移除apikey 认证
反正没人用而且核心功能依赖OAuth 产出的 JWT就删了
1 parent db80ce2 commit b2071bc

26 files changed

Lines changed: 245 additions & 549 deletions

‎Android-APP/app/src/main/assets/server_bundle/server.cjs‎

Lines changed: 28 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -110479,7 +110479,7 @@ async function runClaimCli(config, mode2) {
110479110479
const cred = await loadCredential();
110480110480
const jwt = cred?.jwt;
110481110481
if (!jwt) {
110482-
console.error("Claim requires oauth mode (no JWT stored). Run: zcode-proxy auth login <zai|bigmodel>");
110482+
console.error("Claim requires a logged-in oauth credential (no JWT stored). Run: zcode-proxy auth login <zai|bigmodel>");
110483110483
process.exit(1);
110484110484
}
110485110485
const client = createClaimClient({
@@ -110566,7 +110566,7 @@ var init_runtime = __esm({
110566110566
quota_exhausted: "daily claim quota exhausted",
110567110567
invalid_request: "invalid request",
110568110568
captcha: "captcha verification failed",
110569-
login_required: "not logged in (oauth mode required)",
110569+
login_required: "not logged in",
110570110570
http_error: "HTTP error",
110571110571
unknown: "unknown failure"
110572110572
};
@@ -110590,7 +110590,6 @@ var ENV = {
110590110590
PORT: "ZCODE_PROXY_PORT",
110591110591
PROXY_API_KEY: "ZCODE_PROXY_API_KEY",
110592110592
PROVIDER: "ZCODE_PROVIDER",
110593-
API_KEY: "ZCODE_API_KEY",
110594110593
APP_VERSION: "ZCODE_APP_VERSION",
110595110594
SOURCE_TITLE: "ZCODE_SOURCE_TITLE",
110596110595
REFERER_ORIGIN: "ZCODE_REFERER_ORIGIN",
@@ -110659,20 +110658,16 @@ function loadConfig(path2) {
110659110658
const port = resolvePort(process.env[ENV.PORT] ?? parsed?.server?.port);
110660110659
const host2 = typeof parsed?.server?.host === "string" ? parsed.server.host : DEFAULTS.HOST;
110661110660
const proxyApiKey = process.env[ENV.PROXY_API_KEY] ?? parsed?.auth?.proxyApiKey;
110662-
const mode2 = parsed?.auth?.mode === "oauth" ? "oauth" : "apikey";
110663-
const apiKey = process.env[ENV.API_KEY] ?? parsed?.auth?.apiKey;
110664110661
const oauthCredentialsPath = parsed?.auth?.oauthCredentialsPath;
110665110662
const provider = resolveProvider(process.env[ENV.PROVIDER] ?? parsed?.provider);
110666110663
const plan = resolvePlan(parsed?.plan);
110667110664
const zai = {
110668110665
anthropicBase: parsed?.providers?.zai?.anthropicBase ?? DEFAULTS.ZAI_ANTHROPIC_BASE,
110669-
openaiBase: parsed?.providers?.zai?.openaiBase ?? DEFAULTS.ZAI_OPENAI_BASE,
110670-
credential: parsed?.providers?.zai?.credential
110666+
openaiBase: parsed?.providers?.zai?.openaiBase ?? DEFAULTS.ZAI_OPENAI_BASE
110671110667
};
110672110668
const bigmodel = {
110673110669
anthropicBase: parsed?.providers?.bigmodel?.anthropicBase ?? DEFAULTS.BIGMODEL_ANTHROPIC_BASE,
110674-
openaiBase: parsed?.providers?.bigmodel?.openaiBase ?? DEFAULTS.BIGMODEL_OPENAI_BASE,
110675-
credential: parsed?.providers?.bigmodel?.credential
110670+
openaiBase: parsed?.providers?.bigmodel?.openaiBase ?? DEFAULTS.BIGMODEL_OPENAI_BASE
110676110671
};
110677110672
const defaultModel = typeof parsed?.defaultModel === "string" ? parsed.defaultModel : DEFAULTS.DEFAULT_MODEL;
110678110673
const models = Array.isArray(parsed?.models) ? parsed.models : [defaultModel];
@@ -110695,7 +110690,7 @@ function loadConfig(path2) {
110695110690
const clientSigning = resolveClientSigningConfig(parsed?.clientSigning);
110696110691
const config = {
110697110692
server: { port, host: host2 },
110698-
auth: { proxyApiKey, mode: mode2, apiKey, oauthCredentialsPath },
110693+
auth: { proxyApiKey, oauthCredentialsPath },
110699110694
provider,
110700110695
plan,
110701110696
providers: { zai, bigmodel },
@@ -110888,15 +110883,6 @@ function validate(config) {
110888110883
if (config.server.port < 1 || config.server.port > 65535) {
110889110884
throw new Error(`server.port ${config.server.port} is out of range (1-65535)`);
110890110885
}
110891-
if (config.auth.mode === "apikey") {
110892-
const hasGlobal = typeof config.auth.apiKey === "string" && config.auth.apiKey.length > 0;
110893-
const hasProvider = typeof config.providers[config.provider].credential === "string";
110894-
if (!hasGlobal && !hasProvider) {
110895-
throw new Error(
110896-
`auth.apiKey is required when auth.mode is "apikey" (or set providers.${config.provider}.credential)`
110897-
);
110898-
}
110899-
}
110900110886
if (!config.models.includes(config.defaultModel)) {
110901110887
config.models.push(config.defaultModel);
110902110888
}
@@ -110908,20 +110894,13 @@ var EXAMPLE_CONFIG_YAML = `server:
110908110894
host: "0.0.0.0"
110909110895

110910110896
auth:
110911-
# "apikey" = use a pre-obtained API key directly
110912-
# "oauth" = use OAuth login flow (run \`bun run src/index.ts auth login\` first)
110913-
mode: apikey
110914-
110915-
# For apikey mode:
110916-
# Z.AI: "yourApiKey.yourSecretKey"
110917-
# Bigmodel: "yourApiKey"
110918-
apiKey: "YOUR_API_KEY_HERE"
110919-
110920110897
# Key that clients must provide to use the proxy.
110921110898
# Set to null/omit to disable client auth.
110922110899
proxyApiKey: "your-proxy-secret"
110923110900

110924-
# For oauth mode (path to stored credentials from login flow):
110901+
# Upstream credentials come from the OAuth login flow \u2014 run this first:
110902+
# bun run src/index.ts auth login <zai|bigmodel>
110903+
# Path to the stored credentials (default shown):
110925110904
# oauthCredentialsPath: "~/.zcode-proxy/credentials.json"
110926110905

110927110906
# Which upstream provider to use: "zai" or "bigmodel"
@@ -111009,57 +110988,24 @@ logging:
111009110988
level: info
111010110989
`;
111011110990

111012-
// src/auth/apikey.ts
111013-
function createApiKeyCredential(provider, key) {
111014-
if (!key || key.trim().length === 0) {
111015-
throw new Error("API key must not be empty");
111016-
}
111017-
const trimmed = key.trim();
111018-
const dotIdx = trimmed.indexOf(".");
111019-
if (dotIdx > 0 && dotIdx < trimmed.length - 1) {
111020-
const apiKey = trimmed.slice(0, dotIdx);
111021-
const secret = trimmed.slice(dotIdx + 1);
111022-
return { apiKey, secret, provider };
111023-
}
111024-
return { apiKey: trimmed, provider };
111025-
}
111026-
111027110991
// src/auth/manager.ts
111028110992
var AuthManager = class {
111029-
mode;
111030-
provider;
111031-
cachedApiKeyCred = null;
111032110993
oauthCred = null;
111033-
constructor(opts) {
111034-
this.mode = opts.mode;
111035-
this.provider = opts.provider;
111036-
if (opts.mode === "apikey" && opts.apiKey) {
111037-
this.cachedApiKeyCred = createApiKeyCredential(this.provider, opts.apiKey);
111038-
}
111039-
}
111040110994
/** Returns the current credential, refreshing if necessary. */
111041110995
async getCredential() {
111042-
if (this.mode === "apikey") {
111043-
if (this.cachedApiKeyCred) return this.cachedApiKeyCred;
111044-
throw new Error("apikey mode configured but no credential was set");
111045-
}
111046110996
if (this.oauthCred) {
111047110997
if (this.oauthCred.expiresAt && Date.now() >= this.oauthCred.expiresAt) {
111048110998
this.oauthCred = null;
111049-
throw new Error("OAuth credential expired; re-authentication required (T9/T10 not yet implemented)");
110999+
throw new Error("OAuth credential expired; re-authentication required \u2014 run: zcode-proxy auth login");
111050111000
}
111051111001
return this.oauthCred;
111052111002
}
111053-
throw new Error("OAuth credential not available \u2014 run login flow first (T9/T10 not yet implemented)");
111003+
throw new Error("OAuth credential not available \u2014 run: zcode-proxy auth login");
111054111004
}
111055-
/** Set the OAuth credential (used by T9/T10 OAuth flow). */
111005+
/** Set the OAuth credential (used by the `auth login` flow). */
111056111006
setOAuthCredential(cred) {
111057111007
this.oauthCred = cred;
111058111008
}
111059-
/** Current auth mode. */
111060-
getMode() {
111061-
return this.mode;
111062-
}
111063111009
};
111064111010

111065111011
// src/server/server.ts
@@ -117656,7 +117602,7 @@ async function resolveCredential(opts) {
117656117602
response: errorResponse(
117657117603
400,
117658117604
"async_credentials_unavailable",
117659-
"async endpoints require oauth mode (credential lacks JWT). Re-login via `auth login` or use sync /v1/* endpoints."
117605+
"async endpoints require a logged-in oauth credential (JWT missing). Re-run `auth login` or use sync /v1/* endpoints."
117660117606
)
117661117607
};
117662117608
}
@@ -118928,39 +118874,32 @@ async function serve(configPath, debug) {
118928118874
(0, import_node_fs5.writeFileSync)(path2, EXAMPLE_CONFIG_YAML, "utf-8");
118929118875
ensureDeviceMidInConfig(path2);
118930118876
console.log(`Created ${path2} from bundled template.`);
118931-
console.log(`Edit auth.apiKey, or run: zcode-proxy auth login <zai|bigmodel>
118877+
console.log(`Run: zcode-proxy auth login <zai|bigmodel>
118932118878
`);
118933118879
}
118934118880
const config = loadConfig(path2);
118935-
const auth = new AuthManager({
118936-
mode: config.auth.mode,
118937-
provider: config.provider,
118938-
apiKey: config.auth.apiKey ?? config.providers[config.provider].credential
118939-
});
118940-
if (config.auth.mode === "oauth") {
118941-
const cred = await loadCredential();
118942-
if (!cred) {
118943-
console.error("Not logged in. Run: zcode-proxy auth login " + config.provider);
118944-
process.exit(1);
118945-
}
118946-
auth.setOAuthCredential(cred);
118881+
const auth = new AuthManager();
118882+
const cred = await loadCredential();
118883+
if (!cred) {
118884+
console.error("Not logged in. Run: zcode-proxy auth login " + config.provider);
118885+
process.exit(1);
118947118886
}
118948-
if (debug) printDebugBanner(config, path2);
118887+
auth.setOAuthCredential(cred);
118888+
if (debug) printDebugBanner(config, path2, cred);
118949118889
const server = await startServer(buildServerOptions(config, auth, debug));
118950118890
const url2 = `http://${server.hostname}:${server.port}`;
118951118891
console.log(`zcode-proxy listening on ${url2}`);
118952118892
if (config.plan === "start-plan") {
118953118893
Promise.resolve().then(() => (init_captcha(), captcha_exports)).then((m) => m.startCaptchaPool(config.identity.appVersion)).catch((err) => console.error(`[captcha] pool warmup failed: ${err.message}`));
118954118894
}
118955-
if (config.claim.enabled && config.claim.auto && config.auth.mode === "oauth") {
118895+
if (config.claim.enabled && config.claim.auto) {
118956118896
Promise.resolve().then(() => (init_runtime(), runtime_exports)).then((m) => {
118957118897
m.startAutoClaim(config, auth);
118958118898
console.log(` claim: auto ON (poll ${Math.round(config.claim.pollIntervalMs / 1e3)}s)`);
118959118899
}).catch((err) => console.error(`[claim] scheduler failed to start: ${err.message}`));
118960118900
}
118961118901
console.log(` provider: ${config.provider}`);
118962118902
console.log(` plan: ${config.plan}`);
118963-
console.log(` auth mode: ${config.auth.mode}`);
118964118903
console.log(` models: ${config.models.length} available`);
118965118904
if (config.responses.enabled) console.log(` /v1/responses: ON`);
118966118905
if (debug) console.log(` debug: ON`);
@@ -119007,19 +118946,13 @@ async function runAndroid() {
119007118946
logBuffer.push("[warn] " + args.join(" "));
119008118947
origWarn(...args);
119009118948
};
119010-
let auth = new AuthManager({
119011-
mode: config.auth.mode,
119012-
provider: config.provider,
119013-
apiKey: config.auth.apiKey ?? config.providers[config.provider].credential
119014-
});
118949+
const auth = new AuthManager();
119015118950
const serverRef = { current: null };
119016118951
async function startProxy() {
119017118952
if (serverRef.current) return { ok: false, error: "already_running" };
119018-
if (config.auth.mode === "oauth") {
119019-
const cred = await loadCredential().catch(() => null);
119020-
if (!cred) return { ok: false, error: "not_logged_in" };
119021-
auth.setOAuthCredential(cred);
119022-
}
118953+
const cred = await loadCredential().catch(() => null);
118954+
if (!cred) return { ok: false, error: "not_logged_in" };
118955+
auth.setOAuthCredential(cred);
119023118956
try {
119024118957
const s = await startServer(buildServerOptions(config, auth, false));
119025118958
serverRef.current = s;
@@ -119045,17 +118978,12 @@ async function runAndroid() {
119045118978
if (serverRef.current) return { ok: false, error: "stop_proxy_first" };
119046118979
if (changes.provider) config.provider = changes.provider;
119047118980
if (changes.plan) config.plan = changes.plan;
119048-
auth = new AuthManager({
119049-
mode: config.auth.mode,
119050-
provider: config.provider,
119051-
apiKey: config.auth.apiKey ?? config.providers[config.provider].credential
119052-
});
119053118981
updateConfigYaml(path2, { provider: config.provider, plan: config.plan });
119054118982
console.log(`config updated: provider=${config.provider} plan=${config.plan}`);
119055118983
return { ok: true, provider: config.provider, plan: config.plan };
119056118984
}
119057118985
console.log("control listener ready; proxy stopped \u2014 use startProxy command to start");
119058-
if (config.claim.enabled && config.claim.auto && config.auth.mode === "oauth") {
118986+
if (config.claim.enabled && config.claim.auto) {
119059118987
Promise.resolve().then(() => (init_runtime(), runtime_exports)).then((m) => {
119060118988
m.startAutoClaim(config, auth);
119061118989
console.log(`[claim] auto ON (poll ${Math.round(config.claim.pollIntervalMs / 1e3)}s; waits for login)`);
@@ -119095,9 +119023,8 @@ function updateConfigYaml(path2, fields) {
119095119023
parsed.plan = fields.plan;
119096119024
(0, import_node_fs5.writeFileSync)(path2, (0, import_yaml2.stringify)(parsed), "utf-8");
119097119025
}
119098-
function printDebugBanner(config, path2) {
119099-
const cred = config.providers[config.provider].credential ?? config.auth.apiKey;
119100-
const credShape = cred ? `${cred.slice(0, 6)}...${cred.slice(-4)} (${cred.length} chars)` : "(none \u2014 oauth)";
119026+
function printDebugBanner(config, path2, cred) {
119027+
const credShape = cred ? `${cred.apiKey.slice(0, 6)}...${cred.apiKey.slice(-4)} (${cred.apiKey.length} chars)` : "(none)";
119101119028
const active = config.providers[config.provider];
119102119029
console.log("=== zcode-proxy DEBUG MODE ===");
119103119030
console.log(` config file: ${path2}`);

0 commit comments

Comments
 (0)