authorize.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package authority
  2. import (
  3. "net/http"
  4. "time"
  5. "github.com/pkg/errors"
  6. "gopkg.in/square/go-jose.v2/jwt"
  7. )
  8. type idUsed struct {
  9. UsedAt int64 `json:"ua,omitempty"`
  10. Subject string `json:"sub,omitempty"`
  11. }
  12. // matchesOne returns true if A and B share at least one element.
  13. func matchesOne(as, bs []string) bool {
  14. if len(bs) == 0 || len(as) == 0 {
  15. return false
  16. }
  17. for _, b := range bs {
  18. for _, a := range as {
  19. if b == a {
  20. return true
  21. }
  22. }
  23. }
  24. return false
  25. }
  26. // Authorize authorizes a signature request by validating and authenticating
  27. // a OTT that must be sent w/ the request.
  28. func (a *Authority) Authorize(ott string) ([]interface{}, error) {
  29. var (
  30. errContext = map[string]interface{}{"ott": ott}
  31. claims = jwt.Claims{}
  32. )
  33. // Validate payload
  34. token, err := jwt.ParseSigned(ott)
  35. if err != nil {
  36. return nil, &apiError{errors.Wrapf(err, "authorize: error parsing token"),
  37. http.StatusUnauthorized, errContext}
  38. }
  39. kid := token.Headers[0].KeyID // JWT will only have 1 header.
  40. if len(kid) == 0 {
  41. return nil, &apiError{errors.New("authorize: token KeyID cannot be empty"),
  42. http.StatusUnauthorized, errContext}
  43. }
  44. val, ok := a.provisionerIDIndex.Load(kid)
  45. if !ok {
  46. return nil, &apiError{errors.Errorf("authorize: provisioner with KeyID %s not found", kid),
  47. http.StatusUnauthorized, errContext}
  48. }
  49. p, ok := val.(*Provisioner)
  50. if !ok {
  51. return nil, &apiError{errors.Errorf("authorize: invalid provisioner type"),
  52. http.StatusInternalServerError, errContext}
  53. }
  54. if err = token.Claims(p.Key, &claims); err != nil {
  55. return nil, &apiError{err, http.StatusUnauthorized, errContext}
  56. }
  57. // According to "rfc7519 JSON Web Token" acceptable skew should be no
  58. // more than a few minutes.
  59. if err = claims.ValidateWithLeeway(jwt.Expected{
  60. Issuer: p.Issuer,
  61. }, time.Minute); err != nil {
  62. return nil, &apiError{errors.Wrapf(err, "authorize: invalid token"),
  63. http.StatusUnauthorized, errContext}
  64. }
  65. // Do not accept tokens issued before the start of the ca.
  66. // This check is meant as a stopgap solution to the current lack of a persistence layer.
  67. if a.config.AuthorityConfig != nil && !a.config.AuthorityConfig.DisableIssuedAtCheck {
  68. if claims.IssuedAt > 0 && claims.IssuedAt.Time().Before(a.startTime) {
  69. return nil, &apiError{errors.New("token issued before the bootstrap of certificate authority"),
  70. http.StatusUnauthorized, errContext}
  71. }
  72. }
  73. if !matchesOne(claims.Audience, a.audiences) {
  74. return nil, &apiError{errors.New("authorize: token audience invalid"), http.StatusUnauthorized,
  75. errContext}
  76. }
  77. if claims.Subject == "" {
  78. return nil, &apiError{errors.New("authorize: token subject cannot be empty"),
  79. http.StatusUnauthorized, errContext}
  80. }
  81. signOps := []interface{}{
  82. &commonNameClaim{claims.Subject},
  83. &dnsNamesClaim{claims.Subject},
  84. &ipAddressesClaim{claims.Subject},
  85. p,
  86. }
  87. // Store the token to protect against reuse.
  88. if _, ok := a.ottMap.LoadOrStore(claims.ID, &idUsed{
  89. UsedAt: time.Now().Unix(),
  90. Subject: claims.Subject,
  91. }); ok {
  92. return nil, &apiError{errors.Errorf("token already used"), http.StatusUnauthorized,
  93. errContext}
  94. }
  95. return signOps, nil
  96. }