github.com/lestrrat-go/jwx/v2@v2.0.21/jwk/interface.go (about)

     1  package jwk
     2  
     3  import (
     4  	"context"
     5  	"sync"
     6  
     7  	"github.com/lestrrat-go/iter/arrayiter"
     8  	"github.com/lestrrat-go/iter/mapiter"
     9  	"github.com/lestrrat-go/jwx/v2/internal/iter"
    10  	"github.com/lestrrat-go/jwx/v2/internal/json"
    11  )
    12  
    13  // AsymmetricKey describes a Key that represents an key in an asymmetric key pair,
    14  // which in turn can be either a private or a public key. This interface
    15  // allows those keys to be queried if they are one or the other.
    16  type AsymmetricKey interface {
    17  	IsPrivate() bool
    18  }
    19  
    20  // KeyUsageType is used to denote what this key should be used for
    21  type KeyUsageType string
    22  
    23  const (
    24  	// ForSignature is the value used in the headers to indicate that
    25  	// this key should be used for signatures
    26  	ForSignature KeyUsageType = "sig"
    27  	// ForEncryption is the value used in the headers to indicate that
    28  	// this key should be used for encrypting
    29  	ForEncryption KeyUsageType = "enc"
    30  )
    31  
    32  type KeyOperation string
    33  type KeyOperationList []KeyOperation
    34  
    35  const (
    36  	KeyOpSign       KeyOperation = "sign"       // (compute digital signature or MAC)
    37  	KeyOpVerify     KeyOperation = "verify"     // (verify digital signature or MAC)
    38  	KeyOpEncrypt    KeyOperation = "encrypt"    // (encrypt content)
    39  	KeyOpDecrypt    KeyOperation = "decrypt"    // (decrypt content and validate decryption, if applicable)
    40  	KeyOpWrapKey    KeyOperation = "wrapKey"    // (encrypt key)
    41  	KeyOpUnwrapKey  KeyOperation = "unwrapKey"  // (decrypt key and validate decryption, if applicable)
    42  	KeyOpDeriveKey  KeyOperation = "deriveKey"  // (derive key)
    43  	KeyOpDeriveBits KeyOperation = "deriveBits" // (derive bits not to be used as a key)
    44  )
    45  
    46  // Set represents JWKS object, a collection of jwk.Key objects.
    47  //
    48  // Sets can be safely converted to and from JSON using the standard
    49  // `"encoding/json".Marshal` and `"encoding/json".Unmarshal`. However,
    50  // if you do not know if the payload contains a single JWK or a JWK set,
    51  // consider using `jwk.Parse()` to always get a `jwk.Set` out of it.
    52  //
    53  // Since v1.2.12, JWK sets with private parameters can be parsed as well.
    54  // Such private parameters can be accessed via the `Field()` method.
    55  // If a resource contains a single JWK instead of a JWK set, private parameters
    56  // are stored in _both_ the resulting `jwk.Set` object and the `jwk.Key` object .
    57  //
    58  //nolint:interfacebloat
    59  type Set interface {
    60  	// AddKey adds the specified key. If the key already exists in the set,
    61  	// an error is returned.
    62  	AddKey(Key) error
    63  
    64  	// Clear resets the list of keys associated with this set, emptying the
    65  	// internal list of `jwk.Key`s, as well as clearing any other non-key
    66  	// fields
    67  	Clear() error
    68  
    69  	// Get returns the key at index `idx`. If the index is out of range,
    70  	// then the second return value is false.
    71  	Key(int) (Key, bool)
    72  
    73  	// Get returns the value of a private field in the key set.
    74  	//
    75  	// For the purposes of a key set, any field other than the "keys" field is
    76  	// considered to be a private field. In other words, you cannot use this
    77  	// method to directly access the list of keys in the set
    78  	Get(string) (interface{}, bool)
    79  
    80  	// Set sets the value of a single field.
    81  	//
    82  	// This method, which takes an `interface{}`, exists because
    83  	// these objects can contain extra _arbitrary_ fields that users can
    84  	// specify, and there is no way of knowing what type they could be.
    85  	Set(string, interface{}) error
    86  
    87  	// Remove removes the specified non-key field from the set.
    88  	// Keys may not be removed using this method. See RemoveKey for
    89  	// removing keys.
    90  	Remove(string) error
    91  
    92  	// Index returns the index where the given key exists, -1 otherwise
    93  	Index(Key) int
    94  
    95  	// Len returns the number of keys in the set
    96  	Len() int
    97  
    98  	// LookupKeyID returns the first key matching the given key id.
    99  	// The second return value is false if there are no keys matching the key id.
   100  	// The set *may* contain multiple keys with the same key id. If you
   101  	// need all of them, use `Iterate()`
   102  	LookupKeyID(string) (Key, bool)
   103  
   104  	// RemoveKey removes the key from the set.
   105  	// RemoveKey returns an error when the specified key does not exist
   106  	// in set.
   107  	RemoveKey(Key) error
   108  
   109  	// Keys creates an iterator to iterate through all keys in the set.
   110  	Keys(context.Context) KeyIterator
   111  
   112  	// Iterate creates an iterator to iterate through all fields other than the keys
   113  	Iterate(context.Context) HeaderIterator
   114  
   115  	// Clone create a new set with identical keys. Keys themselves are not cloned.
   116  	Clone() (Set, error)
   117  }
   118  
   119  type set struct {
   120  	keys          []Key
   121  	mu            sync.RWMutex
   122  	dc            DecodeCtx
   123  	privateParams map[string]interface{}
   124  }
   125  
   126  type HeaderVisitor = iter.MapVisitor
   127  type HeaderVisitorFunc = iter.MapVisitorFunc
   128  type HeaderPair = mapiter.Pair
   129  type HeaderIterator = mapiter.Iterator
   130  type KeyPair = arrayiter.Pair
   131  type KeyIterator = arrayiter.Iterator
   132  
   133  type PublicKeyer interface {
   134  	// PublicKey creates the corresponding PublicKey type for this object.
   135  	// All fields are copied onto the new public key, except for those that are not allowed.
   136  	// Returned value must not be the receiver itself.
   137  	PublicKey() (Key, error)
   138  }
   139  
   140  type DecodeCtx interface {
   141  	json.DecodeCtx
   142  	IgnoreParseError() bool
   143  }
   144  type KeyWithDecodeCtx interface {
   145  	SetDecodeCtx(DecodeCtx)
   146  	DecodeCtx() DecodeCtx
   147  }