2015-12-08 04:59:58 +05:30
|
|
|
package manager
|
2015-08-18 05:57:27 +05:30
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"github.com/jonboulle/clockwork"
|
|
|
|
|
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/pkg/log"
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-03-02 03:39:10 +05:30
|
|
|
ErrorEmailAlreadyVerified = errors.New("email already verified")
|
2015-08-18 05:57:27 +05:30
|
|
|
ErrorPasswordAlreadyChanged = errors.New("password has already been changed")
|
|
|
|
)
|
|
|
|
|
|
|
|
// Manager performs user-related "business-logic" functions on user and related objects.
|
|
|
|
// This is in contrast to the Repos which perform little more than CRUD operations.
|
2015-12-08 04:59:58 +05:30
|
|
|
type UserManager struct {
|
2015-08-18 05:57:27 +05:30
|
|
|
Clock clockwork.Clock
|
|
|
|
|
2015-12-08 04:59:58 +05:30
|
|
|
userRepo user.UserRepo
|
|
|
|
pwRepo user.PasswordInfoRepo
|
2015-12-08 06:49:55 +05:30
|
|
|
connCfgRepo connector.ConnectorConfigRepo
|
2015-08-18 05:57:27 +05:30
|
|
|
begin repo.TransactionFactory
|
2015-12-08 04:59:58 +05:30
|
|
|
userIDGenerator user.UserIDGenerator
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
type ManagerOptions struct {
|
|
|
|
// This is empty right now but will soon contain configuration information
|
|
|
|
// such as passowrd length, name length, password expiration time and other
|
|
|
|
// variable policies
|
|
|
|
}
|
|
|
|
|
2015-12-08 06:49:55 +05:30
|
|
|
func NewUserManager(userRepo user.UserRepo, pwRepo user.PasswordInfoRepo, connCfgRepo connector.ConnectorConfigRepo, txnFactory repo.TransactionFactory, options ManagerOptions) *UserManager {
|
2015-12-08 04:59:58 +05:30
|
|
|
return &UserManager{
|
2015-08-18 05:57:27 +05:30
|
|
|
Clock: clockwork.NewRealClock(),
|
|
|
|
|
|
|
|
userRepo: userRepo,
|
|
|
|
pwRepo: pwRepo,
|
2015-12-08 06:49:55 +05:30
|
|
|
connCfgRepo: connCfgRepo,
|
2015-08-18 05:57:27 +05:30
|
|
|
begin: txnFactory,
|
2015-12-08 04:59:58 +05:30
|
|
|
userIDGenerator: user.DefaultUserIDGenerator,
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-08 04:59:58 +05:30
|
|
|
func (m *UserManager) Get(id string) (user.User, error) {
|
2015-08-18 05:57:27 +05:30
|
|
|
return m.userRepo.Get(nil, id)
|
|
|
|
}
|
|
|
|
|
2015-12-08 04:59:58 +05:30
|
|
|
func (m *UserManager) List(filter user.UserFilter, maxResults int, nextPageToken string) ([]user.User, string, error) {
|
2015-08-18 05:57:27 +05:30
|
|
|
return m.userRepo.List(nil, filter, maxResults, nextPageToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateUser creates a new user with the given hashedPassword; the connID should be the ID of the local connector.
|
|
|
|
// The userID of the created user is returned as the first argument.
|
2015-12-08 04:59:58 +05:30
|
|
|
func (m *UserManager) CreateUser(usr user.User, hashedPassword user.Password, connID string) (string, error) {
|
2015-08-18 05:57:27 +05:30
|
|
|
tx, err := m.begin()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-12-08 04:59:58 +05:30
|
|
|
insertedUser, err := m.insertNewUser(tx, usr.Email, usr.EmailVerified)
|
2015-08-18 05:57:27 +05:30
|
|
|
if err != nil {
|
|
|
|
rollback(tx)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-12-08 04:59:58 +05:30
|
|
|
usr.ID = insertedUser.ID
|
|
|
|
usr.CreatedAt = insertedUser.CreatedAt
|
|
|
|
err = m.userRepo.Update(tx, usr)
|
2015-08-18 05:57:27 +05:30
|
|
|
if err != nil {
|
|
|
|
rollback(tx)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-12-08 04:59:58 +05:30
|
|
|
rid := user.RemoteIdentity{
|
2015-08-18 05:57:27 +05:30
|
|
|
ConnectorID: connID,
|
2015-12-08 04:59:58 +05:30
|
|
|
ID: usr.ID,
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
2015-12-08 06:49:55 +05:30
|
|
|
if err := m.addRemoteIdentity(tx, usr.ID, rid); err != nil {
|
2015-08-18 05:57:27 +05:30
|
|
|
rollback(tx)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-12-08 04:59:58 +05:30
|
|
|
pwi := user.PasswordInfo{
|
|
|
|
UserID: usr.ID,
|
2015-08-18 05:57:27 +05:30
|
|
|
Password: hashedPassword,
|
|
|
|
}
|
|
|
|
err = m.pwRepo.Create(tx, pwi)
|
|
|
|
if err != nil {
|
|
|
|
rollback(tx)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tx.Commit()
|
|
|
|
if err != nil {
|
|
|
|
rollback(tx)
|
|
|
|
return "", err
|
|
|
|
}
|
2015-12-08 04:59:58 +05:30
|
|
|
return usr.ID, nil
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
2015-12-08 04:59:58 +05:30
|
|
|
func (m *UserManager) Disable(userID string, disabled bool) error {
|
2015-09-29 02:21:48 +05:30
|
|
|
tx, err := m.begin()
|
|
|
|
|
|
|
|
if err = m.userRepo.Disable(tx, userID, disabled); err != nil {
|
|
|
|
rollback(tx)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = tx.Commit(); err != nil {
|
|
|
|
rollback(tx)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-18 05:57:27 +05:30
|
|
|
// RegisterWithRemoteIdentity creates new user and attaches the given remote identity.
|
2015-12-08 04:59:58 +05:30
|
|
|
func (m *UserManager) RegisterWithRemoteIdentity(email string, emailVerified bool, rid user.RemoteIdentity) (string, error) {
|
2015-08-18 05:57:27 +05:30
|
|
|
tx, err := m.begin()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = m.userRepo.GetByRemoteIdentity(tx, rid); err == nil {
|
|
|
|
rollback(tx)
|
2015-12-08 04:59:58 +05:30
|
|
|
return "", user.ErrorDuplicateRemoteIdentity
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
2015-12-08 04:59:58 +05:30
|
|
|
if err != user.ErrorNotFound {
|
2015-08-18 05:57:27 +05:30
|
|
|
rollback(tx)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-12-08 04:59:58 +05:30
|
|
|
usr, err := m.insertNewUser(tx, email, emailVerified)
|
2015-08-18 05:57:27 +05:30
|
|
|
if err != nil {
|
|
|
|
rollback(tx)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-12-08 06:49:55 +05:30
|
|
|
if err := m.addRemoteIdentity(tx, usr.ID, rid); err != nil {
|
2015-08-18 05:57:27 +05:30
|
|
|
rollback(tx)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tx.Commit()
|
|
|
|
if err != nil {
|
|
|
|
rollback(tx)
|
|
|
|
return "", err
|
|
|
|
}
|
2015-12-08 04:59:58 +05:30
|
|
|
return usr.ID, nil
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterWithPassword creates a new user with the given name and password.
|
|
|
|
// connID is the connector ID of the ConnectorLocal connector.
|
2015-12-08 04:59:58 +05:30
|
|
|
func (m *UserManager) RegisterWithPassword(email, plaintext, connID string) (string, error) {
|
2015-08-18 05:57:27 +05:30
|
|
|
tx, err := m.begin()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-12-08 04:59:58 +05:30
|
|
|
if !user.ValidPassword(plaintext) {
|
2015-08-18 05:57:27 +05:30
|
|
|
rollback(tx)
|
2015-12-08 04:59:58 +05:30
|
|
|
return "", user.ErrorInvalidPassword
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
2015-12-08 04:59:58 +05:30
|
|
|
usr, err := m.insertNewUser(tx, email, false)
|
2015-08-18 05:57:27 +05:30
|
|
|
if err != nil {
|
|
|
|
rollback(tx)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-12-08 04:59:58 +05:30
|
|
|
rid := user.RemoteIdentity{
|
2015-08-18 05:57:27 +05:30
|
|
|
ConnectorID: connID,
|
2015-12-08 04:59:58 +05:30
|
|
|
ID: usr.ID,
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
2015-12-08 06:49:55 +05:30
|
|
|
if err := m.addRemoteIdentity(tx, usr.ID, rid); err != nil {
|
2015-08-18 05:57:27 +05:30
|
|
|
rollback(tx)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-12-08 04:59:58 +05:30
|
|
|
password, err := user.NewPasswordFromPlaintext(plaintext)
|
2015-08-18 05:57:27 +05:30
|
|
|
if err != nil {
|
|
|
|
rollback(tx)
|
|
|
|
return "", err
|
|
|
|
}
|
2015-12-08 04:59:58 +05:30
|
|
|
pwi := user.PasswordInfo{
|
|
|
|
UserID: usr.ID,
|
2015-08-18 05:57:27 +05:30
|
|
|
Password: password,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = m.pwRepo.Create(tx, pwi)
|
|
|
|
if err != nil {
|
|
|
|
rollback(tx)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tx.Commit()
|
|
|
|
if err != nil {
|
|
|
|
rollback(tx)
|
|
|
|
return "", err
|
|
|
|
}
|
2015-12-08 04:59:58 +05:30
|
|
|
return usr.ID, nil
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
2015-11-10 04:05:11 +05:30
|
|
|
type EmailVerifiable interface {
|
|
|
|
UserID() string
|
|
|
|
Email() string
|
|
|
|
Callback() *url.URL
|
|
|
|
}
|
|
|
|
|
2015-08-18 05:57:27 +05:30
|
|
|
// VerifyEmail sets EmailVerified to true for the user for the given EmailVerification.
|
|
|
|
// The email in the EmailVerification must match the User's email in the
|
|
|
|
// repository, and it must not already be verified.
|
|
|
|
// This function expects that ParseAndVerifyEmailVerificationToken was used to
|
|
|
|
// create it, ensuring that the token was signed and that the JWT was not
|
|
|
|
// expired.
|
|
|
|
// The callback url (i.e. where to send the user after the verification) is returned.
|
2015-12-08 04:59:58 +05:30
|
|
|
func (m *UserManager) VerifyEmail(ev EmailVerifiable) (*url.URL, error) {
|
2015-08-18 05:57:27 +05:30
|
|
|
tx, err := m.begin()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-03-02 03:39:10 +05:30
|
|
|
usr, err := m.userRepo.GetByEmail(tx, ev.Email())
|
2015-08-18 05:57:27 +05:30
|
|
|
if err != nil {
|
|
|
|
rollback(tx)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-03-02 03:39:10 +05:30
|
|
|
if usr.ID != ev.UserID() {
|
2015-08-18 05:57:27 +05:30
|
|
|
rollback(tx)
|
2016-03-02 03:39:10 +05:30
|
|
|
return nil, user.ErrorNotFound
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
2015-12-08 04:59:58 +05:30
|
|
|
if usr.EmailVerified {
|
2015-08-18 05:57:27 +05:30
|
|
|
rollback(tx)
|
|
|
|
return nil, ErrorEmailAlreadyVerified
|
|
|
|
}
|
|
|
|
|
2015-12-08 04:59:58 +05:30
|
|
|
usr.EmailVerified = true
|
2015-08-18 05:57:27 +05:30
|
|
|
|
2015-12-08 04:59:58 +05:30
|
|
|
err = m.userRepo.Update(tx, usr)
|
2015-08-18 05:57:27 +05:30
|
|
|
if err != nil {
|
|
|
|
rollback(tx)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tx.Commit()
|
|
|
|
if err != nil {
|
|
|
|
rollback(tx)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ev.Callback(), nil
|
|
|
|
}
|
|
|
|
|
2015-11-10 04:05:11 +05:30
|
|
|
type PasswordChangeable interface {
|
|
|
|
UserID() string
|
2015-12-08 04:59:58 +05:30
|
|
|
Password() user.Password
|
2015-11-10 04:05:11 +05:30
|
|
|
Callback() *url.URL
|
|
|
|
}
|
|
|
|
|
2015-12-08 04:59:58 +05:30
|
|
|
func (m *UserManager) ChangePassword(pwr PasswordChangeable, plaintext string) (*url.URL, error) {
|
2015-08-18 05:57:27 +05:30
|
|
|
tx, err := m.begin()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-12-08 04:59:58 +05:30
|
|
|
if !user.ValidPassword(plaintext) {
|
2015-08-18 05:57:27 +05:30
|
|
|
rollback(tx)
|
2015-12-08 04:59:58 +05:30
|
|
|
return nil, user.ErrorInvalidPassword
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
pwi, err := m.pwRepo.Get(tx, pwr.UserID())
|
|
|
|
if err != nil {
|
|
|
|
rollback(tx)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(pwi.Password) != string(pwr.Password()) {
|
|
|
|
rollback(tx)
|
|
|
|
return nil, ErrorPasswordAlreadyChanged
|
|
|
|
}
|
|
|
|
|
2015-12-08 04:59:58 +05:30
|
|
|
newPass, err := user.NewPasswordFromPlaintext(plaintext)
|
2015-08-18 05:57:27 +05:30
|
|
|
if err != nil {
|
|
|
|
rollback(tx)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pwi.Password = newPass
|
|
|
|
err = m.pwRepo.Update(tx, pwi)
|
|
|
|
if err != nil {
|
|
|
|
rollback(tx)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tx.Commit()
|
|
|
|
if err != nil {
|
|
|
|
rollback(tx)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return pwr.Callback(), nil
|
|
|
|
}
|
|
|
|
|
2015-12-08 04:59:58 +05:30
|
|
|
func (m *UserManager) insertNewUser(tx repo.Transaction, email string, emailVerified bool) (user.User, error) {
|
|
|
|
if !user.ValidEmail(email) {
|
|
|
|
return user.User{}, user.ErrorInvalidEmail
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if _, err = m.userRepo.GetByEmail(tx, email); err == nil {
|
2015-12-08 04:59:58 +05:30
|
|
|
return user.User{}, user.ErrorDuplicateEmail
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
2015-12-08 04:59:58 +05:30
|
|
|
if err != user.ErrorNotFound {
|
|
|
|
return user.User{}, err
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
userID, err := m.userIDGenerator()
|
|
|
|
if err != nil {
|
2015-12-08 04:59:58 +05:30
|
|
|
return user.User{}, err
|
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
|
|
|
ID: userID,
|
|
|
|
Email: email,
|
|
|
|
EmailVerified: emailVerified,
|
|
|
|
CreatedAt: m.Clock.Now(),
|
|
|
|
}
|
|
|
|
|
2015-12-08 04:59:58 +05:30
|
|
|
err = m.userRepo.Create(tx, usr)
|
2015-08-18 05:57:27 +05:30
|
|
|
if err != nil {
|
2015-12-08 04:59:58 +05:30
|
|
|
return user.User{}, err
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
2015-12-08 04:59:58 +05:30
|
|
|
return usr, nil
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
2015-12-08 06:49:55 +05:30
|
|
|
func (m *UserManager) addRemoteIdentity(tx repo.Transaction, userID string, rid user.RemoteIdentity) error {
|
|
|
|
if _, err := m.connCfgRepo.GetConnectorByID(tx, rid.ConnectorID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := m.userRepo.AddRemoteIdentity(tx, userID, rid); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-18 05:57:27 +05:30
|
|
|
func rollback(tx repo.Transaction) {
|
|
|
|
err := tx.Rollback()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("unable to rollback: %v", err)
|
|
|
|
}
|
|
|
|
}
|