2015-12-08 04:59:58 +05:30
|
|
|
package manager
|
2015-08-18 05:57:27 +05:30
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/coreos/go-oidc/jose"
|
|
|
|
"github.com/jonboulle/clockwork"
|
|
|
|
"github.com/kylelemons/godebug/pretty"
|
|
|
|
|
2015-12-08 06:49:55 +05:30
|
|
|
"github.com/coreos/dex/connector"
|
2015-08-18 05:57:27 +05:30
|
|
|
"github.com/coreos/dex/repo"
|
2015-12-08 04:59:58 +05:30
|
|
|
"github.com/coreos/dex/user"
|
2015-08-18 05:57:27 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
type testFixtures struct {
|
2015-12-08 04:59:58 +05:30
|
|
|
ur user.UserRepo
|
|
|
|
pwr user.PasswordInfoRepo
|
2015-12-08 06:49:55 +05:30
|
|
|
ccr connector.ConnectorConfigRepo
|
2015-12-08 04:59:58 +05:30
|
|
|
mgr *UserManager
|
2015-08-18 05:57:27 +05:30
|
|
|
clock clockwork.Clock
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeTestFixtures() *testFixtures {
|
|
|
|
f := &testFixtures{}
|
|
|
|
f.clock = clockwork.NewFakeClock()
|
|
|
|
|
2015-12-08 04:59:58 +05:30
|
|
|
f.ur = user.NewUserRepoFromUsers([]user.UserWithRemoteIdentities{
|
2015-08-18 05:57:27 +05:30
|
|
|
{
|
2015-12-08 04:59:58 +05:30
|
|
|
User: user.User{
|
2015-08-18 05:57:27 +05:30
|
|
|
ID: "ID-1",
|
|
|
|
Email: "Email-1@example.com",
|
|
|
|
},
|
2015-12-08 04:59:58 +05:30
|
|
|
RemoteIdentities: []user.RemoteIdentity{
|
2015-08-18 05:57:27 +05:30
|
|
|
{
|
|
|
|
ConnectorID: "local",
|
|
|
|
ID: "1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, {
|
2015-12-08 04:59:58 +05:30
|
|
|
User: user.User{
|
2015-08-18 05:57:27 +05:30
|
|
|
ID: "ID-2",
|
|
|
|
Email: "Email-2@example.com",
|
|
|
|
EmailVerified: true,
|
|
|
|
},
|
2015-12-08 04:59:58 +05:30
|
|
|
RemoteIdentities: []user.RemoteIdentity{
|
2015-08-18 05:57:27 +05:30
|
|
|
{
|
|
|
|
ConnectorID: "local",
|
|
|
|
ID: "2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2015-12-08 04:59:58 +05:30
|
|
|
f.pwr = user.NewPasswordInfoRepoFromPasswordInfos([]user.PasswordInfo{
|
2015-08-18 05:57:27 +05:30
|
|
|
{
|
|
|
|
UserID: "ID-1",
|
|
|
|
Password: []byte("password-1"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
UserID: "ID-2",
|
|
|
|
Password: []byte("password-2"),
|
|
|
|
},
|
|
|
|
})
|
2015-12-08 06:49:55 +05:30
|
|
|
f.ccr = connector.NewConnectorConfigRepoFromConfigs([]connector.ConnectorConfig{
|
|
|
|
&connector.LocalConnectorConfig{ID: "local"},
|
|
|
|
})
|
|
|
|
f.mgr = NewUserManager(f.ur, f.pwr, f.ccr, repo.InMemTransactionFactory, ManagerOptions{})
|
2015-08-18 05:57:27 +05:30
|
|
|
f.mgr.Clock = f.clock
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRegisterWithRemoteIdentity(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
email string
|
|
|
|
emailVerified bool
|
2015-12-08 04:59:58 +05:30
|
|
|
rid user.RemoteIdentity
|
2015-08-18 05:57:27 +05:30
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
email: "email@example.com",
|
|
|
|
emailVerified: false,
|
2015-12-08 04:59:58 +05:30
|
|
|
rid: user.RemoteIdentity{
|
2015-08-18 05:57:27 +05:30
|
|
|
ConnectorID: "local",
|
|
|
|
ID: "1234",
|
|
|
|
},
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
emailVerified: false,
|
2015-12-08 04:59:58 +05:30
|
|
|
rid: user.RemoteIdentity{
|
2015-08-18 05:57:27 +05:30
|
|
|
ConnectorID: "local",
|
|
|
|
ID: "1234",
|
|
|
|
},
|
2015-12-08 04:59:58 +05:30
|
|
|
err: user.ErrorInvalidEmail,
|
2015-08-18 05:57:27 +05:30
|
|
|
},
|
|
|
|
{
|
|
|
|
email: "email@example.com",
|
|
|
|
emailVerified: false,
|
2015-12-08 04:59:58 +05:30
|
|
|
rid: user.RemoteIdentity{
|
2015-08-18 05:57:27 +05:30
|
|
|
ConnectorID: "local",
|
|
|
|
ID: "1",
|
|
|
|
},
|
2015-12-08 04:59:58 +05:30
|
|
|
err: user.ErrorDuplicateRemoteIdentity,
|
2015-08-18 05:57:27 +05:30
|
|
|
},
|
2015-12-08 06:49:55 +05:30
|
|
|
{
|
|
|
|
email: "anotheremail@example.com",
|
|
|
|
emailVerified: false,
|
|
|
|
rid: user.RemoteIdentity{
|
|
|
|
ConnectorID: "idonotexist",
|
|
|
|
ID: "1",
|
|
|
|
},
|
|
|
|
err: connector.ErrorNotFound,
|
|
|
|
},
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
|
|
|
f := makeTestFixtures()
|
|
|
|
userID, err := f.mgr.RegisterWithRemoteIdentity(
|
|
|
|
tt.email,
|
|
|
|
tt.emailVerified,
|
|
|
|
tt.rid)
|
|
|
|
|
|
|
|
if tt.err != nil {
|
|
|
|
if tt.err != err {
|
|
|
|
t.Errorf("case %d: want=%q, got=%q", i, tt.err, err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
usr, err := f.ur.Get(nil, userID)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("case %d: err != nil: %q", i, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if usr.Email != tt.email {
|
|
|
|
t.Errorf("case %d: user.Email: want=%q, got=%q", i, tt.email, usr.Email)
|
|
|
|
}
|
|
|
|
if usr.EmailVerified != tt.emailVerified {
|
|
|
|
t.Errorf("case %d: user.EmailVerified: want=%v, got=%v", i, tt.emailVerified, usr.EmailVerified)
|
|
|
|
}
|
|
|
|
|
|
|
|
ridUSR, err := f.ur.GetByRemoteIdentity(nil, tt.rid)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("case %d: err != nil: %q", i, err)
|
|
|
|
}
|
|
|
|
if diff := pretty.Compare(usr, ridUSR); diff != "" {
|
|
|
|
t.Errorf("case %d: Compare(want, got) = %v", i, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRegisterWithPassword(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
email string
|
|
|
|
plaintext string
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
email: "email@example.com",
|
|
|
|
plaintext: "secretpassword123",
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
plaintext: "secretpassword123",
|
2015-12-08 04:59:58 +05:30
|
|
|
err: user.ErrorInvalidEmail,
|
2015-08-18 05:57:27 +05:30
|
|
|
},
|
|
|
|
{
|
|
|
|
email: "email@example.com",
|
2015-12-08 04:59:58 +05:30
|
|
|
err: user.ErrorInvalidPassword,
|
2015-08-18 05:57:27 +05:30
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
|
|
|
f := makeTestFixtures()
|
2015-12-08 06:49:55 +05:30
|
|
|
connID := "local"
|
2015-08-18 05:57:27 +05:30
|
|
|
userID, err := f.mgr.RegisterWithPassword(
|
|
|
|
tt.email,
|
|
|
|
tt.plaintext,
|
|
|
|
connID)
|
|
|
|
|
|
|
|
if tt.err != nil {
|
|
|
|
if tt.err != err {
|
|
|
|
t.Errorf("case %d: want=%q, got=%q", i, tt.err, err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
usr, err := f.ur.Get(nil, userID)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("case %d: err != nil: %q", i, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if usr.Email != tt.email {
|
|
|
|
t.Errorf("case %d: user.Email: want=%q, got=%q", i, tt.email, usr.Email)
|
|
|
|
}
|
|
|
|
if usr.EmailVerified != false {
|
|
|
|
t.Errorf("case %d: user.EmailVerified: want=%v, got=%v", i, false, usr.EmailVerified)
|
|
|
|
}
|
|
|
|
|
2015-12-08 04:59:58 +05:30
|
|
|
ridUSR, err := f.ur.GetByRemoteIdentity(nil, user.RemoteIdentity{
|
2015-08-18 05:57:27 +05:30
|
|
|
ID: userID,
|
|
|
|
ConnectorID: connID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("case %d: err != nil: %q", i, err)
|
|
|
|
}
|
|
|
|
if diff := pretty.Compare(usr, ridUSR); diff != "" {
|
|
|
|
t.Errorf("case %d: Compare(want, got) = %v", i, diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
pwi, err := f.pwr.Get(nil, userID)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("case %d: err != nil: %q", i, err)
|
|
|
|
}
|
|
|
|
ident, err := pwi.Authenticate(tt.plaintext)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("case %d: err != nil: %q", i, err)
|
|
|
|
}
|
|
|
|
if ident.ID != userID {
|
|
|
|
t.Errorf("case %d: ident.ID: want=%q, got=%q", i, userID, ident.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = pwi.Authenticate(tt.plaintext + "WRONG")
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("case %d: want non-nil err", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVerifyEmail(t *testing.T) {
|
|
|
|
now := time.Now()
|
|
|
|
issuer, _ := url.Parse("http://example.com")
|
|
|
|
clientID := "myclient"
|
|
|
|
callback := "http://client.example.com/callback"
|
|
|
|
expires := time.Hour * 3
|
|
|
|
|
2015-12-08 04:59:58 +05:30
|
|
|
makeClaims := func(usr user.User) jose.Claims {
|
2015-08-18 05:57:27 +05:30
|
|
|
return map[string]interface{}{
|
|
|
|
"iss": issuer.String(),
|
|
|
|
"aud": clientID,
|
2015-12-08 04:59:58 +05:30
|
|
|
user.ClaimEmailVerificationCallback: callback,
|
|
|
|
user.ClaimEmailVerificationEmail: usr.Email,
|
2015-08-18 05:57:27 +05:30
|
|
|
"exp": float64(now.Add(expires).Unix()),
|
|
|
|
"sub": usr.ID,
|
|
|
|
"iat": float64(now.Unix()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
evClaims jose.Claims
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
// happy path
|
2015-12-08 04:59:58 +05:30
|
|
|
evClaims: makeClaims(user.User{ID: "ID-1", Email: "Email-1@example.com"}),
|
2015-08-18 05:57:27 +05:30
|
|
|
},
|
|
|
|
{
|
|
|
|
// non-matching email
|
2015-12-08 04:59:58 +05:30
|
|
|
evClaims: makeClaims(user.User{ID: "ID-1", Email: "Email-2@example.com"}),
|
2015-08-18 05:57:27 +05:30
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// already verified email
|
2015-12-08 04:59:58 +05:30
|
|
|
evClaims: makeClaims(user.User{ID: "ID-2", Email: "Email-2@example.com"}),
|
2015-08-18 05:57:27 +05:30
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// non-existent user.
|
2015-12-08 04:59:58 +05:30
|
|
|
evClaims: makeClaims(user.User{ID: "ID-UNKNOWN", Email: "noone@example.com"}),
|
2015-08-18 05:57:27 +05:30
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
|
|
|
f := makeTestFixtures()
|
2015-12-08 04:59:58 +05:30
|
|
|
cb, err := f.mgr.VerifyEmail(user.EmailVerification{tt.evClaims})
|
2015-08-18 05:57:27 +05:30
|
|
|
if tt.wantErr {
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("case %d: want non-nil err", i)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("case %d: want err=nil got=%q", i, err)
|
|
|
|
}
|
|
|
|
|
2015-12-08 04:59:58 +05:30
|
|
|
if cb.String() != tt.evClaims[user.ClaimEmailVerificationCallback] {
|
2015-08-18 05:57:27 +05:30
|
|
|
t.Errorf("case %d: want=%q, got=%q", i, cb.String(),
|
2015-12-08 04:59:58 +05:30
|
|
|
tt.evClaims[user.ClaimEmailVerificationCallback])
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChangePassword(t *testing.T) {
|
|
|
|
now := time.Now()
|
|
|
|
issuer, _ := url.Parse("http://example.com")
|
|
|
|
clientID := "myclient"
|
|
|
|
callback := "http://client.example.com/callback"
|
|
|
|
expires := time.Hour * 3
|
|
|
|
password := "password-1"
|
|
|
|
|
|
|
|
makeClaims := func(usrID, callback string) jose.Claims {
|
|
|
|
return map[string]interface{}{
|
|
|
|
"iss": issuer.String(),
|
|
|
|
"aud": clientID,
|
2015-12-08 04:59:58 +05:30
|
|
|
user.ClaimPasswordResetCallback: callback,
|
|
|
|
user.ClaimPasswordResetPassword: password,
|
2015-08-18 05:57:27 +05:30
|
|
|
"exp": float64(now.Add(expires).Unix()),
|
|
|
|
"sub": usrID,
|
|
|
|
"iat": float64(now.Unix()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
pwrClaims jose.Claims
|
|
|
|
newPassword string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
// happy path
|
|
|
|
pwrClaims: makeClaims("ID-1", callback),
|
|
|
|
newPassword: "password-1.1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// happy path with no callback
|
|
|
|
pwrClaims: makeClaims("ID-1", ""),
|
|
|
|
newPassword: "password-1.1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// passwords don't match changed
|
|
|
|
pwrClaims: makeClaims("ID-2", callback),
|
|
|
|
newPassword: "password-1.1",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// user doesn't exist
|
|
|
|
pwrClaims: makeClaims("ID-123", callback),
|
|
|
|
newPassword: "password-1.1",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
|
|
|
f := makeTestFixtures()
|
2015-12-08 04:59:58 +05:30
|
|
|
cb, err := f.mgr.ChangePassword(user.PasswordReset{tt.pwrClaims}, tt.newPassword)
|
2015-08-18 05:57:27 +05:30
|
|
|
if tt.wantErr {
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("case %d: want non-nil err", i)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("case %d: want err=nil got=%q", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var cbString string
|
|
|
|
if cb != nil {
|
|
|
|
cbString = cb.String()
|
|
|
|
}
|
2015-12-08 04:59:58 +05:30
|
|
|
if cbString != tt.pwrClaims[user.ClaimPasswordResetCallback] {
|
2015-08-18 05:57:27 +05:30
|
|
|
t.Errorf("case %d: want=%q, got=%q", i, cb.String(),
|
2015-12-08 04:59:58 +05:30
|
|
|
tt.pwrClaims[user.ClaimPasswordResetCallback])
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateUser(t *testing.T) {
|
|
|
|
tests := []struct {
|
2015-12-08 04:59:58 +05:30
|
|
|
usr user.User
|
|
|
|
hashedPW user.Password
|
2015-12-08 06:49:55 +05:30
|
|
|
localID string // defaults to "local"
|
2015-08-18 05:57:27 +05:30
|
|
|
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
2015-12-08 04:59:58 +05:30
|
|
|
usr: user.User{
|
2015-08-18 05:57:27 +05:30
|
|
|
DisplayName: "Bob Exampleson",
|
|
|
|
Email: "bob@example.com",
|
|
|
|
},
|
2015-12-08 04:59:58 +05:30
|
|
|
hashedPW: user.Password("I am a hash"),
|
2015-08-18 05:57:27 +05:30
|
|
|
},
|
|
|
|
{
|
2015-12-08 04:59:58 +05:30
|
|
|
usr: user.User{
|
2015-08-18 05:57:27 +05:30
|
|
|
DisplayName: "Al Adminson",
|
|
|
|
Email: "al@example.com",
|
|
|
|
Admin: true,
|
|
|
|
},
|
2015-12-08 04:59:58 +05:30
|
|
|
hashedPW: user.Password("I am a hash"),
|
2015-08-18 05:57:27 +05:30
|
|
|
},
|
|
|
|
{
|
2015-12-08 04:59:58 +05:30
|
|
|
usr: user.User{
|
2015-08-18 05:57:27 +05:30
|
|
|
DisplayName: "Ed Emailless",
|
|
|
|
},
|
2015-12-08 04:59:58 +05:30
|
|
|
hashedPW: user.Password("I am a hash"),
|
2015-08-18 05:57:27 +05:30
|
|
|
wantErr: true,
|
|
|
|
},
|
2015-12-08 06:49:55 +05:30
|
|
|
{
|
|
|
|
usr: user.User{
|
|
|
|
DisplayName: "Eric Exampleson",
|
|
|
|
Email: "eric@example.com",
|
|
|
|
},
|
|
|
|
hashedPW: user.Password("I am a hash"),
|
|
|
|
localID: "abadlocalid",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
|
|
|
f := makeTestFixtures()
|
2015-12-08 06:49:55 +05:30
|
|
|
localID := "local"
|
|
|
|
if tt.localID != "" {
|
|
|
|
localID = tt.localID
|
|
|
|
}
|
|
|
|
id, err := f.mgr.CreateUser(tt.usr, tt.hashedPW, localID)
|
2015-08-18 05:57:27 +05:30
|
|
|
if tt.wantErr {
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("case %d: want non-nil err", i)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if id == "" {
|
|
|
|
t.Errorf("case %d: want non-empty id", i)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("case %d: unexpected err: %v", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
gotUsr, err := f.ur.Get(nil, id)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("case %d: unexpected err: %v", i, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tt.usr.ID = id
|
|
|
|
tt.usr.CreatedAt = f.clock.Now()
|
|
|
|
if diff := pretty.Compare(tt.usr, gotUsr); diff != "" {
|
|
|
|
t.Errorf("case %d: Compare(want, got) = %v", i, diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
pwi, err := f.pwr.Get(nil, id)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("case %d: unexpected err: %v", i, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(pwi.Password) != string(tt.hashedPW) {
|
|
|
|
t.Errorf("case %d: want=%q, got=%q", i, tt.hashedPW, pwi.Password)
|
|
|
|
}
|
|
|
|
|
2015-12-08 04:59:58 +05:30
|
|
|
ridUser, err := f.ur.GetByRemoteIdentity(nil, user.RemoteIdentity{
|
2015-08-18 05:57:27 +05:30
|
|
|
ID: id,
|
|
|
|
ConnectorID: "local",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("case %d: err != nil: %q", i, err)
|
|
|
|
}
|
|
|
|
if diff := pretty.Compare(gotUsr, ridUser); diff != "" {
|
|
|
|
t.Errorf("case %d: Compare(want, got) = %v", i, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|