interface.go 5.4 KB

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