Skip to content

Commit

Permalink
Merge pull request #113 from nats-io/port_108
Browse files Browse the repository at this point in the history
[ADDED] IsClaimRevoked to check user claim and fixed some comments.
  • Loading branch information
kozlovic authored Oct 30, 2020
2 parents e11ce31 + 8363724 commit c31b0a8
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 c31b0a8

Please sign in to comment.