@@ -2,6 +2,10 @@ package desktop
22
33import (
44 "context"
5+ "log/slog"
6+ "time"
7+
8+ "github.com/golang-jwt/jwt/v5"
59)
610
711type DockerHubInfo struct {
@@ -12,9 +16,37 @@ type DockerHubInfo struct {
1216func 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+
1850func GetUserInfo (ctx context.Context ) DockerHubInfo {
1951 var info DockerHubInfo
2052 _ = ClientBackend .Get (ctx , "/registry/username" , & info )
0 commit comments