normalize login email (#992)

transform email to lower case before user creating, updating, authorization
This commit is contained in:
Yaroslav Vorobiov 2019-01-08 15:54:12 +02:00 committed by GitHub
parent 58b16c2ada
commit 425ac45d89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 4 deletions

View File

@ -25,7 +25,6 @@ var (
mon = monkit.Package()
)
// maxLimit specifies the limit for all paged queries
const (
// maxLimit specifies the limit for all paged queries
maxLimit = 50
@ -63,13 +62,17 @@ func (s *Service) CreateUser(ctx context.Context, user CreateUser) (u *User, err
return nil, err
}
//TODO: store original email input in the db,
// add normalization
email := normalizeEmail(user.Email)
hash, err := bcrypt.GenerateFromPassword([]byte(user.Password), bcrypt.DefaultCost)
if err != nil {
return nil, err
}
//passwordHash := sha256.Sum256()
return s.store.Users().Insert(ctx, &User{
Email: user.Email,
Email: email,
FirstName: user.FirstName,
LastName: user.LastName,
PasswordHash: hash,
@ -79,6 +82,9 @@ func (s *Service) CreateUser(ctx context.Context, user CreateUser) (u *User, err
// Token authenticates User by credentials and returns auth token
func (s *Service) Token(ctx context.Context, email, password string) (token string, err error) {
defer mon.Task()(&ctx)(&err)
email = normalizeEmail(email)
user, err := s.store.Users().GetByEmail(ctx, email)
if err != nil {
return "", err
@ -127,11 +133,15 @@ func (s *Service) UpdateAccount(ctx context.Context, info UserInfo) (err error)
return err
}
//TODO: store original email input in the db,
// add normalization
email := normalizeEmail(info.Email)
return s.store.Users().Update(ctx, &User{
ID: auth.User.ID,
FirstName: info.FirstName,
LastName: info.LastName,
Email: info.Email,
Email: email,
PasswordHash: nil,
})
}

View File

@ -4,6 +4,7 @@
package satellite
import (
"strings"
"unicode"
"github.com/zeebo/errs"
@ -80,3 +81,9 @@ func validatePassword(pass string) error {
return errs.Combine()
}
// normalizeEmail converts emails with different casing into equal strings
// Note: won't work with µıſͅςϐϑϕϖϰϱϵᲀᲁᲂᲃᲄᲅᲆᲇᲈẛι
func normalizeEmail(s string) string {
return strings.ToLower(s)
}