oidc.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package provisioner
  2. import (
  3. "crypto/x509"
  4. "encoding/json"
  5. "net/http"
  6. "time"
  7. "github.com/pkg/errors"
  8. "github.com/smallstep/cli/jose"
  9. )
  10. // openIDConfiguration contains the necessary properties in the
  11. // `/.well-known/openid-configuration` document.
  12. type openIDConfiguration struct {
  13. Issuer string `json:"issuer"`
  14. JWKSetURI string `json:"jwks_uri"`
  15. }
  16. // Validate validates the values in a well-known OpenID configuration endpoint.
  17. func (c openIDConfiguration) Validate() error {
  18. switch {
  19. case c.Issuer == "":
  20. return errors.New("issuer cannot be empty")
  21. case c.JWKSetURI == "":
  22. return errors.New("jwks_uri cannot be empty")
  23. default:
  24. return nil
  25. }
  26. }
  27. // openIDPayload represents the fields on the id_token JWT payload.
  28. type openIDPayload struct {
  29. jose.Claims
  30. AtHash string `json:"at_hash"`
  31. AuthorizedParty string `json:"azp"`
  32. Email string `json:"email"`
  33. EmailVerified bool `json:"email_verified"`
  34. Hd string `json:"hd"`
  35. Nonce string `json:"nonce"`
  36. }
  37. // OIDC represents an OAuth 2.0 OpenID Connect provider.
  38. type OIDC struct {
  39. Type string `json:"type"`
  40. Name string `json:"name"`
  41. ClientID string `json:"clientID"`
  42. ConfigurationEndpoint string `json:"configurationEndpoint"`
  43. Claims *Claims `json:"claims,omitempty"`
  44. Admins []string `json:"admins,omitempty"`
  45. configuration openIDConfiguration
  46. keyStore *keyStore
  47. }
  48. // IsAdmin returns true if the given email is in the Admins whitelist, false
  49. // otherwise.
  50. func (o *OIDC) IsAdmin(email string) bool {
  51. for _, e := range o.Admins {
  52. if e == email {
  53. return true
  54. }
  55. }
  56. return false
  57. }
  58. // GetID returns the provisioner unique identifier, the OIDC provisioner the
  59. // uses the clientID for this.
  60. func (o *OIDC) GetID() string {
  61. return o.ClientID
  62. }
  63. // GetName returns the name of the provisioner.
  64. func (o *OIDC) GetName() string {
  65. return o.Name
  66. }
  67. // GetType returns the type of provisioner.
  68. func (o *OIDC) GetType() Type {
  69. return TypeOIDC
  70. }
  71. // GetEncryptedKey is not available in an OIDC provisioner.
  72. func (o *OIDC) GetEncryptedKey() (kid string, key string, ok bool) {
  73. return "", "", false
  74. }
  75. // Init validates and initializes the OIDC provider.
  76. func (o *OIDC) Init(config Config) (err error) {
  77. switch {
  78. case o.Name == "":
  79. return errors.New("name cannot be empty")
  80. case o.ClientID == "":
  81. return errors.New("clientID cannot be empty")
  82. case o.ConfigurationEndpoint == "":
  83. return errors.New("configurationEndpoint cannot be empty")
  84. }
  85. // Update claims with global ones
  86. if o.Claims, err = o.Claims.Init(&config.Claims); err != nil {
  87. return err
  88. }
  89. // Decode and validate openid-configuration endpoint
  90. if err := getAndDecode(o.ConfigurationEndpoint, &o.configuration); err != nil {
  91. return err
  92. }
  93. if err := o.configuration.Validate(); err != nil {
  94. return errors.Wrapf(err, "error parsing %s", o.ConfigurationEndpoint)
  95. }
  96. // Get JWK key set
  97. o.keyStore, err = newKeyStore(o.configuration.JWKSetURI)
  98. if err != nil {
  99. return err
  100. }
  101. return nil
  102. }
  103. // ValidatePayload validates the given token payload.
  104. func (o *OIDC) ValidatePayload(p openIDPayload) error {
  105. // According to "rfc7519 JSON Web Token" acceptable skew should be no more
  106. // than a few minutes.
  107. if err := p.ValidateWithLeeway(jose.Expected{
  108. Issuer: o.configuration.Issuer,
  109. Audience: jose.Audience{o.ClientID},
  110. Time: time.Now().UTC(),
  111. }, time.Minute); err != nil {
  112. return errors.Wrap(err, "failed to validate payload")
  113. }
  114. if p.AuthorizedParty != "" && p.AuthorizedParty != o.ClientID {
  115. return errors.New("failed to validate payload: invalid azp")
  116. }
  117. return nil
  118. }
  119. // Authorize validates the given token.
  120. func (o *OIDC) Authorize(token string) ([]SignOption, error) {
  121. jwt, err := jose.ParseSigned(token)
  122. if err != nil {
  123. return nil, errors.Wrapf(err, "error parsing token")
  124. }
  125. // Parse claims to get the kid
  126. var claims openIDPayload
  127. if err := jwt.UnsafeClaimsWithoutVerification(&claims); err != nil {
  128. return nil, errors.Wrap(err, "error parsing claims")
  129. }
  130. found := false
  131. kid := jwt.Headers[0].KeyID
  132. keys := o.keyStore.Get(kid)
  133. for _, key := range keys {
  134. if err := jwt.Claims(key, &claims); err == nil {
  135. found = true
  136. break
  137. }
  138. }
  139. if !found {
  140. return nil, errors.New("cannot validate token")
  141. }
  142. if err := o.ValidatePayload(claims); err != nil {
  143. return nil, err
  144. }
  145. // Admins should be able to authorize any SAN
  146. if o.IsAdmin(claims.Email) {
  147. return []SignOption{
  148. profileDefaultDuration(o.Claims.DefaultTLSCertDuration()),
  149. newProvisionerExtensionOption(TypeOIDC, o.Name, o.ClientID),
  150. newValidityValidator(o.Claims.MinTLSCertDuration(), o.Claims.MaxTLSCertDuration()),
  151. }, nil
  152. }
  153. return []SignOption{
  154. emailOnlyIdentity(claims.Email),
  155. profileDefaultDuration(o.Claims.DefaultTLSCertDuration()),
  156. newProvisionerExtensionOption(TypeOIDC, o.Name, o.ClientID),
  157. newValidityValidator(o.Claims.MinTLSCertDuration(), o.Claims.MaxTLSCertDuration()),
  158. }, nil
  159. }
  160. // AuthorizeRenewal returns an error if the renewal is disabled.
  161. func (o *OIDC) AuthorizeRenewal(cert *x509.Certificate) error {
  162. if o.Claims.IsDisableRenewal() {
  163. return errors.Errorf("renew is disabled for provisioner %s", o.GetID())
  164. }
  165. return nil
  166. }
  167. // AuthorizeRevoke returns an error if the provisioner does not have rights to
  168. // revoke the certificate with serial number in the `sub` property.
  169. func (o *OIDC) AuthorizeRevoke(token string) error {
  170. return errors.New("not implemented")
  171. }
  172. func getAndDecode(uri string, v interface{}) error {
  173. resp, err := http.Get(uri)
  174. if err != nil {
  175. return errors.Wrapf(err, "failed to connect to %s", uri)
  176. }
  177. defer resp.Body.Close()
  178. if err := json.NewDecoder(resp.Body).Decode(v); err != nil {
  179. return errors.Wrapf(err, "error reading %s", uri)
  180. }
  181. return nil
  182. }