2016-07-25 20:00:28 +00:00
|
|
|
// Package mock implements a mock connector which requires no user interaction.
|
|
|
|
package mock
|
|
|
|
|
|
|
|
import (
|
2016-08-03 04:14:24 +00:00
|
|
|
"bytes"
|
|
|
|
"errors"
|
2016-07-25 20:00:28 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"github.com/coreos/poke/connector"
|
|
|
|
)
|
|
|
|
|
|
|
|
// New returns a mock connector which requires no user interaction. It always returns
|
|
|
|
// the same (fake) identity.
|
|
|
|
func New() connector.Connector {
|
|
|
|
return mockConnector{}
|
|
|
|
}
|
|
|
|
|
2016-08-03 04:14:24 +00:00
|
|
|
var (
|
|
|
|
_ connector.CallbackConnector = mockConnector{}
|
|
|
|
_ connector.GroupsConnector = mockConnector{}
|
|
|
|
)
|
|
|
|
|
2016-07-25 20:00:28 +00:00
|
|
|
type mockConnector struct{}
|
|
|
|
|
|
|
|
func (m mockConnector) Close() error { return nil }
|
|
|
|
|
|
|
|
func (m mockConnector) LoginURL(callbackURL, state string) (string, error) {
|
|
|
|
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 04:14:24 +00:00
|
|
|
var connectorData = []byte("foobar")
|
|
|
|
|
|
|
|
func (m mockConnector) HandleCallback(r *http.Request) (connector.Identity, string, error) {
|
|
|
|
return connector.Identity{
|
2016-07-25 20:00:28 +00:00
|
|
|
UserID: "0-385-28089-0",
|
|
|
|
Username: "Kilgore Trout",
|
|
|
|
Email: "kilgore@kilgore.trout",
|
|
|
|
EmailVerified: true,
|
2016-08-03 04:14:24 +00:00
|
|
|
ConnectorData: connectorData,
|
2016-07-25 20:00:28 +00:00
|
|
|
}, r.URL.Query().Get("state"), nil
|
|
|
|
}
|
|
|
|
|
2016-08-03 04:14:24 +00:00
|
|
|
func (m mockConnector) Groups(identity connector.Identity) ([]string, error) {
|
|
|
|
if !bytes.Equal(identity.ConnectorData, connectorData) {
|
|
|
|
return nil, errors.New("connector data mismatch")
|
|
|
|
}
|
2016-07-25 20:00:28 +00:00
|
|
|
return []string{"authors"}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Config holds the configuration parameters for the mock connector.
|
|
|
|
type Config struct{}
|
|
|
|
|
|
|
|
// Open returns an authentication strategy which requires no user interaction.
|
|
|
|
func (c *Config) Open() (connector.Connector, error) {
|
|
|
|
return New(), nil
|
|
|
|
}
|