Skip to content

Commit

Permalink
fix(api): clean spaces in username migration
Browse files Browse the repository at this point in the history
  • Loading branch information
richardlt authored and bnjjj committed Jan 14, 2020
1 parent bd70519 commit 7dd9c5f
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 6 deletions.
4 changes: 4 additions & 0 deletions engine/api/authentication/local/dao_registration.go
Expand Up @@ -42,6 +42,10 @@ func LoadRegistrationByID(ctx context.Context, db gorp.SqlExecutor, id string) (

// InsertRegistration in database.
func InsertRegistration(ctx context.Context, db gorp.SqlExecutor, ur *sdk.UserRegistration) error {
if !sdk.UsernameRegex.MatchString(ur.Username) {
return sdk.WithStack(sdk.ErrInvalidUsername)
}

if ur.ID == "" {
ur.ID = sdk.UUID()
}
Expand Down
2 changes: 1 addition & 1 deletion engine/api/authentication/local/driver.go
Expand Up @@ -49,7 +49,7 @@ func (d AuthDriver) CheckSignupRequest(req sdk.AuthConsumerSigninRequest) error
return sdk.NewErrorFrom(sdk.ErrWrongRequest, "missing fullname for local signup")
}
if username, ok := req["username"]; !ok || username == "" {
return sdk.NewErrorFrom(sdk.ErrWrongRequest, "missing username for local signup")
return sdk.NewErrorFrom(sdk.ErrWrongRequest, "missing or invalid username for local signup")
}
if email, ok := req["email"]; !ok || !sdk.IsValidEmail(email) || !d.isAllowedDomain(email) {
return sdk.NewErrorFrom(sdk.ErrWrongRequest, "missing or invalid email for local signup")
Expand Down
3 changes: 2 additions & 1 deletion engine/api/migrate/refactor_authentication_user.go
Expand Up @@ -2,6 +2,7 @@ package migrate

import (
"context"
"strings"

"github.com/ovh/cds/engine/api/cache"
"github.com/ovh/cds/engine/api/database/gorpmapping"
Expand Down Expand Up @@ -67,7 +68,7 @@ func refactorAuthenticationUser(ctx context.Context, db *gorp.DbMap, store cache
log.Info(ctx, "migrate.RefactorAuthenticationUser> starting user migration %s - %s", u.Username, u.Fullname)

var newUser = sdk.AuthentifiedUser{
Username: u.Username,
Username: strings.Trim(u.Username, " "), // fix existing account that starts or ends with spaces
Fullname: u.Fullname,
OldUserStruct: &u,
}
Expand Down
5 changes: 1 addition & 4 deletions engine/api/user/dao.go
Expand Up @@ -2,7 +2,6 @@ package user

import (
"context"
"regexp"
"time"

"github.com/go-gorp/gorp"
Expand Down Expand Up @@ -127,11 +126,9 @@ func CountAdmin(db gorp.SqlExecutor) (int64, error) {
return count, nil
}

var usernameRegex = regexp.MustCompile("[a-z0-9._-]{3,32}")

// Insert a user in database.
func Insert(ctx context.Context, db gorp.SqlExecutor, au *sdk.AuthentifiedUser) error {
if !usernameRegex.MatchString(au.Username) {
if !sdk.UsernameRegex.MatchString(au.Username) {
return sdk.WithStack(sdk.ErrInvalidUsername)
}

Expand Down
2 changes: 2 additions & 0 deletions sdk/user.go
Expand Up @@ -74,6 +74,8 @@ type UserRegistration struct {
Hash string `json:"-" db:"hash"` // do no return hash in json
}

var UsernameRegex = regexp.MustCompile("[a-z0-9._-]{3,32}")

// AuthentifiedUser struct contains all information about a cds user.
type AuthentifiedUser struct {
ID string `json:"id" yaml:"id" cli:"id" db:"id"`
Expand Down

0 comments on commit 7dd9c5f

Please sign in to comment.