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 (
|
2016-08-03 09:44:24 +05:30
|
|
|
"bytes"
|
|
|
|
"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"
|
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-08-26 01:30:53 +05:30
|
|
|
func NewCallbackConnector() connector.Connector {
|
|
|
|
return callbackConnector{}
|
2016-07-26 01:30:28 +05:30
|
|
|
}
|
|
|
|
|
2016-08-03 09:44:24 +05:30
|
|
|
var (
|
2016-08-26 01:30:53 +05:30
|
|
|
_ connector.CallbackConnector = callbackConnector{}
|
|
|
|
_ connector.GroupsConnector = callbackConnector{}
|
|
|
|
|
|
|
|
_ connector.PasswordConnector = passwordConnector{}
|
2016-08-03 09:44:24 +05:30
|
|
|
)
|
|
|
|
|
2016-08-26 01:30:53 +05:30
|
|
|
type callbackConnector struct{}
|
2016-07-26 01:30:28 +05:30
|
|
|
|
2016-08-26 01:30:53 +05:30
|
|
|
func (m callbackConnector) Close() error { return nil }
|
2016-07-26 01:30:28 +05:30
|
|
|
|
2016-08-26 01:30:53 +05:30
|
|
|
func (m callbackConnector) LoginURL(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-10-27 22:38:08 +05:30
|
|
|
func (m callbackConnector) HandleCallback(r *http.Request) (connector.Identity, error) {
|
2016-08-03 09:44:24 +05:30
|
|
|
return connector.Identity{
|
2016-07-26 01:30:28 +05:30
|
|
|
UserID: "0-385-28089-0",
|
|
|
|
Username: "Kilgore Trout",
|
|
|
|
Email: "kilgore@kilgore.trout",
|
|
|
|
EmailVerified: true,
|
2016-08-03 09:44:24 +05:30
|
|
|
ConnectorData: connectorData,
|
2016-10-27 22:38:08 +05:30
|
|
|
}, nil
|
2016-07-26 01:30:28 +05:30
|
|
|
}
|
|
|
|
|
2016-08-26 01:30:53 +05:30
|
|
|
func (m callbackConnector) Groups(identity connector.Identity) ([]string, error) {
|
2016-08-03 09:44:24 +05:30
|
|
|
if !bytes.Equal(identity.ConnectorData, connectorData) {
|
|
|
|
return nil, errors.New("connector data mismatch")
|
|
|
|
}
|
2016-07-26 01:30:28 +05:30
|
|
|
return []string{"authors"}, nil
|
|
|
|
}
|
|
|
|
|
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.
|
2016-08-26 01:30:53 +05:30
|
|
|
func (c *CallbackConfig) Open() (connector.Connector, error) {
|
|
|
|
return NewCallbackConnector(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
func (c *PasswordConfig) Open() (connector.Connector, error) {
|
|
|
|
if c.Username == "" {
|
|
|
|
return nil, errors.New("no username supplied")
|
|
|
|
}
|
|
|
|
if c.Password == "" {
|
|
|
|
return nil, errors.New("no password supplied")
|
|
|
|
}
|
|
|
|
return &passwordConnector{c.Username, c.Password}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type passwordConnector struct {
|
|
|
|
username string
|
|
|
|
password string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p passwordConnector) Close() error { return nil }
|
|
|
|
|
|
|
|
func (p passwordConnector) Login(username, password string) (identity connector.Identity, validPassword bool, err error) {
|
|
|
|
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
|
|
|
}
|