validator

package module
v0.0.0-...-c284a2f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 7, 2025 License: MIT Imports: 4 Imported by: 12

README

Password validator library for Go

Latest tag GoDoc Tests status Go report License

Installation

go get -u github.com/go-passwd/validator

Usage

import "github.com/go-passwd/validator"

passwordValidator := validator.New(validator.MinLength(5, nil), validator.MaxLength(10, nil))
err := passwordValidator.Validate(form.Password)
if err != nil {
  panic(err)
}

You can pass to every validator functions customError parameter witch will be returned on error instead of default error.

import "github.com/go-passwd/validator"

passwordValidator := validator.New(validator.MinLength(5, errors.New("too short")), validator.MaxLength(10, errors.New("too long")))
err := passwordValidator.Validate(form.Password)
if err != nil {
  panic(err)
}

Validators

CommonPassword

Check if password is a common password.

Common password list is based on list created by Mark Burnett: https://xato.net/passwords/more-top-worst-passwords/

passwordValidator := validator.New(validator.CommonPassword(nil))
ContainsAtLeast

Count occurrences of a chars and compares it with required value.

passwordValidator := validator.New(validator.ContainsAtLeast(5, "abcdefghijklmnopqrstuvwxyz", nil))
ContainsOnly

Check if password contains only selected chars.

passwordValidator := validator.New(validator.ContainsOnly("abcdefghijklmnopqrstuvwxyz", nil))
MaxLength

Check if password length is not greater that defined length.

passwordValidator := validator.New(validator.MaxLength(10, nil))
MinLength

Check if password length is not lower that defined length.

passwordValidator := validator.New(validator.MinLength(5, nil))
Noop

Always return custom error.

passwordValidator := validator.New(validator.Noop(nil))
Regex

Check if password match regexp pattern.

passwordValidator := validator.New(validator.Regex("^\\w+$", nil))
Similarity

Check if password is sufficiently different from the attributes.

Attributes can be: user login, email, first name, last name, …

passwordValidator := validator.New(validator.Similarity([]string{"username", "username@example.com"}, nil, nil))
StartsWith

Check if password starts with one of letter.

passwordValidator := validator.New(validator.StartsWith("abcdefghijklmnopqrstuvwxyz", nil))
Unique

Check if password contains only unique chars.

passwordValidator := validator.New(validator.Unique(nil))

Documentation

Overview

Package validator is a set of functions to validate passwords

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Ratio

func Ratio(pass, attr string) float64

Ratio returns a measure of the sequences' similarity (float in [0,1]).

Where T is the total number of elements in both sequences, and M is the number of matches, this is 2.0*M / T. Note that this is 1 if the sequences are identical, and 0 if they have nothing in common.

Types

type ValidateFunc

type ValidateFunc func(password string) error

ValidateFunc defines a function to validate.

func CommonPassword

func CommonPassword(customError error) ValidateFunc

CommonPassword returns ValidateFunc that validate whether the password is a common password.

The password is rejected if it occurs in one of the provided lists: https://xato.net/passwords/more-top-worst-passwords/ - created by Mark Burnett https://nordpass.com/most-common-passwords-list/ - created by NordPass https://github.com/danielmiessler/SecLists/blob/master/Passwords/Common-Credentials/100k-most-used-passwords-NCSC.txt - created by NCSC

Example
package main

import (
	"fmt"

	"github.com/go-passwd/validator"
)

func main() {
	passwordValidator := validator.CommonPassword(nil)
	fmt.Println(passwordValidator("password"))
	fmt.Println(passwordValidator("qaz123"))
	fmt.Println(passwordValidator("pa$$w0rd@"))
	fmt.Println(passwordValidator("Mod7tygrysow"))
}
Output:

Password can't be a commonly used password
Password can't be a commonly used password
<nil>
Password can't be a commonly used password
Example (CustomError)
package main

import (
	"errors"
	"fmt"

	"github.com/go-passwd/validator"
)

func main() {
	err := errors.New("custom error message")
	passwordValidator := validator.CommonPassword(err)
	fmt.Println(passwordValidator("password"))
}
Output:

custom error message

func ContainsAtLeast

func ContainsAtLeast(chars string, occurrences int, customError error) ValidateFunc

ContainsAtLeast returns a ValidateFunc that count occurrences of a chars and compares it with required value.

Example
package main

import (
	"fmt"

	"github.com/go-passwd/validator"
)

func main() {
	passwordValidator := validator.ContainsAtLeast("abcdefghijklmnopqrstuvwxyz", 4, nil)
	fmt.Println(passwordValidator("password"))
	fmt.Println(passwordValidator("PASSWORD"))
	fmt.Println(passwordValidator("passWORD"))
}
Output:

<nil>
Password must contains at least 4 chars from abcdefghijklmnopqrstuvwxyz
<nil>
Example (CustomError)
package main

import (
	"errors"
	"fmt"

	"github.com/go-passwd/validator"
)

func main() {
	err := errors.New("custom error message")
	passwordValidator := validator.ContainsAtLeast("abcdefghijklmnopqrstuvwxyz", 4, err)
	fmt.Println(passwordValidator("PASSWORD"))
}
Output:

custom error message

func ContainsOnly

func ContainsOnly(chars string, customError error) ValidateFunc

ContainsOnly returns a ValidateFunc that check if password contains only selected chars.

Example
package main

import (
	"fmt"

	"github.com/go-passwd/validator"
)

func main() {
	passwordValidator := validator.ContainsOnly("abcdefghijklmnopqrstuvwxyz", nil)
	fmt.Println(passwordValidator("password"))
	fmt.Println(passwordValidator("password0"))
	fmt.Println(passwordValidator("passWORD"))
}
Output:

<nil>
the password must contains only abcdefghijklmnopqrstuvwxyz
the password must contains only abcdefghijklmnopqrstuvwxyz
Example (CustomError)
package main

import (
	"errors"
	"fmt"

	"github.com/go-passwd/validator"
)

func main() {
	err := errors.New("custom error message")
	passwordValidator := validator.ContainsOnly("abcdefghijklmnopqrstuvwxyz", err)
	fmt.Println(passwordValidator("password"))
	fmt.Println(passwordValidator("password0"))
	fmt.Println(passwordValidator("passWORD"))
}
Output:

<nil>
custom error message
custom error message

func MaxLength

func MaxLength(length int, customError error) ValidateFunc

MaxLength returns a ValidateFunc that check if password length is not greater that "length".

Example
package main

import (
	"fmt"

	"github.com/go-passwd/validator"
)

func main() {
	passwordValidator := validator.MaxLength(5, nil)
	fmt.Println(passwordValidator("password"))
	fmt.Println(passwordValidator("pass"))
	fmt.Println(passwordValidator("passw"))
}
Output:

Password length must be not greater that 5 chars
<nil>
<nil>
Example (CustomError)
package main

import (
	"errors"
	"fmt"

	"github.com/go-passwd/validator"
)

func main() {
	err := errors.New("custom error message")
	passwordValidator := validator.MaxLength(5, err)
	fmt.Println(passwordValidator("password"))
}
Output:

custom error message

func MinLength

func MinLength(length int, customError error) ValidateFunc

MinLength returns a ValidateFunc that check if password length is not lower that "length".

Example
package main

import (
	"fmt"

	"github.com/go-passwd/validator"
)

func main() {
	passwordValidator := validator.MinLength(5, nil)
	fmt.Println(passwordValidator("password"))
	fmt.Println(passwordValidator("pass"))
	fmt.Println(passwordValidator("passw"))
}
Output:

<nil>
Password length must be not lower that 5 chars
<nil>
Example (CustomError)
package main

import (
	"errors"
	"fmt"

	"github.com/go-passwd/validator"
)

func main() {
	err := errors.New("custom error message")
	passwordValidator := validator.MinLength(5, err)
	fmt.Println(passwordValidator("pass"))
}
Output:

custom error message

func Noop

func Noop(customError error) ValidateFunc

Noop returns a ValidateFunc that always return custom error.

Example
package main

import (
	"fmt"

	"github.com/go-passwd/validator"
)

func main() {
	passwordValidator := validator.Noop(nil)
	fmt.Println(passwordValidator("password"))
}
Output:

<nil>
Example (CustomError)
package main

import (
	"errors"
	"fmt"

	"github.com/go-passwd/validator"
)

func main() {
	err := errors.New("custom error message")
	passwordValidator := validator.Noop(err)
	fmt.Println(passwordValidator("password"))
}
Output:

custom error message

func Regex

func Regex(pattern string, customError error) ValidateFunc

Regex returns ValidateFunc that check if password match regexp pattern.

Example
package main

import (
	"fmt"

	"github.com/go-passwd/validator"
)

func main() {
	passwordValidator := validator.Regex("^\\w+$", nil)
	fmt.Println(passwordValidator("password"))
	fmt.Println(passwordValidator("pa$$w0rd"))
}
Output:

Password shouldn't match "^\w+$" pattern
<nil>
Example (CustomError)
package main

import (
	"errors"
	"fmt"

	"github.com/go-passwd/validator"
)

func main() {
	err := errors.New("custom error message")
	passwordValidator := validator.Regex("^\\w+$", err)
	fmt.Println(passwordValidator("password"))
}
Output:

custom error message

func Similarity

func Similarity(attributes []string, maxSimilarity *float64, customError error) ValidateFunc

Similarity returns ValidateFunc that validate whether the password is sufficiently different from the attributes

Attributes can be: user login, email, first name, last name, ….

Example
package main

import (
	"fmt"

	"github.com/go-passwd/validator"
)

func main() {
	passwordValidator := validator.Similarity([]string{"username", "username@example.com"}, nil, nil)
	fmt.Println(passwordValidator("username"))
	fmt.Println(passwordValidator("example"))
	similarity := 0.5
	passwordValidator = validator.Similarity([]string{"username", "username@example.com"}, &similarity, nil)
	fmt.Println(passwordValidator("username"))
	fmt.Println(passwordValidator("examplecom"))
}
Output:

The password is too similar to the username
<nil>
The password is too similar to the username
The password is too similar to the username@example.com
Example (CustomError)
package main

import (
	"errors"
	"fmt"

	"github.com/go-passwd/validator"
)

func main() {
	err := errors.New("custom error message")
	passwordValidator := validator.Similarity([]string{"username", "username@example.com"}, nil, err)
	fmt.Println(passwordValidator("username"))
}
Output:

custom error message

func StartsWith

func StartsWith(letters string, customError error) ValidateFunc

StartsWith returns ValidateFunc that validate whether the password is starts with one of letter.

Example
package main

import (
	"fmt"

	"github.com/go-passwd/validator"
)

func main() {
	passwordValidator := validator.StartsWith("abc", nil)
	fmt.Println(passwordValidator("bui87j"))
	fmt.Println(passwordValidator("qwerty"))
}
Output:

<nil>
the password must starts with one of: abc
Example (CustomError)
package main

import (
	"errors"
	"fmt"

	"github.com/go-passwd/validator"
)

func main() {
	err := errors.New("custom error message")
	passwordValidator := validator.StartsWith("abc", err)
	fmt.Println(passwordValidator("bui87j"))
	fmt.Println(passwordValidator("qwerty"))
}
Output:

<nil>
custom error message

func Unique

func Unique(customError error) ValidateFunc

Unique returns ValidateFunc that validate whether the password has only unique chars.

Example
package main

import (
	"fmt"

	"github.com/go-passwd/validator"
)

func main() {
	passwordValidator := validator.Unique(nil)
	fmt.Println(passwordValidator("bui87j"))
	fmt.Println(passwordValidator("qwerte"))
}
Output:

<nil>
the password must contains unique chars
Example (CustomError)
package main

import (
	"errors"
	"fmt"

	"github.com/go-passwd/validator"
)

func main() {
	err := errors.New("custom error message")
	passwordValidator := validator.Unique(err)
	fmt.Println(passwordValidator("bui87j"))
	fmt.Println(passwordValidator("qwerte"))
}
Output:

<nil>
custom error message

type Validator

type Validator []ValidateFunc

Validator represents set of password validators.

func New

func New(vfunc ...ValidateFunc) *Validator

New return new instance of Validator.

func (*Validator) Validate

func (v *Validator) Validate(password string) error

Validate the password.

Example
package main

import (
	"fmt"

	"github.com/go-passwd/validator"
)

func main() {
	passwordValidator := validator.Validator{validator.MinLength(5, nil), validator.MaxLength(10, nil)}
	fmt.Println(passwordValidator.Validate("password"))
	fmt.Println(passwordValidator.Validate("pass1"))
	fmt.Println(passwordValidator.Validate("password12"))
	fmt.Println(passwordValidator.Validate("pass"))
	fmt.Println(passwordValidator.Validate("password123"))
}
Output:

<nil>
<nil>
<nil>
Password length must be not lower that 5 chars
Password length must be not greater that 10 chars

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL