From 683f7133190aa350cdd1ea2608c90fe5f47b35cd Mon Sep 17 00:00:00 2001 From: David Lu Date: Wed, 6 Jul 2016 18:54:54 -0400 Subject: PLT-1465 Added password requirements (#3489) * Added password requirements * added tweaks * fixed error code * removed http.StatusNotAcceptable --- utils/password.go | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 utils/password.go (limited to 'utils/password.go') diff --git a/utils/password.go b/utils/password.go new file mode 100644 index 000000000..dc1d771b8 --- /dev/null +++ b/utils/password.go @@ -0,0 +1,64 @@ +// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package utils + +import ( + "github.com/mattermost/platform/model" + "strings" +) + +func IsPasswordValid(password string) *model.AppError { + id := "model.user.is_valid.pwd" + isError := false + min := model.PASSWORD_MINIMUM_LENGTH + + if IsLicensed && *License.Features.PasswordRequirements { + if len(password) < *Cfg.PasswordSettings.MinimumLength || len(password) > model.PASSWORD_MAXIMUM_LENGTH { + isError = true + } + + if *Cfg.PasswordSettings.Lowercase { + if !strings.ContainsAny(password, model.LOWERCASE_LETTERS) { + isError = true + } + + id = id + "_lowercase" + } + + if *Cfg.PasswordSettings.Uppercase { + if !strings.ContainsAny(password, model.UPPERCASE_LETTERS) { + isError = true + } + + id = id + "_uppercase" + } + + if *Cfg.PasswordSettings.Number { + if !strings.ContainsAny(password, model.NUMBERS) { + isError = true + } + + id = id + "_number" + } + + if *Cfg.PasswordSettings.Symbol { + if !strings.ContainsAny(password, model.SYMBOLS) { + isError = true + } + + id = id + "_symbol" + } + + min = *Cfg.PasswordSettings.MinimumLength + } else if len(password) > model.PASSWORD_MAXIMUM_LENGTH || len(password) < model.PASSWORD_MINIMUM_LENGTH { + isError = true + min = model.PASSWORD_MINIMUM_LENGTH + } + + if isError { + return model.NewLocAppError("User.IsValid", id+".app_error", map[string]interface{}{"Min": min}, "") + } + + return nil +} -- cgit v1.2.3-1-g7c22