feat: check passwordpolicy on login (#477)

* fix: password complexity policy

* feat: check password policy

* feat: check password policy

* fix: password policy on password change

* fix: remove double policy check

* feat: check pw policy on register

* feat: check pw policy on init

* fix: hover on secondary buttons

* fix: use data set instead of hidden inputs

* fix: disabled button

* fix: en login

* fix: read policy

* feat: check if org exists

* multiple checks

* feat: validate all forms

* fix: check all forms

* fix: remove unused err

Co-authored-by: Livio Amstutz <[email protected]>
This commit is contained in:
Fabi
2020-07-16 14:26:08 +02:00
committed by GitHub
co-authored by Livio Amstutz
parent c34f6b1074
commit 2a3ecc0c6a
36 changed files with 3262 additions and 2662 deletions
@@ -12,8 +12,9 @@ const (
)
type changePasswordData struct {
OldPassword string `schema:"old_password"`
NewPassword string `schema:"new_password"`
OldPassword string `schema:"change-old-password"`
NewPassword string `schema:"change-new-password"`
NewPasswordConfirmation string `schema:"change-password-confirmation"`
}
func (l *Login) handleChangePassword(w http.ResponseWriter, r *http.Request) {
@@ -23,6 +24,7 @@ func (l *Login) handleChangePassword(w http.ResponseWriter, r *http.Request) {
l.renderError(w, r, authReq, err)
return
}
err = l.authRepo.ChangePassword(setContext(r.Context(), authReq.UserOrgID), authReq.UserID, data.OldPassword, data.NewPassword)
if err != nil {
l.renderChangePassword(w, r, authReq, err)
@@ -36,10 +38,27 @@ func (l *Login) renderChangePassword(w http.ResponseWriter, r *http.Request, aut
if err != nil {
errMessage = l.getErrorMessage(r, err)
}
data := userData{
data := passwordData{
baseData: l.getBaseData(r, authReq, "Change Password", errType, errMessage),
LoginName: authReq.LoginName,
}
policy, description, _ := l.getPasswordComplexityPolicy(r, authReq.UserOrgID)
if policy != nil {
data.PasswordPolicyDescription = description
data.MinLength = policy.MinLength
if policy.HasUppercase {
data.HasUppercase = UpperCaseRegex
}
if policy.HasLowercase {
data.HasLowercase = LowerCaseRegex
}
if policy.HasSymbol {
data.HasSymbol = SymbolRegex
}
if policy.HasNumber {
data.HasNumber = NumberRegex
}
}
l.renderer.RenderTemplate(w, r, l.renderer.Templates[tmplChangePassword], data, nil)
}
@@ -25,8 +25,14 @@ type initPasswordFormData struct {
type initPasswordData struct {
baseData
Code string
UserID string
Code string
UserID string
PasswordPolicyDescription string
MinLength uint64
HasUppercase string
HasLowercase string
HasNumber string
HasSymbol string
}
func (l *Login) handleInitPassword(w http.ResponseWriter, r *http.Request) {
@@ -85,11 +91,29 @@ func (l *Login) renderInitPassword(w http.ResponseWriter, r *http.Request, authR
if userID == "" && authReq != nil {
userID = authReq.UserID
}
data := initPasswordData{
baseData: l.getBaseData(r, authReq, "Init Password", errType, errMessage),
UserID: userID,
Code: code,
}
policy, description, _ := l.getPasswordComplexityPolicyByUserID(r, userID)
if policy != nil {
data.PasswordPolicyDescription = description
data.MinLength = policy.MinLength
if policy.HasUppercase {
data.HasUppercase = UpperCaseRegex
}
if policy.HasLowercase {
data.HasLowercase = LowerCaseRegex
}
if policy.HasSymbol {
data.HasSymbol = SymbolRegex
}
if policy.HasNumber {
data.HasNumber = NumberRegex
}
}
l.renderer.RenderTemplate(w, r, l.renderer.Templates[tmplInitPassword], data, nil)
}
+26 -3
View File
@@ -28,9 +28,15 @@ type initUserFormData struct {
type initUserData struct {
baseData
Code string
UserID string
PasswordSet bool
Code string
UserID string
PasswordSet bool
PasswordPolicyDescription string
MinLength uint64
HasUppercase string
HasLowercase string
HasNumber string
HasSymbol string
}
func (l *Login) handleInitUser(w http.ResponseWriter, r *http.Request) {
@@ -96,6 +102,23 @@ func (l *Login) renderInitUser(w http.ResponseWriter, r *http.Request, authReq *
Code: code,
PasswordSet: passwordSet,
}
policy, description, _ := l.getPasswordComplexityPolicyByUserID(r, userID)
if policy != nil {
data.PasswordPolicyDescription = description
data.MinLength = policy.MinLength
if policy.HasUppercase {
data.HasUppercase = UpperCaseRegex
}
if policy.HasLowercase {
data.HasLowercase = LowerCaseRegex
}
if policy.HasSymbol {
data.HasSymbol = SymbolRegex
}
if policy.HasNumber {
data.HasNumber = NumberRegex
}
}
l.renderer.RenderTemplate(w, r, l.renderer.Templates[tmplInitUser], data, nil)
}
@@ -0,0 +1,98 @@
package handler
import (
"github.com/caos/zitadel/internal/auth_request/model"
"github.com/caos/zitadel/internal/errors"
policy_model "github.com/caos/zitadel/internal/policy/model"
"net/http"
"regexp"
"strconv"
)
const (
LowerCaseRegex = `[a-z]`
UpperCaseRegex = `[A-Z]`
NumberRegex = `[0-9]`
SymbolRegex = `[^A-Za-z0-9]`
)
var (
hasStringLowerCase = regexp.MustCompile(LowerCaseRegex).MatchString
hasStringUpperCase = regexp.MustCompile(UpperCaseRegex).MatchString
hasNumber = regexp.MustCompile(NumberRegex).MatchString
hasSymbol = regexp.MustCompile(SymbolRegex).MatchString
)
func (l *Login) getPasswordComplexityPolicy(r *http.Request, orgID string) (*policy_model.PasswordComplexityPolicy, string, error) {
policy, err := l.authRepo.GetMyPasswordComplexityPolicy(setContext(r.Context(), orgID))
if err != nil {
return nil, err.Error(), err
}
description, err := l.generatePolicyDescription(r, policy)
return policy, description, nil
}
func (l *Login) getPasswordComplexityPolicyByUserID(r *http.Request, userID string) (*policy_model.PasswordComplexityPolicy, string, error) {
user, err := l.authRepo.UserByID(r.Context(), userID)
if err != nil {
return nil, "", nil
}
policy, err := l.authRepo.GetMyPasswordComplexityPolicy(setContext(r.Context(), user.ResourceOwner))
if err != nil {
return nil, err.Error(), err
}
description, err := l.generatePolicyDescription(r, policy)
return policy, description, nil
}
func (l *Login) generatePolicyDescription(r *http.Request, policy *policy_model.PasswordComplexityPolicy) (string, error) {
description := "<ul id=\"passwordcomplexity\">"
minLength := l.renderer.LocalizeFromRequest(r, "Password.MinLength", nil)
description += "<li id=\"minlength\" class=\"invalid\"><i class=\"material-icons\">clear</i>" + minLength + " " + strconv.Itoa(int(policy.MinLength)) + "</li>"
if policy.HasUppercase {
uppercase := l.renderer.LocalizeFromRequest(r, "Password.HasUppercase", nil)
description += "<li id=\"uppercase\" class=\"invalid\"><i class=\"material-icons\">clear</i>" + uppercase + "</li>"
}
if policy.HasLowercase {
lowercase := l.renderer.LocalizeFromRequest(r, "Password.HasLowercase", nil)
description += "<li id=\"lowercase\" class=\"invalid\"><i class=\"material-icons\">clear</i>" + lowercase + "</li>"
}
if policy.HasNumber {
hasnumber := l.renderer.LocalizeFromRequest(r, "Password.HasNumber", nil)
description += "<li id=\"number\" class=\"invalid\"><i class=\"material-icons\">clear</i>" + hasnumber + "</li>"
}
if policy.HasSymbol {
hassymbol := l.renderer.LocalizeFromRequest(r, "Password.HasSymbol", nil)
description += "<li id=\"symbol\" class=\"invalid\"><i class=\"material-icons\">clear</i>" + hassymbol + "</li>"
}
description += "</ul>"
return description, nil
}
func (l *Login) checkPasswordComplexityPolicy(password string, r *http.Request, authReq *model.AuthRequest) error {
policy, err := l.authRepo.GetMyPasswordComplexityPolicy(setContext(r.Context(), authReq.UserOrgID))
if err != nil {
return nil
}
if policy.MinLength != 0 && uint64(len(password)) < policy.MinLength {
return errors.ThrowInvalidArgument(nil, "POLICY-LSo0p", "Errors.User.PasswordComplexityPolicy.MinLength")
}
if policy.HasLowercase && !hasStringLowerCase(password) {
return errors.ThrowInvalidArgument(nil, "POLICY-4Sjsf", "Errors.User.PasswordComplexityPolicy.HasLower")
}
if policy.HasUppercase && !hasStringUpperCase(password) {
return errors.ThrowInvalidArgument(nil, "POLICY-6Sjc9", "Errors.User.PasswordComplexityPolicy.HasUpper")
}
if policy.HasNumber && !hasNumber(password) {
return errors.ThrowInvalidArgument(nil, "POLICY-2Fksi", "Errors.User.PasswordComplexityPolicy.HasNumber")
}
if policy.HasSymbol && !hasSymbol(password) {
return errors.ThrowInvalidArgument(nil, "POLICY-0Js6e", "Errors.User.PasswordComplexityPolicy.HasSymbol")
}
return nil
}
@@ -10,7 +10,7 @@ const (
tmplPassword = "password"
)
type passwordData struct {
type passwordFormData struct {
Password string `schema:"password"`
}
@@ -27,7 +27,7 @@ func (l *Login) renderPassword(w http.ResponseWriter, r *http.Request, authReq *
}
func (l *Login) handlePasswordCheck(w http.ResponseWriter, r *http.Request) {
data := new(passwordData)
data := new(passwordFormData)
authReq, err := l.getAuthRequestAndParseData(r, data)
if err != nil {
l.renderError(w, r, authReq, err)
+30 -2
View File
@@ -23,13 +23,19 @@ type registerFormData struct {
Lastname string `schema:"lastname"`
Language string `schema:"language"`
Gender int32 `schema:"gender"`
Password string `schema:"password"`
Password2 string `schema:"password2"`
Password string `schema:"register-password"`
Password2 string `schema:"register-password-confirmation"`
}
type registerData struct {
baseData
registerFormData
PasswordPolicyDescription string
MinLength uint64
HasUppercase string
HasLowercase string
HasNumber string
HasSymbol string
}
func (l *Login) handleRegister(w http.ResponseWriter, r *http.Request) {
@@ -88,10 +94,32 @@ func (l *Login) renderRegister(w http.ResponseWriter, r *http.Request, authReque
if formData.Language == "" {
formData.Language = l.renderer.Lang(r).String()
}
data := registerData{
baseData: l.getBaseData(r, authRequest, "Register", errType, errMessage),
registerFormData: *formData,
}
iam, _ := l.authRepo.GetIam(r.Context())
if iam != nil {
policy, description, _ := l.getPasswordComplexityPolicy(r, iam.GlobalOrgID)
if policy != nil {
data.PasswordPolicyDescription = description
data.MinLength = policy.MinLength
if policy.HasUppercase {
data.HasUppercase = UpperCaseRegex
}
if policy.HasLowercase {
data.HasLowercase = LowerCaseRegex
}
if policy.HasSymbol {
data.HasSymbol = SymbolRegex
}
if policy.HasNumber {
data.HasNumber = NumberRegex
}
}
}
funcs := map[string]interface{}{
"selectedLanguage": func(l string) bool {
if formData == nil {
+12 -2
View File
@@ -3,12 +3,11 @@ package handler
import (
"errors"
"fmt"
"github.com/gorilla/csrf"
"html/template"
"net/http"
"path"
"github.com/gorilla/csrf"
"github.com/caos/zitadel/internal/api/http/middleware"
"github.com/caos/zitadel/internal/auth_request/model"
caos_errs "github.com/caos/zitadel/internal/errors"
@@ -264,6 +263,17 @@ type userData struct {
SelectedMfaProvider model.MfaType
}
type passwordData struct {
baseData
LoginName string
PasswordPolicyDescription string
MinLength uint64
HasUppercase string
HasLowercase string
HasNumber string
HasSymbol string
}
type userSelectionData struct {
baseData
Users []model.UserSelection