MM-41236: Sentry crash: Fix nil reference to token (#19417)

The AND condition would mean that it would try to dereference
token.Valid if there was an error. And there's no guarantee
to always have a non-nil token in case of an error. We need
to track those conditions separately.

https://mattermost.atlassian.net/browse/MM-41236

```release-note
NONE
```
Этот коммит содержится в:
Agniva De Sarker
2022-01-28 12:37:02 +05:30
коммит произвёл GitHub
родитель 8f35243604
Коммит 0c9262c4d1
2 изменённых файлов: 12 добавлений и 1 удалений

Просмотреть файл

@@ -358,9 +358,12 @@ func (s *Server) renewalTokenValid(tokenString, signingKey string) (bool, error)
token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
return []byte(signingKey), nil
})
if err != nil && !token.Valid {
if err != nil {
return false, errors.Wrapf(err, "Error validating JWT token")
}
if !token.Valid {
return false, errors.New("invalid JWT token")
}
expirationTime := time.Unix(claims.ExpiresAt, 0)
if expirationTime.Before(time.Now().UTC()) {
return false, nil

Просмотреть файл

@@ -4,9 +4,11 @@
package app
import (
"errors"
"testing"
"time"
"github.com/dgrijalva/jwt-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -80,6 +82,12 @@ func TestGenerateRenewalToken(t *testing.T) {
th := Setup(t)
defer th.TearDown()
t.Run("test invalid token", func(t *testing.T) {
_, err := th.App.Srv().renewalTokenValid("badtoken", "")
var vErr *jwt.ValidationError
require.True(t, errors.As(err, &vErr))
})
t.Run("renewal token generated correctly", func(t *testing.T) {
setLicense(th, nil)
token, appErr := th.App.Srv().GenerateRenewalToken(JWTDefaultTokenExpiration)