pingcode-cli-unofficial 2.2.0 → 2.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.
@@ -3426,7 +3426,7 @@ var VERSION;
3426
3426
  var init_version = __esm({
3427
3427
  "src/version.ts"() {
3428
3428
  "use strict";
3429
- VERSION = "2.2.0";
3429
+ VERSION = "2.3.0";
3430
3430
  }
3431
3431
  });
3432
3432
 
@@ -3611,12 +3611,33 @@ function coerceConfig(raw) {
3611
3611
  if (token !== void 0) config.token = token;
3612
3612
  const userToken = coerceUserToken(record.userToken);
3613
3613
  if (userToken !== void 0) config.userToken = userToken;
3614
+ const patToken = coercePatToken(record.patToken);
3615
+ if (patToken !== void 0) config.patToken = patToken;
3614
3616
  const authMode = asAuthMode(record.authMode);
3615
3617
  if (authMode !== void 0) config.authMode = authMode;
3616
3618
  return config;
3617
3619
  }
3618
3620
  function asAuthMode(raw) {
3619
- return raw === "enterprise" || raw === "user" ? raw : void 0;
3621
+ return raw === "enterprise" || raw === "user" || raw === "pat" ? raw : void 0;
3622
+ }
3623
+ function normalizeToken(raw) {
3624
+ return raw.trim().replace(/^Bearer\s+/i, "").trim();
3625
+ }
3626
+ function coercePatToken(raw) {
3627
+ if (typeof raw !== "object" || raw === null) return void 0;
3628
+ const record = raw;
3629
+ if (asAuthMode(record.kind) !== "pat") return void 0;
3630
+ const base = coerceTokenBase(record);
3631
+ if (base === void 0) return void 0;
3632
+ const token = {
3633
+ ...base,
3634
+ accessToken: normalizeToken(base.accessToken),
3635
+ kind: "pat",
3636
+ unrefreshable: true
3637
+ };
3638
+ const scope = asString(record.scope);
3639
+ if (scope !== void 0) token.scope = scope;
3640
+ return token;
3620
3641
  }
3621
3642
  function coerceUserToken(raw) {
3622
3643
  if (typeof raw !== "object" || raw === null) return void 0;
@@ -3673,8 +3694,32 @@ function resolveSettings(input) {
3673
3694
  readEnv(env, ENV_USER_CLIENT_SECRET),
3674
3695
  file.userClientSecret
3675
3696
  ).value;
3676
- const authMode = inferAuthMode(file);
3677
- const activeToken = authMode === "user" ? file.userToken : file.token;
3697
+ const envTokenRaw = readEnv(env, ENV_TOKEN);
3698
+ const envToken = envTokenRaw === void 0 ? void 0 : normalizeToken(envTokenRaw);
3699
+ const authMode = envToken !== void 0 ? "pat" : inferAuthMode(file);
3700
+ let activeToken;
3701
+ let tokenSource;
3702
+ const envPatToken = envToken === void 0 ? void 0 : {
3703
+ accessToken: envToken,
3704
+ expiresAtMs: 0,
3705
+ obtainedAtMs: 0,
3706
+ kind: "pat",
3707
+ unrefreshable: true
3708
+ };
3709
+ if (envPatToken !== void 0) {
3710
+ activeToken = envPatToken;
3711
+ tokenSource = "env";
3712
+ } else if (authMode === "pat") {
3713
+ activeToken = file.patToken;
3714
+ tokenSource = activeToken === void 0 ? "none" : "file";
3715
+ } else if (authMode === "user") {
3716
+ activeToken = file.userToken;
3717
+ tokenSource = activeToken === void 0 ? "none" : "file";
3718
+ } else {
3719
+ activeToken = file.token;
3720
+ tokenSource = activeToken === void 0 ? "none" : "file";
3721
+ }
3722
+ const tokenRefreshable = activeToken !== void 0 && activeToken.unrefreshable !== true && activeToken.kind !== "pat" && authMode !== "pat";
3678
3723
  return {
3679
3724
  host,
3680
3725
  apiBase,
@@ -3683,6 +3728,9 @@ function resolveSettings(input) {
3683
3728
  userClientId,
3684
3729
  userClientSecret,
3685
3730
  token: activeToken,
3731
+ patToken: envPatToken ?? file.patToken,
3732
+ tokenSource,
3733
+ tokenRefreshable,
3686
3734
  authMode,
3687
3735
  sources: {
3688
3736
  host: hostSource,
@@ -3694,6 +3742,7 @@ function resolveSettings(input) {
3694
3742
  }
3695
3743
  function inferAuthMode(file) {
3696
3744
  if (file.authMode !== void 0) return file.authMode;
3745
+ if (file.patToken !== void 0) return "pat";
3697
3746
  if (file.userToken !== void 0) return "user";
3698
3747
  if (file.token !== void 0) return "enterprise";
3699
3748
  return "user";
@@ -3732,7 +3781,7 @@ function isNotFound(error) {
3732
3781
  function errorMessage(error) {
3733
3782
  return error instanceof Error ? error.message : String(error);
3734
3783
  }
3735
- var DEFAULT_HOST, CONFIG_DIR_MODE, CONFIG_FILE_MODE, ENV_CONFIG_DIR, ENV_HOST, ENV_CLIENT_ID, ENV_CLIENT_SECRET, ENV_USER_CLIENT_ID, ENV_USER_CLIENT_SECRET;
3784
+ var DEFAULT_HOST, CONFIG_DIR_MODE, CONFIG_FILE_MODE, ENV_CONFIG_DIR, ENV_HOST, ENV_CLIENT_ID, ENV_CLIENT_SECRET, ENV_USER_CLIENT_ID, ENV_USER_CLIENT_SECRET, ENV_TOKEN;
3736
3785
  var init_config = __esm({
3737
3786
  "src/core/config.ts"() {
3738
3787
  "use strict";
@@ -3747,6 +3796,7 @@ var init_config = __esm({
3747
3796
  ENV_CLIENT_SECRET = "PINGCODE_CLIENT_SECRET";
3748
3797
  ENV_USER_CLIENT_ID = "PINGCODE_USER_CLIENT_ID";
3749
3798
  ENV_USER_CLIENT_SECRET = "PINGCODE_USER_CLIENT_SECRET";
3799
+ ENV_TOKEN = "PINGCODE_TOKEN";
3750
3800
  }
3751
3801
  });
3752
3802
 
@@ -3828,10 +3878,10 @@ function buildContext(input) {
3828
3878
  const override = input.credentials;
3829
3879
  const activeClientId = mode === "user" ? override?.clientId ?? settings2.userClientId ?? settings2.clientId : override?.clientId ?? settings2.clientId;
3830
3880
  const activeClientSecret = mode === "user" ? override?.clientSecret ?? settings2.userClientSecret ?? settings2.clientSecret : override?.clientSecret ?? settings2.clientSecret;
3831
- const activeToken = mode === "user" ? file.userToken : file.token;
3881
+ const activeToken = mode === "pat" ? settings2.patToken : mode === "user" ? file.userToken : file.token;
3832
3882
  const ctx = createContext({
3833
3883
  apiBase: settings2.apiBase,
3834
- credentials: { clientId: activeClientId, clientSecret: activeClientSecret },
3884
+ credentials: mode === "pat" ? {} : { clientId: activeClientId, clientSecret: activeClientSecret },
3835
3885
  auth: {
3836
3886
  token: activeToken,
3837
3887
  clampWarned: false,
@@ -3845,7 +3895,8 @@ function buildContext(input) {
3845
3895
  env,
3846
3896
  // Mode-aware persist (D5): route by the token's kind to the right slot and
3847
3897
  // stamp the active mode. saveConfig re-reads + merges, so this writes only
3848
- // the owned field (never a whole stale Config).
3898
+ // the owned field (never a whole stale Config). A PAT is never refreshed, so
3899
+ // this only ever fires from `auth login --mode pat`.
3849
3900
  persistToken: (token) => {
3850
3901
  if (token.kind === "user") {
3851
3902
  saveConfig(
@@ -3855,6 +3906,8 @@ function buildContext(input) {
3855
3906
  },
3856
3907
  env
3857
3908
  );
3909
+ } else if (token.kind === "pat") {
3910
+ saveConfig({ patToken: { ...token, kind: "pat", unrefreshable: true }, authMode: "pat" }, env);
3858
3911
  } else {
3859
3912
  saveConfig({ token, authMode: "enterprise" }, env);
3860
3913
  }
@@ -5108,8 +5161,37 @@ async function refreshUserToken(ctx, refreshToken) {
5108
5161
  ctx.persistToken?.(token);
5109
5162
  return token;
5110
5163
  }
5164
+ function setPersonalAccessToken(ctx, raw, options = {}) {
5165
+ const accessToken = normalizeToken(raw);
5166
+ if (accessToken === "") {
5167
+ throw new AuthError("the personal access token is empty", { hint: PAT_HINT });
5168
+ }
5169
+ const now = ctx.now();
5170
+ const token = {
5171
+ accessToken,
5172
+ expiresAtMs: 0,
5173
+ obtainedAtMs: now,
5174
+ kind: "pat",
5175
+ unrefreshable: true
5176
+ };
5177
+ ctx.auth.token = token;
5178
+ ctx.auth.mode = "pat";
5179
+ if (options.persist !== false) ctx.persistToken?.(token);
5180
+ return token;
5181
+ }
5182
+ function personalAccessToken(ctx, force) {
5183
+ const accessToken = ctx.auth.token?.accessToken ?? "";
5184
+ if (accessToken === "") {
5185
+ throw new AuthError("no personal access token configured", { hint: PAT_HINT });
5186
+ }
5187
+ if (force) {
5188
+ throw new AuthError("PingCode rejected the personal access token (401)", { hint: PAT_HINT });
5189
+ }
5190
+ return accessToken;
5191
+ }
5111
5192
  async function ensureFreshToken(ctx, options = {}) {
5112
5193
  const force = options.force === true;
5194
+ if (ctx.auth.mode === "pat") return personalAccessToken(ctx, force);
5113
5195
  if (force) {
5114
5196
  ctx.auth.inflight = void 0;
5115
5197
  if (ctx.auth.mode !== "user") ctx.auth.token = void 0;
@@ -5124,6 +5206,11 @@ async function ensureFreshToken(ctx, options = {}) {
5124
5206
  return token.accessToken;
5125
5207
  }
5126
5208
  async function acquireForMode(ctx) {
5209
+ if (ctx.auth.mode === "pat") {
5210
+ throw new AuthError("a personal access token cannot be acquired or refreshed", {
5211
+ hint: PAT_HINT
5212
+ });
5213
+ }
5127
5214
  if (ctx.auth.mode === "user") {
5128
5215
  const refreshToken = ctx.auth.token?.refreshToken;
5129
5216
  if (refreshToken === void 0) {
@@ -5143,16 +5230,18 @@ function clearToken(ctx) {
5143
5230
  ctx.auth.token = void 0;
5144
5231
  ctx.auth.inflight = void 0;
5145
5232
  }
5146
- var THIRTY_DAYS_MS, REFRESH_WINDOW_MS, TOKEN_PATH;
5233
+ var THIRTY_DAYS_MS, REFRESH_WINDOW_MS, TOKEN_PATH, PAT_HINT;
5147
5234
  var init_auth = __esm({
5148
5235
  "src/core/auth.ts"() {
5149
5236
  "use strict";
5237
+ init_config();
5150
5238
  init_errors();
5151
5239
  init_redact();
5152
5240
  init_wire();
5153
5241
  THIRTY_DAYS_MS = 30 * 24 * 3600 * 1e3;
5154
5242
  REFRESH_WINDOW_MS = 12e4;
5155
5243
  TOKEN_PATH = "/v1/auth/token";
5244
+ PAT_HINT = "personal access tokens cannot be renewed by this CLI: check that it is not expired or revoked, that its scopes cover this endpoint, or create a new token in PingCode personal settings and run `pingcode auth login --mode pat --token <token>` (or set PINGCODE_TOKEN)";
5156
5245
  }
5157
5246
  });
5158
5247
 
@@ -8015,11 +8104,11 @@ function isPlaceholder2(segment) {
8015
8104
  }
8016
8105
  function refuseUserTokenEndpoint(ctx, candidates) {
8017
8106
  if (!candidates.every((entry2) => entry2.tokenType === "USER")) return;
8018
- if (ctx.auth.mode === "user") return;
8107
+ if (ctx.auth.mode === "user" || ctx.auth.mode === "pat") return;
8019
8108
  const entry = candidates[0];
8020
8109
  if (entry === void 0) return;
8021
8110
  throw new UsageError(
8022
- `${entry.method} ${entry.path} requires a \u7528\u6237\u4EE4\u724C (user token), but the active token is an \u4F01\u4E1A\u4EE4\u724C (enterprise) \u2014 run \`pingcode auth login --mode user\` to hold a user token`,
8111
+ `${entry.method} ${entry.path} requires a \u7528\u6237\u4EE4\u724C (user token), but the active token is an \u4F01\u4E1A\u4EE4\u724C (enterprise) \u2014 run \`pingcode auth login --mode user\`, or use a personal access token with --mode pat / PINGCODE_TOKEN`,
8023
8112
  {
8024
8113
  hint: "seven endpoints are user-token-only \u2014 /v1/myself, /v1/permission/my/* and /v1/permission/check/*. GET /v1/permission/points does work with the enterprise token. " + AUTHORIZE_HINT
8025
8114
  }
@@ -9904,12 +9993,15 @@ var init_oauth = __esm({
9904
9993
  // src/cli/commands/auth.ts
9905
9994
  import { createInterface } from "readline/promises";
9906
9995
  function registerAuthCommands(program2) {
9907
- const auth = program2.command("auth").description("authenticate against PingCode (enterprise app or user token)");
9996
+ const auth = program2.command("auth").description("authenticate against PingCode (enterprise app, user token or personal access token)");
9908
9997
  addGlobalOptions(
9909
9998
  auth.command("login").description("acquire a token (user token by default) and verify it").option("--client-id <id>", "app client id (or PINGCODE_CLIENT_ID)").option("--client-secret <secret>", "app client secret (or PINGCODE_CLIENT_SECRET)").option("--save", "also store the client id/secret in the config file (mode 0600)").option(
9910
9999
  "--mode <mode>",
9911
- "auth mode: user (authorization_code, default) or enterprise (client_credentials)"
9912
- ).option("--code <code>", "authorization code (skips the interactive paste)"),
10000
+ "auth mode: user (authorization_code, default), enterprise (client_credentials) or pat (personal access token)"
10001
+ ).option("--code <code>", "authorization code (skips the interactive paste)").option(
10002
+ "--token <token>",
10003
+ "personal access token for --mode pat (or PINGCODE_TOKEN; skips the hidden prompt)"
10004
+ ),
9913
10005
  { hidden: true }
9914
10006
  ).action(async (flags, command) => {
9915
10007
  await runLogin(flags, command);
@@ -9921,7 +10013,7 @@ function registerAuthCommands(program2) {
9921
10013
  await runStatus(flags, command);
9922
10014
  });
9923
10015
  addGlobalOptions(
9924
- auth.command("logout").description("clear the active token slot (both with --all) and the metadata cache").option("--all", "clear both the enterprise and user token slots (and authMode)"),
10016
+ auth.command("logout").description("clear the active token slot (all three with --all) and the metadata cache").option("--all", "clear the enterprise, user and personal-token slots (and authMode)"),
9925
10017
  { hidden: true }
9926
10018
  ).action((flags, command) => {
9927
10019
  runLogout(flags, command);
@@ -9930,6 +10022,10 @@ function registerAuthCommands(program2) {
9930
10022
  async function runLogin(flags, command) {
9931
10023
  const probe = contextFor(command);
9932
10024
  const authMode = await resolveMode(flags.mode, probe.ctx.json);
10025
+ if (authMode === "pat") {
10026
+ await runLoginPat(flags, command);
10027
+ return;
10028
+ }
9933
10029
  let built = contextFor(
9934
10030
  command,
9935
10031
  { clientId: flags.clientId, clientSecret: flags.clientSecret },
@@ -9983,12 +10079,52 @@ async function runLogin(flags, command) {
9983
10079
  clearMetadataCache(ctx);
9984
10080
  finishLogin(built, flags, { authMode, tokenKind, user, verifiedWith, token });
9985
10081
  }
10082
+ async function runLoginPat(flags, command) {
10083
+ const built = contextFor(command, void 0, "pat");
10084
+ const { ctx, settings: settings2 } = built;
10085
+ const stored = ctx.auth.token?.accessToken;
10086
+ const provided = flags.token !== void 0 && flags.token !== "" ? flags.token : void 0;
10087
+ const source = provided !== void 0 ? "flag" : stored === void 0 ? "prompt" : settings2.tokenSource === "env" ? "env" : "file";
10088
+ const raw = provided ?? stored ?? await loginHooks.readToken(ctx.json);
10089
+ clearToken(ctx);
10090
+ const token = setPersonalAccessToken(ctx, raw, { persist: false });
10091
+ const verified = await verifyPatToken(ctx);
10092
+ ctx.persistToken?.(token);
10093
+ clearMetadataCache(ctx);
10094
+ finishLogin(built, flags, {
10095
+ authMode: "pat",
10096
+ tokenKind: "pat",
10097
+ tokenSource: source,
10098
+ token,
10099
+ ...verified
10100
+ });
10101
+ }
10102
+ async function verifyPatToken(ctx) {
10103
+ try {
10104
+ const me = await getMyself(ctx);
10105
+ const user = { id: me.id };
10106
+ if (me.name !== void 0) user.name = me.name;
10107
+ if (me.display_name !== void 0) user.display_name = me.display_name;
10108
+ if (me.username !== void 0) user.username = me.username;
10109
+ return { verifiedWith: "GET /v1/myself", user };
10110
+ } catch (error) {
10111
+ if (!(error instanceof PermissionError)) throw error;
10112
+ }
10113
+ try {
10114
+ const projectsTotal = await verifyAccess(ctx);
10115
+ return { verifiedWith: "GET /v1/pjm/projects?page_size=1", projectsTotal };
10116
+ } catch (error) {
10117
+ if (!(error instanceof PermissionError)) throw error;
10118
+ return {};
10119
+ }
10120
+ }
9986
10121
  function finishLogin(built, flags, outcome) {
9987
10122
  const { settings: settings2, file } = built;
9988
10123
  const save = flags.save === true;
9989
10124
  const patch = { host: settings2.host };
9990
10125
  let storeId = false;
9991
10126
  let storeSecret = false;
10127
+ const isPat = outcome.authMode === "pat";
9992
10128
  if (outcome.authMode === "user") {
9993
10129
  const userId = flags.clientId ?? settings2.userClientId;
9994
10130
  if (userId !== void 0) {
@@ -10008,16 +10144,25 @@ function finishLogin(built, flags, outcome) {
10008
10144
  storeSecret = true;
10009
10145
  }
10010
10146
  }
10011
- } else {
10147
+ } else if (!isPat) {
10012
10148
  storeId = shouldPersistSecret(settings2.sources.clientId, save);
10013
10149
  storeSecret = shouldPersistSecret(settings2.sources.clientSecret, save);
10014
10150
  if (storeId) patch.clientId = settings2.clientId;
10015
10151
  if (storeSecret) patch.clientSecret = settings2.clientSecret;
10016
10152
  }
10017
10153
  saveConfig(patch, process.env);
10018
- const alreadyInFile = outcome.authMode === "user" ? file.userClientId !== void 0 && file.userClientSecret !== void 0 : file.clientId !== void 0 && file.clientSecret !== void 0;
10154
+ const alreadyInFile = isPat ? true : outcome.authMode === "user" ? file.userClientId !== void 0 && file.userClientSecret !== void 0 : file.clientId !== void 0 && file.clientSecret !== void 0;
10019
10155
  const credentialsStored = alreadyInFile || storeId && storeSecret;
10020
10156
  const mode = modeOf(built.ctx);
10157
+ const effectiveSource = isPat ? outcome.tokenSource ?? "none" : "file";
10158
+ const refreshable = !isPat;
10159
+ if (isPat && outcome.verifiedWith === void 0) {
10160
+ errLine(
10161
+ paint.yellow(
10162
+ "stored, but no granted scope allowed a live check \u2014 a 401 will still fail hard, and `auth status --check` will tell you when that happens"
10163
+ )
10164
+ );
10165
+ }
10021
10166
  if (mode.json) {
10022
10167
  printJson({
10023
10168
  ok: true,
@@ -10025,33 +10170,42 @@ function finishLogin(built, flags, outcome) {
10025
10170
  kind: outcome.tokenKind,
10026
10171
  host: settings2.host,
10027
10172
  api_base: settings2.apiBase,
10028
- client_id: maskIdentifier(settings2.clientId),
10029
- credentials_stored: credentialsStored,
10173
+ client_id: isPat ? null : maskIdentifier(settings2.clientId),
10174
+ credentials_stored: isPat ? false : credentialsStored,
10030
10175
  ...outcome.user === void 0 ? {} : { user: outcome.user },
10031
10176
  ...outcome.projectsTotal === void 0 ? {} : { projects_total: outcome.projectsTotal },
10032
- token_expires_at: Math.floor(outcome.token.expiresAtMs / 1e3),
10033
- verified_with: outcome.verifiedWith
10177
+ // A PAT's validity is set in PingCode and unknown to us — never invent one.
10178
+ token_expires_at: isPat ? null : Math.floor(outcome.token.expiresAtMs / 1e3),
10179
+ token_source: effectiveSource,
10180
+ token_refreshable: refreshable,
10181
+ verified_with: outcome.verifiedWith ?? null
10034
10182
  });
10035
10183
  return;
10036
10184
  }
10037
10185
  const fields = [
10038
10186
  ["host", settings2.host],
10039
- ["api base", settings2.apiBase],
10040
- ["client id", maskIdentifier(settings2.clientId)],
10041
- ["mode", outcome.authMode],
10042
- ["token kind", outcome.tokenKind]
10187
+ ["api base", settings2.apiBase]
10043
10188
  ];
10189
+ if (isPat) {
10190
+ fields.push(["token source", effectiveSource]);
10191
+ } else {
10192
+ fields.push(["client id", maskIdentifier(settings2.clientId)]);
10193
+ }
10194
+ fields.push(["mode", outcome.authMode], ["token kind", outcome.tokenKind]);
10044
10195
  if (outcome.user !== void 0) {
10045
10196
  fields.push(["user", outcome.user.display_name ?? outcome.user.name ?? outcome.user.username ?? outcome.user.id]);
10046
10197
  }
10047
10198
  if (outcome.projectsTotal !== void 0) {
10048
10199
  fields.push(["projects visible", String(outcome.projectsTotal)]);
10049
10200
  }
10050
- fields.push(["token expires", formatTimestamp(Math.floor(outcome.token.expiresAtMs / 1e3))]);
10201
+ fields.push([
10202
+ "token expires",
10203
+ isPat ? "unknown \u2014 set by the token in PingCode personal settings" : formatTimestamp(Math.floor(outcome.token.expiresAtMs / 1e3))
10204
+ ]);
10051
10205
  fields.push(["config file", configFilePath(process.env)]);
10052
10206
  printFields(fields);
10053
- errLine(paint.green("authenticated"));
10054
- if (!credentialsStored) {
10207
+ errLine(paint.green(outcome.verifiedWith === void 0 ? "token stored" : "authenticated"));
10208
+ if (!isPat && !credentialsStored) {
10055
10209
  errLine(
10056
10210
  paint.dim(
10057
10211
  "the client id/secret were not written to disk (pass --save to store them); the token was stored, but a re-login will be needed once it expires"
@@ -10060,9 +10214,9 @@ function finishLogin(built, flags, outcome) {
10060
10214
  }
10061
10215
  }
10062
10216
  async function resolveMode(flag, json) {
10063
- if (flag === "enterprise" || flag === "user") return flag;
10217
+ if (flag === "enterprise" || flag === "user" || flag === "pat") return flag;
10064
10218
  if (flag !== void 0) {
10065
- throw new UsageError(`--mode must be "enterprise" or "user", got "${flag}"`);
10219
+ throw new UsageError(`--mode must be "enterprise", "user" or "pat", got "${flag}"`);
10066
10220
  }
10067
10221
  return await loginHooks.selectMode(json);
10068
10222
  }
@@ -10076,12 +10230,13 @@ async function runStatus(flags, command) {
10076
10230
  const { ctx, settings: settings2, file } = contextFor(command);
10077
10231
  const mode = modeOf(ctx);
10078
10232
  const token = settings2.token;
10079
- const expiresAt = token === void 0 ? void 0 : Math.floor(token.expiresAtMs / 1e3);
10233
+ const expiresAt = token === void 0 || !settings2.tokenRefreshable ? void 0 : Math.floor(token.expiresAtMs / 1e3);
10080
10234
  const authMode = settings2.authMode;
10081
- const tokenKind = token?.kind ?? "enterprise";
10235
+ const tokenKind = token?.kind ?? (authMode === "pat" ? "pat" : "enterprise");
10236
+ const fresh = settings2.tokenRefreshable ? tokenIsFresh(token, ctx.now()) : null;
10082
10237
  let check;
10083
10238
  if (flags.check === true) {
10084
- if (authMode === "user") {
10239
+ if (authMode === "user" || authMode === "pat") {
10085
10240
  const me = await getMyself(ctx);
10086
10241
  check = { ok: true, endpoint: "GET /v1/myself", user: me.display_name ?? me.name ?? me.username ?? me.id };
10087
10242
  } else {
@@ -10102,39 +10257,57 @@ async function runStatus(flags, command) {
10102
10257
  credentials_stored: file.clientId !== void 0 && file.clientSecret !== void 0,
10103
10258
  token_present: token !== void 0,
10104
10259
  token_kind: tokenKind,
10105
- token_fresh: tokenIsFresh(token, ctx.now()),
10260
+ token_source: settings2.tokenSource,
10261
+ token_refreshable: settings2.tokenRefreshable,
10262
+ token_fresh: fresh,
10106
10263
  token_expires_at: expiresAt ?? null,
10107
10264
  token_scope: token?.scope ?? null,
10108
10265
  // User-slot refresh-token presence (AC8) — boolean only, never the value.
10109
10266
  user_token_present: file.userToken !== void 0,
10110
10267
  user_refresh_token_present: file.userToken?.refreshToken !== void 0,
10268
+ // Personal-token slot presence — boolean only, never the value.
10269
+ pat_token_present: file.patToken !== void 0,
10270
+ env_token_present: settings2.tokenSource === "env" && authMode === "pat",
10111
10271
  config_file: configFilePath(process.env),
10112
10272
  ...check === void 0 ? {} : { check }
10113
10273
  });
10114
10274
  return;
10115
10275
  }
10116
- printFields([
10276
+ const fields = [
10117
10277
  ["host", settings2.host],
10118
- ["api base", settings2.apiBase],
10119
- ["client id", `${maskIdentifier(settings2.clientId)} (${settings2.sources.clientId})`],
10120
- [
10121
- "client secret",
10122
- settings2.clientSecret === void 0 ? "(not set)" : `(set, ${settings2.sources.clientSecret})`
10123
- ],
10278
+ ["api base", settings2.apiBase]
10279
+ ];
10280
+ if (authMode === "pat") {
10281
+ fields.push(["token source", settings2.tokenSource]);
10282
+ } else {
10283
+ fields.push(
10284
+ ["client id", `${maskIdentifier(settings2.clientId)} (${settings2.sources.clientId})`],
10285
+ [
10286
+ "client secret",
10287
+ settings2.clientSecret === void 0 ? "(not set)" : `(set, ${settings2.sources.clientSecret})`
10288
+ ]
10289
+ );
10290
+ }
10291
+ fields.push(
10124
10292
  ["auth mode", authMode],
10125
10293
  [
10126
10294
  "token",
10127
- token === void 0 ? "(none) \u2014 run `pingcode auth login`" : tokenIsFresh(token, ctx.now()) ? `present (${tokenKind})` : `present but stale (${tokenKind}) \u2014 it will be re-acquired on the next call`
10295
+ token === void 0 ? "(none) \u2014 run `pingcode auth login`" : fresh === null ? `present (${tokenKind}; not refreshable)` : fresh ? `present (${tokenKind})` : `present but stale (${tokenKind}) \u2014 it will be re-acquired on the next call`
10296
+ ],
10297
+ [
10298
+ "token expires",
10299
+ expiresAt === void 0 ? authMode === "pat" && token !== void 0 ? "unknown \u2014 set by the token in PingCode personal settings" : "" : formatTimestamp(expiresAt)
10128
10300
  ],
10129
- ["token expires", expiresAt === void 0 ? "" : formatTimestamp(expiresAt)],
10130
10301
  ["token scope", token?.scope ?? ""],
10131
10302
  ["user slot", file.userToken === void 0 ? "(none)" : "present (with refresh token)"],
10303
+ ["personal token slot", file.patToken === void 0 ? "(none)" : "present (not refreshable)"],
10132
10304
  ["config file", configFilePath(process.env)],
10133
10305
  [
10134
10306
  "live check",
10135
10307
  check === void 0 ? "(skipped \u2014 pass --check)" : check.endpoint === "GET /v1/myself" ? `ok \xB7 signed in as ${check.user} via ${check.endpoint}` : `ok \xB7 ${check.projects_total} project(s) visible via ${check.endpoint}`
10136
10308
  ]
10137
- ]);
10309
+ );
10310
+ printFields(fields);
10138
10311
  }
10139
10312
  function runLogout(flags, command) {
10140
10313
  const { ctx, settings: settings2 } = contextFor(command);
@@ -10143,14 +10316,38 @@ function runLogout(flags, command) {
10143
10316
  let cleared;
10144
10317
  if (all) {
10145
10318
  saveConfig(
10146
- { host: settings2.host, token: null, userToken: null, authMode: null, clientId: null, clientSecret: null },
10319
+ {
10320
+ host: settings2.host,
10321
+ token: null,
10322
+ userToken: null,
10323
+ patToken: null,
10324
+ authMode: null,
10325
+ clientId: null,
10326
+ clientSecret: null,
10327
+ userClientId: null,
10328
+ userClientSecret: null
10329
+ },
10147
10330
  process.env
10148
10331
  );
10149
- cleared = ["token", "user_token", "auth_mode", "client_id", "client_secret", "metadata_cache"];
10332
+ cleared = [
10333
+ "token",
10334
+ "user_token",
10335
+ "pat_token",
10336
+ "auth_mode",
10337
+ "client_id",
10338
+ "client_secret",
10339
+ "user_client_id",
10340
+ "user_client_secret",
10341
+ "metadata_cache"
10342
+ ];
10150
10343
  } else {
10151
- const slot = settings2.authMode === "user" ? "userToken" : "token";
10344
+ const slot = settings2.authMode === "pat" ? "patToken" : settings2.authMode === "user" ? "userToken" : "token";
10152
10345
  saveConfig({ [slot]: null, authMode: null }, process.env);
10153
- cleared = [slot === "userToken" ? "user_token" : "token", "auth_mode", "metadata_cache"];
10346
+ cleared = [
10347
+ slot === "patToken" ? "pat_token" : slot === "userToken" ? "user_token" : "token",
10348
+ "auth_mode",
10349
+ "metadata_cache"
10350
+ ];
10154
10351
  }
10155
10352
  clearToken(ctx);
10156
10353
  clearMetadataCache(ctx);
@@ -10165,8 +10362,10 @@ function runLogout(flags, command) {
10165
10362
  errLine(`cleared ${cleared.join(", ")} (${configFilePath(process.env)})`);
10166
10363
  }
10167
10364
  function missingCredential(what) {
10365
+ const flag = what === "client id" ? "--client-id" : what === "personal access token" ? "--token" : "--client-secret";
10366
+ const env = what === "client id" ? "PINGCODE_CLIENT_ID" : what === "personal access token" ? "PINGCODE_TOKEN" : "PINGCODE_CLIENT_SECRET";
10168
10367
  return new UsageError(`no ${what} available`, {
10169
- hint: `pass --${what === "client id" ? "client-id" : "client-secret"}, set PINGCODE_${what === "client id" ? "CLIENT_ID" : "CLIENT_SECRET"}, or run the command from a terminal to be prompted`
10368
+ hint: `pass ${flag}, set ${env}, or run the command from a terminal to be prompted`
10170
10369
  });
10171
10370
  }
10172
10371
  async function promptVisible(question, json) {
@@ -10180,9 +10379,9 @@ async function promptVisible(question, json) {
10180
10379
  rl.close();
10181
10380
  }
10182
10381
  }
10183
- async function promptHidden(question, json) {
10382
+ async function promptHidden(question, json, what = "client secret") {
10184
10383
  const input = process.stdin;
10185
- if (json || input.isTTY !== true) throw missingCredential("client secret");
10384
+ if (json || input.isTTY !== true) throw missingCredential(what);
10186
10385
  process.stderr.write(question);
10187
10386
  input.setRawMode(true);
10188
10387
  input.resume();
@@ -10218,7 +10417,7 @@ async function promptHidden(question, json) {
10218
10417
  input.on("data", onData);
10219
10418
  });
10220
10419
  const trimmed = value.trim();
10221
- if (trimmed === "") throw missingCredential("client secret");
10420
+ if (trimmed === "") throw missingCredential(what);
10222
10421
  return trimmed;
10223
10422
  }
10224
10423
  async function readPasteFromTerminal(json) {
@@ -10234,6 +10433,12 @@ async function readPasteFromTerminal(json) {
10234
10433
  rl.close();
10235
10434
  }
10236
10435
  }
10436
+ async function readTokenFromTerminal(json) {
10437
+ if (json || process.stdin.isTTY !== true) throw missingCredential("personal access token");
10438
+ const token = await promptHidden("personal access token: ", json, "personal access token");
10439
+ if (token.trim() === "") throw missingCredential("personal access token");
10440
+ return token;
10441
+ }
10237
10442
  async function promptChoice(question, choices, defaultKey) {
10238
10443
  const rl = createInterface({ input: process.stdin, output: process.stderr });
10239
10444
  try {
@@ -10260,6 +10465,7 @@ async function defaultSelectMode(json) {
10260
10465
  if (json || process.stdin.isTTY !== true) return "user";
10261
10466
  return await promptChoice("auth mode", [
10262
10467
  { key: "user", label: "authorization_code \u2014 acts as you (default)" },
10468
+ { key: "pat", label: "personal access token \u2014 paste a token, no app needed" },
10263
10469
  { key: "enterprise", label: "client_credentials \u2014 app/admin identity" }
10264
10470
  ], "user");
10265
10471
  }
@@ -10280,7 +10486,8 @@ var init_auth2 = __esm({
10280
10486
  init_common();
10281
10487
  loginHooks = {
10282
10488
  selectMode: defaultSelectMode,
10283
- readPaste: readPasteFromTerminal
10489
+ readPaste: readPasteFromTerminal,
10490
+ readToken: readTokenFromTerminal
10284
10491
  };
10285
10492
  }
10286
10493
  });