2017-04-12 08:27:57 -04:00
|
|
|
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
|
2016-07-06 18:54:54 -04:00
|
|
|
// See License.txt for license information.
|
|
|
|
|
|
|
|
|
|
package utils
|
|
|
|
|
|
|
|
|
|
import (
|
2017-01-30 08:30:02 -05:00
|
|
|
"net/http"
|
2016-07-06 18:54:54 -04:00
|
|
|
"strings"
|
2017-01-30 08:30:02 -05:00
|
|
|
|
2017-09-06 23:05:10 -07:00
|
|
|
"github.com/mattermost/mattermost-server/model"
|
2016-07-06 18:54:54 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func IsPasswordValid(password string) *model.AppError {
|
2017-11-20 11:04:04 -06:00
|
|
|
if len(password) > model.PASSWORD_MAXIMUM_LENGTH || len(password) < model.PASSWORD_MINIMUM_LENGTH {
|
|
|
|
|
return model.NewAppError("User.IsValid", "model.user.is_valid.pwd.app_error", map[string]interface{}{"Min": model.PASSWORD_MINIMUM_LENGTH}, "", http.StatusBadRequest)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func IsPasswordValidWithSettings(password string, settings *model.PasswordSettings) *model.AppError {
|
2016-07-06 18:54:54 -04:00
|
|
|
id := "model.user.is_valid.pwd"
|
|
|
|
|
isError := false
|
|
|
|
|
|
2017-11-20 11:04:04 -06:00
|
|
|
if len(password) < *settings.MinimumLength || len(password) > model.PASSWORD_MAXIMUM_LENGTH {
|
|
|
|
|
isError = true
|
|
|
|
|
}
|
2016-07-06 18:54:54 -04:00
|
|
|
|
2017-11-20 11:04:04 -06:00
|
|
|
if *settings.Lowercase {
|
|
|
|
|
if !strings.ContainsAny(password, model.LOWERCASE_LETTERS) {
|
|
|
|
|
isError = true
|
2016-07-06 18:54:54 -04:00
|
|
|
}
|
|
|
|
|
|
2017-11-20 11:04:04 -06:00
|
|
|
id = id + "_lowercase"
|
|
|
|
|
}
|
2016-07-06 18:54:54 -04:00
|
|
|
|
2017-11-20 11:04:04 -06:00
|
|
|
if *settings.Uppercase {
|
|
|
|
|
if !strings.ContainsAny(password, model.UPPERCASE_LETTERS) {
|
|
|
|
|
isError = true
|
2016-07-06 18:54:54 -04:00
|
|
|
}
|
|
|
|
|
|
2017-11-20 11:04:04 -06:00
|
|
|
id = id + "_uppercase"
|
|
|
|
|
}
|
2016-07-06 18:54:54 -04:00
|
|
|
|
2017-11-20 11:04:04 -06:00
|
|
|
if *settings.Number {
|
|
|
|
|
if !strings.ContainsAny(password, model.NUMBERS) {
|
|
|
|
|
isError = true
|
2016-07-06 18:54:54 -04:00
|
|
|
}
|
|
|
|
|
|
2017-11-20 11:04:04 -06:00
|
|
|
id = id + "_number"
|
|
|
|
|
}
|
2016-07-06 18:54:54 -04:00
|
|
|
|
2017-11-20 11:04:04 -06:00
|
|
|
if *settings.Symbol {
|
|
|
|
|
if !strings.ContainsAny(password, model.SYMBOLS) {
|
|
|
|
|
isError = true
|
2016-07-06 18:54:54 -04:00
|
|
|
}
|
|
|
|
|
|
2017-11-20 11:04:04 -06:00
|
|
|
id = id + "_symbol"
|
2016-07-06 18:54:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if isError {
|
2017-11-20 11:04:04 -06:00
|
|
|
return model.NewAppError("User.IsValid", id+".app_error", map[string]interface{}{"Min": *settings.MinimumLength}, "", http.StatusBadRequest)
|
2016-07-06 18:54:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|