Skip to content

Commit b487ba5

Browse files
committed
refactor: persist public key in postgres
Signed-off-by: Felix Gateru <[email protected]>
1 parent c7e25d7 commit b487ba5

File tree

6 files changed

+366
-163
lines changed

6 files changed

+366
-163
lines changed

auth/keymanager.go

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,57 @@
44
package auth
55

66
import (
7+
"context"
8+
"time"
9+
710
"github.com/lestrrat-go/jwx/v2/jwk"
811
"github.com/lestrrat-go/jwx/v2/jwt"
912
)
1013

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.
1234
type KeyManager interface {
1335
SignJWT(token jwt.Token) ([]byte, error)
1436

1537
ParseJWT(token string) (jwt.Token, error)
1638

1739
PublicJWKS() []jwk.Key
1840

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
2060
}

0 commit comments

Comments
 (0)