2016-09-26 18:48:55 +00:00
|
|
|
package service
|
2016-09-01 04:51:38 +00:00
|
|
|
|
|
|
|
|
import (
|
2017-03-15 15:55:30 +00:00
|
|
|
"context"
|
2017-01-15 23:23:09 +00:00
|
|
|
"errors"
|
2016-09-15 19:27:55 +00:00
|
|
|
"strings"
|
2017-01-15 23:23:09 +00:00
|
|
|
"unicode"
|
2016-09-15 19:27:55 +00:00
|
|
|
|
2017-06-22 19:50:45 +00:00
|
|
|
"github.com/kolide/fleet/server/contexts/viewer"
|
|
|
|
|
"github.com/kolide/fleet/server/kolide"
|
2016-09-01 04:51:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func (mw validationMiddleware) NewUser(ctx context.Context, p kolide.UserPayload) (*kolide.User, error) {
|
2016-09-29 02:44:05 +00:00
|
|
|
invalid := &invalidArgumentError{}
|
2016-09-01 04:51:38 +00:00
|
|
|
if p.Username == nil {
|
2016-09-29 02:44:05 +00:00
|
|
|
invalid.Append("username", "missing required argument")
|
2016-12-15 17:28:53 +00:00
|
|
|
} else {
|
|
|
|
|
if *p.Username == "" {
|
|
|
|
|
invalid.Append("username", "cannot be empty")
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-15 19:27:55 +00:00
|
|
|
if strings.Contains(*p.Username, "@") {
|
2016-09-29 02:44:05 +00:00
|
|
|
invalid.Append("username", "'@' character not allowed in usernames")
|
2016-09-15 19:27:55 +00:00
|
|
|
}
|
|
|
|
|
}
|
2016-12-15 17:28:53 +00:00
|
|
|
|
2017-05-10 16:26:05 +00:00
|
|
|
// we don't need a password for single sign on
|
2017-12-04 14:43:43 +00:00
|
|
|
if p.SSOInvite == nil || !*p.SSOInvite {
|
2017-05-10 16:26:05 +00:00
|
|
|
if p.Password == nil {
|
|
|
|
|
invalid.Append("password", "missing required argument")
|
|
|
|
|
} else {
|
|
|
|
|
if *p.Password == "" {
|
|
|
|
|
invalid.Append("password", "cannot be empty")
|
|
|
|
|
}
|
|
|
|
|
if err := validatePasswordRequirements(*p.Password); err != nil {
|
|
|
|
|
invalid.Append("password", err.Error())
|
|
|
|
|
}
|
2017-01-15 23:23:09 +00:00
|
|
|
}
|
2016-09-01 04:51:38 +00:00
|
|
|
}
|
2016-12-15 17:28:53 +00:00
|
|
|
|
2016-09-01 04:51:38 +00:00
|
|
|
if p.Email == nil {
|
2016-09-29 02:44:05 +00:00
|
|
|
invalid.Append("email", "missing required argument")
|
2016-12-15 17:28:53 +00:00
|
|
|
} else {
|
|
|
|
|
if *p.Email == "" {
|
|
|
|
|
invalid.Append("email", "cannot be empty")
|
|
|
|
|
}
|
2016-09-16 15:23:48 +00:00
|
|
|
}
|
2016-12-15 17:28:53 +00:00
|
|
|
|
2016-09-29 02:44:05 +00:00
|
|
|
if p.InviteToken == nil {
|
|
|
|
|
invalid.Append("invite_token", "missing required argument")
|
2016-12-15 17:28:53 +00:00
|
|
|
} else {
|
|
|
|
|
if *p.InviteToken == "" {
|
|
|
|
|
invalid.Append("invite_token", "cannot be empty")
|
|
|
|
|
}
|
2016-09-29 02:44:05 +00:00
|
|
|
}
|
2016-12-15 17:28:53 +00:00
|
|
|
|
2016-09-29 02:44:05 +00:00
|
|
|
if invalid.HasErrors() {
|
|
|
|
|
return nil, invalid
|
2016-09-01 04:51:38 +00:00
|
|
|
}
|
|
|
|
|
return mw.Service.NewUser(ctx, p)
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-15 17:28:53 +00:00
|
|
|
func (mw validationMiddleware) ModifyUser(ctx context.Context, userID uint, p kolide.UserPayload) (*kolide.User, error) {
|
|
|
|
|
invalid := &invalidArgumentError{}
|
|
|
|
|
if p.Username != nil {
|
|
|
|
|
if *p.Username == "" {
|
|
|
|
|
invalid.Append("username", "cannot be empty")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if strings.Contains(*p.Username, "@") {
|
|
|
|
|
invalid.Append("username", "'@' character not allowed in usernames")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if p.Name != nil {
|
|
|
|
|
if *p.Name == "" {
|
|
|
|
|
invalid.Append("name", "cannot be empty")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if p.Email != nil {
|
|
|
|
|
if *p.Email == "" {
|
|
|
|
|
invalid.Append("email", "cannot be empty")
|
|
|
|
|
}
|
2017-01-27 13:35:58 +00:00
|
|
|
// if the user is not an admin, or if an admin is changing their own email
|
|
|
|
|
// address a password is required,
|
|
|
|
|
if passwordRequiredForEmailChange(ctx, userID, invalid) {
|
|
|
|
|
if p.Password == nil {
|
|
|
|
|
invalid.Append("password", "cannot be empty if email is changed")
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-12-15 17:28:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if invalid.HasErrors() {
|
|
|
|
|
return nil, invalid
|
|
|
|
|
}
|
|
|
|
|
return mw.Service.ModifyUser(ctx, userID, p)
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-27 13:35:58 +00:00
|
|
|
func passwordRequiredForEmailChange(ctx context.Context, uid uint, invalid *invalidArgumentError) bool {
|
|
|
|
|
vc, ok := viewer.FromContext(ctx)
|
|
|
|
|
if !ok {
|
|
|
|
|
invalid.Append("viewer", "not present")
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
// if a user is changing own email need a password no matter what
|
|
|
|
|
if vc.UserID() == uid {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
// if an admin is changing another users email no password needed
|
2018-09-13 20:59:53 +00:00
|
|
|
if vc.CanPerformAdminActions() {
|
2017-01-27 13:35:58 +00:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
// should never get here because a non admin can't change the email of another
|
|
|
|
|
// user
|
|
|
|
|
invalid.Append("auth", "this user can't change another user's email")
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-14 18:11:43 +00:00
|
|
|
func (mw validationMiddleware) ChangePassword(ctx context.Context, oldPass, newPass string) error {
|
|
|
|
|
invalid := &invalidArgumentError{}
|
|
|
|
|
if oldPass == "" {
|
2016-12-15 17:28:53 +00:00
|
|
|
invalid.Append("old_password", "cannot be empty")
|
2016-12-14 18:11:43 +00:00
|
|
|
}
|
|
|
|
|
if newPass == "" {
|
2016-12-15 17:28:53 +00:00
|
|
|
invalid.Append("new_password", "cannot be empty")
|
2016-12-14 18:11:43 +00:00
|
|
|
}
|
2017-01-15 23:23:09 +00:00
|
|
|
|
|
|
|
|
if err := validatePasswordRequirements(newPass); err != nil {
|
|
|
|
|
invalid.Append("new_password", err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-14 18:11:43 +00:00
|
|
|
if invalid.HasErrors() {
|
|
|
|
|
return invalid
|
|
|
|
|
}
|
|
|
|
|
return mw.Service.ChangePassword(ctx, oldPass, newPass)
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-15 14:52:17 +00:00
|
|
|
func (mw validationMiddleware) ResetPassword(ctx context.Context, token, password string) error {
|
2016-09-29 02:44:05 +00:00
|
|
|
invalid := &invalidArgumentError{}
|
2016-09-15 14:52:17 +00:00
|
|
|
if token == "" {
|
2016-09-29 02:44:05 +00:00
|
|
|
invalid.Append("token", "cannot be empty field")
|
2016-09-01 04:51:38 +00:00
|
|
|
}
|
2016-09-15 14:52:17 +00:00
|
|
|
if password == "" {
|
2016-09-29 02:44:05 +00:00
|
|
|
invalid.Append("new_password", "cannot be empty field")
|
2016-09-16 15:23:48 +00:00
|
|
|
}
|
2017-01-15 23:23:09 +00:00
|
|
|
if err := validatePasswordRequirements(password); err != nil {
|
|
|
|
|
invalid.Append("new_password", err.Error())
|
|
|
|
|
}
|
2016-09-29 02:44:05 +00:00
|
|
|
if invalid.HasErrors() {
|
|
|
|
|
return invalid
|
2016-09-01 04:51:38 +00:00
|
|
|
}
|
2016-09-15 14:52:17 +00:00
|
|
|
return mw.Service.ResetPassword(ctx, token, password)
|
2016-09-01 04:51:38 +00:00
|
|
|
}
|
2016-09-16 15:23:48 +00:00
|
|
|
|
2017-01-15 23:23:09 +00:00
|
|
|
// Requirements for user password:
|
|
|
|
|
// at least 7 character length
|
|
|
|
|
// at least 1 symbol
|
|
|
|
|
// at least 1 number
|
|
|
|
|
func validatePasswordRequirements(password string) error {
|
|
|
|
|
var (
|
|
|
|
|
number bool
|
|
|
|
|
symbol bool
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
for _, s := range password {
|
|
|
|
|
switch {
|
|
|
|
|
case unicode.IsNumber(s):
|
|
|
|
|
number = true
|
|
|
|
|
case unicode.IsPunct(s) || unicode.IsSymbol(s):
|
|
|
|
|
symbol = true
|
|
|
|
|
}
|
2016-09-23 02:41:58 +00:00
|
|
|
}
|
2016-09-16 15:23:48 +00:00
|
|
|
|
2017-01-15 23:23:09 +00:00
|
|
|
if len(password) >= 7 &&
|
|
|
|
|
number &&
|
|
|
|
|
symbol {
|
|
|
|
|
return nil
|
2016-09-16 15:23:48 +00:00
|
|
|
}
|
2017-01-15 23:23:09 +00:00
|
|
|
|
|
|
|
|
return errors.New("password does not meet validation requirements")
|
2016-09-16 15:23:48 +00:00
|
|
|
}
|