2016-07-25 20:00:28 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2017-03-08 18:33:19 +00:00
|
|
|
"context"
|
2020-10-01 19:32:23 +00:00
|
|
|
"crypto/rsa"
|
2017-04-17 22:41:41 +00:00
|
|
|
"encoding/json"
|
2016-07-25 20:00:28 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-10-15 14:50:39 +00:00
|
|
|
"io/fs"
|
2016-07-25 20:00:28 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2021-03-22 10:45:17 +00:00
|
|
|
"os"
|
2016-07-25 20:00:28 +00:00
|
|
|
"path"
|
2021-10-06 12:29:14 +00:00
|
|
|
"sort"
|
2017-12-20 15:03:32 +00:00
|
|
|
"strconv"
|
2017-10-26 17:00:43 +00:00
|
|
|
"strings"
|
2017-04-17 22:41:41 +00:00
|
|
|
"sync"
|
2016-08-11 03:51:58 +00:00
|
|
|
"sync/atomic"
|
2016-07-25 20:00:28 +00:00
|
|
|
"time"
|
|
|
|
|
2021-02-11 00:03:25 +00:00
|
|
|
gosundheit "github.com/AppsFlyer/go-sundheit"
|
2019-12-18 14:53:34 +00:00
|
|
|
"github.com/felixge/httpsnoop"
|
|
|
|
"github.com/gorilla/handlers"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2016-10-05 23:38:20 +00:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
|
2018-09-03 06:44:44 +00:00
|
|
|
"github.com/dexidp/dex/connector"
|
2019-08-09 13:05:50 +00:00
|
|
|
"github.com/dexidp/dex/connector/atlassiancrowd"
|
2018-09-03 06:44:44 +00:00
|
|
|
"github.com/dexidp/dex/connector/authproxy"
|
2018-10-04 16:29:36 +00:00
|
|
|
"github.com/dexidp/dex/connector/bitbucketcloud"
|
2020-05-26 11:54:40 +00:00
|
|
|
"github.com/dexidp/dex/connector/gitea"
|
2018-09-03 06:44:44 +00:00
|
|
|
"github.com/dexidp/dex/connector/github"
|
|
|
|
"github.com/dexidp/dex/connector/gitlab"
|
2018-02-03 11:52:46 +00:00
|
|
|
"github.com/dexidp/dex/connector/google"
|
2018-12-13 11:22:53 +00:00
|
|
|
"github.com/dexidp/dex/connector/keystone"
|
2018-09-03 06:44:44 +00:00
|
|
|
"github.com/dexidp/dex/connector/ldap"
|
|
|
|
"github.com/dexidp/dex/connector/linkedin"
|
|
|
|
"github.com/dexidp/dex/connector/microsoft"
|
|
|
|
"github.com/dexidp/dex/connector/mock"
|
|
|
|
"github.com/dexidp/dex/connector/oidc"
|
2019-12-10 13:51:09 +00:00
|
|
|
"github.com/dexidp/dex/connector/openshift"
|
2018-09-03 06:44:44 +00:00
|
|
|
"github.com/dexidp/dex/connector/saml"
|
2019-02-22 12:19:23 +00:00
|
|
|
"github.com/dexidp/dex/pkg/log"
|
2018-09-03 06:44:44 +00:00
|
|
|
"github.com/dexidp/dex/storage"
|
2021-03-22 10:45:17 +00:00
|
|
|
"github.com/dexidp/dex/web"
|
2016-07-25 20:00:28 +00:00
|
|
|
)
|
|
|
|
|
2017-04-17 22:41:41 +00:00
|
|
|
// LocalConnector is the local passwordDB connector which is an internal
|
|
|
|
// connector maintained by the server.
|
|
|
|
const LocalConnector = "local"
|
|
|
|
|
|
|
|
// Connector is a connector with resource version metadata.
|
2016-07-25 20:00:28 +00:00
|
|
|
type Connector struct {
|
2017-04-17 22:41:41 +00:00
|
|
|
ResourceVersion string
|
|
|
|
Connector connector.Connector
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Config holds the server's configuration options.
|
2016-08-20 00:24:49 +00:00
|
|
|
//
|
|
|
|
// Multiple servers using the same storage are expected to be configured identically.
|
2016-07-25 20:00:28 +00:00
|
|
|
type Config struct {
|
|
|
|
Issuer string
|
|
|
|
|
|
|
|
// The backing persistence layer.
|
|
|
|
Storage storage.Storage
|
|
|
|
|
2016-08-20 00:24:49 +00:00
|
|
|
// Valid values are "code" to enable the code flow and "token" to enable the implicit
|
|
|
|
// flow. If no response types are supplied this value defaults to "code".
|
|
|
|
SupportedResponseTypes []string
|
|
|
|
|
2017-01-14 09:18:48 +00:00
|
|
|
// List of allowed origins for CORS requests on discovery, token and keys endpoint.
|
2016-12-29 09:25:16 +00:00
|
|
|
// If none are indicated, CORS requests are disabled. Passing in "*" will allow any
|
|
|
|
// domain.
|
2017-01-14 09:18:48 +00:00
|
|
|
AllowedOrigins []string
|
2016-12-29 09:25:16 +00:00
|
|
|
|
2016-10-07 18:53:01 +00:00
|
|
|
// If enabled, the server won't prompt the user to approve authorization requests.
|
|
|
|
// Logging in implies approval.
|
|
|
|
SkipApprovalScreen bool
|
|
|
|
|
2019-08-06 17:18:46 +00:00
|
|
|
// If enabled, the connectors selection page will always be shown even if there's only one
|
|
|
|
AlwaysShowLoginScreen bool
|
|
|
|
|
2020-01-28 19:14:30 +00:00
|
|
|
RotateKeysAfter time.Duration // Defaults to 6 hours.
|
|
|
|
IDTokensValidFor time.Duration // Defaults to 24 hours
|
|
|
|
AuthRequestsValidFor time.Duration // Defaults to 24 hours
|
|
|
|
DeviceRequestsValidFor time.Duration // Defaults to 5 minutes
|
2020-10-28 06:26:34 +00:00
|
|
|
|
|
|
|
// Refresh token expiration settings
|
|
|
|
RefreshTokenPolicy *RefreshTokenPolicy
|
|
|
|
|
2018-01-03 03:15:01 +00:00
|
|
|
// If set, the server will use this connector to handle password grants
|
|
|
|
PasswordConnector string
|
2020-02-04 15:07:18 +00:00
|
|
|
|
2016-10-13 01:51:32 +00:00
|
|
|
GCFrequency time.Duration // Defaults to 5 minutes
|
|
|
|
|
2016-07-25 20:00:28 +00:00
|
|
|
// If specified, the server will use this function for determining time.
|
|
|
|
Now func() time.Time
|
2016-08-25 20:10:19 +00:00
|
|
|
|
2016-11-30 22:26:54 +00:00
|
|
|
Web WebConfig
|
2016-11-22 23:35:46 +00:00
|
|
|
|
2019-02-22 12:19:23 +00:00
|
|
|
Logger log.Logger
|
2017-12-20 15:03:32 +00:00
|
|
|
|
|
|
|
PrometheusRegistry *prometheus.Registry
|
2021-02-11 00:03:25 +00:00
|
|
|
|
|
|
|
HealthChecker gosundheit.Health
|
2016-11-30 22:26:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WebConfig holds the server's frontend templates and asset configuration.
|
|
|
|
type WebConfig struct {
|
2021-03-22 10:45:17 +00:00
|
|
|
// A file path to static web assets.
|
2016-11-30 22:26:54 +00:00
|
|
|
//
|
|
|
|
// It is expected to contain the following directories:
|
|
|
|
//
|
|
|
|
// * static - Static static served at "( issuer URL )/static".
|
|
|
|
// * templates - HTML templates controlled by dex.
|
|
|
|
// * themes/(theme) - Static static served at "( issuer URL )/theme".
|
2020-10-15 01:19:23 +00:00
|
|
|
Dir string
|
2016-11-30 22:26:54 +00:00
|
|
|
|
2021-03-22 10:45:17 +00:00
|
|
|
// Alternative way to programatically configure static web assets.
|
|
|
|
// If Dir is specified, WebFS is ignored.
|
|
|
|
// It's expected to contain the same files and directories as mentioned above.
|
2021-03-04 15:17:05 +00:00
|
|
|
//
|
2021-03-22 10:45:17 +00:00
|
|
|
// Note: this is experimental. Might get removed without notice!
|
2020-10-15 14:50:39 +00:00
|
|
|
WebFS fs.FS
|
|
|
|
|
2016-11-30 22:26:54 +00:00
|
|
|
// Defaults to "( issuer URL )/theme/logo.png"
|
|
|
|
LogoURL string
|
|
|
|
|
|
|
|
// Defaults to "dex"
|
|
|
|
Issuer string
|
|
|
|
|
2021-01-12 18:20:38 +00:00
|
|
|
// Defaults to "light"
|
2016-11-30 22:26:54 +00:00
|
|
|
Theme string
|
2019-08-06 17:14:53 +00:00
|
|
|
|
|
|
|
// Map of extra values passed into the templates
|
|
|
|
Extra map[string]string
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func value(val, defaultValue time.Duration) time.Duration {
|
|
|
|
if val == 0 {
|
|
|
|
return defaultValue
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
|
|
|
// Server is the top level object.
|
|
|
|
type Server struct {
|
|
|
|
issuerURL url.URL
|
|
|
|
|
2017-04-17 22:41:41 +00:00
|
|
|
// mutex for the connectors map.
|
|
|
|
mu sync.Mutex
|
|
|
|
// Map of connector IDs to connectors.
|
2016-07-25 20:00:28 +00:00
|
|
|
connectors map[string]Connector
|
|
|
|
|
|
|
|
storage storage.Storage
|
|
|
|
|
|
|
|
mux http.Handler
|
|
|
|
|
2016-08-25 20:10:19 +00:00
|
|
|
templates *templates
|
|
|
|
|
2016-07-25 20:00:28 +00:00
|
|
|
// If enabled, don't prompt user for approval after logging in through connector.
|
|
|
|
skipApproval bool
|
|
|
|
|
2019-08-06 17:18:46 +00:00
|
|
|
// If enabled, show the connector selection screen even if there's only one
|
|
|
|
alwaysShowLogin bool
|
|
|
|
|
2018-01-03 03:15:01 +00:00
|
|
|
// Used for password grant
|
|
|
|
passwordConnector string
|
|
|
|
|
2016-08-20 00:24:49 +00:00
|
|
|
supportedResponseTypes map[string]bool
|
|
|
|
|
2021-09-03 09:48:37 +00:00
|
|
|
supportedGrantTypes []string
|
|
|
|
|
2016-07-25 20:00:28 +00:00
|
|
|
now func() time.Time
|
|
|
|
|
2020-01-28 19:14:30 +00:00
|
|
|
idTokensValidFor time.Duration
|
|
|
|
authRequestsValidFor time.Duration
|
|
|
|
deviceRequestsValidFor time.Duration
|
2016-11-22 23:35:46 +00:00
|
|
|
|
2020-10-28 06:26:34 +00:00
|
|
|
refreshTokenPolicy *RefreshTokenPolicy
|
|
|
|
|
2019-02-22 12:19:23 +00:00
|
|
|
logger log.Logger
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
|
2016-10-04 07:26:04 +00:00
|
|
|
// NewServer constructs a server from the provided config.
|
2016-10-13 01:51:32 +00:00
|
|
|
func NewServer(ctx context.Context, c Config) (*Server, error) {
|
|
|
|
return newServer(ctx, c, defaultRotationStrategy(
|
2016-07-25 20:00:28 +00:00
|
|
|
value(c.RotateKeysAfter, 6*time.Hour),
|
|
|
|
value(c.IDTokensValidFor, 24*time.Hour),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2020-10-01 19:32:23 +00:00
|
|
|
// NewServerWithKey constructs a server from the provided config and a static signing key.
|
|
|
|
func NewServerWithKey(ctx context.Context, c Config, privateKey *rsa.PrivateKey) (*Server, error) {
|
|
|
|
return newServer(ctx, c, staticRotationStrategy(
|
|
|
|
privateKey,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2016-10-13 01:51:32 +00:00
|
|
|
func newServer(ctx context.Context, c Config, rotationStrategy rotationStrategy) (*Server, error) {
|
2016-07-25 20:00:28 +00:00
|
|
|
issuerURL, err := url.Parse(c.Issuer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("server: can't parse issuer URL")
|
|
|
|
}
|
2016-10-05 23:38:20 +00:00
|
|
|
|
2016-07-25 20:00:28 +00:00
|
|
|
if c.Storage == nil {
|
|
|
|
return nil, errors.New("server: storage cannot be nil")
|
|
|
|
}
|
2020-09-25 15:59:42 +00:00
|
|
|
|
2016-08-20 00:24:49 +00:00
|
|
|
if len(c.SupportedResponseTypes) == 0 {
|
|
|
|
c.SupportedResponseTypes = []string{responseTypeCode}
|
|
|
|
}
|
|
|
|
|
2021-09-03 09:48:37 +00:00
|
|
|
supportedRes := make(map[string]bool)
|
2016-08-20 00:24:49 +00:00
|
|
|
for _, respType := range c.SupportedResponseTypes {
|
|
|
|
switch respType {
|
2017-01-09 18:46:16 +00:00
|
|
|
case responseTypeCode, responseTypeIDToken, responseTypeToken:
|
2016-08-20 00:24:49 +00:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unsupported response_type %q", respType)
|
|
|
|
}
|
2021-09-03 09:48:37 +00:00
|
|
|
supportedRes[respType] = true
|
|
|
|
}
|
|
|
|
|
2021-09-15 07:58:27 +00:00
|
|
|
supportedGrant := []string{grantTypeAuthorizationCode, grantTypeRefreshToken, grantTypeDeviceCode} // default
|
2021-09-03 09:48:37 +00:00
|
|
|
if c.PasswordConnector != "" {
|
|
|
|
supportedGrant = append(supportedGrant, grantTypePassword)
|
2016-08-20 00:24:49 +00:00
|
|
|
}
|
2021-10-06 12:29:14 +00:00
|
|
|
sort.Strings(supportedGrant)
|
2016-07-25 20:00:28 +00:00
|
|
|
|
2021-03-22 10:45:17 +00:00
|
|
|
webFS := web.FS()
|
|
|
|
if c.Web.Dir != "" {
|
|
|
|
webFS = os.DirFS(c.Web.Dir)
|
|
|
|
} else if c.Web.WebFS != nil {
|
|
|
|
webFS = c.Web.WebFS
|
|
|
|
}
|
|
|
|
|
2020-10-15 14:50:39 +00:00
|
|
|
web := webConfig{
|
2021-03-22 10:45:17 +00:00
|
|
|
webFS: webFS,
|
2020-10-15 14:50:39 +00:00
|
|
|
logoURL: c.Web.LogoURL,
|
|
|
|
issuerURL: c.Issuer,
|
|
|
|
issuer: c.Web.Issuer,
|
|
|
|
theme: c.Web.Theme,
|
|
|
|
extra: c.Web.Extra,
|
2020-10-15 01:19:23 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 14:50:39 +00:00
|
|
|
static, theme, tmpls, err := loadWebConfig(web)
|
2016-08-25 20:10:19 +00:00
|
|
|
if err != nil {
|
2020-10-15 14:50:39 +00:00
|
|
|
return nil, fmt.Errorf("server: failed to load web static: %v", err)
|
2016-08-25 20:10:19 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 20:00:28 +00:00
|
|
|
now := c.Now
|
|
|
|
if now == nil {
|
|
|
|
now = time.Now
|
|
|
|
}
|
|
|
|
|
|
|
|
s := &Server{
|
2017-01-14 09:18:48 +00:00
|
|
|
issuerURL: *issuerURL,
|
|
|
|
connectors: make(map[string]Connector),
|
|
|
|
storage: newKeyCacher(c.Storage, now),
|
2021-09-03 09:48:37 +00:00
|
|
|
supportedResponseTypes: supportedRes,
|
|
|
|
supportedGrantTypes: supportedGrant,
|
2017-01-14 09:18:48 +00:00
|
|
|
idTokensValidFor: value(c.IDTokensValidFor, 24*time.Hour),
|
2018-12-13 10:40:58 +00:00
|
|
|
authRequestsValidFor: value(c.AuthRequestsValidFor, 24*time.Hour),
|
2020-01-28 19:14:30 +00:00
|
|
|
deviceRequestsValidFor: value(c.DeviceRequestsValidFor, 5*time.Minute),
|
2020-10-28 06:26:34 +00:00
|
|
|
refreshTokenPolicy: c.RefreshTokenPolicy,
|
2017-01-14 09:18:48 +00:00
|
|
|
skipApproval: c.SkipApprovalScreen,
|
2019-08-06 17:18:46 +00:00
|
|
|
alwaysShowLogin: c.AlwaysShowLoginScreen,
|
2017-01-14 09:18:48 +00:00
|
|
|
now: now,
|
|
|
|
templates: tmpls,
|
2018-01-03 03:15:01 +00:00
|
|
|
passwordConnector: c.PasswordConnector,
|
2017-01-14 09:18:48 +00:00
|
|
|
logger: c.Logger,
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 22:41:41 +00:00
|
|
|
// Retrieves connector objects in backend storage. This list includes the static connectors
|
|
|
|
// defined in the ConfigMap and dynamic connectors retrieved from the storage.
|
|
|
|
storageConnectors, err := c.Storage.ListConnectors()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("server: failed to list connector objects from storage: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(storageConnectors) == 0 && len(s.connectors) == 0 {
|
|
|
|
return nil, errors.New("server: no connectors specified")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, conn := range storageConnectors {
|
|
|
|
if _, err := s.OpenConnector(conn); err != nil {
|
|
|
|
return nil, fmt.Errorf("server: Failed to open connector %s: %v", conn.ID, err)
|
|
|
|
}
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
|
2021-01-15 15:22:38 +00:00
|
|
|
instrumentHandlerCounter := func(_ string, handler http.Handler) http.HandlerFunc {
|
|
|
|
return handler.ServeHTTP
|
2017-12-20 15:03:32 +00:00
|
|
|
}
|
|
|
|
|
2018-02-21 16:23:50 +00:00
|
|
|
if c.PrometheusRegistry != nil {
|
|
|
|
requestCounter := prometheus.NewCounterVec(prometheus.CounterOpts{
|
|
|
|
Name: "http_requests_total",
|
|
|
|
Help: "Count of all HTTP requests.",
|
|
|
|
}, []string{"handler", "code", "method"})
|
|
|
|
|
|
|
|
err = c.PrometheusRegistry.Register(requestCounter)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("server: Failed to register Prometheus HTTP metrics: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
instrumentHandlerCounter = func(handlerName string, handler http.Handler) http.HandlerFunc {
|
2021-01-15 15:22:38 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2018-02-21 16:23:50 +00:00
|
|
|
m := httpsnoop.CaptureMetrics(handler, w, r)
|
|
|
|
requestCounter.With(prometheus.Labels{"handler": handlerName, "code": strconv.Itoa(m.Code), "method": r.Method}).Inc()
|
2021-01-15 15:22:38 +00:00
|
|
|
}
|
2018-02-21 16:23:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-25 20:00:28 +00:00
|
|
|
r := mux.NewRouter()
|
2019-02-04 17:45:13 +00:00
|
|
|
handle := func(p string, h http.Handler) {
|
|
|
|
r.Handle(path.Join(issuerURL.Path, p), instrumentHandlerCounter(p, h))
|
|
|
|
}
|
2016-07-25 20:00:28 +00:00
|
|
|
handleFunc := func(p string, h http.HandlerFunc) {
|
2019-02-04 17:45:13 +00:00
|
|
|
handle(p, h)
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
2016-11-30 22:26:54 +00:00
|
|
|
handlePrefix := func(p string, h http.Handler) {
|
|
|
|
prefix := path.Join(issuerURL.Path, p)
|
|
|
|
r.PathPrefix(prefix).Handler(http.StripPrefix(prefix, h))
|
|
|
|
}
|
2017-01-14 09:18:48 +00:00
|
|
|
handleWithCORS := func(p string, h http.HandlerFunc) {
|
|
|
|
var handler http.Handler = h
|
|
|
|
if len(c.AllowedOrigins) > 0 {
|
2020-10-05 13:53:48 +00:00
|
|
|
allowedHeaders := []string{
|
|
|
|
"Authorization",
|
|
|
|
}
|
|
|
|
cors := handlers.CORS(
|
|
|
|
handlers.AllowedOrigins(c.AllowedOrigins),
|
|
|
|
handlers.AllowedHeaders(allowedHeaders),
|
|
|
|
)
|
|
|
|
handler = cors(handler)
|
2017-01-14 09:18:48 +00:00
|
|
|
}
|
2019-04-19 21:15:32 +00:00
|
|
|
r.Handle(path.Join(issuerURL.Path, p), instrumentHandlerCounter(p, handler))
|
2017-01-14 09:18:48 +00:00
|
|
|
}
|
2020-10-17 21:54:27 +00:00
|
|
|
r.NotFoundHandler = http.NotFoundHandler()
|
2016-07-25 20:00:28 +00:00
|
|
|
|
2016-08-25 23:18:09 +00:00
|
|
|
discoveryHandler, err := s.discoveryHandler()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-01-14 09:18:48 +00:00
|
|
|
handleWithCORS("/.well-known/openid-configuration", discoveryHandler)
|
2016-08-25 23:18:09 +00:00
|
|
|
|
2016-07-25 20:00:28 +00:00
|
|
|
// TODO(ericchiang): rate limit certain paths based on IP.
|
2017-01-14 09:18:48 +00:00
|
|
|
handleWithCORS("/token", s.handleToken)
|
|
|
|
handleWithCORS("/keys", s.handlePublicKeys)
|
2019-05-27 07:17:39 +00:00
|
|
|
handleWithCORS("/userinfo", s.handleUserInfo)
|
2016-07-25 20:00:28 +00:00
|
|
|
handleFunc("/auth", s.handleAuthorization)
|
|
|
|
handleFunc("/auth/{connector}", s.handleConnectorLogin)
|
2021-05-21 10:03:22 +00:00
|
|
|
handleFunc("/auth/{connector}/login", s.handlePasswordLogin)
|
2020-01-28 19:14:30 +00:00
|
|
|
handleFunc("/device", s.handleDeviceExchange)
|
|
|
|
handleFunc("/device/auth/verify_code", s.verifyUserCode)
|
2020-01-16 15:55:07 +00:00
|
|
|
handleFunc("/device/code", s.handleDeviceCode)
|
2021-02-24 13:14:28 +00:00
|
|
|
// TODO(nabokihms): "/device/token" endpoint is deprecated, consider using /token endpoint instead
|
2021-02-25 07:53:25 +00:00
|
|
|
handleFunc("/device/token", s.handleDeviceTokenDeprecated)
|
2020-05-13 19:38:43 +00:00
|
|
|
handleFunc(deviceCallbackURI, s.handleDeviceCallback)
|
2017-10-26 17:00:43 +00:00
|
|
|
r.HandleFunc(path.Join(issuerURL.Path, "/callback"), func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Strip the X-Remote-* headers to prevent security issues on
|
|
|
|
// misconfigured authproxy connector setups.
|
|
|
|
for key := range r.Header {
|
|
|
|
if strings.HasPrefix(strings.ToLower(key), "x-remote-") {
|
|
|
|
r.Header.Del(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.handleConnectorCallback(w, r)
|
|
|
|
})
|
2017-10-21 14:54:54 +00:00
|
|
|
// For easier connector-specific web server configuration, e.g. for the
|
|
|
|
// "authproxy" connector.
|
|
|
|
handleFunc("/callback/{connector}", s.handleConnectorCallback)
|
2016-07-25 20:00:28 +00:00
|
|
|
handleFunc("/approval", s.handleApproval)
|
2021-02-11 00:03:25 +00:00
|
|
|
handle("/healthz", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !c.HealthChecker.IsHealthy() {
|
|
|
|
s.renderError(r, w, http.StatusInternalServerError, "Health check failed.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "Health check passed")
|
|
|
|
}))
|
2020-10-15 01:19:23 +00:00
|
|
|
|
2020-10-15 14:50:39 +00:00
|
|
|
handlePrefix("/static", static)
|
|
|
|
handlePrefix("/theme", theme)
|
2016-07-25 20:00:28 +00:00
|
|
|
s.mux = r
|
|
|
|
|
2016-12-12 22:54:01 +00:00
|
|
|
s.startKeyRotation(ctx, rotationStrategy, now)
|
|
|
|
s.startGarbageCollection(ctx, value(c.GCFrequency, 5*time.Minute), now)
|
2016-10-13 01:51:32 +00:00
|
|
|
|
2016-07-25 20:00:28 +00:00
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
s.mux.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) absPath(pathItems ...string) string {
|
|
|
|
paths := make([]string, len(pathItems)+1)
|
|
|
|
paths[0] = s.issuerURL.Path
|
|
|
|
copy(paths[1:], pathItems)
|
|
|
|
return path.Join(paths...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) absURL(pathItems ...string) string {
|
|
|
|
u := s.issuerURL
|
|
|
|
u.Path = s.absPath(pathItems...)
|
|
|
|
return u.String()
|
|
|
|
}
|
2016-08-11 03:51:58 +00:00
|
|
|
|
2016-10-05 23:38:20 +00:00
|
|
|
func newPasswordDB(s storage.Storage) interface {
|
|
|
|
connector.Connector
|
|
|
|
connector.PasswordConnector
|
|
|
|
} {
|
|
|
|
return passwordDB{s}
|
|
|
|
}
|
|
|
|
|
|
|
|
type passwordDB struct {
|
|
|
|
s storage.Storage
|
|
|
|
}
|
|
|
|
|
2016-11-18 21:40:41 +00:00
|
|
|
func (db passwordDB) Login(ctx context.Context, s connector.Scopes, email, password string) (connector.Identity, bool, error) {
|
2016-10-05 23:38:20 +00:00
|
|
|
p, err := db.s.GetPassword(email)
|
|
|
|
if err != nil {
|
|
|
|
if err != storage.ErrNotFound {
|
2016-12-12 22:54:01 +00:00
|
|
|
return connector.Identity{}, false, fmt.Errorf("get password: %v", err)
|
2016-10-05 23:38:20 +00:00
|
|
|
}
|
2016-11-01 21:03:22 +00:00
|
|
|
return connector.Identity{}, false, nil
|
2016-10-05 23:38:20 +00:00
|
|
|
}
|
2017-08-17 21:46:07 +00:00
|
|
|
// This check prevents dex users from logging in using static passwords
|
|
|
|
// configured with hash costs that are too high or low.
|
|
|
|
if err := checkCost(p.Hash); err != nil {
|
2017-07-25 21:26:47 +00:00
|
|
|
return connector.Identity{}, false, err
|
2016-10-05 23:38:20 +00:00
|
|
|
}
|
2017-08-17 21:46:07 +00:00
|
|
|
if err := bcrypt.CompareHashAndPassword(p.Hash, []byte(password)); err != nil {
|
|
|
|
return connector.Identity{}, false, nil
|
|
|
|
}
|
2016-10-05 23:38:20 +00:00
|
|
|
return connector.Identity{
|
|
|
|
UserID: p.UserID,
|
|
|
|
Username: p.Username,
|
|
|
|
Email: p.Email,
|
|
|
|
EmailVerified: true,
|
|
|
|
}, true, nil
|
|
|
|
}
|
|
|
|
|
2016-11-18 21:40:41 +00:00
|
|
|
func (db passwordDB) Refresh(ctx context.Context, s connector.Scopes, identity connector.Identity) (connector.Identity, error) {
|
|
|
|
// If the user has been deleted, the refresh token will be rejected.
|
|
|
|
p, err := db.s.GetPassword(identity.Email)
|
|
|
|
if err != nil {
|
|
|
|
if err == storage.ErrNotFound {
|
|
|
|
return connector.Identity{}, errors.New("user not found")
|
|
|
|
}
|
|
|
|
return connector.Identity{}, fmt.Errorf("get password: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// User removed but a new user with the same email exists.
|
|
|
|
if p.UserID != identity.UserID {
|
|
|
|
return connector.Identity{}, errors.New("user not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a user has updated their username, that will be reflected in the
|
|
|
|
// refreshed token.
|
|
|
|
//
|
|
|
|
// No other fields are expected to be refreshable as email is effectively used
|
|
|
|
// as an ID and this implementation doesn't deal with groups.
|
|
|
|
identity.Username = p.Username
|
|
|
|
|
|
|
|
return identity, nil
|
|
|
|
}
|
|
|
|
|
2017-11-07 09:28:21 +00:00
|
|
|
func (db passwordDB) Prompt() string {
|
|
|
|
return "Email Address"
|
|
|
|
}
|
|
|
|
|
2016-08-11 03:51:58 +00:00
|
|
|
// newKeyCacher returns a storage which caches keys so long as the next
|
|
|
|
func newKeyCacher(s storage.Storage, now func() time.Time) storage.Storage {
|
|
|
|
if now == nil {
|
|
|
|
now = time.Now
|
|
|
|
}
|
|
|
|
return &keyCacher{Storage: s, now: now}
|
|
|
|
}
|
|
|
|
|
|
|
|
type keyCacher struct {
|
|
|
|
storage.Storage
|
|
|
|
|
|
|
|
now func() time.Time
|
|
|
|
keys atomic.Value // Always holds nil or type *storage.Keys.
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *keyCacher) GetKeys() (storage.Keys, error) {
|
|
|
|
keys, ok := k.keys.Load().(*storage.Keys)
|
|
|
|
if ok && keys != nil && k.now().Before(keys.NextRotation) {
|
|
|
|
return *keys, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
storageKeys, err := k.Storage.GetKeys()
|
|
|
|
if err != nil {
|
|
|
|
return storageKeys, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if k.now().Before(storageKeys.NextRotation) {
|
|
|
|
k.keys.Store(&storageKeys)
|
|
|
|
}
|
|
|
|
return storageKeys, nil
|
|
|
|
}
|
2016-10-13 01:51:32 +00:00
|
|
|
|
2016-12-12 22:54:01 +00:00
|
|
|
func (s *Server) startGarbageCollection(ctx context.Context, frequency time.Duration, now func() time.Time) {
|
2016-10-13 01:51:32 +00:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-time.After(frequency):
|
2016-12-12 22:54:01 +00:00
|
|
|
if r, err := s.storage.GarbageCollect(now()); err != nil {
|
|
|
|
s.logger.Errorf("garbage collection failed: %v", err)
|
2021-01-11 08:33:10 +00:00
|
|
|
} else if !r.IsEmpty() {
|
|
|
|
s.logger.Infof("garbage collection run, delete auth requests=%d, auth codes=%d, device requests=%d, device tokens=%d",
|
2020-01-16 15:55:07 +00:00
|
|
|
r.AuthRequests, r.AuthCodes, r.DeviceRequests, r.DeviceTokens)
|
2016-10-13 01:51:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2017-04-17 22:41:41 +00:00
|
|
|
|
|
|
|
// ConnectorConfig is a configuration that can open a connector.
|
|
|
|
type ConnectorConfig interface {
|
2019-02-22 12:19:23 +00:00
|
|
|
Open(id string, logger log.Logger) (connector.Connector, error)
|
2017-04-17 22:41:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ConnectorsConfig variable provides an easy way to return a config struct
|
|
|
|
// depending on the connector type.
|
|
|
|
var ConnectorsConfig = map[string]func() ConnectorConfig{
|
2018-11-27 10:28:46 +00:00
|
|
|
"keystone": func() ConnectorConfig { return new(keystone.Config) },
|
2018-10-04 16:29:36 +00:00
|
|
|
"mockCallback": func() ConnectorConfig { return new(mock.CallbackConfig) },
|
|
|
|
"mockPassword": func() ConnectorConfig { return new(mock.PasswordConfig) },
|
|
|
|
"ldap": func() ConnectorConfig { return new(ldap.Config) },
|
2020-05-26 11:54:40 +00:00
|
|
|
"gitea": func() ConnectorConfig { return new(gitea.Config) },
|
2018-10-04 16:29:36 +00:00
|
|
|
"github": func() ConnectorConfig { return new(github.Config) },
|
|
|
|
"gitlab": func() ConnectorConfig { return new(gitlab.Config) },
|
2018-02-03 11:52:46 +00:00
|
|
|
"google": func() ConnectorConfig { return new(google.Config) },
|
2018-10-04 16:29:36 +00:00
|
|
|
"oidc": func() ConnectorConfig { return new(oidc.Config) },
|
|
|
|
"saml": func() ConnectorConfig { return new(saml.Config) },
|
|
|
|
"authproxy": func() ConnectorConfig { return new(authproxy.Config) },
|
|
|
|
"linkedin": func() ConnectorConfig { return new(linkedin.Config) },
|
|
|
|
"microsoft": func() ConnectorConfig { return new(microsoft.Config) },
|
|
|
|
"bitbucket-cloud": func() ConnectorConfig { return new(bitbucketcloud.Config) },
|
2019-12-10 13:51:09 +00:00
|
|
|
"openshift": func() ConnectorConfig { return new(openshift.Config) },
|
2019-08-09 13:05:50 +00:00
|
|
|
"atlassian-crowd": func() ConnectorConfig { return new(atlassiancrowd.Config) },
|
2017-04-17 22:41:41 +00:00
|
|
|
// Keep around for backwards compatibility.
|
|
|
|
"samlExperimental": func() ConnectorConfig { return new(saml.Config) },
|
|
|
|
}
|
|
|
|
|
|
|
|
// openConnector will parse the connector config and open the connector.
|
2019-02-22 12:19:23 +00:00
|
|
|
func openConnector(logger log.Logger, conn storage.Connector) (connector.Connector, error) {
|
2017-04-17 22:41:41 +00:00
|
|
|
var c connector.Connector
|
|
|
|
|
|
|
|
f, ok := ConnectorsConfig[conn.Type]
|
|
|
|
if !ok {
|
2018-12-13 11:22:53 +00:00
|
|
|
return c, fmt.Errorf("unknown connector type %q", conn.Type)
|
2017-04-17 22:41:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
connConfig := f()
|
|
|
|
if len(conn.Config) != 0 {
|
|
|
|
data := []byte(string(conn.Config))
|
|
|
|
if err := json.Unmarshal(data, connConfig); err != nil {
|
|
|
|
return c, fmt.Errorf("parse connector config: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-21 14:54:54 +00:00
|
|
|
c, err := connConfig.Open(conn.ID, logger)
|
2017-04-17 22:41:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return c, fmt.Errorf("failed to create connector %s: %v", conn.ID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenConnector updates server connector map with specified connector object.
|
|
|
|
func (s *Server) OpenConnector(conn storage.Connector) (Connector, error) {
|
|
|
|
var c connector.Connector
|
|
|
|
|
|
|
|
if conn.Type == LocalConnector {
|
|
|
|
c = newPasswordDB(s.storage)
|
|
|
|
} else {
|
|
|
|
var err error
|
2019-02-22 20:26:30 +00:00
|
|
|
c, err = openConnector(s.logger, conn)
|
2017-04-17 22:41:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return Connector{}, fmt.Errorf("failed to open connector: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
connector := Connector{
|
|
|
|
ResourceVersion: conn.ResourceVersion,
|
|
|
|
Connector: c,
|
|
|
|
}
|
|
|
|
s.mu.Lock()
|
|
|
|
s.connectors[conn.ID] = connector
|
|
|
|
s.mu.Unlock()
|
|
|
|
|
|
|
|
return connector, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getConnector retrieves the connector object with the given id from the storage
|
|
|
|
// and updates the connector list for server if necessary.
|
|
|
|
func (s *Server) getConnector(id string) (Connector, error) {
|
|
|
|
storageConnector, err := s.storage.GetConnector(id)
|
|
|
|
if err != nil {
|
|
|
|
return Connector{}, fmt.Errorf("failed to get connector object from storage: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var conn Connector
|
|
|
|
var ok bool
|
|
|
|
s.mu.Lock()
|
|
|
|
conn, ok = s.connectors[id]
|
|
|
|
s.mu.Unlock()
|
|
|
|
|
|
|
|
if !ok || storageConnector.ResourceVersion != conn.ResourceVersion {
|
|
|
|
// Connector object does not exist in server connectors map or
|
|
|
|
// has been updated in the storage. Need to get latest.
|
|
|
|
conn, err := s.OpenConnector(storageConnector)
|
|
|
|
if err != nil {
|
|
|
|
return Connector{}, fmt.Errorf("failed to open connector: %v", err)
|
|
|
|
}
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return conn, nil
|
|
|
|
}
|