2016-07-25 20:00:28 +00:00
|
|
|
// Package oidc implements logging in through OpenID Connect providers.
|
|
|
|
package oidc
|
2016-08-08 18:45:17 +00:00
|
|
|
|
|
|
|
import (
|
2017-03-08 18:33:19 +00:00
|
|
|
"context"
|
2016-08-08 18:45:17 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2017-03-20 15:38:52 +00:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2016-08-08 18:45:17 +00:00
|
|
|
|
2016-11-17 23:20:41 +00:00
|
|
|
"github.com/coreos/go-oidc"
|
2016-08-08 18:45:17 +00:00
|
|
|
"golang.org/x/oauth2"
|
|
|
|
|
2018-09-03 06:44:44 +00:00
|
|
|
"github.com/dexidp/dex/connector"
|
2019-02-22 12:19:23 +00:00
|
|
|
"github.com/dexidp/dex/pkg/log"
|
2016-08-08 18:45:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config holds configuration options for OpenID Connect logins.
|
|
|
|
type Config struct {
|
2016-11-03 21:32:23 +00:00
|
|
|
Issuer string `json:"issuer"`
|
|
|
|
ClientID string `json:"clientID"`
|
|
|
|
ClientSecret string `json:"clientSecret"`
|
|
|
|
RedirectURI string `json:"redirectURI"`
|
2016-08-08 18:45:17 +00:00
|
|
|
|
2017-03-20 15:38:52 +00:00
|
|
|
// Causes client_secret to be passed as POST parameters instead of basic
|
|
|
|
// auth. This is specifically "NOT RECOMMENDED" by the OAuth2 RFC, but some
|
|
|
|
// providers require it.
|
|
|
|
//
|
|
|
|
// https://tools.ietf.org/html/rfc6749#section-2.3.1
|
|
|
|
BasicAuthUnsupported *bool `json:"basicAuthUnsupported"`
|
|
|
|
|
2016-11-03 21:32:23 +00:00
|
|
|
Scopes []string `json:"scopes"` // defaults to "profile" and "email"
|
2017-03-20 15:38:52 +00:00
|
|
|
|
2017-06-22 05:56:02 +00:00
|
|
|
// Optional list of whitelisted domains when using Google
|
|
|
|
// If this field is nonempty, only users from a listed domain will be allowed to log in
|
2017-07-21 22:48:21 +00:00
|
|
|
HostedDomains []string `json:"hostedDomains"`
|
2019-03-05 21:24:02 +00:00
|
|
|
|
|
|
|
// Override the value of email_verifed to true in the returned claims
|
|
|
|
InsecureSkipEmailVerified bool `json:"insecureSkipEmailVerified"`
|
2019-04-24 20:58:35 +00:00
|
|
|
|
|
|
|
// GetUserInfo uses the userinfo endpoint to get additional claims for
|
|
|
|
// the token. This is especially useful where upstreams return "thin"
|
|
|
|
// id tokens
|
|
|
|
GetUserInfo bool `json:"getUserInfo"`
|
2019-05-24 03:51:42 +00:00
|
|
|
|
|
|
|
// Configurable key which contains the user id claim
|
|
|
|
UserIDKey string `json:"userIDKey"`
|
2019-06-02 19:33:53 +00:00
|
|
|
|
|
|
|
// Configurable key which contains the user name claim
|
|
|
|
UserNameKey string `json:"userNameKey"`
|
2017-03-20 15:38:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Domains that don't support basic auth. golang.org/x/oauth2 has an internal
|
|
|
|
// list, but it only matches specific URLs, not top level domains.
|
|
|
|
var brokenAuthHeaderDomains = []string{
|
2018-09-03 06:44:44 +00:00
|
|
|
// See: https://github.com/dexidp/dex/issues/859
|
2017-03-20 15:38:52 +00:00
|
|
|
"okta.com",
|
|
|
|
"oktapreview.com",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Detect auth header provider issues for known providers. This lets users
|
|
|
|
// avoid having to explicitly set "basicAuthUnsupported" in their config.
|
|
|
|
//
|
|
|
|
// Setting the config field always overrides values returned by this function.
|
|
|
|
func knownBrokenAuthHeaderProvider(issuerURL string) bool {
|
|
|
|
if u, err := url.Parse(issuerURL); err == nil {
|
|
|
|
for _, host := range brokenAuthHeaderDomains {
|
|
|
|
if u.Host == host || strings.HasSuffix(u.Host, "."+host) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// golang.org/x/oauth2 doesn't do internal locking. Need to do it in this
|
|
|
|
// package ourselves and hope that other packages aren't calling it at the
|
|
|
|
// same time.
|
|
|
|
var registerMu = new(sync.Mutex)
|
|
|
|
|
|
|
|
func registerBrokenAuthHeaderProvider(url string) {
|
|
|
|
registerMu.Lock()
|
|
|
|
defer registerMu.Unlock()
|
|
|
|
|
|
|
|
oauth2.RegisterBrokenAuthHeaderProvider(url)
|
2016-08-08 18:45:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Open returns a connector which can be used to login users through an upstream
|
|
|
|
// OpenID Connect provider.
|
2019-02-22 12:19:23 +00:00
|
|
|
func (c *Config) Open(id string, logger log.Logger) (conn connector.Connector, err error) {
|
2016-08-08 18:45:17 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
provider, err := oidc.NewProvider(ctx, c.Issuer)
|
|
|
|
if err != nil {
|
|
|
|
cancel()
|
|
|
|
return nil, fmt.Errorf("failed to get provider: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-03-20 15:38:52 +00:00
|
|
|
if c.BasicAuthUnsupported != nil {
|
|
|
|
// Setting "basicAuthUnsupported" always overrides our detection.
|
|
|
|
if *c.BasicAuthUnsupported {
|
|
|
|
registerBrokenAuthHeaderProvider(provider.Endpoint().TokenURL)
|
|
|
|
}
|
|
|
|
} else if knownBrokenAuthHeaderProvider(c.Issuer) {
|
|
|
|
registerBrokenAuthHeaderProvider(provider.Endpoint().TokenURL)
|
|
|
|
}
|
|
|
|
|
2016-08-08 18:45:17 +00:00
|
|
|
scopes := []string{oidc.ScopeOpenID}
|
|
|
|
if len(c.Scopes) > 0 {
|
|
|
|
scopes = append(scopes, c.Scopes...)
|
|
|
|
} else {
|
|
|
|
scopes = append(scopes, "profile", "email")
|
|
|
|
}
|
|
|
|
|
2016-10-22 20:36:31 +00:00
|
|
|
clientID := c.ClientID
|
2016-08-08 18:45:17 +00:00
|
|
|
return &oidcConnector{
|
2019-04-24 20:58:35 +00:00
|
|
|
provider: provider,
|
2016-08-08 18:45:17 +00:00
|
|
|
redirectURI: c.RedirectURI,
|
|
|
|
oauth2Config: &oauth2.Config{
|
|
|
|
ClientID: clientID,
|
2016-10-22 20:36:31 +00:00
|
|
|
ClientSecret: c.ClientSecret,
|
2016-08-08 18:45:17 +00:00
|
|
|
Endpoint: provider.Endpoint(),
|
|
|
|
Scopes: scopes,
|
|
|
|
RedirectURL: c.RedirectURI,
|
|
|
|
},
|
2016-11-17 23:20:41 +00:00
|
|
|
verifier: provider.Verifier(
|
2017-03-08 18:33:19 +00:00
|
|
|
&oidc.Config{ClientID: clientID},
|
2016-08-08 18:45:17 +00:00
|
|
|
),
|
2019-03-05 21:24:02 +00:00
|
|
|
logger: logger,
|
|
|
|
cancel: cancel,
|
|
|
|
hostedDomains: c.HostedDomains,
|
|
|
|
insecureSkipEmailVerified: c.InsecureSkipEmailVerified,
|
2019-04-24 20:58:35 +00:00
|
|
|
getUserInfo: c.GetUserInfo,
|
2019-05-24 03:51:42 +00:00
|
|
|
userIDKey: c.UserIDKey,
|
2019-06-02 19:33:53 +00:00
|
|
|
userNameKey: c.UserNameKey,
|
2016-08-08 18:45:17 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ connector.CallbackConnector = (*oidcConnector)(nil)
|
2017-03-23 21:06:30 +00:00
|
|
|
_ connector.RefreshConnector = (*oidcConnector)(nil)
|
2016-08-08 18:45:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type oidcConnector struct {
|
2019-04-24 20:58:35 +00:00
|
|
|
provider *oidc.Provider
|
2019-03-05 21:24:02 +00:00
|
|
|
redirectURI string
|
|
|
|
oauth2Config *oauth2.Config
|
|
|
|
verifier *oidc.IDTokenVerifier
|
|
|
|
cancel context.CancelFunc
|
|
|
|
logger log.Logger
|
|
|
|
hostedDomains []string
|
|
|
|
insecureSkipEmailVerified bool
|
2019-04-24 20:58:35 +00:00
|
|
|
getUserInfo bool
|
2019-05-24 03:51:42 +00:00
|
|
|
userIDKey string
|
2019-06-02 19:33:53 +00:00
|
|
|
userNameKey string
|
2016-08-08 18:45:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *oidcConnector) Close() error {
|
|
|
|
c.cancel()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-18 21:40:41 +00:00
|
|
|
func (c *oidcConnector) LoginURL(s connector.Scopes, callbackURL, state string) (string, error) {
|
2016-08-08 18:45:17 +00:00
|
|
|
if c.redirectURI != callbackURL {
|
2017-06-13 22:52:33 +00:00
|
|
|
return "", fmt.Errorf("expected callback URL %q did not match the URL in the config %q", callbackURL, c.redirectURI)
|
2016-08-08 18:45:17 +00:00
|
|
|
}
|
2017-06-21 05:47:28 +00:00
|
|
|
|
2017-06-22 05:56:02 +00:00
|
|
|
if len(c.hostedDomains) > 0 {
|
|
|
|
preferredDomain := c.hostedDomains[0]
|
|
|
|
if len(c.hostedDomains) > 1 {
|
|
|
|
preferredDomain = "*"
|
|
|
|
}
|
|
|
|
return c.oauth2Config.AuthCodeURL(state, oauth2.SetAuthURLParam("hd", preferredDomain)), nil
|
2017-06-21 05:47:28 +00:00
|
|
|
}
|
2017-06-22 05:56:02 +00:00
|
|
|
return c.oauth2Config.AuthCodeURL(state), nil
|
2016-08-08 18:45:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type oauth2Error struct {
|
|
|
|
error string
|
|
|
|
errorDescription string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *oauth2Error) Error() string {
|
|
|
|
if e.errorDescription == "" {
|
|
|
|
return e.error
|
|
|
|
}
|
|
|
|
return e.error + ": " + e.errorDescription
|
|
|
|
}
|
|
|
|
|
2016-11-18 21:40:41 +00:00
|
|
|
func (c *oidcConnector) HandleCallback(s connector.Scopes, r *http.Request) (identity connector.Identity, err error) {
|
2016-08-08 18:45:17 +00:00
|
|
|
q := r.URL.Query()
|
|
|
|
if errType := q.Get("error"); errType != "" {
|
2016-10-27 17:08:08 +00:00
|
|
|
return identity, &oauth2Error{errType, q.Get("error_description")}
|
2016-08-08 18:45:17 +00:00
|
|
|
}
|
2016-11-17 23:20:41 +00:00
|
|
|
token, err := c.oauth2Config.Exchange(r.Context(), q.Get("code"))
|
2016-08-08 18:45:17 +00:00
|
|
|
if err != nil {
|
2016-10-27 17:08:08 +00:00
|
|
|
return identity, fmt.Errorf("oidc: failed to get token: %v", err)
|
2016-08-08 18:45:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rawIDToken, ok := token.Extra("id_token").(string)
|
|
|
|
if !ok {
|
2016-10-27 17:08:08 +00:00
|
|
|
return identity, errors.New("oidc: no id_token in token response")
|
2016-08-08 18:45:17 +00:00
|
|
|
}
|
2016-11-17 23:20:41 +00:00
|
|
|
idToken, err := c.verifier.Verify(r.Context(), rawIDToken)
|
2016-08-08 18:45:17 +00:00
|
|
|
if err != nil {
|
2016-10-27 17:08:08 +00:00
|
|
|
return identity, fmt.Errorf("oidc: failed to verify ID Token: %v", err)
|
2016-08-08 18:45:17 +00:00
|
|
|
}
|
|
|
|
|
2019-05-24 03:51:42 +00:00
|
|
|
var claims map[string]interface{}
|
2016-08-08 18:45:17 +00:00
|
|
|
if err := idToken.Claims(&claims); err != nil {
|
2016-10-27 17:08:08 +00:00
|
|
|
return identity, fmt.Errorf("oidc: failed to decode claims: %v", err)
|
2016-08-08 18:45:17 +00:00
|
|
|
}
|
|
|
|
|
2019-09-13 18:10:44 +00:00
|
|
|
// We immediately want to run getUserInfo if configured before we validate the claims
|
|
|
|
if c.getUserInfo {
|
|
|
|
userInfo, err := c.provider.UserInfo(r.Context(), oauth2.StaticTokenSource(token))
|
|
|
|
if err != nil {
|
|
|
|
return identity, fmt.Errorf("oidc: error loading userinfo: %v", err)
|
|
|
|
}
|
|
|
|
if err := userInfo.Claims(&claims); err != nil {
|
|
|
|
return identity, fmt.Errorf("oidc: failed to decode userinfo claims: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-02 19:33:53 +00:00
|
|
|
userNameKey := "name"
|
|
|
|
if c.userNameKey != "" {
|
|
|
|
userNameKey = c.userNameKey
|
|
|
|
}
|
|
|
|
name, found := claims[userNameKey].(string)
|
2019-05-24 03:51:42 +00:00
|
|
|
if !found {
|
2019-06-02 19:33:53 +00:00
|
|
|
return identity, fmt.Errorf("missing \"%s\" claim", userNameKey)
|
2019-05-24 03:51:42 +00:00
|
|
|
}
|
|
|
|
email, found := claims["email"].(string)
|
|
|
|
if !found {
|
|
|
|
return identity, errors.New("missing \"email\" claim")
|
|
|
|
}
|
|
|
|
emailVerified, found := claims["email_verified"].(bool)
|
|
|
|
if !found {
|
2019-05-28 12:43:00 +00:00
|
|
|
if c.insecureSkipEmailVerified {
|
|
|
|
emailVerified = true
|
|
|
|
} else {
|
|
|
|
return identity, errors.New("missing \"email_verified\" claim")
|
|
|
|
}
|
2019-05-24 03:51:42 +00:00
|
|
|
}
|
|
|
|
hostedDomain, _ := claims["hd"].(string)
|
|
|
|
|
2017-06-22 05:56:02 +00:00
|
|
|
if len(c.hostedDomains) > 0 {
|
|
|
|
found := false
|
|
|
|
for _, domain := range c.hostedDomains {
|
2019-05-24 03:51:42 +00:00
|
|
|
if hostedDomain == domain {
|
2017-06-22 05:56:02 +00:00
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
2019-05-24 03:51:42 +00:00
|
|
|
return identity, fmt.Errorf("oidc: unexpected hd claim %v", hostedDomain)
|
2017-06-22 05:56:02 +00:00
|
|
|
}
|
2017-06-21 05:47:28 +00:00
|
|
|
}
|
|
|
|
|
2016-08-08 18:45:17 +00:00
|
|
|
identity = connector.Identity{
|
|
|
|
UserID: idToken.Subject,
|
2019-05-24 03:51:42 +00:00
|
|
|
Username: name,
|
|
|
|
Email: email,
|
|
|
|
EmailVerified: emailVerified,
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.userIDKey != "" {
|
|
|
|
userID, found := claims[c.userIDKey].(string)
|
|
|
|
if !found {
|
|
|
|
return identity, fmt.Errorf("oidc: not found %v claim", c.userIDKey)
|
|
|
|
}
|
|
|
|
identity.UserID = userID
|
2016-08-08 18:45:17 +00:00
|
|
|
}
|
2019-05-24 03:51:42 +00:00
|
|
|
|
2016-10-27 17:08:08 +00:00
|
|
|
return identity, nil
|
2016-08-08 18:45:17 +00:00
|
|
|
}
|
2017-03-23 21:06:30 +00:00
|
|
|
|
|
|
|
// Refresh is implemented for backwards compatibility, even though it's a no-op.
|
|
|
|
func (c *oidcConnector) Refresh(ctx context.Context, s connector.Scopes, identity connector.Identity) (connector.Identity, error) {
|
|
|
|
return identity, nil
|
|
|
|
}
|