2016-08-26 01:30:53 +05:30
|
|
|
// Package mock implements connectors which help test various server components.
|
2016-07-26 01:30:28 +05:30
|
|
|
package mock
|
|
|
|
|
|
|
|
import (
|
2017-03-09 00:03:19 +05:30
|
|
|
"context"
|
2016-08-03 09:44:24 +05:30
|
|
|
"errors"
|
2016-07-26 01:30:28 +05:30
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
2016-08-11 11:01:42 +05:30
|
|
|
"github.com/coreos/dex/connector"
|
2017-07-26 02:15:17 +05:30
|
|
|
"github.com/sirupsen/logrus"
|
2016-07-26 01:30:28 +05:30
|
|
|
)
|
|
|
|
|
2016-08-26 01:30:53 +05:30
|
|
|
// NewCallbackConnector returns a mock connector which requires no user interaction. It always returns
|
2016-07-26 01:30:28 +05:30
|
|
|
// the same (fake) identity.
|
2016-11-23 05:05:46 +05:30
|
|
|
func NewCallbackConnector(logger logrus.FieldLogger) connector.Connector {
|
2016-11-22 01:46:36 +05:30
|
|
|
return &Callback{
|
|
|
|
Identity: connector.Identity{
|
|
|
|
UserID: "0-385-28089-0",
|
|
|
|
Username: "Kilgore Trout",
|
|
|
|
Email: "kilgore@kilgore.trout",
|
|
|
|
EmailVerified: true,
|
|
|
|
Groups: []string{"authors"},
|
|
|
|
ConnectorData: connectorData,
|
|
|
|
},
|
2016-11-23 05:05:46 +05:30
|
|
|
Logger: logger,
|
2016-11-22 01:46:36 +05:30
|
|
|
}
|
2016-07-26 01:30:28 +05:30
|
|
|
}
|
|
|
|
|
2016-08-03 09:44:24 +05:30
|
|
|
var (
|
2016-11-22 01:46:36 +05:30
|
|
|
_ connector.CallbackConnector = &Callback{}
|
2016-08-26 01:30:53 +05:30
|
|
|
|
|
|
|
_ connector.PasswordConnector = passwordConnector{}
|
2016-08-03 09:44:24 +05:30
|
|
|
)
|
|
|
|
|
2016-11-22 01:46:36 +05:30
|
|
|
// Callback is a connector that requires no user interaction and always returns the same identity.
|
|
|
|
type Callback struct {
|
|
|
|
// The returned identity.
|
|
|
|
Identity connector.Identity
|
2016-11-23 05:05:46 +05:30
|
|
|
Logger logrus.FieldLogger
|
2016-11-22 01:46:36 +05:30
|
|
|
}
|
2016-07-26 01:30:28 +05:30
|
|
|
|
2016-11-22 01:46:36 +05:30
|
|
|
// LoginURL returns the URL to redirect the user to login with.
|
|
|
|
func (m *Callback) LoginURL(s connector.Scopes, callbackURL, state string) (string, error) {
|
2016-07-26 01:30:28 +05:30
|
|
|
u, err := url.Parse(callbackURL)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to parse callbackURL %q: %v", callbackURL, err)
|
|
|
|
}
|
|
|
|
v := u.Query()
|
|
|
|
v.Set("state", state)
|
|
|
|
u.RawQuery = v.Encode()
|
|
|
|
return u.String(), nil
|
|
|
|
}
|
|
|
|
|
2016-08-03 09:44:24 +05:30
|
|
|
var connectorData = []byte("foobar")
|
|
|
|
|
2016-11-22 01:46:36 +05:30
|
|
|
// HandleCallback parses the request and returns the user's identity
|
|
|
|
func (m *Callback) HandleCallback(s connector.Scopes, r *http.Request) (connector.Identity, error) {
|
|
|
|
return m.Identity, nil
|
|
|
|
}
|
2016-11-19 03:10:41 +05:30
|
|
|
|
2016-11-22 01:46:36 +05:30
|
|
|
// Refresh updates the identity during a refresh token request.
|
|
|
|
func (m *Callback) Refresh(ctx context.Context, s connector.Scopes, identity connector.Identity) (connector.Identity, error) {
|
|
|
|
return m.Identity, nil
|
2016-07-26 01:30:28 +05:30
|
|
|
}
|
|
|
|
|
2016-08-26 01:30:53 +05:30
|
|
|
// CallbackConfig holds the configuration parameters for a connector which requires no interaction.
|
|
|
|
type CallbackConfig struct{}
|
2016-07-26 01:30:28 +05:30
|
|
|
|
|
|
|
// Open returns an authentication strategy which requires no user interaction.
|
2017-10-21 20:24:54 +05:30
|
|
|
func (c *CallbackConfig) Open(id string, logger logrus.FieldLogger) (connector.Connector, error) {
|
2016-11-23 05:05:46 +05:30
|
|
|
return NewCallbackConnector(logger), nil
|
2016-08-26 01:30:53 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// PasswordConfig holds the configuration for a mock connector which prompts for the supplied
|
|
|
|
// username and password.
|
|
|
|
type PasswordConfig struct {
|
2016-11-04 03:02:23 +05:30
|
|
|
Username string `json:"username"`
|
|
|
|
Password string `json:"password"`
|
2016-08-26 01:30:53 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Open returns an authentication strategy which prompts for a predefined username and password.
|
2017-10-21 20:24:54 +05:30
|
|
|
func (c *PasswordConfig) Open(id string, logger logrus.FieldLogger) (connector.Connector, error) {
|
2016-08-26 01:30:53 +05:30
|
|
|
if c.Username == "" {
|
|
|
|
return nil, errors.New("no username supplied")
|
|
|
|
}
|
|
|
|
if c.Password == "" {
|
|
|
|
return nil, errors.New("no password supplied")
|
|
|
|
}
|
2016-11-23 05:05:46 +05:30
|
|
|
return &passwordConnector{c.Username, c.Password, logger}, nil
|
2016-08-26 01:30:53 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
type passwordConnector struct {
|
|
|
|
username string
|
|
|
|
password string
|
2016-11-23 05:05:46 +05:30
|
|
|
logger logrus.FieldLogger
|
2016-08-26 01:30:53 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func (p passwordConnector) Close() error { return nil }
|
|
|
|
|
2016-11-19 03:10:41 +05:30
|
|
|
func (p passwordConnector) Login(ctx context.Context, s connector.Scopes, username, password string) (identity connector.Identity, validPassword bool, err error) {
|
2016-08-26 01:30:53 +05:30
|
|
|
if username == p.username && password == p.password {
|
|
|
|
return connector.Identity{
|
|
|
|
UserID: "0-385-28089-0",
|
|
|
|
Username: "Kilgore Trout",
|
|
|
|
Email: "kilgore@kilgore.trout",
|
|
|
|
EmailVerified: true,
|
|
|
|
}, true, nil
|
|
|
|
}
|
|
|
|
return identity, false, nil
|
2016-07-26 01:30:28 +05:30
|
|
|
}
|
2017-11-07 14:58:21 +05:30
|
|
|
|
|
|
|
func (p passwordConnector) Prompt() string { return "" }
|