mirror of
https://github.com/andwati/zitadel.git
synced 2026-11-25 05:58:45 +00:00
feat: Policy check (#149)
* check password complexity policy * check password complexity policy * fix tests * Update internal/admin/repository/eventsourcing/setup/setup.go Co-authored-by: Livio Amstutz <[email protected]> * changes for mr Co-authored-by: Livio Amstutz <[email protected]>
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package model
|
||||
|
||||
import "github.com/caos/zitadel/internal/eventstore/models"
|
||||
import (
|
||||
"github.com/caos/zitadel/internal/eventstore/models"
|
||||
)
|
||||
|
||||
type PasswordAgePolicy struct {
|
||||
models.ObjectRoot
|
||||
|
||||
@@ -0,0 +1,197 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCheckPasswordComplexityPolicy(t *testing.T) {
|
||||
type args struct {
|
||||
policy *PasswordComplexityPolicy
|
||||
password string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
hasError bool
|
||||
}{
|
||||
{
|
||||
name: "has minlength ok",
|
||||
args: args{
|
||||
policy: &PasswordComplexityPolicy{
|
||||
HasLowercase: false,
|
||||
HasUppercase: false,
|
||||
HasSymbol: false,
|
||||
HasNumber: false,
|
||||
MinLength: 10,
|
||||
},
|
||||
password: "password12",
|
||||
},
|
||||
hasError: false,
|
||||
},
|
||||
{
|
||||
name: "has minlength not ok",
|
||||
args: args{
|
||||
policy: &PasswordComplexityPolicy{
|
||||
HasLowercase: false,
|
||||
HasUppercase: false,
|
||||
HasSymbol: false,
|
||||
HasNumber: false,
|
||||
MinLength: 10,
|
||||
},
|
||||
password: "password",
|
||||
},
|
||||
hasError: true,
|
||||
},
|
||||
{
|
||||
name: "has lowercase ok",
|
||||
args: args{
|
||||
policy: &PasswordComplexityPolicy{
|
||||
HasLowercase: true,
|
||||
HasUppercase: false,
|
||||
HasSymbol: false,
|
||||
HasNumber: false,
|
||||
MinLength: 0,
|
||||
},
|
||||
password: "password",
|
||||
},
|
||||
hasError: false,
|
||||
},
|
||||
{
|
||||
name: "has lowercase not ok",
|
||||
args: args{
|
||||
policy: &PasswordComplexityPolicy{
|
||||
HasLowercase: true,
|
||||
HasUppercase: false,
|
||||
HasSymbol: false,
|
||||
HasNumber: false,
|
||||
MinLength: 0,
|
||||
},
|
||||
password: "PASSWORD",
|
||||
},
|
||||
hasError: true,
|
||||
},
|
||||
{
|
||||
name: "has uppercase ok",
|
||||
args: args{
|
||||
policy: &PasswordComplexityPolicy{
|
||||
HasLowercase: false,
|
||||
HasUppercase: true,
|
||||
HasSymbol: false,
|
||||
HasNumber: false,
|
||||
MinLength: 0,
|
||||
},
|
||||
password: "PASSWORD",
|
||||
},
|
||||
hasError: false,
|
||||
},
|
||||
{
|
||||
name: "has uppercase not ok",
|
||||
args: args{
|
||||
policy: &PasswordComplexityPolicy{
|
||||
HasLowercase: false,
|
||||
HasUppercase: true,
|
||||
HasSymbol: false,
|
||||
HasNumber: false,
|
||||
MinLength: 0,
|
||||
},
|
||||
password: "password",
|
||||
},
|
||||
hasError: true,
|
||||
},
|
||||
{
|
||||
name: "has symbol ok",
|
||||
args: args{
|
||||
policy: &PasswordComplexityPolicy{
|
||||
HasLowercase: false,
|
||||
HasUppercase: false,
|
||||
HasSymbol: true,
|
||||
HasNumber: false,
|
||||
MinLength: 0,
|
||||
},
|
||||
password: "!G$",
|
||||
},
|
||||
hasError: false,
|
||||
},
|
||||
{
|
||||
name: "has symbol not ok",
|
||||
args: args{
|
||||
policy: &PasswordComplexityPolicy{
|
||||
HasLowercase: false,
|
||||
HasUppercase: false,
|
||||
HasSymbol: true,
|
||||
HasNumber: false,
|
||||
MinLength: 0,
|
||||
},
|
||||
password: "PASSWORD",
|
||||
},
|
||||
hasError: true,
|
||||
},
|
||||
{
|
||||
name: "has number ok",
|
||||
args: args{
|
||||
policy: &PasswordComplexityPolicy{
|
||||
HasLowercase: false,
|
||||
HasUppercase: false,
|
||||
HasSymbol: false,
|
||||
HasNumber: true,
|
||||
MinLength: 0,
|
||||
},
|
||||
password: "123456",
|
||||
},
|
||||
hasError: false,
|
||||
},
|
||||
{
|
||||
name: "has number not ok",
|
||||
args: args{
|
||||
policy: &PasswordComplexityPolicy{
|
||||
HasLowercase: false,
|
||||
HasUppercase: false,
|
||||
HasSymbol: false,
|
||||
HasNumber: true,
|
||||
MinLength: 0,
|
||||
},
|
||||
password: "PASSWORD",
|
||||
},
|
||||
hasError: true,
|
||||
},
|
||||
{
|
||||
name: "has everything ok",
|
||||
args: args{
|
||||
policy: &PasswordComplexityPolicy{
|
||||
HasLowercase: true,
|
||||
HasUppercase: true,
|
||||
HasSymbol: true,
|
||||
HasNumber: true,
|
||||
MinLength: 10,
|
||||
},
|
||||
password: "Password1!",
|
||||
},
|
||||
hasError: false,
|
||||
},
|
||||
{
|
||||
name: "has everything not ok",
|
||||
args: args{
|
||||
policy: &PasswordComplexityPolicy{
|
||||
HasLowercase: true,
|
||||
HasUppercase: true,
|
||||
HasSymbol: true,
|
||||
HasNumber: true,
|
||||
MinLength: 10,
|
||||
},
|
||||
password: "password",
|
||||
},
|
||||
hasError: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := tt.args.policy.Check(tt.args.password)
|
||||
if !tt.hasError && err != nil {
|
||||
t.Errorf("should not get err: %v", err)
|
||||
}
|
||||
if tt.hasError && err == nil {
|
||||
t.Errorf("should have error: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,17 @@
|
||||
package model
|
||||
|
||||
import "github.com/caos/zitadel/internal/eventstore/models"
|
||||
import (
|
||||
caos_errs "github.com/caos/zitadel/internal/errors"
|
||||
"github.com/caos/zitadel/internal/eventstore/models"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
var (
|
||||
hasStringLowerCase = regexp.MustCompile(`[a-z]`).MatchString
|
||||
hasStringUpperCase = regexp.MustCompile(`[A-Z]`).MatchString
|
||||
hasNumber = regexp.MustCompile(`[0-9]`).MatchString
|
||||
hasSymbol = regexp.MustCompile(`[^A-Za-z0-9]`).MatchString
|
||||
)
|
||||
|
||||
type PasswordComplexityPolicy struct {
|
||||
models.ObjectRoot
|
||||
@@ -17,3 +28,26 @@ type PasswordComplexityPolicy struct {
|
||||
func (p *PasswordComplexityPolicy) IsValid() bool {
|
||||
return p.Description != ""
|
||||
}
|
||||
|
||||
func (p *PasswordComplexityPolicy) Check(password string) error {
|
||||
if p.MinLength != 0 && uint64(len(password)) < p.MinLength {
|
||||
return caos_errs.ThrowInvalidArgumentf(nil, "MODEL-HuJf6", "Passwordpolicy doesn't match: Minlength %v", p.MinLength)
|
||||
}
|
||||
|
||||
if p.HasLowercase && !hasStringLowerCase(password) {
|
||||
return caos_errs.ThrowInvalidArgument(nil, "MODEL-co3Xw", "Passwordpolicy doesn't match: HasLowerCase")
|
||||
}
|
||||
|
||||
if p.HasUppercase && !hasStringUpperCase(password) {
|
||||
return caos_errs.ThrowInvalidArgument(nil, "MODEL-VoaRj", "Passwordpolicy doesn't match: HasUpperCase")
|
||||
}
|
||||
|
||||
if p.HasNumber && !hasNumber(password) {
|
||||
return caos_errs.ThrowInvalidArgument(nil, "MODEL-ZBv4H", "Passwordpolicy doesn't match: HasNumber")
|
||||
}
|
||||
|
||||
if p.HasSymbol && !hasSymbol(password) {
|
||||
return caos_errs.ThrowInvalidArgument(nil, "MODEL-ZDLwA", "Passwordpolicy doesn't match: HasSymbol")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user