Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADDED] IsClaimRevoked to check user claim and fixed some comments. #108

Merged
merged 1 commit into from
Oct 20, 2020
Merged
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
24 changes: 17 additions & 7 deletions account_claims.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ func (a *AccountClaims) Revoke(pubKey string) {
a.RevokeAt(pubKey, time.Now())
}

// RevokeAt enters a revocation by publickey and timestamp into this export
// RevokeAt enters a revocation by public key and timestamp into this account
// This will revoke all jwt issued for pubKey, prior to timestamp
// If there is already a revocation for this public key that is newer, it is kept.
func (a *AccountClaims) RevokeAt(pubKey string, timestamp time.Time) {
if a.Revocations == nil {
Expand All @@ -209,14 +210,23 @@ func (a *AccountClaims) ClearRevocation(pubKey string) {
a.Revocations.ClearRevocation(pubKey)
}

// IsRevokedAt 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
// be used for testing.
// IsRevokedAt 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 the subject and issue time of the jwt to be tested.
// DO NOT pass time.Now(), it will not produce a stable/expected response.
func (a *AccountClaims) IsRevokedAt(pubKey string, timestamp time.Time) bool {
return a.Revocations.IsRevoked(pubKey, timestamp)
}

// IsRevoked checks if the public key is in the revoked list with time.Now()
func (a *AccountClaims) IsRevoked(pubKey string) bool {
return a.Revocations.IsRevoked(pubKey, time.Now())
// IsRevoked does not perform a valid check. Use IsRevokedAt instead.
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 @@ -24,7 +24,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