summaryrefslogtreecommitdiff
path: root/validator.go
blob: d6a4bb53f393afde2b3d869cfad62ae5a2ce2230 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package main

import (
	"fmt"
	"regexp"
)

var specialsRxp *regexp.Regexp = regexp.MustCompile("[,.\\[\\]!@#$%^&*?_\\(\\)-]")
var numbersRxp *regexp.Regexp = regexp.MustCompile("[0-9]")

func validatePassword(password string) error {
	if len(password) < 10 {
		return fmt.Errorf("Your password needs to be at least 10 characters long. But was %d characters.", len(password))
	}
	// check uppercase and lowercase
	if match, err := regexp.MatchString("[a-z]", password); err != nil || !match {
		return fmt.Errorf("Your password needs at least one lowercase character")
	}
	if match, err := regexp.MatchString("[A-Z]", password); err != nil || !match {
		return fmt.Errorf("Your password needs at least one uppercase character")
	}
	// check specials + numbers (at least 3)
	specials := specialsRxp.FindAllString(password, -1)
	numbers := numbersRxp.FindAllString(password, -1)
	if len(specials)+len(numbers) < 3 {
		return fmt.Errorf("Your password needs at least three special characters or numbers")
	}

	// call out to password hash check service...
	return nil
}