<p align="center">
<strong>ECDSA JWT WITH REDIS LIFECYCLE AND DEVICE BINDING</strong>
</p>

<p align="center">
<a href="https://pkg.go.dev/github.com/pardnchiu/go-jwt/core"><img src="https://img.shields.io/badge/GO-REFERENCE-blue?include_prereleases&style=for-the-badge" alt="Go Reference"></a>
<a href="https://github.com/pardnchiu/go-jwt/releases"><img src="https://img.shields.io/github/v/tag/pardnchiu/go-jwt?include_prereleases&style=for-the-badge" alt="Release"></a>
<a href="https://github.com/pardnchiu/go-jwt/blob/main/LICENSE"><img src="https://img.shields.io/github/license/pardnchiu/go-jwt?include_prereleases&style=for-the-badge" alt="License"></a>
</p>

<p align="center">
<a href="https://github.com/avelino/awesome-go"><img src="https://awesome.re/mentioned-badge.svg" height="32" alt="Mentioned in Awesome Go"></a>
</p>

---

> リフレッシュトークンのローテーション、Redis による失効管理、Gin ミドルウェアを備えた Go JWT 認証ライブラリ

## 目次

- [機能](#機能)
- [アーキテクチャ](#アーキテクチャ)
- [ライセンス](#ライセンス)
- [作者](#作者)

## 機能

> `go get github.com/pardnchiu/go-jwt@latest` · インポートパス `github.com/pardnchiu/go-jwt/core`

- **Redis で制御するトークンライフサイクル** — Access Token は ES256 署名と Redis の JTI ホワイトリストの両方を通過する必要があるため、ログアウト時には失効レコードが書き込まれ、JWT の有効期限を待たずに即座に反映されます。
- **デバイスフィンガープリントによるバインド** — OS、ブラウザ、デバイス種別、デバイス ID から算出した SHA-256 フィンガープリントが Access Token と Refresh ID の両方をバインドするため、盗まれたトークンは別のデバイスでは失敗します。
- **ロックで保護された透過的リフレッシュ** — 期限切れのトークンは Refresh ID から自動的に再署名され、`SETNX` ロックと Lua の比較削除によるアンロックで、インスタンスをまたいでも Refresh ID ごとにリフレッシュは 1 回だけ実行されます。
- **バージョン付き Refresh ID ローテーション** — リフレッシュ回数が `MaxVersion` を超えるか残り TTL がしきい値を下回るとトークンペア全体を再発行し、それ以外は Access Token のみを再署名して Redis への書き込みを抑えます。
- **ES256 鍵の自動生成と 2 種類のミドルウェア** — 鍵はファイルパスまたはインライン PEM から読み込み、なければ初回起動時に P-256 鍵ペアを生成します。Gin と `net/http` のミドルウェアをそのまま組み込めます。

## アーキテクチャ

```mermaid
graph TB
    REQ[HTTP Request] --> MW[Gin / net/http Middleware]
    MW --> V[Verify]
    V --> FP[Device Fingerprint]
    V -->|Signature + JTI valid| OK[Return Auth]
    V -->|Access Token expired or missing| RF[Refresh]
    RF -->|Below threshold| RS[Re-sign Access Token]
    RF -->|Over MaxVersion / TTL threshold| CR[Create full reissue]
    V --> REDIS[(Redis)]
    RF --> REDIS
```

## ライセンス

このプロジェクトは [MIT LICENSE](https://github.com/pardnchiu/go-jwt/blob/main/LICENSE) の下でライセンスされています。

## 作者

アイデアがあれば気軽に [issue を作成](https://github.com/pardnchiu/go-jwt/issues/new)してください。

<a href="https://github.com/pardnchiu/go-jwt/graphs/contributors">
  <img src="https://contrib.rocks/image?repo=pardnchiu/go-jwt&cache_bust=2026-10-07" alt="go-jwt contributors" />
</a>

---

©️ 2025 [邱敬幃 Pardn Chiu](https://www.linkedin.com/in/pardnchiu)
