2016-12-21 01:24:17 +00:00
|
|
|
// Package saml contains login methods for SAML.
|
|
|
|
package saml
|
|
|
|
|
|
|
|
import (
|
2018-09-26 09:50:34 +00:00
|
|
|
"bytes"
|
2016-12-21 01:24:17 +00:00
|
|
|
"crypto/x509"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/pem"
|
|
|
|
"encoding/xml"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"strings"
|
2020-10-17 21:02:29 +00:00
|
|
|
"sync"
|
2016-12-21 01:24:17 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/beevik/etree"
|
2020-12-08 03:01:28 +00:00
|
|
|
xrv "github.com/mattermost/xml-roundtrip-validator"
|
|
|
|
"github.com/pkg/errors"
|
2019-12-18 14:53:34 +00:00
|
|
|
dsig "github.com/russellhaering/goxmldsig"
|
|
|
|
"github.com/russellhaering/goxmldsig/etreeutils"
|
|
|
|
|
2019-02-22 12:19:23 +00:00
|
|
|
"github.com/dexidp/dex/connector"
|
2019-09-10 13:13:50 +00:00
|
|
|
"github.com/dexidp/dex/pkg/groups"
|
2019-02-22 12:19:23 +00:00
|
|
|
"github.com/dexidp/dex/pkg/log"
|
2016-12-21 01:24:17 +00:00
|
|
|
)
|
|
|
|
|
2019-07-30 09:08:57 +00:00
|
|
|
// nolint
|
2016-12-21 01:24:17 +00:00
|
|
|
const (
|
|
|
|
bindingRedirect = "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect"
|
|
|
|
bindingPOST = "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"
|
|
|
|
|
|
|
|
nameIDFormatEmailAddress = "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress"
|
|
|
|
nameIDFormatUnspecified = "urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified"
|
|
|
|
nameIDFormatX509Subject = "urn:oasis:names:tc:SAML:1.1:nameid-format:X509SubjectName"
|
|
|
|
nameIDFormatWindowsDN = "urn:oasis:names:tc:SAML:1.1:nameid-format:WindowsDomainQualifiedName"
|
|
|
|
nameIDFormatEncrypted = "urn:oasis:names:tc:SAML:2.0:nameid-format:encrypted"
|
|
|
|
nameIDFormatEntity = "urn:oasis:names:tc:SAML:2.0:nameid-format:entity"
|
|
|
|
nameIDFormatKerberos = "urn:oasis:names:tc:SAML:2.0:nameid-format:kerberos"
|
|
|
|
nameIDFormatPersistent = "urn:oasis:names:tc:SAML:2.0:nameid-format:persistent"
|
|
|
|
nameIDformatTransient = "urn:oasis:names:tc:SAML:2.0:nameid-format:transient"
|
2017-01-26 18:05:40 +00:00
|
|
|
|
|
|
|
// top level status codes
|
|
|
|
statusCodeSuccess = "urn:oasis:names:tc:SAML:2.0:status:Success"
|
|
|
|
|
|
|
|
// subject confirmation methods
|
|
|
|
subjectConfirmationMethodBearer = "urn:oasis:names:tc:SAML:2.0:cm:bearer"
|
|
|
|
|
|
|
|
// allowed clock drift for timestamp validation
|
|
|
|
allowedClockDrift = time.Duration(30) * time.Second
|
2016-12-21 01:24:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
nameIDFormats = []string{
|
|
|
|
nameIDFormatEmailAddress,
|
|
|
|
nameIDFormatUnspecified,
|
|
|
|
nameIDFormatX509Subject,
|
|
|
|
nameIDFormatWindowsDN,
|
|
|
|
nameIDFormatEncrypted,
|
|
|
|
nameIDFormatEntity,
|
|
|
|
nameIDFormatKerberos,
|
|
|
|
nameIDFormatPersistent,
|
|
|
|
nameIDformatTransient,
|
|
|
|
}
|
|
|
|
nameIDFormatLookup = make(map[string]string)
|
|
|
|
|
2020-10-17 21:02:29 +00:00
|
|
|
lookupOnce sync.Once
|
|
|
|
)
|
2016-12-21 01:24:17 +00:00
|
|
|
|
|
|
|
// Config represents configuration options for the SAML provider.
|
|
|
|
type Config struct {
|
|
|
|
// TODO(ericchiang): A bunch of these fields could be auto-filled if
|
|
|
|
// we supported SAML metadata discovery.
|
|
|
|
//
|
|
|
|
// https://www.oasis-open.org/committees/download.php/35391/sstc-saml-metadata-errata-2.0-wd-04-diff.pdf
|
|
|
|
|
2017-03-29 01:06:49 +00:00
|
|
|
EntityIssuer string `json:"entityIssuer"`
|
|
|
|
SSOIssuer string `json:"ssoIssuer"`
|
|
|
|
SSOURL string `json:"ssoURL"`
|
2016-12-21 01:24:17 +00:00
|
|
|
|
|
|
|
// X509 CA file or raw data to verify XML signatures.
|
|
|
|
CA string `json:"ca"`
|
|
|
|
CAData []byte `json:"caData"`
|
|
|
|
|
|
|
|
InsecureSkipSignatureValidation bool `json:"insecureSkipSignatureValidation"`
|
|
|
|
|
|
|
|
// Assertion attribute names to lookup various claims with.
|
|
|
|
UsernameAttr string `json:"usernameAttr"`
|
|
|
|
EmailAttr string `json:"emailAttr"`
|
|
|
|
GroupsAttr string `json:"groupsAttr"`
|
|
|
|
// If GroupsDelim is supplied the connector assumes groups are returned as a
|
|
|
|
// single string instead of multiple attribute values. This delimiter will be
|
|
|
|
// used split the groups string.
|
2019-09-10 13:13:50 +00:00
|
|
|
GroupsDelim string `json:"groupsDelim"`
|
|
|
|
AllowedGroups []string `json:"allowedGroups"`
|
2020-05-11 09:42:26 +00:00
|
|
|
FilterGroups bool `json:"filterGroups"`
|
2019-09-10 13:13:50 +00:00
|
|
|
RedirectURI string `json:"redirectURI"`
|
2016-12-21 01:24:17 +00:00
|
|
|
|
|
|
|
// Requested format of the NameID. The NameID value is is mapped to the ID Token
|
|
|
|
// 'sub' claim.
|
|
|
|
//
|
|
|
|
// This can be an abbreviated form of the full URI with just the last component. For
|
|
|
|
// example, if this value is set to "emailAddress" the format will resolve to:
|
|
|
|
//
|
|
|
|
// urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress
|
|
|
|
//
|
|
|
|
// If no value is specified, this value defaults to:
|
|
|
|
//
|
|
|
|
// urn:oasis:names:tc:SAML:2.0:nameid-format:persistent
|
|
|
|
//
|
|
|
|
NameIDPolicyFormat string `json:"nameIDPolicyFormat"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type certStore struct {
|
|
|
|
certs []*x509.Certificate
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c certStore) Certificates() (roots []*x509.Certificate, err error) {
|
|
|
|
return c.certs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open validates the config and returns a connector. It does not actually
|
|
|
|
// validate connectivity with the provider.
|
2019-02-22 12:19:23 +00:00
|
|
|
func (c *Config) Open(id string, logger log.Logger) (connector.Connector, error) {
|
2016-12-21 01:24:17 +00:00
|
|
|
return c.openConnector(logger)
|
|
|
|
}
|
|
|
|
|
2019-02-22 12:19:23 +00:00
|
|
|
func (c *Config) openConnector(logger log.Logger) (*provider, error) {
|
2016-12-21 01:24:17 +00:00
|
|
|
requiredFields := []struct {
|
|
|
|
name, val string
|
|
|
|
}{
|
|
|
|
{"ssoURL", c.SSOURL},
|
|
|
|
{"usernameAttr", c.UsernameAttr},
|
|
|
|
{"emailAttr", c.EmailAttr},
|
|
|
|
{"redirectURI", c.RedirectURI},
|
|
|
|
}
|
|
|
|
var missing []string
|
|
|
|
for _, f := range requiredFields {
|
|
|
|
if f.val == "" {
|
|
|
|
missing = append(missing, f.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch len(missing) {
|
|
|
|
case 0:
|
|
|
|
case 1:
|
|
|
|
return nil, fmt.Errorf("missing required field %q", missing[0])
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("missing required fields %q", missing)
|
|
|
|
}
|
|
|
|
|
|
|
|
p := &provider{
|
2019-09-10 13:13:50 +00:00
|
|
|
entityIssuer: c.EntityIssuer,
|
|
|
|
ssoIssuer: c.SSOIssuer,
|
|
|
|
ssoURL: c.SSOURL,
|
|
|
|
now: time.Now,
|
|
|
|
usernameAttr: c.UsernameAttr,
|
|
|
|
emailAttr: c.EmailAttr,
|
|
|
|
groupsAttr: c.GroupsAttr,
|
|
|
|
groupsDelim: c.GroupsDelim,
|
|
|
|
allowedGroups: c.AllowedGroups,
|
2020-05-11 09:42:26 +00:00
|
|
|
filterGroups: c.FilterGroups,
|
2019-09-10 13:13:50 +00:00
|
|
|
redirectURI: c.RedirectURI,
|
|
|
|
logger: logger,
|
2016-12-21 01:24:17 +00:00
|
|
|
|
|
|
|
nameIDPolicyFormat: c.NameIDPolicyFormat,
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.nameIDPolicyFormat == "" {
|
|
|
|
p.nameIDPolicyFormat = nameIDFormatPersistent
|
|
|
|
} else {
|
2020-10-17 21:02:29 +00:00
|
|
|
lookupOnce.Do(func() {
|
|
|
|
suffix := func(s, sep string) string {
|
|
|
|
if i := strings.LastIndex(s, sep); i > 0 {
|
|
|
|
return s[i+1:]
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
for _, format := range nameIDFormats {
|
|
|
|
nameIDFormatLookup[suffix(format, ":")] = format
|
|
|
|
nameIDFormatLookup[format] = format
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2016-12-21 01:24:17 +00:00
|
|
|
if format, ok := nameIDFormatLookup[p.nameIDPolicyFormat]; ok {
|
|
|
|
p.nameIDPolicyFormat = format
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("invalid nameIDPolicyFormat: %q", p.nameIDPolicyFormat)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !c.InsecureSkipSignatureValidation {
|
|
|
|
if (c.CA == "") == (c.CAData == nil) {
|
|
|
|
return nil, errors.New("must provide either 'ca' or 'caData'")
|
|
|
|
}
|
|
|
|
|
|
|
|
var caData []byte
|
|
|
|
if c.CA != "" {
|
|
|
|
data, err := ioutil.ReadFile(c.CA)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("read ca file: %v", err)
|
|
|
|
}
|
|
|
|
caData = data
|
|
|
|
} else {
|
|
|
|
caData = c.CAData
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
certs []*x509.Certificate
|
|
|
|
block *pem.Block
|
|
|
|
)
|
|
|
|
for {
|
|
|
|
block, caData = pem.Decode(caData)
|
|
|
|
if block == nil {
|
2018-09-26 09:50:34 +00:00
|
|
|
caData = bytes.TrimSpace(caData)
|
|
|
|
if len(caData) > 0 { // if there's some left, we've been given bad caData
|
|
|
|
return nil, fmt.Errorf("parse cert: trailing data: %q", string(caData))
|
|
|
|
}
|
2016-12-21 01:24:17 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
cert, err := x509.ParseCertificate(block.Bytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("parse cert: %v", err)
|
|
|
|
}
|
|
|
|
certs = append(certs, cert)
|
|
|
|
}
|
|
|
|
if len(certs) == 0 {
|
|
|
|
return nil, errors.New("no certificates found in ca data")
|
|
|
|
}
|
|
|
|
p.validator = dsig.NewDefaultValidationContext(certStore{certs})
|
|
|
|
}
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type provider struct {
|
2017-03-29 01:06:49 +00:00
|
|
|
entityIssuer string
|
|
|
|
ssoIssuer string
|
|
|
|
ssoURL string
|
2016-12-21 01:24:17 +00:00
|
|
|
|
|
|
|
now func() time.Time
|
|
|
|
|
|
|
|
// If nil, don't do signature validation.
|
|
|
|
validator *dsig.ValidationContext
|
|
|
|
|
|
|
|
// Attribute mappings
|
2019-09-10 13:13:50 +00:00
|
|
|
usernameAttr string
|
|
|
|
emailAttr string
|
|
|
|
groupsAttr string
|
|
|
|
groupsDelim string
|
|
|
|
allowedGroups []string
|
2020-05-11 09:42:26 +00:00
|
|
|
filterGroups bool
|
2016-12-21 01:24:17 +00:00
|
|
|
|
|
|
|
redirectURI string
|
|
|
|
|
|
|
|
nameIDPolicyFormat string
|
|
|
|
|
2019-02-22 12:19:23 +00:00
|
|
|
logger log.Logger
|
2016-12-21 01:24:17 +00:00
|
|
|
}
|
|
|
|
|
2017-03-21 20:16:42 +00:00
|
|
|
func (p *provider) POSTData(s connector.Scopes, id string) (action, value string, err error) {
|
2016-12-21 01:24:17 +00:00
|
|
|
r := &authnRequest{
|
|
|
|
ProtocolBinding: bindingPOST,
|
2017-03-21 20:16:42 +00:00
|
|
|
ID: id,
|
2016-12-21 01:24:17 +00:00
|
|
|
IssueInstant: xmlTime(p.now()),
|
|
|
|
Destination: p.ssoURL,
|
|
|
|
NameIDPolicy: &nameIDPolicy{
|
|
|
|
AllowCreate: true,
|
|
|
|
Format: p.nameIDPolicyFormat,
|
|
|
|
},
|
2017-01-26 18:05:40 +00:00
|
|
|
AssertionConsumerServiceURL: p.redirectURI,
|
2016-12-21 01:24:17 +00:00
|
|
|
}
|
2017-03-29 01:06:49 +00:00
|
|
|
if p.entityIssuer != "" {
|
2017-03-21 20:16:42 +00:00
|
|
|
// Issuer for the request is optional. For example, okta always ignores
|
|
|
|
// this value.
|
2017-03-29 01:06:49 +00:00
|
|
|
r.Issuer = &issuer{Issuer: p.entityIssuer}
|
2017-03-21 20:16:42 +00:00
|
|
|
}
|
2016-12-21 01:24:17 +00:00
|
|
|
|
|
|
|
data, err := xml.MarshalIndent(r, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return "", "", fmt.Errorf("marshal authn request: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-03-21 20:16:42 +00:00
|
|
|
// See: https://docs.oasis-open.org/security/saml/v2.0/saml-bindings-2.0-os.pdf
|
|
|
|
// "3.5.4 Message Encoding"
|
2017-01-26 18:05:40 +00:00
|
|
|
return p.ssoURL, base64.StdEncoding.EncodeToString(data), nil
|
2016-12-21 01:24:17 +00:00
|
|
|
}
|
|
|
|
|
2017-04-07 00:11:28 +00:00
|
|
|
// HandlePOST interprets a request from a SAML provider attempting to verify a
|
|
|
|
// user's identity.
|
|
|
|
//
|
|
|
|
// The steps taken are:
|
|
|
|
//
|
2020-12-08 03:01:28 +00:00
|
|
|
// * Validate XML document does not contain malicious inputs.
|
2017-04-07 00:11:28 +00:00
|
|
|
// * Verify signature on XML document (or verify sig on assertion elements).
|
|
|
|
// * Verify various parts of the Assertion element. Conditions, audience, etc.
|
|
|
|
// * Map the Assertion's attribute elements to user info.
|
|
|
|
//
|
2017-03-21 20:16:42 +00:00
|
|
|
func (p *provider) HandlePOST(s connector.Scopes, samlResponse, inResponseTo string) (ident connector.Identity, err error) {
|
2016-12-21 01:24:17 +00:00
|
|
|
rawResp, err := base64.StdEncoding.DecodeString(samlResponse)
|
|
|
|
if err != nil {
|
|
|
|
return ident, fmt.Errorf("decode response: %v", err)
|
|
|
|
}
|
2017-04-04 07:26:51 +00:00
|
|
|
|
2020-12-08 03:01:28 +00:00
|
|
|
byteReader := bytes.NewReader(rawResp)
|
|
|
|
if xrvErr := xrv.Validate(byteReader); xrvErr != nil {
|
|
|
|
return ident, errors.Wrap(xrvErr, "validating XML response")
|
|
|
|
}
|
|
|
|
|
2017-04-07 00:11:28 +00:00
|
|
|
// Root element is allowed to not be signed if the Assertion element is.
|
2017-04-04 07:26:51 +00:00
|
|
|
rootElementSigned := true
|
2016-12-21 01:24:17 +00:00
|
|
|
if p.validator != nil {
|
2017-04-04 07:26:51 +00:00
|
|
|
rawResp, rootElementSigned, err = verifyResponseSig(p.validator, rawResp)
|
|
|
|
if err != nil {
|
2016-12-21 01:24:17 +00:00
|
|
|
return ident, fmt.Errorf("verify signature: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp response
|
|
|
|
if err := xml.Unmarshal(rawResp, &resp); err != nil {
|
|
|
|
return ident, fmt.Errorf("unmarshal response: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-04-07 00:11:28 +00:00
|
|
|
// If the root element isn't signed, there's no reason to inspect these
|
|
|
|
// elements. They're not verified.
|
2017-04-04 07:26:51 +00:00
|
|
|
if rootElementSigned {
|
2017-03-29 01:06:49 +00:00
|
|
|
if p.ssoIssuer != "" && resp.Issuer != nil && resp.Issuer.Issuer != p.ssoIssuer {
|
2017-04-06 21:50:44 +00:00
|
|
|
return ident, fmt.Errorf("expected Issuer value %s, got %s", p.ssoIssuer, resp.Issuer.Issuer)
|
2017-04-04 07:26:51 +00:00
|
|
|
}
|
2017-03-21 20:16:42 +00:00
|
|
|
|
2017-04-04 07:26:51 +00:00
|
|
|
// Verify InResponseTo value matches the expected ID associated with
|
|
|
|
// the RelayState.
|
|
|
|
if resp.InResponseTo != inResponseTo {
|
|
|
|
return ident, fmt.Errorf("expected InResponseTo value %s, got %s", inResponseTo, resp.InResponseTo)
|
|
|
|
}
|
2017-03-21 20:16:42 +00:00
|
|
|
|
2017-04-04 07:26:51 +00:00
|
|
|
// Destination is optional.
|
|
|
|
if resp.Destination != "" && resp.Destination != p.redirectURI {
|
|
|
|
return ident, fmt.Errorf("expected destination %q got %q", p.redirectURI, resp.Destination)
|
2017-04-07 00:11:28 +00:00
|
|
|
}
|
2016-12-21 01:24:17 +00:00
|
|
|
|
2017-04-07 00:11:28 +00:00
|
|
|
// Status is a required element.
|
|
|
|
if resp.Status == nil {
|
2019-12-18 14:50:36 +00:00
|
|
|
return ident, fmt.Errorf("response did not contain a Status element")
|
2017-04-04 07:26:51 +00:00
|
|
|
}
|
2016-12-21 01:24:17 +00:00
|
|
|
|
2017-04-07 00:11:28 +00:00
|
|
|
if err = p.validateStatus(resp.Status); err != nil {
|
2017-04-04 07:26:51 +00:00
|
|
|
return ident, err
|
|
|
|
}
|
2017-01-26 18:05:40 +00:00
|
|
|
}
|
|
|
|
|
2016-12-21 01:24:17 +00:00
|
|
|
assertion := resp.Assertion
|
|
|
|
if assertion == nil {
|
|
|
|
return ident, fmt.Errorf("response did not contain an assertion")
|
|
|
|
}
|
2017-04-07 00:11:28 +00:00
|
|
|
|
|
|
|
// Subject is usually optional, but we need it for the user ID, so complain
|
|
|
|
// if it's not present.
|
2016-12-21 01:24:17 +00:00
|
|
|
subject := assertion.Subject
|
|
|
|
if subject == nil {
|
|
|
|
return ident, fmt.Errorf("response did not contain a subject")
|
|
|
|
}
|
|
|
|
|
2017-04-07 00:11:28 +00:00
|
|
|
// Validate that the response is to the request we originally sent.
|
|
|
|
if err = p.validateSubject(subject, inResponseTo); err != nil {
|
2017-01-26 18:05:40 +00:00
|
|
|
return ident, err
|
|
|
|
}
|
2017-04-07 00:11:28 +00:00
|
|
|
|
|
|
|
// Conditions element is optional, but must be validated if present.
|
|
|
|
if assertion.Conditions != nil {
|
|
|
|
// Validate that dex is the intended audience of this response.
|
|
|
|
if err = p.validateConditions(assertion.Conditions); err != nil {
|
|
|
|
return ident, err
|
|
|
|
}
|
2017-01-26 18:05:40 +00:00
|
|
|
}
|
|
|
|
|
2016-12-21 01:24:17 +00:00
|
|
|
switch {
|
|
|
|
case subject.NameID != nil:
|
|
|
|
if ident.UserID = subject.NameID.Value; ident.UserID == "" {
|
2020-10-17 21:02:29 +00:00
|
|
|
return ident, fmt.Errorf("element NameID does not contain a value")
|
2016-12-21 01:24:17 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
return ident, fmt.Errorf("subject does not contain an NameID element")
|
|
|
|
}
|
|
|
|
|
2017-04-07 00:11:28 +00:00
|
|
|
// After verifying the assertion, map data in the attribute statements to
|
|
|
|
// various user info.
|
2016-12-21 01:24:17 +00:00
|
|
|
attributes := assertion.AttributeStatement
|
|
|
|
if attributes == nil {
|
|
|
|
return ident, fmt.Errorf("response did not contain a AttributeStatement")
|
|
|
|
}
|
|
|
|
|
2017-08-11 17:17:30 +00:00
|
|
|
// Log the actual attributes we got back from the server. This helps debug
|
|
|
|
// configuration errors on the server side, where the SAML server doesn't
|
|
|
|
// send us the correct attributes.
|
|
|
|
p.logger.Infof("parsed and verified saml response attributes %s", attributes)
|
|
|
|
|
2017-04-07 00:11:28 +00:00
|
|
|
// Grab the email.
|
2016-12-21 01:24:17 +00:00
|
|
|
if ident.Email, _ = attributes.get(p.emailAttr); ident.Email == "" {
|
2017-03-21 20:16:42 +00:00
|
|
|
return ident, fmt.Errorf("no attribute with name %q: %s", p.emailAttr, attributes.names())
|
2016-12-21 01:24:17 +00:00
|
|
|
}
|
2017-04-07 00:11:28 +00:00
|
|
|
// TODO(ericchiang): Does SAML have an email_verified equivalent?
|
2016-12-21 01:24:17 +00:00
|
|
|
ident.EmailVerified = true
|
|
|
|
|
2017-04-07 00:11:28 +00:00
|
|
|
// Grab the username.
|
2016-12-21 01:24:17 +00:00
|
|
|
if ident.Username, _ = attributes.get(p.usernameAttr); ident.Username == "" {
|
2017-03-21 20:16:42 +00:00
|
|
|
return ident, fmt.Errorf("no attribute with name %q: %s", p.usernameAttr, attributes.names())
|
2016-12-21 01:24:17 +00:00
|
|
|
}
|
|
|
|
|
2019-09-10 13:13:50 +00:00
|
|
|
if len(p.allowedGroups) == 0 && (!s.Groups || p.groupsAttr == "") {
|
2017-04-07 00:11:28 +00:00
|
|
|
// Groups not requested or not configured. We're done.
|
|
|
|
return ident, nil
|
2016-12-21 01:24:17 +00:00
|
|
|
}
|
|
|
|
|
2019-09-10 13:13:50 +00:00
|
|
|
if len(p.allowedGroups) > 0 && (!s.Groups || p.groupsAttr == "") {
|
|
|
|
// allowedGroups set but no groups or groupsAttr. Disallowing.
|
2019-12-18 14:50:36 +00:00
|
|
|
return ident, fmt.Errorf("user not a member of allowed groups")
|
2019-09-10 13:13:50 +00:00
|
|
|
}
|
|
|
|
|
2017-04-07 00:11:28 +00:00
|
|
|
// Grab the groups.
|
|
|
|
if p.groupsDelim != "" {
|
|
|
|
groupsStr, ok := attributes.get(p.groupsAttr)
|
|
|
|
if !ok {
|
|
|
|
return ident, fmt.Errorf("no attribute with name %q: %s", p.groupsAttr, attributes.names())
|
|
|
|
}
|
|
|
|
// TODO(ericchiang): Do we need to further trim whitespace?
|
|
|
|
ident.Groups = strings.Split(groupsStr, p.groupsDelim)
|
|
|
|
} else {
|
|
|
|
groups, ok := attributes.all(p.groupsAttr)
|
|
|
|
if !ok {
|
|
|
|
return ident, fmt.Errorf("no attribute with name %q: %s", p.groupsAttr, attributes.names())
|
|
|
|
}
|
|
|
|
ident.Groups = groups
|
|
|
|
}
|
2019-09-10 13:13:50 +00:00
|
|
|
|
|
|
|
if len(p.allowedGroups) == 0 {
|
|
|
|
// No allowed groups set, just return the ident
|
|
|
|
return ident, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look for membership in one of the allowed groups
|
|
|
|
groupMatches := groups.Filter(ident.Groups, p.allowedGroups)
|
|
|
|
|
|
|
|
if len(groupMatches) == 0 {
|
|
|
|
// No group membership matches found, disallowing
|
2019-12-18 14:50:36 +00:00
|
|
|
return ident, fmt.Errorf("user not a member of allowed groups")
|
2019-09-10 13:13:50 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 09:42:26 +00:00
|
|
|
if p.filterGroups {
|
|
|
|
ident.Groups = groupMatches
|
|
|
|
}
|
|
|
|
|
2019-09-10 13:13:50 +00:00
|
|
|
// Otherwise, we're good
|
2016-12-21 01:24:17 +00:00
|
|
|
return ident, nil
|
|
|
|
}
|
|
|
|
|
2017-04-07 00:11:28 +00:00
|
|
|
// validateStatus verifies that the response has a good status code or
|
2020-12-20 03:01:50 +00:00
|
|
|
// formats a human readable error based on the bad status.
|
2017-04-07 00:11:28 +00:00
|
|
|
func (p *provider) validateStatus(status *status) error {
|
2017-01-26 18:05:40 +00:00
|
|
|
// StatusCode is mandatory in the Status type
|
|
|
|
statusCode := status.StatusCode
|
|
|
|
if statusCode == nil {
|
|
|
|
return fmt.Errorf("response did not contain a StatusCode")
|
|
|
|
}
|
2017-04-07 00:11:28 +00:00
|
|
|
|
2017-01-26 18:05:40 +00:00
|
|
|
if statusCode.Value != statusCodeSuccess {
|
|
|
|
parts := strings.Split(statusCode.Value, ":")
|
|
|
|
lastPart := parts[len(parts)-1]
|
|
|
|
errorMessage := fmt.Sprintf("status code of the Response was not Success, was %q", lastPart)
|
|
|
|
statusMessage := status.StatusMessage
|
|
|
|
if statusMessage != nil && statusMessage.Value != "" {
|
|
|
|
errorMessage += " -> " + statusMessage.Value
|
|
|
|
}
|
|
|
|
return fmt.Errorf(errorMessage)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-07 00:11:28 +00:00
|
|
|
// validateSubject ensures the response is to the request we expect.
|
|
|
|
//
|
2017-01-26 18:05:40 +00:00
|
|
|
// This is described in the spec "Profiles for the OASIS Security
|
|
|
|
// Assertion Markup Language" in section 3.3 Bearer.
|
|
|
|
// see https://www.oasis-open.org/committees/download.php/35389/sstc-saml-profiles-errata-2.0-wd-06-diff.pdf
|
2017-04-07 00:11:28 +00:00
|
|
|
//
|
|
|
|
// Some of these fields are optional, but we're going to be strict here since
|
2020-12-20 03:01:46 +00:00
|
|
|
// we have no other way of guaranteeing that this is actually the response to
|
2017-04-07 00:11:28 +00:00
|
|
|
// the request we expect.
|
|
|
|
func (p *provider) validateSubject(subject *subject, inResponseTo string) error {
|
|
|
|
// Optional according to the spec, but again, we're going to be strict here.
|
|
|
|
if len(subject.SubjectConfirmations) == 0 {
|
2019-12-18 14:50:36 +00:00
|
|
|
return fmt.Errorf("subject contained no SubjectConfirmations")
|
2017-04-07 00:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var errs []error
|
|
|
|
// One of these must match our assumptions, not all.
|
|
|
|
for _, c := range subject.SubjectConfirmations {
|
|
|
|
err := func() error {
|
|
|
|
if c.Method != subjectConfirmationMethodBearer {
|
|
|
|
return fmt.Errorf("unexpected subject confirmation method: %v", c.Method)
|
2017-01-26 18:05:40 +00:00
|
|
|
}
|
2017-04-07 00:11:28 +00:00
|
|
|
|
|
|
|
data := c.SubjectConfirmationData
|
|
|
|
if data == nil {
|
2020-10-17 21:02:29 +00:00
|
|
|
return fmt.Errorf("no SubjectConfirmationData field found in SubjectConfirmation")
|
2017-01-26 18:05:40 +00:00
|
|
|
}
|
2017-04-07 00:11:28 +00:00
|
|
|
if data.InResponseTo != inResponseTo {
|
|
|
|
return fmt.Errorf("expected SubjectConfirmationData InResponseTo value %q, got %q", inResponseTo, data.InResponseTo)
|
2017-01-26 18:05:40 +00:00
|
|
|
}
|
2017-04-07 00:11:28 +00:00
|
|
|
|
|
|
|
notBefore := time.Time(data.NotBefore)
|
|
|
|
notOnOrAfter := time.Time(data.NotOnOrAfter)
|
2017-01-26 18:05:40 +00:00
|
|
|
now := p.now()
|
2017-04-07 00:11:28 +00:00
|
|
|
if !notBefore.IsZero() && before(now, notBefore) {
|
|
|
|
return fmt.Errorf("at %s got response that cannot be processed before %s", now, notBefore)
|
2017-01-26 18:05:40 +00:00
|
|
|
}
|
2017-04-07 00:11:28 +00:00
|
|
|
if !notOnOrAfter.IsZero() && after(now, notOnOrAfter) {
|
|
|
|
return fmt.Errorf("at %s got response that cannot be processed because it expired at %s", now, notOnOrAfter)
|
2017-01-26 18:05:40 +00:00
|
|
|
}
|
2017-04-07 00:11:28 +00:00
|
|
|
if r := data.Recipient; r != "" && r != p.redirectURI {
|
|
|
|
return fmt.Errorf("expected Recipient %q got %q", p.redirectURI, r)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}()
|
|
|
|
if err == nil {
|
|
|
|
// Subject is valid.
|
|
|
|
return nil
|
2017-01-26 18:05:40 +00:00
|
|
|
}
|
2017-04-07 00:11:28 +00:00
|
|
|
errs = append(errs, err)
|
2017-01-26 18:05:40 +00:00
|
|
|
}
|
2017-04-07 00:11:28 +00:00
|
|
|
|
|
|
|
if len(errs) == 1 {
|
|
|
|
return fmt.Errorf("failed to validate subject confirmation: %v", errs[0])
|
2017-01-26 18:05:40 +00:00
|
|
|
}
|
2017-04-07 00:11:28 +00:00
|
|
|
return fmt.Errorf("failed to validate subject confirmation: %v", errs)
|
2017-01-26 18:05:40 +00:00
|
|
|
}
|
|
|
|
|
2017-04-07 00:11:28 +00:00
|
|
|
// validationConditions ensures that dex is the intended audience
|
|
|
|
// for the request, and not another service provider.
|
2017-03-21 20:16:42 +00:00
|
|
|
//
|
|
|
|
// See: https://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf
|
|
|
|
// "2.3.3 Element <Assertion>"
|
2017-04-07 00:11:28 +00:00
|
|
|
func (p *provider) validateConditions(conditions *conditions) error {
|
|
|
|
// Ensure the conditions haven't expired.
|
2017-01-26 18:05:40 +00:00
|
|
|
now := p.now()
|
|
|
|
notBefore := time.Time(conditions.NotBefore)
|
2017-04-07 00:11:28 +00:00
|
|
|
if !notBefore.IsZero() && before(now, notBefore) {
|
|
|
|
return fmt.Errorf("at %s got response that cannot be processed before %s", now, notBefore)
|
2017-01-26 18:05:40 +00:00
|
|
|
}
|
2017-04-07 00:11:28 +00:00
|
|
|
|
2017-01-26 18:05:40 +00:00
|
|
|
notOnOrAfter := time.Time(conditions.NotOnOrAfter)
|
2017-04-07 00:11:28 +00:00
|
|
|
if !notOnOrAfter.IsZero() && after(now, notOnOrAfter) {
|
|
|
|
return fmt.Errorf("at %s got response that cannot be processed because it expired at %s", now, notOnOrAfter)
|
2017-01-26 18:05:40 +00:00
|
|
|
}
|
2017-04-07 00:11:28 +00:00
|
|
|
|
|
|
|
// Sometimes, dex's issuer string can be different than the redirect URI,
|
|
|
|
// but if dex's issuer isn't explicitly provided assume the redirect URI.
|
|
|
|
expAud := p.entityIssuer
|
|
|
|
if expAud == "" {
|
|
|
|
expAud = p.redirectURI
|
|
|
|
}
|
|
|
|
|
|
|
|
// AudienceRestriction elements indicate the intended audience(s) of an
|
|
|
|
// assertion. If dex isn't in these audiences, reject the assertion.
|
|
|
|
//
|
|
|
|
// Note that if there are multiple AudienceRestriction elements, each must
|
|
|
|
// individually contain dex in their audience list.
|
|
|
|
for _, r := range conditions.AudienceRestriction {
|
|
|
|
values := make([]string, len(r.Audiences))
|
|
|
|
issuerInAudiences := false
|
|
|
|
for i, aud := range r.Audiences {
|
|
|
|
if aud.Value == expAud {
|
|
|
|
issuerInAudiences = true
|
|
|
|
break
|
2017-01-26 18:05:40 +00:00
|
|
|
}
|
2017-04-07 00:11:28 +00:00
|
|
|
values[i] = aud.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
if !issuerInAudiences {
|
|
|
|
return fmt.Errorf("required audience %s was not in Response audiences %s", expAud, values)
|
2017-01-26 18:05:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-04 07:26:51 +00:00
|
|
|
// verifyResponseSig attempts to verify the signature of a SAML response or
|
|
|
|
// the assertion.
|
|
|
|
//
|
|
|
|
// If the root element is properly signed, this method returns it.
|
|
|
|
//
|
|
|
|
// The SAML spec requires supporting responses where the root element is
|
|
|
|
// unverified, but the sub <Assertion> elements are signed. In these cases,
|
|
|
|
// this method returns rootVerified=false to indicate that the <Assertion>
|
|
|
|
// elements should be trusted, but all other elements MUST be ignored.
|
|
|
|
//
|
|
|
|
// Note: we still don't support multiple <Assertion> tags. If there are
|
|
|
|
// multiple present this code will only process the first.
|
|
|
|
func verifyResponseSig(validator *dsig.ValidationContext, data []byte) (signed []byte, rootVerified bool, err error) {
|
2016-12-21 01:24:17 +00:00
|
|
|
doc := etree.NewDocument()
|
2017-01-26 18:05:40 +00:00
|
|
|
if err = doc.ReadFromBytes(data); err != nil {
|
2017-04-04 07:26:51 +00:00
|
|
|
return nil, false, fmt.Errorf("parse document: %v", err)
|
2016-12-21 01:24:17 +00:00
|
|
|
}
|
2017-04-04 07:26:51 +00:00
|
|
|
|
2017-01-26 18:05:40 +00:00
|
|
|
response := doc.Root()
|
|
|
|
transformedResponse, err := validator.Validate(response)
|
|
|
|
if err == nil {
|
2017-04-04 07:26:51 +00:00
|
|
|
// Root element is verified, return it.
|
2017-01-26 18:05:40 +00:00
|
|
|
doc.SetRoot(transformedResponse)
|
2017-04-04 07:26:51 +00:00
|
|
|
signed, err = doc.WriteToBytes()
|
|
|
|
return signed, true, err
|
2017-01-26 18:05:40 +00:00
|
|
|
}
|
2017-04-04 07:26:51 +00:00
|
|
|
|
2017-03-22 23:56:25 +00:00
|
|
|
// Ensures xmlns are copied down to the assertion element when they are defined in the root
|
2017-04-04 07:26:51 +00:00
|
|
|
//
|
|
|
|
// TODO: Only select from child elements of the root.
|
2017-03-22 23:56:25 +00:00
|
|
|
assertion, err := etreeutils.NSSelectOne(response, "urn:oasis:names:tc:SAML:2.0:assertion", "Assertion")
|
|
|
|
if err != nil {
|
2017-04-04 07:26:51 +00:00
|
|
|
return nil, false, fmt.Errorf("response does not contain an Assertion element")
|
2017-01-26 18:05:40 +00:00
|
|
|
}
|
|
|
|
transformedAssertion, err := validator.Validate(assertion)
|
2017-04-04 07:26:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, fmt.Errorf("response does not contain a valid signature element: %v", err)
|
2017-01-26 18:05:40 +00:00
|
|
|
}
|
2017-04-04 07:26:51 +00:00
|
|
|
|
|
|
|
// Verified an assertion but not the response. Can't trust any child elements,
|
|
|
|
// except the assertion. Remove them all.
|
|
|
|
for _, el := range response.ChildElements() {
|
|
|
|
response.RemoveChild(el)
|
2016-12-21 01:24:17 +00:00
|
|
|
}
|
2017-04-04 07:26:51 +00:00
|
|
|
|
|
|
|
// We still return the full <Response> element, even though it's unverified
|
|
|
|
// because the <Assertion> element is not a valid XML document on its own.
|
|
|
|
// It still requires the root element to define things like namespaces.
|
|
|
|
response.AddChild(transformedAssertion)
|
|
|
|
signed, err = doc.WriteToBytes()
|
|
|
|
return signed, false, err
|
2016-12-21 01:24:17 +00:00
|
|
|
}
|
|
|
|
|
2017-04-07 00:11:28 +00:00
|
|
|
// before determines if a given time is before the current time, with an
|
|
|
|
// allowed clock drift.
|
|
|
|
func before(now, notBefore time.Time) bool {
|
|
|
|
return now.Add(allowedClockDrift).Before(notBefore)
|
|
|
|
}
|
|
|
|
|
|
|
|
// after determines if a given time is after the current time, with an
|
|
|
|
// allowed clock drift.
|
|
|
|
func after(now, notOnOrAfter time.Time) bool {
|
|
|
|
return now.After(notOnOrAfter.Add(allowedClockDrift))
|
2016-12-21 01:24:17 +00:00
|
|
|
}
|