[FB-1621] account for token expiration as float and int (#2188)

* account token expiration as float and int

* update tests
This commit is contained in:
Samir Patel 2022-08-10 17:26:10 -05:00 • committed by GitHub
parent 7cfa516f74
commit 3c2ef484bd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 6 deletions

View file

@ -190,8 +190,21 @@ func (a *Auth) Authenticate(access, refresh string) (*UserInfo, error) {
claims := *token.Claims.(*jwt.MapClaims)
// expiry check
if exp, ok := claims["exp"].(string); ok {
if expiry, err := strconv.ParseInt(exp, 10, 64); err != nil || expiry < time.Now().UTC().Unix() {
if exp, ok := claims["exp"]; ok {
var expiry int64
switch v := exp.(type) {
case string:
expiry, err = strconv.ParseInt(v, 10, 64)
if err != nil {
return nil, fmt.Errorf("parsing exp string: %v", err)
}
case float64:
expiry = int64(v)
case int64:
expiry = v
}
if expiry < time.Now().UTC().Unix() {
access, refresh, err = a.refreshToken(access, refresh)
if err != nil {
return nil, fmt.Errorf("token is expired: %w", err)

View file

@ -267,7 +267,7 @@ func TestAuthenticate(t *testing.T) {
claims["oid"] = test.uid
claims["name"] = test.uname
if test.exp != 0 {
claims["exp"] = strconv.Itoa(int(test.exp))
claims["exp"] = float64(test.exp)
}
token, err = tkn.SignedString(a.SecretKey())
if err != nil {
@ -298,7 +298,7 @@ func TestAuthenticate(t *testing.T) {
claims := tkn.Claims.(jwt.MapClaims)
claims["oid"] = test.uid
claims["name"] = test.uname
expiry := strconv.Itoa(int(time.Now().Add(2 * time.Hour).Unix()))
expiry := float64(time.Now().Add(2 * time.Hour).Unix())
claims["exp"] = expiry
fresh, err := tkn.SignedString(a.SecretKey())
if err != nil {
@ -306,7 +306,7 @@ func TestAuthenticate(t *testing.T) {
}
a.groupsCache[fresh] = cachedGroups{time.Now(), test.groups}
fmt.Fprintf(w, `{"access_token": "`+fresh+`", "refresh_token": "blah", "token_type": "bearer", "expires": `+expiry+` }`)
fmt.Fprintf(w, `{"access_token": "`+fresh+`", "refresh_token": "blah", "token_type": "bearer", "expires": `+strconv.FormatFloat(expiry, 'f', 0, 64)+` }`)
}))
defer srv.Close()
a.oAuthConfig.Endpoint.TokenURL = srv.URL
@ -571,7 +571,7 @@ func TestHandlers(t *testing.T) {
claims["name"] = "user name"
expiresIn := 2 * time.Hour
exp := time.Now().Add(expiresIn)
expiry := strconv.Itoa(int(exp.Unix()))
expiry := float64(exp.Unix())
claims["exp"] = expiry
fresh, err := tkn.SignedString(a.SecretKey())
if err != nil {