func (s *Service) createUserCommon(db *gorm.DB, roleID, username, password string) (*models.OauthUser, error) { // Start with a user without a password user := &models.OauthUser{ RoleID: util.StringOrNull(roleID), Username: strings.ToLower(username), Password: util.StringOrNull(""), } // If the password is being set already, create a bcrypt hash if password != "" { if len(password) < MinPasswordLength { return nil, ErrPasswordTooShort } 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 }
func (s *Service) setPasswordCommon(db *gorm.DB, user *models.OauthUser, password string) error { if len(password) < MinPasswordLength { return ErrPasswordTooShort } // Create a bcrypt hash passwordHash, err := pass.HashPassword(password) if err != nil { return err } // Set the password on the user object return db.Model(user).UpdateColumns(models.OauthUser{ Password: util.StringOrNull(string(passwordHash)), Model: gorm.Model{UpdatedAt: time.Now().UTC()}, }).Error }
func (s *Service) createClientCommon(db *gorm.DB, clientID, secret, redirectURI string) (*models.OauthClient, error) { // Check client ID if s.ClientExists(clientID) { return nil, ErrClientIDTaken } // Hash password secretHash, err := password.HashPassword(secret) if err != nil { return nil, err } client := &models.OauthClient{ Key: strings.ToLower(clientID), Secret: string(secretHash), RedirectURI: util.StringOrNull(redirectURI), } if err := db.Create(client).Error; err != nil { return nil, err } return client, nil }