authorize.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package authority
  2. import (
  3. "crypto/x509"
  4. "net/http"
  5. "time"
  6. "github.com/pkg/errors"
  7. "github.com/smallstep/certificates/authority/provisioner"
  8. "github.com/smallstep/cli/jose"
  9. )
  10. type idUsed struct {
  11. UsedAt int64 `json:"ua,omitempty"`
  12. Subject string `json:"sub,omitempty"`
  13. }
  14. // Claims extends jose.Claims with step attributes.
  15. type Claims struct {
  16. jose.Claims
  17. SANs []string `json:"sans,omitempty"`
  18. Email string `json:"email,omitempty"`
  19. Nonce string `json:"nonce,omitempty"`
  20. }
  21. // Authorize authorizes a signature request by validating and authenticating
  22. // a OTT that must be sent w/ the request.
  23. // TODO(mariano): protection against reuse for oidc
  24. func (a *Authority) Authorize(ott string) ([]provisioner.SignOption, error) {
  25. var errContext = map[string]interface{}{"ott": ott}
  26. // Validate payload
  27. token, err := jose.ParseSigned(ott)
  28. if err != nil {
  29. return nil, &apiError{errors.Wrapf(err, "authorize: error parsing token"),
  30. http.StatusUnauthorized, errContext}
  31. }
  32. // Get claims w/out verification. We need to look up the provisioner
  33. // key in order to verify the claims and we need the issuer from the claims
  34. // before we can look up the provisioner.
  35. var claims Claims
  36. if err = token.UnsafeClaimsWithoutVerification(&claims); err != nil {
  37. return nil, &apiError{err, http.StatusUnauthorized, errContext}
  38. }
  39. // Do not accept tokens issued before the start of the ca.
  40. // This check is meant as a stopgap solution to the current lack of a persistence layer.
  41. if a.config.AuthorityConfig != nil && !a.config.AuthorityConfig.DisableIssuedAtCheck {
  42. if claims.IssuedAt > 0 && claims.IssuedAt.Time().Before(a.startTime) {
  43. return nil, &apiError{errors.New("authorize: token issued before the bootstrap of certificate authority"),
  44. http.StatusUnauthorized, errContext}
  45. }
  46. }
  47. // This method will also validate the audiences for JWK provisioners.
  48. p, ok := a.provisioners.LoadByToken(token, &claims.Claims)
  49. if !ok {
  50. return nil, &apiError{errors.New("authorize: provisioner not found or invalid audience"),
  51. http.StatusUnauthorized, errContext}
  52. }
  53. // Store the token to protect against reuse.
  54. if p.GetType() == provisioner.TypeJWK && claims.ID != "" {
  55. if _, ok := a.ottMap.LoadOrStore(claims.ID, &idUsed{
  56. UsedAt: time.Now().Unix(),
  57. Subject: claims.Subject,
  58. }); ok {
  59. return nil, &apiError{errors.Errorf("authorize: token already used"), http.StatusUnauthorized, errContext}
  60. }
  61. }
  62. // Call the provisioner Authorize method to get the signing options
  63. opts, err := p.Authorize(ott)
  64. if err != nil {
  65. return nil, &apiError{errors.Wrap(err, "authorize"), http.StatusUnauthorized, errContext}
  66. }
  67. return opts, nil
  68. }
  69. // authorizeRenewal tries to locate the step provisioner extension, and checks
  70. // if for the configured provisioner, the renewal is enabled or not. If the
  71. // extra extension cannot be found, authorize the renewal by default.
  72. //
  73. // TODO(mariano): should we authorize by default?
  74. func (a *Authority) authorizeRenewal(crt *x509.Certificate) error {
  75. errContext := map[string]interface{}{"serialNumber": crt.SerialNumber.String()}
  76. p, ok := a.provisioners.LoadByCertificate(crt)
  77. if !ok {
  78. return &apiError{
  79. err: errors.New("provisioner not found"),
  80. code: http.StatusUnauthorized,
  81. context: errContext,
  82. }
  83. }
  84. if err := p.AuthorizeRenewal(crt); err != nil {
  85. return &apiError{
  86. err: err,
  87. code: http.StatusUnauthorized,
  88. context: errContext,
  89. }
  90. }
  91. return nil
  92. }