View on GitHub

Password Toolkit for Go

package validator

import "github.com/go-passwd/validator"
Installation Overview API

Installation

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

Overview

Password validation library for Go

Validate password:

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

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

API

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 a provided list created by Mark Burnett: https://xato.net/passwords/more-top-worst-passwords/

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

func ContainsOnly

func ContainsOnly(chars string, customError error) ValidateFunc

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

func MaxLength

func MaxLength(length int, customError error) ValidateFunc

MaxLength returns a ValidateFunc that check if password length is not greater that “length”

func MinLength

func MinLength(length int, customError error) ValidateFunc

MinLength returns a ValidateFunc that check if password length is not lower that “length”

func Noop

func Noop(customError error) ValidateFunc

Noop returns a ValidateFunc that always return custom error

func Regex

func Regex(pattern string, customError error) ValidateFunc

Regex returns ValidateFunc that check if password match regexp pattern

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, …

func StartsWith

func StartsWith(letters string, customError error) ValidateFunc

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

func Unique

func Unique(customError error) ValidateFunc

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

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