Skip to content

Commit aacb3cb

Browse files
authored
fix panic in auth.TokenExpired (#736)
1 parent 09684be commit aacb3cb

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

internal/pkg/auth/user_token_flow.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ func TokenExpired(token string) (bool, error) {
118118
expirationTimestampNumeric, err := tokenParsed.Claims.GetExpirationTime()
119119
if err != nil {
120120
return false, fmt.Errorf("get expiration timestamp from access token: %w", err)
121+
} else if expirationTimestampNumeric == nil {
122+
return false, nil
121123
}
122124
expirationTimestamp := expirationTimestampNumeric.Time
123125
now := time.Now()

internal/pkg/auth/user_token_flow_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,3 +381,40 @@ func createTokens(accessTokenExpiresAt, refreshTokenExpiresAt time.Time) (access
381381

382382
return accessToken, refreshToken, nil
383383
}
384+
385+
func TestTokenExpired(t *testing.T) {
386+
tests := []struct {
387+
desc string
388+
token string
389+
expected bool
390+
}{
391+
{
392+
desc: "token without exp",
393+
token: `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c`,
394+
expected: false,
395+
},
396+
{
397+
desc: "exp 0",
398+
token: `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjB9.rIhVGrtR0B0gUYPZDnB6LZ_w7zckH_9qFZBWG4rCkRY`,
399+
expected: true,
400+
},
401+
{
402+
desc: "exp 9007199254740991",
403+
token: `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjIyNTc2MDkwNzExMTExMTExfQ.aStshPjoSKTIcBeESbLJWvbMVuw-XWInXcf1P7tiWaE`,
404+
expected: false,
405+
},
406+
}
407+
408+
for _, tt := range tests {
409+
t.Run(tt.desc, func(t *testing.T) {
410+
actual, err := TokenExpired(tt.token)
411+
if err != nil {
412+
t.Fatalf("TokenExpired() error = %v", err)
413+
}
414+
415+
if actual != tt.expected {
416+
t.Errorf("TokenExpired() = %v, want %v", actual, tt.expected)
417+
}
418+
})
419+
}
420+
}

0 commit comments

Comments
 (0)