2019-03-05 01:58:20 +00:00
|
|
|
package provisioner
|
|
|
|
|
|
|
|
import (
|
2019-03-06 22:54:56 +00:00
|
|
|
"crypto/x509"
|
2019-03-05 01:58:20 +00:00
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2019-03-15 20:49:50 +00:00
|
|
|
"strings"
|
2019-03-05 01:58:20 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/smallstep/cli/jose"
|
|
|
|
)
|
|
|
|
|
2019-03-06 22:54:56 +00:00
|
|
|
// openIDConfiguration contains the necessary properties in the
|
|
|
|
// `/.well-known/openid-configuration` document.
|
2019-03-05 01:58:20 +00:00
|
|
|
type openIDConfiguration struct {
|
|
|
|
Issuer string `json:"issuer"`
|
|
|
|
JWKSetURI string `json:"jwks_uri"`
|
|
|
|
}
|
|
|
|
|
2019-03-11 19:48:46 +00:00
|
|
|
// Validate validates the values in a well-known OpenID configuration endpoint.
|
|
|
|
func (c openIDConfiguration) Validate() error {
|
|
|
|
switch {
|
|
|
|
case c.Issuer == "":
|
|
|
|
return errors.New("issuer cannot be empty")
|
|
|
|
case c.JWKSetURI == "":
|
|
|
|
return errors.New("jwks_uri cannot be empty")
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-05 01:58:20 +00:00
|
|
|
// openIDPayload represents the fields on the id_token JWT payload.
|
|
|
|
type openIDPayload struct {
|
|
|
|
jose.Claims
|
2019-06-25 08:50:55 +00:00
|
|
|
AtHash string `json:"at_hash"`
|
|
|
|
AuthorizedParty string `json:"azp"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
EmailVerified bool `json:"email_verified"`
|
|
|
|
Hd string `json:"hd"`
|
|
|
|
Nonce string `json:"nonce"`
|
|
|
|
Groups []string `json:"groups"`
|
2019-03-05 01:58:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OIDC represents an OAuth 2.0 OpenID Connect provider.
|
2019-03-15 01:00:11 +00:00
|
|
|
//
|
2019-03-15 18:10:52 +00:00
|
|
|
// ClientSecret is mandatory, but it can be an empty string.
|
2019-03-05 01:58:20 +00:00
|
|
|
type OIDC struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
ClientID string `json:"clientID"`
|
2019-03-15 18:10:52 +00:00
|
|
|
ClientSecret string `json:"clientSecret"`
|
2019-03-05 01:58:20 +00:00
|
|
|
ConfigurationEndpoint string `json:"configurationEndpoint"`
|
2019-03-19 18:23:18 +00:00
|
|
|
Admins []string `json:"admins,omitempty"`
|
|
|
|
Domains []string `json:"domains,omitempty"`
|
2019-06-25 08:50:55 +00:00
|
|
|
Groups []string `json:"groups,omitempty"`
|
2019-03-05 01:58:20 +00:00
|
|
|
Claims *Claims `json:"claims,omitempty"`
|
|
|
|
configuration openIDConfiguration
|
|
|
|
keyStore *keyStore
|
2019-03-19 22:10:52 +00:00
|
|
|
claimer *Claimer
|
2019-03-05 01:58:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsAdmin returns true if the given email is in the Admins whitelist, false
|
|
|
|
// otherwise.
|
|
|
|
func (o *OIDC) IsAdmin(email string) bool {
|
2019-03-15 20:49:50 +00:00
|
|
|
email = sanitizeEmail(email)
|
2019-03-05 01:58:20 +00:00
|
|
|
for _, e := range o.Admins {
|
2019-03-15 20:49:50 +00:00
|
|
|
if email == sanitizeEmail(e) {
|
2019-03-05 01:58:20 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-03-15 20:49:50 +00:00
|
|
|
func sanitizeEmail(email string) string {
|
|
|
|
if i := strings.LastIndex(email, "@"); i >= 0 {
|
|
|
|
email = email[:i] + strings.ToLower(email[i:])
|
|
|
|
}
|
|
|
|
return email
|
|
|
|
}
|
|
|
|
|
2019-03-06 22:54:56 +00:00
|
|
|
// GetID returns the provisioner unique identifier, the OIDC provisioner the
|
|
|
|
// uses the clientID for this.
|
|
|
|
func (o *OIDC) GetID() string {
|
|
|
|
return o.ClientID
|
|
|
|
}
|
|
|
|
|
2019-03-05 08:07:13 +00:00
|
|
|
// GetTokenID returns the provisioner unique identifier, the OIDC provisioner the
|
|
|
|
// uses the clientID for this.
|
|
|
|
func (o *OIDC) GetTokenID(ott string) (string, error) {
|
|
|
|
// Validate payload
|
|
|
|
token, err := jose.ParseSigned(ott)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "error parsing token")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get claims w/out verification. We need to look up the provisioner
|
|
|
|
// key in order to verify the claims and we need the issuer from the claims
|
|
|
|
// before we can look up the provisioner.
|
|
|
|
var claims openIDPayload
|
|
|
|
if err = token.UnsafeClaimsWithoutVerification(&claims); err != nil {
|
|
|
|
return "", errors.Wrap(err, "error verifying claims")
|
|
|
|
}
|
|
|
|
return claims.Nonce, nil
|
|
|
|
}
|
|
|
|
|
2019-03-06 22:54:56 +00:00
|
|
|
// GetName returns the name of the provisioner.
|
|
|
|
func (o *OIDC) GetName() string {
|
|
|
|
return o.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetType returns the type of provisioner.
|
|
|
|
func (o *OIDC) GetType() Type {
|
|
|
|
return TypeOIDC
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetEncryptedKey is not available in an OIDC provisioner.
|
|
|
|
func (o *OIDC) GetEncryptedKey() (kid string, key string, ok bool) {
|
|
|
|
return "", "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init validates and initializes the OIDC provider.
|
2019-03-07 02:32:56 +00:00
|
|
|
func (o *OIDC) Init(config Config) (err error) {
|
2019-03-05 01:58:20 +00:00
|
|
|
switch {
|
2019-03-13 22:33:52 +00:00
|
|
|
case o.Type == "":
|
|
|
|
return errors.New("type cannot be empty")
|
2019-03-05 01:58:20 +00:00
|
|
|
case o.Name == "":
|
|
|
|
return errors.New("name cannot be empty")
|
|
|
|
case o.ClientID == "":
|
|
|
|
return errors.New("clientID cannot be empty")
|
|
|
|
case o.ConfigurationEndpoint == "":
|
|
|
|
return errors.New("configurationEndpoint cannot be empty")
|
|
|
|
}
|
|
|
|
|
2019-03-06 22:54:56 +00:00
|
|
|
// Update claims with global ones
|
2019-03-19 22:10:52 +00:00
|
|
|
if o.claimer, err = NewClaimer(o.Claims, config.Claims); err != nil {
|
2019-03-06 22:54:56 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-03-19 22:10:52 +00:00
|
|
|
|
2019-03-11 19:48:46 +00:00
|
|
|
// Decode and validate openid-configuration endpoint
|
2019-03-06 22:54:56 +00:00
|
|
|
if err := getAndDecode(o.ConfigurationEndpoint, &o.configuration); err != nil {
|
2019-03-05 01:58:20 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-03-11 19:48:46 +00:00
|
|
|
if err := o.configuration.Validate(); err != nil {
|
|
|
|
return errors.Wrapf(err, "error parsing %s", o.ConfigurationEndpoint)
|
2019-03-05 01:58:20 +00:00
|
|
|
}
|
|
|
|
// Get JWK key set
|
2019-03-06 22:54:56 +00:00
|
|
|
o.keyStore, err = newKeyStore(o.configuration.JWKSetURI)
|
2019-03-05 01:58:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidatePayload validates the given token payload.
|
|
|
|
func (o *OIDC) ValidatePayload(p openIDPayload) error {
|
|
|
|
// According to "rfc7519 JSON Web Token" acceptable skew should be no more
|
|
|
|
// than a few minutes.
|
|
|
|
if err := p.ValidateWithLeeway(jose.Expected{
|
|
|
|
Issuer: o.configuration.Issuer,
|
|
|
|
Audience: jose.Audience{o.ClientID},
|
2019-03-11 18:12:47 +00:00
|
|
|
Time: time.Now().UTC(),
|
2019-03-05 01:58:20 +00:00
|
|
|
}, time.Minute); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to validate payload")
|
|
|
|
}
|
2019-03-15 20:49:50 +00:00
|
|
|
|
|
|
|
// Validate azp if present
|
2019-03-05 01:58:20 +00:00
|
|
|
if p.AuthorizedParty != "" && p.AuthorizedParty != o.ClientID {
|
|
|
|
return errors.New("failed to validate payload: invalid azp")
|
|
|
|
}
|
2019-03-15 20:49:50 +00:00
|
|
|
|
|
|
|
// Enforce an email claim
|
|
|
|
if p.Email == "" {
|
|
|
|
return errors.New("failed to validate payload: email not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate domains (case-insensitive)
|
|
|
|
if !o.IsAdmin(p.Email) && len(o.Domains) > 0 {
|
|
|
|
email := sanitizeEmail(p.Email)
|
|
|
|
var found bool
|
|
|
|
for _, d := range o.Domains {
|
|
|
|
if strings.HasSuffix(email, "@"+strings.ToLower(d)) {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return errors.New("failed to validate payload: email is not allowed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-25 08:50:55 +00:00
|
|
|
// Filter by oidc group claim
|
|
|
|
if len(o.Groups) > 0 {
|
|
|
|
var found bool
|
|
|
|
for _, group := range o.Groups {
|
|
|
|
for _, g := range p.Groups {
|
|
|
|
if g == group {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return errors.New("validation failed: invalid group")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-05 01:58:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-05 08:07:13 +00:00
|
|
|
// authorizeToken applies the most common provisioner authorization claims,
|
|
|
|
// leaving the rest to context specific methods.
|
|
|
|
func (o *OIDC) authorizeToken(token string) (*openIDPayload, error) {
|
2019-03-05 01:58:20 +00:00
|
|
|
jwt, err := jose.ParseSigned(token)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error parsing token")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse claims to get the kid
|
2019-03-06 22:54:56 +00:00
|
|
|
var claims openIDPayload
|
2019-03-05 01:58:20 +00:00
|
|
|
if err := jwt.UnsafeClaimsWithoutVerification(&claims); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error parsing claims")
|
|
|
|
}
|
|
|
|
|
|
|
|
found := false
|
|
|
|
kid := jwt.Headers[0].KeyID
|
|
|
|
keys := o.keyStore.Get(kid)
|
|
|
|
for _, key := range keys {
|
|
|
|
if err := jwt.Claims(key, &claims); err == nil {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return nil, errors.New("cannot validate token")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := o.ValidatePayload(claims); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-03-05 08:07:13 +00:00
|
|
|
return &claims, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AuthorizeRevoke returns an error if the provisioner does not have rights to
|
|
|
|
// revoke the certificate with serial number in the `sub` property.
|
|
|
|
// Only tokens generated by an admin have the right to revoke a certificate.
|
|
|
|
func (o *OIDC) AuthorizeRevoke(token string) error {
|
|
|
|
claims, err := o.authorizeToken(token)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only admins can revoke certificates.
|
|
|
|
if o.IsAdmin(claims.Email) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.New("cannot revoke with non-admin token")
|
|
|
|
}
|
|
|
|
|
|
|
|
// AuthorizeSign validates the given token.
|
|
|
|
func (o *OIDC) AuthorizeSign(token string) ([]SignOption, error) {
|
|
|
|
claims, err := o.authorizeToken(token)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-11 19:48:46 +00:00
|
|
|
// Admins should be able to authorize any SAN
|
2019-03-05 01:58:20 +00:00
|
|
|
if o.IsAdmin(claims.Email) {
|
2019-03-11 19:48:46 +00:00
|
|
|
return []SignOption{
|
2019-03-19 22:10:52 +00:00
|
|
|
profileDefaultDuration(o.claimer.DefaultTLSCertDuration()),
|
2019-03-11 19:48:46 +00:00
|
|
|
newProvisionerExtensionOption(TypeOIDC, o.Name, o.ClientID),
|
2019-03-19 22:10:52 +00:00
|
|
|
newValidityValidator(o.claimer.MinTLSCertDuration(), o.claimer.MaxTLSCertDuration()),
|
2019-03-11 19:48:46 +00:00
|
|
|
}, nil
|
2019-03-05 01:58:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return []SignOption{
|
|
|
|
emailOnlyIdentity(claims.Email),
|
2019-03-19 22:10:52 +00:00
|
|
|
profileDefaultDuration(o.claimer.DefaultTLSCertDuration()),
|
2019-03-06 22:54:56 +00:00
|
|
|
newProvisionerExtensionOption(TypeOIDC, o.Name, o.ClientID),
|
2019-03-19 22:10:52 +00:00
|
|
|
newValidityValidator(o.claimer.MinTLSCertDuration(), o.claimer.MaxTLSCertDuration()),
|
2019-03-05 01:58:20 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-03-06 22:54:56 +00:00
|
|
|
// AuthorizeRenewal returns an error if the renewal is disabled.
|
|
|
|
func (o *OIDC) AuthorizeRenewal(cert *x509.Certificate) error {
|
2019-03-19 22:10:52 +00:00
|
|
|
if o.claimer.IsDisableRenewal() {
|
2019-03-06 22:54:56 +00:00
|
|
|
return errors.Errorf("renew is disabled for provisioner %s", o.GetID())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-05 01:58:20 +00:00
|
|
|
func getAndDecode(uri string, v interface{}) error {
|
|
|
|
resp, err := http.Get(uri)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to connect to %s", uri)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if err := json.NewDecoder(resp.Body).Decode(v); err != nil {
|
|
|
|
return errors.Wrapf(err, "error reading %s", uri)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|