Skip to content

Commit e8069b9

Browse files
authored
Merge pull request #1269 from dgageot/jwt
Add JWT expiration check in GetToken
2 parents 2cc4bc1 + 26bf992 commit e8069b9

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ require (
2828
github.com/fsnotify/fsnotify v1.9.0
2929
github.com/go-git/go-git/v5 v5.16.4
3030
github.com/goccy/go-yaml v1.19.1
31+
github.com/golang-jwt/jwt/v5 v5.2.2
3132
github.com/google/go-containerregistry v0.20.7
3233
github.com/google/jsonschema-go v0.3.0
3334
github.com/google/uuid v1.6.0

pkg/desktop/login.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ package desktop
22

33
import (
44
"context"
5+
"log/slog"
6+
"time"
7+
8+
"github.com/golang-jwt/jwt/v5"
59
)
610

711
type DockerHubInfo struct {
@@ -12,9 +16,37 @@ type DockerHubInfo struct {
1216
func GetToken(ctx context.Context) string {
1317
var token string
1418
_ = ClientBackend.Get(ctx, "/registry/token", &token)
19+
20+
if token != "" {
21+
checkTokenExpiration(token)
22+
}
23+
1524
return token
1625
}
1726

27+
func checkTokenExpiration(token string) {
28+
// Parse the JWT without validation (we just need the claims)
29+
parsed, _, err := jwt.NewParser().ParseUnverified(token, jwt.MapClaims{})
30+
if err != nil {
31+
slog.Debug("Failed to parse JWT", "error", err)
32+
return
33+
}
34+
35+
exp, err := parsed.Claims.GetExpirationTime()
36+
if err != nil {
37+
slog.Debug("Failed to get expiration time from JWT", "error", err)
38+
return
39+
}
40+
41+
if exp == nil {
42+
slog.Debug("JWT has no expiration time")
43+
return
44+
}
45+
46+
isExpired := exp.Before(time.Now())
47+
slog.Debug("JWT expiration check", "expiration", exp.Time, "expired", isExpired)
48+
}
49+
1850
func GetUserInfo(ctx context.Context) DockerHubInfo {
1951
var info DockerHubInfo
2052
_ = ClientBackend.Get(ctx, "/registry/username", &info)

0 commit comments

Comments
 (0)