2016-07-26 01:30:28 +05:30
|
|
|
// Package oidc implements logging in through OpenID Connect providers.
|
|
|
|
package oidc
|
2016-08-09 00:15:17 +05:30
|
|
|
|
|
|
|
import (
|
2017-03-09 00:03:19 +05:30
|
|
|
"context"
|
2016-08-09 00:15:17 +05:30
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2017-03-20 21:08:52 +05:30
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2016-08-09 00:15:17 +05:30
|
|
|
|
2016-11-18 04:50:41 +05:30
|
|
|
"github.com/coreos/go-oidc"
|
2017-07-26 02:15:17 +05:30
|
|
|
"github.com/sirupsen/logrus"
|
2016-08-09 00:15:17 +05:30
|
|
|
"golang.org/x/oauth2"
|
|
|
|
|
2018-09-03 12:14:44 +05:30
|
|
|
"github.com/dexidp/dex/connector"
|
2016-08-09 00:15:17 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// Config holds configuration options for OpenID Connect logins.
|
|
|
|
type Config struct {
|
2016-11-04 03:02:23 +05:30
|
|
|
Issuer string `json:"issuer"`
|
|
|
|
ClientID string `json:"clientID"`
|
|
|
|
ClientSecret string `json:"clientSecret"`
|
|
|
|
RedirectURI string `json:"redirectURI"`
|
2016-08-09 00:15:17 +05:30
|
|
|
|
2017-03-20 21:08:52 +05:30
|
|
|
// 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-04 03:02:23 +05:30
|
|
|
Scopes []string `json:"scopes"` // defaults to "profile" and "email"
|
2017-03-20 21:08:52 +05:30
|
|
|
|
2017-06-22 11:26:02 +05:30
|
|
|
// 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-22 04:18:21 +05:30
|
|
|
HostedDomains []string `json:"hostedDomains"`
|
2017-03-20 21:08:52 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// 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 12:14:44 +05:30
|
|
|
// See: https://github.com/dexidp/dex/issues/859
|
2017-03-20 21:08:52 +05:30
|
|
|
"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-09 00:15:17 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Open returns a connector which can be used to login users through an upstream
|
|
|
|
// OpenID Connect provider.
|
2017-10-21 20:24:54 +05:30
|
|
|
func (c *Config) Open(id string, logger logrus.FieldLogger) (conn connector.Connector, err error) {
|
2016-08-09 00:15:17 +05:30
|
|
|
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 21:08:52 +05:30
|
|
|
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-09 00:15:17 +05:30
|
|
|
scopes := []string{oidc.ScopeOpenID}
|
|
|
|
if len(c.Scopes) > 0 {
|
|
|
|
scopes = append(scopes, c.Scopes...)
|
|
|
|
} else {
|
|
|
|
scopes = append(scopes, "profile", "email")
|
|
|
|
}
|
|
|
|
|
2016-10-23 02:06:31 +05:30
|
|
|
clientID := c.ClientID
|
2016-08-09 00:15:17 +05:30
|
|
|
return &oidcConnector{
|
|
|
|
redirectURI: c.RedirectURI,
|
|
|
|
oauth2Config: &oauth2.Config{
|
|
|
|
ClientID: clientID,
|
2016-10-23 02:06:31 +05:30
|
|
|
ClientSecret: c.ClientSecret,
|
2016-08-09 00:15:17 +05:30
|
|
|
Endpoint: provider.Endpoint(),
|
|
|
|
Scopes: scopes,
|
|
|
|
RedirectURL: c.RedirectURI,
|
|
|
|
},
|
2016-11-18 04:50:41 +05:30
|
|
|
verifier: provider.Verifier(
|
2017-03-09 00:03:19 +05:30
|
|
|
&oidc.Config{ClientID: clientID},
|
2016-08-09 00:15:17 +05:30
|
|
|
),
|
2017-06-22 11:26:02 +05:30
|
|
|
logger: logger,
|
|
|
|
cancel: cancel,
|
|
|
|
hostedDomains: c.HostedDomains,
|
2016-08-09 00:15:17 +05:30
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ connector.CallbackConnector = (*oidcConnector)(nil)
|
2017-03-24 02:36:30 +05:30
|
|
|
_ connector.RefreshConnector = (*oidcConnector)(nil)
|
2016-08-09 00:15:17 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
type oidcConnector struct {
|
2017-06-22 11:26:02 +05:30
|
|
|
redirectURI string
|
|
|
|
oauth2Config *oauth2.Config
|
|
|
|
verifier *oidc.IDTokenVerifier
|
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
logger logrus.FieldLogger
|
|
|
|
hostedDomains []string
|
2016-08-09 00:15:17 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func (c *oidcConnector) Close() error {
|
|
|
|
c.cancel()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-19 03:10:41 +05:30
|
|
|
func (c *oidcConnector) LoginURL(s connector.Scopes, callbackURL, state string) (string, error) {
|
2016-08-09 00:15:17 +05:30
|
|
|
if c.redirectURI != callbackURL {
|
2017-06-14 04:22:33 +05:30
|
|
|
return "", fmt.Errorf("expected callback URL %q did not match the URL in the config %q", callbackURL, c.redirectURI)
|
2016-08-09 00:15:17 +05:30
|
|
|
}
|
2017-06-21 11:17:28 +05:30
|
|
|
|
2017-06-22 11:26:02 +05:30
|
|
|
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 11:17:28 +05:30
|
|
|
}
|
2017-06-22 11:26:02 +05:30
|
|
|
return c.oauth2Config.AuthCodeURL(state), nil
|
2016-08-09 00:15:17 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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-19 03:10:41 +05:30
|
|
|
func (c *oidcConnector) HandleCallback(s connector.Scopes, r *http.Request) (identity connector.Identity, err error) {
|
2016-08-09 00:15:17 +05:30
|
|
|
q := r.URL.Query()
|
|
|
|
if errType := q.Get("error"); errType != "" {
|
2016-10-27 22:38:08 +05:30
|
|
|
return identity, &oauth2Error{errType, q.Get("error_description")}
|
2016-08-09 00:15:17 +05:30
|
|
|
}
|
2016-11-18 04:50:41 +05:30
|
|
|
token, err := c.oauth2Config.Exchange(r.Context(), q.Get("code"))
|
2016-08-09 00:15:17 +05:30
|
|
|
if err != nil {
|
2016-10-27 22:38:08 +05:30
|
|
|
return identity, fmt.Errorf("oidc: failed to get token: %v", err)
|
2016-08-09 00:15:17 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
rawIDToken, ok := token.Extra("id_token").(string)
|
|
|
|
if !ok {
|
2016-10-27 22:38:08 +05:30
|
|
|
return identity, errors.New("oidc: no id_token in token response")
|
2016-08-09 00:15:17 +05:30
|
|
|
}
|
2016-11-18 04:50:41 +05:30
|
|
|
idToken, err := c.verifier.Verify(r.Context(), rawIDToken)
|
2016-08-09 00:15:17 +05:30
|
|
|
if err != nil {
|
2016-10-27 22:38:08 +05:30
|
|
|
return identity, fmt.Errorf("oidc: failed to verify ID Token: %v", err)
|
2016-08-09 00:15:17 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
var claims struct {
|
|
|
|
Username string `json:"name"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
EmailVerified bool `json:"email_verified"`
|
2017-06-21 11:17:28 +05:30
|
|
|
HostedDomain string `json:"hd"`
|
2016-08-09 00:15:17 +05:30
|
|
|
}
|
|
|
|
if err := idToken.Claims(&claims); err != nil {
|
2016-10-27 22:38:08 +05:30
|
|
|
return identity, fmt.Errorf("oidc: failed to decode claims: %v", err)
|
2016-08-09 00:15:17 +05:30
|
|
|
}
|
|
|
|
|
2017-06-22 11:26:02 +05:30
|
|
|
if len(c.hostedDomains) > 0 {
|
|
|
|
found := false
|
|
|
|
for _, domain := range c.hostedDomains {
|
2017-07-22 04:18:21 +05:30
|
|
|
if claims.HostedDomain == domain {
|
2017-06-22 11:26:02 +05:30
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
return identity, fmt.Errorf("oidc: unexpected hd claim %v", claims.HostedDomain)
|
|
|
|
}
|
2017-06-21 11:17:28 +05:30
|
|
|
}
|
|
|
|
|
2016-08-09 00:15:17 +05:30
|
|
|
identity = connector.Identity{
|
|
|
|
UserID: idToken.Subject,
|
|
|
|
Username: claims.Username,
|
|
|
|
Email: claims.Email,
|
|
|
|
EmailVerified: claims.EmailVerified,
|
|
|
|
}
|
2016-10-27 22:38:08 +05:30
|
|
|
return identity, nil
|
2016-08-09 00:15:17 +05:30
|
|
|
}
|
2017-03-24 02:36:30 +05:30
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|