provisioner.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package provisioner
  2. import (
  3. "crypto/x509"
  4. "encoding/json"
  5. "strings"
  6. "github.com/pkg/errors"
  7. )
  8. // Interface is the interface that all provisioner types must implement.
  9. type Interface interface {
  10. GetID() string
  11. GetName() string
  12. GetType() Type
  13. GetEncryptedKey() (kid string, key string, ok bool)
  14. Init(config Config) error
  15. Authorize(token string) ([]SignOption, error)
  16. AuthorizeRenewal(cert *x509.Certificate) error
  17. AuthorizeRevoke(token string) error
  18. }
  19. // Type indicates the provisioner Type.
  20. type Type int
  21. const (
  22. noopType Type = 0
  23. // TypeJWK is used to indicate the JWK provisioners.
  24. TypeJWK Type = 1
  25. // TypeOIDC is used to indicate the OIDC provisioners.
  26. TypeOIDC Type = 2
  27. )
  28. // Config defines the default parameters used in the initialization of
  29. // provisioners.
  30. type Config struct {
  31. // Claims are the default claims.
  32. Claims Claims
  33. // Audiences are the audiences used in the default provisioner, (JWK).
  34. Audiences []string
  35. }
  36. type provisioner struct {
  37. Type string `json:"type"`
  38. }
  39. // List represents a list of provisioners.
  40. type List []Interface
  41. // UnmarshalJSON implements json.Unmarshaler and allows to unmarshal a list of a
  42. // interfaces into the right type.
  43. func (l *List) UnmarshalJSON(data []byte) error {
  44. ps := []json.RawMessage{}
  45. if err := json.Unmarshal(data, &ps); err != nil {
  46. return errors.Wrap(err, "error unmarshaling provisioner list")
  47. }
  48. *l = List{}
  49. for _, data := range ps {
  50. var typ provisioner
  51. if err := json.Unmarshal(data, &typ); err != nil {
  52. return errors.Errorf("error unmarshaling provisioner")
  53. }
  54. var p Interface
  55. switch strings.ToLower(typ.Type) {
  56. case "jwk":
  57. p = &JWK{}
  58. case "oidc":
  59. p = &OIDC{}
  60. default:
  61. return errors.Errorf("provisioner type %s not supported", typ.Type)
  62. }
  63. if err := json.Unmarshal(data, p); err != nil {
  64. return errors.Errorf("error unmarshaling provisioner")
  65. }
  66. *l = append(*l, p)
  67. }
  68. return nil
  69. }