Skip to content

Commit

Permalink
[ADDED] IsClaimRevoked to check user claim and fixed some comments.
Browse files Browse the repository at this point in the history
Comment for RevokeAt(), IsRevokedAt() have been fixed.

The function IsRevoked() must no longer be used (and removed in v2).
It will now always return true. Use IsRevokedAt() instead with
proper timestamp.

Note: This was done in v2 and backported to branch_v1 but was not
added to the v1 in the main branch. So this is a port of #108.

Signed-off-by: Ivan Kozlovic <ivan@synadia.com>
  • Loading branch information
kozlovic committed Oct 28, 2020
1 parent e11ce31 commit 8363724
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
9 changes: 9 additions & 0 deletions account_claims.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,12 @@ func (a *AccountClaims) IsRevokedAt(pubKey string, timestamp time.Time) bool {
func (a *AccountClaims) IsRevoked(_ string) bool {
return true
}

// IsClaimRevoked checks if the account revoked the claim passed in.
// Invalid claims (nil, no Subject or IssuedAt) will return true.
func (a *AccountClaims) IsClaimRevoked(claim *UserClaims) bool {
if claim == nil || claim.IssuedAt == 0 || claim.Subject == "" {
return true
}
return a.Revocations.IsRevoked(claim.Subject, time.Unix(claim.IssuedAt, 0))
}
13 changes: 10 additions & 3 deletions account_claims_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,14 @@ func TestUserRevocation(t *testing.T) {
apk := publicKey(akp, t)
account := NewAccountClaims(apk)

pubKey := "bar"
ukp := createUserNKey(t)
pubKey := publicKey(ukp, t)
uc := NewUserClaims(pubKey)
uJwt, _ := uc.Encode(akp)
uc, err := DecodeUserClaims(uJwt)
if err != nil {
t.Errorf("Failed to decode user claim: %v", err)
}
now := time.Now()

// test that clear is safe before we add any
Expand Down Expand Up @@ -523,13 +530,13 @@ func TestUserRevocation(t *testing.T) {

account.ClearRevocation(pubKey)

if account.IsRevokedAt(pubKey, now) {
if account.IsClaimRevoked(uc) {
t.Errorf("revocations should be cleared")
}

account.RevokeAt(pubKey, now.Add(time.Second*1000))

if !account.IsRevoked(pubKey) {
if !account.IsClaimRevoked(uc) {
t.Errorf("revocation be true we revoked in the future")
}
}
2 changes: 1 addition & 1 deletion revocation_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (r RevocationList) ClearRevocation(pubKey string) {
}

// IsRevoked checks if the public key is in the revoked list with a timestamp later than
// the one passed in. Generally this method is called with time.Now() but other time's can
// the one passed in. Generally this method is called with an issue time but other time's can
// be used for testing.
func (r RevocationList) IsRevoked(pubKey string, timestamp time.Time) bool {
ts, ok := r[pubKey]
Expand Down

0 comments on commit 8363724

Please sign in to comment.