2015-08-18 05:57:27 +05:30
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2016-02-10 04:36:07 +05:30
|
|
|
"encoding/base64"
|
2015-08-18 05:57:27 +05:30
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/coreos/go-oidc/key"
|
|
|
|
"github.com/coreos/go-oidc/oidc"
|
|
|
|
|
|
|
|
"github.com/coreos/dex/client"
|
2016-05-12 22:23:01 +05:30
|
|
|
clientmanager "github.com/coreos/dex/client/manager"
|
2015-08-18 05:57:27 +05:30
|
|
|
"github.com/coreos/dex/connector"
|
2016-02-10 00:40:28 +05:30
|
|
|
"github.com/coreos/dex/db"
|
2015-08-18 05:57:27 +05:30
|
|
|
"github.com/coreos/dex/email"
|
2016-06-03 01:35:18 +05:30
|
|
|
"github.com/coreos/dex/refresh/refreshtest"
|
2016-02-10 00:40:28 +05:30
|
|
|
sessionmanager "github.com/coreos/dex/session/manager"
|
2015-08-18 05:57:27 +05:30
|
|
|
"github.com/coreos/dex/user"
|
|
|
|
useremail "github.com/coreos/dex/user/email"
|
2016-02-10 00:40:28 +05:30
|
|
|
usermanager "github.com/coreos/dex/user/manager"
|
2015-08-18 05:57:27 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
templatesLocation = "../static/html"
|
|
|
|
emailTemplatesLocation = "../static/email"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-06-07 01:33:07 +05:30
|
|
|
testUserID1 = "ID-1"
|
|
|
|
testUserEmail1 = "Email-1@example.com"
|
|
|
|
testUserRemoteID1 = "RID-1"
|
|
|
|
|
2016-02-10 04:36:07 +05:30
|
|
|
testIssuerURL = url.URL{Scheme: "http", Host: "server.example.com"}
|
2016-06-07 01:33:07 +05:30
|
|
|
|
|
|
|
testClientID = "client.example.com"
|
|
|
|
clientTestSecret = base64.URLEncoding.EncodeToString([]byte("secret"))
|
|
|
|
testClientCredentials = oidc.ClientCredentials{
|
|
|
|
ID: testClientID,
|
|
|
|
Secret: clientTestSecret,
|
|
|
|
}
|
|
|
|
|
|
|
|
testConnectorID1 = "IDPC-1"
|
2015-08-18 05:57:27 +05:30
|
|
|
|
|
|
|
testRedirectURL = url.URL{Scheme: "http", Host: "client.example.com", Path: "/callback"}
|
|
|
|
|
|
|
|
testUsers = []user.UserWithRemoteIdentities{
|
|
|
|
{
|
|
|
|
User: user.User{
|
2016-06-07 01:33:07 +05:30
|
|
|
ID: testUserID1,
|
|
|
|
Email: testUserEmail1,
|
2015-08-18 05:57:27 +05:30
|
|
|
},
|
|
|
|
RemoteIdentities: []user.RemoteIdentity{
|
|
|
|
{
|
2016-06-07 01:33:07 +05:30
|
|
|
ConnectorID: testConnectorID1,
|
|
|
|
ID: testUserRemoteID1,
|
2015-08-18 05:57:27 +05:30
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-11-10 04:05:11 +05:30
|
|
|
{
|
|
|
|
User: user.User{
|
|
|
|
ID: "ID-Verified",
|
|
|
|
Email: "Email-Verified@example.com",
|
|
|
|
EmailVerified: true,
|
|
|
|
},
|
|
|
|
RemoteIdentities: []user.RemoteIdentity{
|
|
|
|
{
|
|
|
|
ConnectorID: "IDPC-1",
|
|
|
|
ID: "RID-2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
testPasswordInfos = []user.PasswordInfo{
|
|
|
|
{
|
|
|
|
UserID: "ID-1",
|
|
|
|
Password: []byte("password"),
|
|
|
|
},
|
2015-11-10 04:05:11 +05:30
|
|
|
{
|
|
|
|
UserID: "ID-Verified",
|
|
|
|
Password: []byte("password"),
|
|
|
|
},
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
testPrivKey, _ = key.GeneratePrivateKey()
|
2016-06-03 01:35:18 +05:30
|
|
|
|
|
|
|
testClientCreds = oidc.ClientCredentials{
|
|
|
|
ID: testClientID,
|
|
|
|
Secret: base64.URLEncoding.EncodeToString([]byte("secret")),
|
|
|
|
}
|
2015-08-18 05:57:27 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
type testFixtures struct {
|
2016-04-15 04:57:57 +05:30
|
|
|
srv *Server
|
|
|
|
userRepo user.UserRepo
|
|
|
|
sessionManager *sessionmanager.SessionManager
|
|
|
|
emailer *email.TemplatizedEmailer
|
|
|
|
redirectURL url.URL
|
|
|
|
clientRepo client.ClientRepo
|
2016-05-12 22:23:01 +05:30
|
|
|
clientManager *clientmanager.ClientManager
|
2016-06-03 01:35:18 +05:30
|
|
|
clientCreds map[string]oidc.ClientCredentials
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
2016-06-07 05:11:11 +05:30
|
|
|
type testFixtureOptions struct {
|
|
|
|
clients []client.Client
|
|
|
|
}
|
|
|
|
|
2016-02-10 00:40:28 +05:30
|
|
|
func sequentialGenerateCodeFunc() sessionmanager.GenerateCodeFunc {
|
2015-08-18 05:57:27 +05:30
|
|
|
x := 0
|
|
|
|
return func() (string, error) {
|
|
|
|
x += 1
|
|
|
|
return fmt.Sprintf("code-%d", x), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeTestFixtures() (*testFixtures, error) {
|
2016-06-07 05:11:11 +05:30
|
|
|
return makeTestFixturesWithOptions(testFixtureOptions{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeTestFixturesWithOptions(options testFixtureOptions) (*testFixtures, error) {
|
2016-02-10 01:52:40 +05:30
|
|
|
dbMap := db.NewMemDB()
|
|
|
|
userRepo, err := db.NewUserRepoFromUsers(dbMap, testUsers)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-10 02:27:42 +05:30
|
|
|
pwRepo, err := db.NewPasswordInfoRepoFromPasswordInfos(dbMap, testPasswordInfos)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-08-18 05:57:27 +05:30
|
|
|
|
|
|
|
connConfigs := []connector.ConnectorConfig{
|
|
|
|
&connector.OIDCConnectorConfig{
|
|
|
|
ID: "oidc",
|
|
|
|
IssuerURL: testIssuerURL.String(),
|
|
|
|
ClientID: "12345",
|
|
|
|
ClientSecret: "567789",
|
|
|
|
},
|
|
|
|
&connector.OIDCConnectorConfig{
|
|
|
|
ID: "oidc-trusted",
|
|
|
|
IssuerURL: testIssuerURL.String(),
|
|
|
|
ClientID: "12345-trusted",
|
|
|
|
ClientSecret: "567789-trusted",
|
|
|
|
TrustedEmailProvider: true,
|
|
|
|
},
|
|
|
|
&connector.LocalConnectorConfig{
|
|
|
|
ID: "local",
|
|
|
|
},
|
|
|
|
}
|
2016-02-10 05:14:38 +05:30
|
|
|
connCfgRepo := db.NewConnectorConfigRepo(dbMap)
|
|
|
|
if err := connCfgRepo.Set(connConfigs); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-12-08 06:49:55 +05:30
|
|
|
|
2016-05-12 22:23:01 +05:30
|
|
|
userManager := usermanager.NewUserManager(userRepo, pwRepo, connCfgRepo, db.TransactionFactory(dbMap), usermanager.ManagerOptions{})
|
2015-08-18 05:57:27 +05:30
|
|
|
|
2016-02-10 00:40:28 +05:30
|
|
|
sessionManager := sessionmanager.NewSessionManager(db.NewSessionRepo(db.NewMemDB()), db.NewSessionKeyRepo(db.NewMemDB()))
|
2015-08-18 05:57:27 +05:30
|
|
|
sessionManager.GenerateCode = sequentialGenerateCodeFunc()
|
|
|
|
|
2016-06-03 01:35:18 +05:30
|
|
|
refreshTokenRepo := refreshtest.NewTestRefreshTokenRepo()
|
|
|
|
|
2015-08-18 05:57:27 +05:30
|
|
|
emailer, err := email.NewTemplatizedEmailerFromGlobs(
|
|
|
|
emailTemplatesLocation+"/*.txt",
|
|
|
|
emailTemplatesLocation+"/*.html",
|
|
|
|
&email.FakeEmailer{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-06-07 05:11:11 +05:30
|
|
|
var clients []client.Client
|
|
|
|
if options.clients == nil {
|
|
|
|
clients = []client.Client{
|
|
|
|
client.Client{
|
|
|
|
Credentials: testClientCredentials,
|
|
|
|
Metadata: oidc.ClientMetadata{
|
|
|
|
RedirectURIs: []url.URL{
|
|
|
|
testRedirectURL,
|
|
|
|
},
|
2015-08-18 05:57:27 +05:30
|
|
|
},
|
|
|
|
},
|
2016-06-07 05:11:11 +05:30
|
|
|
}
|
|
|
|
} else {
|
|
|
|
clients = options.clients
|
2016-05-12 22:23:01 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
clientIDGenerator := func(hostport string) (string, error) {
|
|
|
|
return hostport, nil
|
|
|
|
}
|
|
|
|
secGen := func() ([]byte, error) {
|
|
|
|
return []byte("secret"), nil
|
|
|
|
}
|
2016-06-07 06:28:16 +05:30
|
|
|
clientRepo, err := db.NewClientRepoFromClients(dbMap, clients)
|
2016-02-10 04:36:07 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-06-07 06:28:16 +05:30
|
|
|
|
|
|
|
clientManager := clientmanager.NewClientManager(clientRepo, db.TransactionFactory(dbMap), clientmanager.ManagerOptions{ClientIDGenerator: clientIDGenerator, SecretGenerator: secGen})
|
|
|
|
|
2015-08-18 05:57:27 +05:30
|
|
|
km := key.NewPrivateKeyManager()
|
|
|
|
err = km.Set(key.NewPrivateKeySet([]*key.PrivateKey{testPrivKey}, time.Now().Add(time.Minute)))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-12-24 00:18:20 +05:30
|
|
|
tpl, err := getTemplates("dex",
|
|
|
|
"https://coreos.com/assets/images/brand/coreos-mark-30px.png",
|
2015-10-01 05:05:58 +05:30
|
|
|
true, templatesLocation)
|
2015-08-18 05:57:27 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
srv := &Server{
|
2016-04-15 04:57:57 +05:30
|
|
|
IssuerURL: testIssuerURL,
|
|
|
|
SessionManager: sessionManager,
|
|
|
|
ClientRepo: clientRepo,
|
|
|
|
Templates: tpl,
|
|
|
|
UserRepo: userRepo,
|
|
|
|
PasswordInfoRepo: pwRepo,
|
2016-05-12 22:23:01 +05:30
|
|
|
UserManager: userManager,
|
|
|
|
ClientManager: clientManager,
|
2016-04-15 04:57:57 +05:30
|
|
|
KeyManager: km,
|
2016-06-03 01:35:18 +05:30
|
|
|
RefreshTokenRepo: refreshTokenRepo,
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
err = setTemplates(srv, tpl)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, config := range connConfigs {
|
|
|
|
if err := srv.AddConnector(config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
srv.UserEmailer = useremail.NewUserEmailer(srv.UserRepo,
|
|
|
|
srv.PasswordInfoRepo,
|
|
|
|
srv.KeyManager.Signer,
|
|
|
|
srv.SessionManager.ValidityWindow,
|
|
|
|
srv.IssuerURL,
|
|
|
|
emailer,
|
|
|
|
"noreply@example.com",
|
|
|
|
srv.absURL(httpPathResetPassword),
|
2015-11-10 04:05:11 +05:30
|
|
|
srv.absURL(httpPathEmailVerify),
|
|
|
|
srv.absURL(httpPathAcceptInvitation),
|
|
|
|
)
|
2015-08-18 05:57:27 +05:30
|
|
|
|
|
|
|
return &testFixtures{
|
2016-04-15 04:57:57 +05:30
|
|
|
srv: srv,
|
|
|
|
redirectURL: testRedirectURL,
|
|
|
|
userRepo: userRepo,
|
|
|
|
sessionManager: sessionManager,
|
|
|
|
emailer: emailer,
|
|
|
|
clientRepo: clientRepo,
|
2016-05-12 22:23:01 +05:30
|
|
|
clientManager: clientManager,
|
2016-06-03 01:35:18 +05:30
|
|
|
clientCreds: map[string]oidc.ClientCredentials{
|
|
|
|
testClientID: testClientCreds,
|
|
|
|
},
|
2015-08-18 05:57:27 +05:30
|
|
|
}, nil
|
|
|
|
}
|