github.com/nats-io/jwt/v2@v2.5.6/v1compat/revocation_list.go (about)

     1  /*
     2   * Copyright 2020 The NATS Authors
     3   * Licensed under the Apache License, Version 2.0 (the "License");
     4   * you may not use this file except in compliance with the License.
     5   * You may obtain a copy of the License at
     6   *
     7   * http://www.apache.org/licenses/LICENSE-2.0
     8   *
     9   * Unless required by applicable law or agreed to in writing, software
    10   * distributed under the License is distributed on an "AS IS" BASIS,
    11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12   * See the License for the specific language governing permissions and
    13   * limitations under the License.
    14   */
    15  
    16  package jwt
    17  
    18  import (
    19  	"time"
    20  )
    21  
    22  const All = "*"
    23  
    24  // RevocationList is used to store a mapping of public keys to unix timestamps
    25  type RevocationList map[string]int64
    26  
    27  // Revoke enters a revocation by publickey and timestamp into this export
    28  // If there is already a revocation for this public key that is newer, it is kept.
    29  func (r RevocationList) Revoke(pubKey string, timestamp time.Time) {
    30  	newTS := timestamp.Unix()
    31  	if ts, ok := r[pubKey]; ok && ts > newTS {
    32  		return
    33  	}
    34  
    35  	r[pubKey] = newTS
    36  }
    37  
    38  // ClearRevocation removes any revocation for the public key
    39  func (r RevocationList) ClearRevocation(pubKey string) {
    40  	delete(r, pubKey)
    41  }
    42  
    43  // IsRevoked checks if the public key is in the revoked list with a timestamp later than
    44  // the one passed in. Generally this method is called with an issue time but other time's can
    45  // be used for testing.
    46  func (r RevocationList) IsRevoked(pubKey string, timestamp time.Time) bool {
    47  	if r.allRevoked(timestamp) {
    48  		return true
    49  	}
    50  	ts, ok := r[pubKey]
    51  	return ok && ts >= timestamp.Unix()
    52  }
    53  
    54  // allRevoked returns true if All is set and the timestamp is later or same as the
    55  // one passed. This is called by IsRevoked.
    56  func (r RevocationList) allRevoked(timestamp time.Time) bool {
    57  	ts, ok := r[All]
    58  	return ok && ts >= timestamp.Unix()
    59  }