File tree Expand file tree Collapse file tree 2 files changed +14
-3
lines changed Expand file tree Collapse file tree 2 files changed +14
-3
lines changed Original file line number Diff line number Diff line change @@ -32,6 +32,7 @@ const (
3232 tokenAPI = "https://service-account.api.stackit.cloud/token" //nolint:gosec // linter false positive
3333 defaultTokenType = "Bearer"
3434 defaultScope = ""
35+ tokenExpirationLeeway = time .Second * 5
3536)
3637
3738// KeyFlow handles auth with SA key
@@ -400,11 +401,15 @@ func tokenExpired(token string) (bool, error) {
400401 if err != nil {
401402 return false , fmt .Errorf ("parse token: %w" , err )
402403 }
404+
403405 expirationTimestampNumeric , err := tokenParsed .Claims .GetExpirationTime ()
404406 if err != nil {
405407 return false , fmt .Errorf ("get expiration timestamp: %w" , err )
406408 }
407- expirationTimestamp := expirationTimestampNumeric .Time
408- now := time .Now ()
409- return now .After (expirationTimestamp ), nil
409+
410+ // Pretend to be `tokenExpirationLeeway` into the future to avoid token expiring
411+ // between retrieving the token and using it in the actual request.
412+ now := time .Now ().Add (tokenExpirationLeeway )
413+
414+ return now .After (expirationTimestampNumeric .Time ), nil
410415}
Original file line number Diff line number Diff line change @@ -209,6 +209,12 @@ func TestTokenExpired(t *testing.T) {
209209 expectedErr : false ,
210210 expectedIsExpired : true ,
211211 },
212+ {
213+ desc : "token almost expired" ,
214+ tokenExpiresAt : time .Now ().Add (tokenExpirationLeeway ),
215+ expectedErr : false ,
216+ expectedIsExpired : true ,
217+ },
212218 {
213219 desc : "token invalid" ,
214220 tokenInvalid : true ,
You can’t perform that action at this time.
0 commit comments