-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: update token invalidation for recent API changes (#1110)
- Update the token invalidation code to use the latest API on SaaS - Return `jwt.ErrTokenExpired` when the token has been invalidated so that it is treated as an auth error and tiggers re-auth
- Loading branch information
1 parent
401af27
commit 6fefbb3
Showing
3 changed files
with
62 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
"testing" | ||
"time" | ||
|
||
"github.com/golang-jwt/jwt/v4" | ||
"github.com/google/uuid" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
@@ -19,6 +20,44 @@ func TestUserNonNilLastAuthAtProto(t *testing.T) { | |
require.WithinDuration(t, expectedTime, u.Proto().LastAuthAt.AsTime(), time.Millisecond) | ||
} | ||
|
||
func TestInvalidation(t *testing.T) { | ||
tt := time.Date(2023, 2, 2, 15, 4, 5, 0, time.UTC) | ||
extSess := ExternalSessions{ | ||
Invalidations: &InvalidationMap{ | ||
DefaultTime: tt.Add(3 * time.Hour), | ||
LastUpdated: tt.Add(9 * time.Hour), | ||
InvalidationTimes: map[string]map[string]time.Time{ | ||
"user-1": { | ||
"logout": tt.Add(9 * time.Hour), | ||
"new-perm": tt.Add(5 * time.Hour), | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
j := JWT{ | ||
StandardClaims: jwt.StandardClaims{ //nolint | ||
IssuedAt: tt.Unix(), | ||
ExpiresAt: tt.Add(20 * time.Hour).Unix(), | ||
}, | ||
UserID: "user-1", | ||
Email: "[email protected]", | ||
Name: "Test User", | ||
OrgRoles: map[OrgID]OrgRoleClaims{}, | ||
} | ||
require.ErrorIs(t, extSess.Validate(&j), jwt.ErrTokenExpired) | ||
|
||
j.StandardClaims.IssuedAt = tt.Add(10 * time.Hour).Unix() | ||
require.NoError(t, extSess.Validate(&j)) | ||
|
||
j.StandardClaims.IssuedAt = tt.Add(6 * time.Hour).Unix() | ||
require.ErrorIs(t, extSess.Validate(&j), jwt.ErrTokenExpired) | ||
|
||
// No such user, using default time | ||
j.UserID = "user-23" | ||
require.NoError(t, extSess.Validate(&j)) | ||
} | ||
|
||
func TestUserWebSetting_Proto(t *testing.T) { | ||
in := UserWebSetting{ | ||
UserID: UserID(42), | ||
|