Skip to content

fix: token+apiKey 共存時のセッション切れを修正#98

Merged
miya0001 merged 2 commits into
mainfrom
feat/proactive-token-refresh
Apr 5, 2026
Merged

fix: token+apiKey 共存時のセッション切れを修正#98
miya0001 merged 2 commits into
mainfrom
feat/proactive-token-refresh

Conversation

@miya0001

@miya0001 miya0001 commented Apr 5, 2026

Copy link
Copy Markdown
Member

Summary

  • canRefresh()&& !this.apiKey の条件により、token と apiKey が config に共存すると JWT のリフレッシュが一切発動しない問題を修正
  • リクエスト前に JWT の exp を確認し、期限切れ/期限間近なら事前にリフレッシュするプロアクティブ方式を追加

Root Cause

auth login で取得した token (JWT) と、別途設定した apiKey が同一プロファイルに共存する場合:

  • buildHeaders() は token を優先して Authorization: Bearer で送信
  • canRefresh()&& !this.apiKeyfalse を返し、リフレッシュを完全にブロック
  • JWT 期限切れ (1時間) 後、401 を受けてもリフレッシュが試みられず、セッション復旧不能

Changes

  • src/client.ts: canRefresh() の条件を修正 — apiKey-only (token なし) の場合のみリフレッシュを無効化
  • src/client.ts: proactiveRefresh() を追加 — request() / rawRequest() の前に JWT exp をチェック
  • tests/client.test.ts: token+apiKey 共存時のリフレッシュテスト、プロアクティブリフレッシュのテスト (11件追加)

Verification

  • geonic me --verbose で修正前: 401 → リフレッシュなし → エラー
  • 修正後: リフレッシュ → 200 OK を実機で確認済み

Test plan

  • lint / typecheck / 全 695 テストパス
  • token+apiKey 共存時に reactive refresh が発動するテスト
  • proactive refresh の Authorization ヘッダー・fetch 順序検証テスト
  • 実機検証: セッション切れ状態から geonic me が 200 OK で復旧

Summary by CodeRabbit

リリースノート

  • バグ修正

    • トークンとAPIキーが同時に設定されている場合にトークンの更新がブロックされ、JWT期限切れ後にセッションが復元できなくなる問題を修正。
  • 新機能

    • APIリクエスト前にJWTの有効期限を確認し、期限切れまたは近接時に事前に自動更新を試行する機能を追加。これによりリクエスト前の失敗を減らし、更新後のトークンを確実に利用します。

canRefresh() が apiKey の存在で常に false を返し、token (JWT) で認証
しているにも関わらずリフレッシュが一切発動しなかった問題を修正。
apiKey-only(token なし)の場合のみリフレッシュを無効化するように変更。

併せて、リクエスト前に JWT の exp を確認し期限切れ/期限間近なら
事前にリフレッシュするプロアクティブ方式を追加。
@coderabbitai

coderabbitai Bot commented Apr 5, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: e88203cc-4b98-47ca-a9ff-1bf7d3ef65ae

📥 Commits

Reviewing files that changed from the base of the PR and between 0edd8a2 and 50fd90f.

⛔ Files ignored due to path filters (1)
  • package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (2)
  • CHANGELOG.md
  • package.json
✅ Files skipped from review due to trivial changes (1)
  • package.json
🚧 Files skipped from review as they are similar to previous changes (1)
  • CHANGELOG.md

📝 Walkthrough

Walkthrough

リクエスト前にJWTのexpを確認して必要なら自動でリフレッシュするプロアクティブ刷新を追加し、canRefresh()の判定を修正。テストを拡充し、CHANGELOGとパッケージバージョンを更新しました。

Changes

Cohort / File(s) Summary
Core client logic
src/client.ts
getTokenStatusを利用するproactiveRefresh()を追加。request()/rawRequest()の先頭で事前リフレッシュを実行。canRefresh()判定を更新し、apiKeyのみやtokenapiKey共存のケースを扱う。
Tests
tests/client.test.ts
createJwtテストヘルパーを追加。プロアクティブリフレッシュ(期限切れ・期限切迫・不要ケース・失敗時フォールバック等)と関連動作の多数のテストを追加/修正。
Changelog
CHANGELOG.md
[0.10.1] - 2026-04-05 を追加。トークン・apiKey共存の修正とJWT有効期限チェックによる事前リフレッシュを記載。[Unreleased]比較リンクを更新。
Package metadata
package.json
パッケージバージョンを 0.10.00.10.1 に更新。

Sequence Diagram

sequenceDiagram
    autonumber
    participant Client as Client
    participant TokenMgr as Token Manager
    participant API as API Service

    Client->>Client: request()/rawRequest()開始
    Client->>TokenMgr: proactiveRefresh()
    TokenMgr->>TokenMgr: getTokenStatus(token)
    alt token is expired or expiring soon
        TokenMgr->>TokenMgr: performTokenRefresh()
        TokenMgr-->>Client: 新しいtokenを返す
    else token valid or non-JWT
        TokenMgr-->>Client: 既存tokenを返す
    end
    Client->>API: HTTPリクエスト(Authorization: Bearer <token>)
    API-->>Client: レスポンス
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Poem

🐰🌿
期限を読む耳でぴょん、
まだらな時刻をすり抜けて、
古い鍵はそっと交換し、
リクエストは途切れず進む、
新しい朝へ、跳ねる一歩。

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed PRのタイトルは、トークンとapiKeyが共存する場合のセッション切れの修正という主要な変更を正確に説明しており、簡潔で明確です。

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/proactive-token-refresh

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@CHANGELOG.md`:
- Around line 11-12: The two CHANGELOG entries mentioning the fix for
canRefresh() and the proactive JWT refresh are missing the required PR suffix;
update both lines so each ends with the PR number in parentheses like
"(`#98`)"—specifically append " (`#98`)" to the end of the line that contains
"canRefresh()" and to the line that describes the proactive JWT `exp` check so
both entries match the project's PR-suffix convention.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: be6756c1-68c7-4841-a4b7-d7f3d372e7e7

📥 Commits

Reviewing files that changed from the base of the PR and between 36f97e9 and 0edd8a2.

📒 Files selected for processing (3)
  • CHANGELOG.md
  • src/client.ts
  • tests/client.test.ts

Comment thread CHANGELOG.md Outdated
@miya0001
miya0001 merged commit 96ede30 into main Apr 5, 2026
7 checks passed
@miya0001
miya0001 deleted the feat/proactive-token-refresh branch April 5, 2026 12:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant