|
4 | 4 | package auth |
5 | 5 |
|
6 | 6 | import ( |
| 7 | + "context" |
| 8 | + "time" |
| 9 | + |
7 | 10 | "github.com/lestrrat-go/jwx/v2/jwk" |
8 | 11 | "github.com/lestrrat-go/jwx/v2/jwt" |
9 | 12 | ) |
10 | 13 |
|
11 | | -// KeyManager is the high-level contract the Auth service depends on. |
| 14 | +// PublicKeyStatus represents the status of a public key. |
| 15 | +type PublicKeyStatus int |
| 16 | + |
| 17 | +const ( |
| 18 | + // ActiveKeyStatus indicates the key is active and can be used for verification. |
| 19 | + ActiveKeyStatus PublicKeyStatus = iota |
| 20 | + // RetiredKeyStatus indicates the key is retired but still valid for verification during grace period. |
| 21 | + RetiredKeyStatus |
| 22 | +) |
| 23 | + |
| 24 | +// PublicKey represents a public key stored in the database. |
| 25 | +type PublicKey struct { |
| 26 | + Kid string `json:"kid" db:"kid"` |
| 27 | + JWKData jwk.Key `json:"jwk_data" db:"jwk_data"` |
| 28 | + CreatedAt time.Time `json:"created_at" db:"created_at"` |
| 29 | + RetiredAt *time.Time `json:"retired_at,omitempty" db:"retired_at"` |
| 30 | + Status PublicKeyStatus `json:"status" db:"status"` |
| 31 | +} |
| 32 | + |
| 33 | +// KeyManager represents a manager for JWT keys. |
12 | 34 | type KeyManager interface { |
13 | 35 | SignJWT(token jwt.Token) ([]byte, error) |
14 | 36 |
|
15 | 37 | ParseJWT(token string) (jwt.Token, error) |
16 | 38 |
|
17 | 39 | PublicJWKS() []jwk.Key |
18 | 40 |
|
19 | | - Rotate() error |
| 41 | + Rotate(ctx context.Context) error |
| 42 | +} |
| 43 | + |
| 44 | +// PublicKeyRepository represents a repository for storing and retrieving public keys. |
| 45 | +type PublicKeyRepository interface { |
| 46 | + // Save stores a public key in the database. |
| 47 | + Save(ctx context.Context, key PublicKey) error |
| 48 | + |
| 49 | + // Retrieve gets a public key by its ID. |
| 50 | + Retrieve(ctx context.Context, kid string) (PublicKey, error) |
| 51 | + |
| 52 | + // RetrieveActive gets all active public keys. |
| 53 | + RetrieveActive(ctx context.Context) ([]PublicKey, error) |
| 54 | + |
| 55 | + // Retire marks a public key as retired. |
| 56 | + Retire(ctx context.Context, kid string) error |
| 57 | + |
| 58 | + // PurgeExpired removes all expired public keys from the database. |
| 59 | + PurgeExpired(ctx context.Context, expiredBefore time.Time) error |
20 | 60 | } |
0 commit comments