@@ -2,6 +2,7 @@ package testenv
22
33import (
44 "context"
5+ "encoding/base64"
56 "encoding/json"
67 "fmt"
78 "net/http"
@@ -150,15 +151,47 @@ func (l *loadedEnv) metadataServer(seed *config.Config) *httptest.Server {
150151 Message : "Wrong Authorization header" ,
151152 })
152153 }
154+ // try parse expiry date from JWT token
155+ exp , err := l .parseExpiryDate (ctx , accessToken )
156+ if err != nil {
157+ logger .Errorf (ctx , "parse expiry date: %s" , err )
158+ exp = time .Now ().Add (2 * time .Minute ).Unix ()
159+ }
153160 l .replyJson (ctx , w , 200 , msiToken {
154161 TokenType : tokenType ,
155162 AccessToken : accessToken ,
156- // TODO: get the real expiry of the token (if we can)
157- ExpiresOn : json .Number (fmt .Sprint (time .Now ().Add (2 * time .Minute ).Unix ())),
163+ ExpiresOn : json .Number (fmt .Sprint (exp )),
158164 })
159165 }))
160166}
161167
168+ func (l * loadedEnv ) parseExpiryDate (ctx context.Context , tokenString string ) (int64 , error ) {
169+ parts := strings .Split (tokenString , "." )
170+ if len (parts ) != 3 {
171+ return 0 , fmt .Errorf ("invalid token format" )
172+ }
173+ payload , err := base64 .RawURLEncoding .DecodeString (parts [1 ])
174+ if err != nil {
175+ return 0 , fmt .Errorf ("payload: %v" , err )
176+ }
177+ var claims map [string ]interface {}
178+ err = json .Unmarshal (payload , & claims )
179+ if err != nil {
180+ return 0 , fmt .Errorf ("json: %v" , err )
181+ }
182+ exp , ok := claims ["exp" ].(float64 )
183+ if ok {
184+ logger .Debugf (ctx , "exp is float64: %d" , exp )
185+ return int64 (exp ), nil
186+ }
187+ expInt , ok := claims ["exp" ].(int64 )
188+ if ok {
189+ logger .Debugf (ctx , "exp is int64: %d" , expInt )
190+ return expInt , nil
191+ }
192+ return 0 , fmt .Errorf ("not found" )
193+ }
194+
162195func (l * loadedEnv ) replyJson (ctx context.Context , w http.ResponseWriter , status int , body any ) {
163196 msg := "<token response>"
164197 apiErrBody , ok := body .(apierr.APIErrorBody )
0 commit comments