2017-10-21 20:24:54 +05:30
|
|
|
// Package authproxy implements a connector which relies on external
|
|
|
|
// authentication (e.g. mod_auth in Apache2) and returns an identity with the
|
|
|
|
// HTTP header X-Remote-User as verified email.
|
|
|
|
package authproxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
2018-09-03 12:14:44 +05:30
|
|
|
"github.com/dexidp/dex/connector"
|
2019-02-22 17:49:23 +05:30
|
|
|
"github.com/dexidp/dex/pkg/log"
|
2017-10-21 20:24:54 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// Config holds the configuration parameters for a connector which returns an
|
|
|
|
// identity with the HTTP header X-Remote-User as verified email.
|
2020-10-20 14:01:34 +05:30
|
|
|
type Config struct {
|
2020-10-20 14:49:48 +05:30
|
|
|
HeaderName string `json:"headerName"`
|
|
|
|
Groups []string `json:"groups"`
|
2020-10-20 14:01:34 +05:30
|
|
|
}
|
2017-10-21 20:24:54 +05:30
|
|
|
|
|
|
|
// Open returns an authentication strategy which requires no user interaction.
|
2019-02-22 17:49:23 +05:30
|
|
|
func (c *Config) Open(id string, logger log.Logger) (connector.Connector, error) {
|
2020-10-20 14:01:34 +05:30
|
|
|
if c.HeaderName == "" {
|
|
|
|
c.HeaderName = "X-Remote-User"
|
|
|
|
}
|
2020-10-20 14:49:48 +05:30
|
|
|
return &callback{headerName: c.HeaderName, logger: logger, pathSuffix: "/" + id, groups: c.Groups}, nil
|
2017-10-21 20:24:54 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Callback is a connector which returns an identity with the HTTP header
|
|
|
|
// X-Remote-User as verified email.
|
|
|
|
type callback struct {
|
2020-10-20 14:01:34 +05:30
|
|
|
headerName string
|
2020-10-20 14:49:48 +05:30
|
|
|
groups []string
|
2019-02-22 17:49:23 +05:30
|
|
|
logger log.Logger
|
2017-10-21 20:24:54 +05:30
|
|
|
pathSuffix string
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoginURL returns the URL to redirect the user to login with.
|
|
|
|
func (m *callback) LoginURL(s connector.Scopes, callbackURL, state string) (string, error) {
|
|
|
|
u, err := url.Parse(callbackURL)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to parse callbackURL %q: %v", callbackURL, err)
|
|
|
|
}
|
2020-10-18 03:24:27 +05:30
|
|
|
u.Path += m.pathSuffix
|
2017-10-21 20:24:54 +05:30
|
|
|
v := u.Query()
|
|
|
|
v.Set("state", state)
|
|
|
|
u.RawQuery = v.Encode()
|
|
|
|
return u.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleCallback parses the request and returns the user's identity
|
|
|
|
func (m *callback) HandleCallback(s connector.Scopes, r *http.Request) (connector.Identity, error) {
|
2020-10-20 14:01:34 +05:30
|
|
|
remoteUser := r.Header.Get(m.headerName)
|
2017-10-21 20:24:54 +05:30
|
|
|
if remoteUser == "" {
|
2020-10-20 14:01:34 +05:30
|
|
|
return connector.Identity{}, fmt.Errorf("required HTTP header %s is not set", m.headerName)
|
2017-10-21 20:24:54 +05:30
|
|
|
}
|
|
|
|
// TODO: add support for X-Remote-Group, see
|
|
|
|
// https://kubernetes.io/docs/admin/authentication/#authenticating-proxy
|
|
|
|
return connector.Identity{
|
2017-10-26 23:17:16 +05:30
|
|
|
UserID: remoteUser, // TODO: figure out if this is a bad ID value.
|
2017-10-21 20:24:54 +05:30
|
|
|
Email: remoteUser,
|
|
|
|
EmailVerified: true,
|
2020-10-20 14:49:48 +05:30
|
|
|
Groups: m.groups,
|
2017-10-21 20:24:54 +05:30
|
|
|
}, nil
|
|
|
|
}
|