2018-10-05 21:48:36 +00:00
|
|
|
package authority
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"gopkg.in/square/go-jose.v2/jwt"
|
|
|
|
)
|
|
|
|
|
|
|
|
type idUsed struct {
|
|
|
|
UsedAt int64 `json:"ua,omitempty"`
|
|
|
|
Subject string `json:"sub,omitempty"`
|
|
|
|
}
|
|
|
|
|
2018-10-19 05:26:39 +00:00
|
|
|
// containsAtLeastOneAudience returns true if 'as' contains at least one element
|
|
|
|
// of 'bs', otherwise returns false.
|
|
|
|
func containsAtLeastOneAudience(as []string, bs []string) bool {
|
|
|
|
if len(bs) == 0 {
|
2018-10-05 21:48:36 +00:00
|
|
|
return true
|
|
|
|
}
|
2018-10-19 05:26:39 +00:00
|
|
|
if len(as) == 0 {
|
2018-10-05 21:48:36 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-10-19 05:26:39 +00:00
|
|
|
for _, b := range bs {
|
|
|
|
for _, a := range as {
|
|
|
|
if b == a {
|
2018-10-05 21:48:36 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Authorize authorizes a signature request by validating and authenticating
|
|
|
|
// a OTT that must be sent w/ the request.
|
2018-10-19 05:26:39 +00:00
|
|
|
func (a *Authority) Authorize(ott string) ([]interface{}, error) {
|
2018-10-05 21:48:36 +00:00
|
|
|
var (
|
|
|
|
errContext = map[string]interface{}{"ott": ott}
|
|
|
|
claims = jwt.Claims{}
|
|
|
|
)
|
|
|
|
|
|
|
|
// Validate payload
|
|
|
|
token, err := jwt.ParseSigned(ott)
|
|
|
|
if err != nil {
|
2018-10-19 05:26:39 +00:00
|
|
|
return nil, &apiError{errors.Wrapf(err, "authorize: error parsing token"),
|
2018-10-05 21:48:36 +00:00
|
|
|
http.StatusUnauthorized, errContext}
|
|
|
|
}
|
|
|
|
|
|
|
|
kid := token.Headers[0].KeyID // JWT will only have 1 header.
|
|
|
|
if len(kid) == 0 {
|
2018-10-19 05:26:39 +00:00
|
|
|
return nil, &apiError{errors.New("authorize: token KeyID cannot be empty"),
|
2018-10-05 21:48:36 +00:00
|
|
|
http.StatusUnauthorized, errContext}
|
|
|
|
}
|
|
|
|
val, ok := a.provisionerIDIndex.Load(kid)
|
|
|
|
if !ok {
|
2018-10-19 05:26:39 +00:00
|
|
|
return nil, &apiError{errors.Errorf("authorize: provisioner with KeyID %s not found", kid),
|
2018-10-05 21:48:36 +00:00
|
|
|
http.StatusUnauthorized, errContext}
|
|
|
|
}
|
2018-10-19 05:26:39 +00:00
|
|
|
p, ok := val.(*Provisioner)
|
2018-10-05 21:48:36 +00:00
|
|
|
if !ok {
|
2018-10-19 05:26:39 +00:00
|
|
|
return nil, &apiError{errors.Errorf("authorize: invalid provisioner type"),
|
|
|
|
http.StatusInternalServerError, errContext}
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = token.Claims(p.Key, &claims); err != nil {
|
|
|
|
return nil, &apiError{err, http.StatusUnauthorized, errContext}
|
|
|
|
}
|
|
|
|
|
|
|
|
// According to "rfc7519 JSON Web Token" acceptable skew should be no
|
|
|
|
// more than a few minutes.
|
|
|
|
if err = claims.ValidateWithLeeway(jwt.Expected{
|
|
|
|
Issuer: p.Issuer,
|
|
|
|
}, time.Minute); err != nil {
|
2018-10-19 05:26:39 +00:00
|
|
|
return nil, &apiError{errors.Wrapf(err, "authorize: invalid token"),
|
2018-10-05 21:48:36 +00:00
|
|
|
http.StatusUnauthorized, errContext}
|
|
|
|
}
|
|
|
|
|
2018-10-25 01:59:48 +00:00
|
|
|
// Do not accept tokens issued before the start of the ca.
|
|
|
|
// This check is meant as a stopgap solution to the current lack of a persistence layer.
|
|
|
|
if a.config.AuthorityConfig != nil && !a.config.AuthorityConfig.DisableIssuedAtCheck {
|
|
|
|
if claims.IssuedAt > 0 && claims.IssuedAt.Time().Before(a.startTime) {
|
|
|
|
return nil, &apiError{errors.New("token issued before the bootstrap of certificate authority"),
|
|
|
|
http.StatusUnauthorized, errContext}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-20 01:25:59 +00:00
|
|
|
if !containsAtLeastOneAudience(claims.Audience, a.audiences) {
|
2018-10-19 05:26:39 +00:00
|
|
|
return nil, &apiError{errors.New("authorize: token audience invalid"), http.StatusUnauthorized,
|
2018-10-05 21:48:36 +00:00
|
|
|
errContext}
|
|
|
|
}
|
|
|
|
|
|
|
|
if claims.Subject == "" {
|
2018-10-19 05:26:39 +00:00
|
|
|
return nil, &apiError{errors.New("authorize: token subject cannot be empty"),
|
2018-10-05 21:48:36 +00:00
|
|
|
http.StatusUnauthorized, errContext}
|
|
|
|
}
|
2018-10-19 05:26:39 +00:00
|
|
|
|
|
|
|
signOps := []interface{}{
|
|
|
|
&commonNameClaim{claims.Subject},
|
|
|
|
&dnsNamesClaim{claims.Subject},
|
|
|
|
&ipAddressesClaim{claims.Subject},
|
|
|
|
withIssuerAlternativeNameExtension(p.Issuer + ":" + p.Key.KeyID),
|
|
|
|
p,
|
|
|
|
}
|
2018-10-05 21:48:36 +00:00
|
|
|
|
|
|
|
// Store the token to protect against reuse.
|
|
|
|
if _, ok := a.ottMap.LoadOrStore(claims.ID, &idUsed{
|
|
|
|
UsedAt: time.Now().Unix(),
|
|
|
|
Subject: claims.Subject,
|
|
|
|
}); ok {
|
|
|
|
return nil, &apiError{errors.Errorf("token already used"), http.StatusUnauthorized,
|
|
|
|
errContext}
|
|
|
|
}
|
|
|
|
|
2018-10-19 05:26:39 +00:00
|
|
|
return signOps, nil
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|