Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions map_claims.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package jwt

import (
"encoding/json"
"errors"
"time"
// "fmt"
)
Expand Down Expand Up @@ -126,20 +125,17 @@ func (m MapClaims) Valid() error {
now := TimeFunc().Unix()

if !m.VerifyExpiresAt(now, false) {
// TODO(oxisto): this should be replaced with ErrTokenExpired
vErr.Inner = errors.New("Token is expired")
vErr.Inner = ErrTokenExpired
vErr.Errors |= ValidationErrorExpired
}

if !m.VerifyIssuedAt(now, false) {
// TODO(oxisto): this should be replaced with ErrTokenUsedBeforeIssued
vErr.Inner = errors.New("Token used before issued")
vErr.Inner = ErrTokenUsedBeforeIssued
vErr.Errors |= ValidationErrorIssuedAt
}

if !m.VerifyNotBefore(now, false) {
// TODO(oxisto): this should be replaced with ErrTokenNotValidYet
vErr.Inner = errors.New("Token is not valid yet")
vErr.Inner = ErrTokenNotValidYet
vErr.Errors |= ValidationErrorNotValidYet
}

Expand Down
40 changes: 40 additions & 0 deletions map_claims_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package jwt

import (
"errors"
"testing"
"time"
)
Expand Down Expand Up @@ -121,3 +122,42 @@ func TestMapClaimsVerifyExpiresAtExpire(t *testing.T) {
t.Fatalf("Failed to verify claims, wanted: %v got %v", want, got)
}
}

func TestMapClaimsValidErrorCombinations(t *testing.T) {
now := time.Now().Unix()

claims := MapClaims{
"exp": float64(now - 3600), // expired
"iat": float64(now + 1800), // used before issued
"nbf": float64(now + 3600), // not valid yet
}

err := claims.Valid()

if err == nil {
t.Fatal("Expected error but got nil")
}

var vErr *ValidationError

if !errors.As(err, &vErr) {
t.Fatalf("Expected ValidationError, got %T", err)
}

expectedFlags := ValidationErrorExpired | ValidationErrorIssuedAt | ValidationErrorNotValidYet
if vErr.Errors != expectedFlags {
t.Errorf("Expected combined error flags %v, got %v", expectedFlags, vErr.Errors)
}

if !errors.Is(err, ErrTokenExpired) {
t.Error("Should detect ErrTokenExpired")
}

if !errors.Is(err, ErrTokenUsedBeforeIssued) {
t.Error("Should detect ErrTokenUsedBeforeIssued")
}

if !errors.Is(err, ErrTokenNotValidYet) {
t.Error("Should detect ErrTokenNotValidYet")
}
}