Exemple #1
0
func (s *Service) createUserCommon(db *gorm.DB, username, password string) (*User, error) {
	// Start with a user without a password
	user := &User{
		Username: username,
		Password: util.StringOrNull(""),
	}

	// If the password is being set already, create a bcrypt hash
	if password != "" {
		passwordHash, err := pass.HashPassword(password)
		if err != nil {
			return nil, err
		}
		user.Password = util.StringOrNull(string(passwordHash))
	}

	// Check the username is available
	if s.UserExists(user.Username) {
		return nil, ErrUsernameTaken
	}

	// Create the user
	if err := db.Create(user).Error; err != nil {
		return nil, err
	}
	return user, nil
}
Exemple #2
0
// CreateUser saves a new user to database
func (s *Service) CreateUser(username, thePassword string) (*User, error) {
	passwordHash, err := password.HashPassword(thePassword)
	if err != nil {
		return nil, errors.New("Bcrypt error")
	}
	user := User{
		Username: username,
		Password: string(passwordHash),
	}
	if err := s.db.Create(&user).Error; err != nil {
		return nil, errors.New("Error saving user to database")
	}
	return &user, nil
}
Exemple #3
0
// CreateClient saves a new client to database
func (s *Service) CreateClient(clientID, secret, redirectURI string) (*Client, error) {
	secretHash, err := password.HashPassword(secret)
	if err != nil {
		return nil, err
	}
	client := &Client{
		Key:         clientID,
		Secret:      string(secretHash),
		RedirectURI: util.StringOrNull(redirectURI),
	}
	if err := s.db.Create(client).Error; err != nil {
		return nil, err
	}
	return client, nil
}
// CreateClient saves a new client to database
func (s *Service) CreateClient(clientID, secret, redirectURI string) (*Client, error) {
	secretHash, err := password.HashPassword(secret)
	if err != nil {
		return nil, errors.New("Bcrypt error")
	}
	client := &Client{
		ClientID:    clientID,
		Secret:      string(secretHash),
		RedirectURI: util.StringOrNull(redirectURI),
	}
	if err := s.db.Create(client).Error; err != nil {
		return nil, errors.New("Error saving client to database")
	}
	return client, nil
}
Exemple #5
0
func createUser(db *gorm.DB, username, password string) (*User, error) {
	// Start with a user without a password
	user := User{
		Username: username,
		Password: util.StringOrNull(""),
	}

	// If the password is being set already, create a bcrypt hash
	if password != "" {
		passwordHash, err := pass.HashPassword(password)
		if err != nil {
			return nil, err
		}
		user.Password = util.StringOrNull(string(passwordHash))
	}

	// Create the user
	if err := db.Create(&user).Error; err != nil {
		return nil, err
	}
	return &user, nil
}
Exemple #6
0
// SetPassword saves a new user to database
func (s *Service) SetPassword(user *User, password string) error {
	// Cannot set password to empty
	if password == "" {
		return errCannotSetEmptyUserPassword
	}

	// Create a bcrypt hash
	passwordHash, err := pass.HashPassword(password)
	if err != nil {
		return err
	}

	// Set the password on the user object
	if err := s.db.Model(user).UpdateColumn(
		"password",
		string(passwordHash),
	).Error; err != nil {
		return err
	}

	return nil
}
Exemple #7
0
// SetPassword saves a new user to database
func (s *Service) SetPassword(user *User, password string) error {
	// Cannot set password to empty
	if password == "" {
		return ErrCannotSetEmptyUserPassword
	}

	// Create a bcrypt hash
	passwordHash, err := pass.HashPassword(password)
	if err != nil {
		return err
	}

	// Set the password on the user object
	if err := s.db.Model(user).UpdateColumns(User{
		Password: util.StringOrNull(string(passwordHash)),
		Model:    gorm.Model{UpdatedAt: time.Now()},
	}).Error; err != nil {
		return err
	}

	return nil
}