strongword

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2020 License: MIT Imports: 2 Imported by: 0

README

strongword - Password strength validator for GO

GoDoc Version

Simple utility library to validate password strengths against a set of rules.

Install

go get github.com/bounoable/strongword

Usage

Default rule set
import "github.com/bounoable/strongword"

errs := strongword.Validate("weakpassword")

for _, err := range errs {
  fmt.Println(err)
}
Custom rule set
import "github.com/bounoable/strongword"

errs := strongword.Validate(
  "weakpassword",
  strongword.MinLength(12),
  strongword.SpecialChars(3),
  strongword.Regexp(regexp.MustCompile("(?)[0-9]{4}"))
)

for _, err := range errs {
  fmt.Println(err)
}

Documentation

Overview

Package strongword is a simple utility for validating password strengths against a provided set of rules.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// DefaultRules are the rules that are used if none are provided.
	DefaultRules = []Rule{
		MinLength(8),
		CharsAndDigits(),
		SpecialChars(1),
	}
)

Functions

func Validate

func Validate(word string, rules ...Rule) []error

Validate validates the given word against the provided rules. If no rules are provided, the default rule set is used: MinLength(8), CharsAndDigits(), SpecialChars(1)

Example
package main

import (
	"fmt"
	"regexp"

	"github.com/bounoable/strongword"
)

func main() {
	// Default rule set
	errs := strongword.Validate("weakpassword")

	// Custom rule set
	errs = strongword.Validate(
		"weakpassword",
		strongword.MinLength(12),
		strongword.SpecialChars(3),
		strongword.Regexp(regexp.MustCompile("(?)[0-9]{4}")),
	)

	for _, err := range errs {
		fmt.Println(err)
	}
}

Types

type CharsAndDigitsError

type CharsAndDigitsError struct {
	Detail string
}

CharsAndDigitsError means the provided word does not contain either any character or any digit.

func (CharsAndDigitsError) Error

func (err CharsAndDigitsError) Error() string

type DigitsError

type DigitsError struct {
	Minimum  int
	Provided int
}

DigitsError means the provided word does not contain enough digits.

func (DigitsError) Error

func (err DigitsError) Error() string

type MinLengthError

type MinLengthError struct {
	MinLength      int
	ProvidedLength int
}

MinLengthError means the provided word is too short.

func (MinLengthError) Error

func (err MinLengthError) Error() string

type RegexpError

type RegexpError struct {
	Regexp *regexp.Regexp
}

RegexpError means the provided word does match against a regular expression.

func (RegexpError) Error

func (err RegexpError) Error() string

type Rule

type Rule interface {
	Validate(string) error
}

Rule is a validation rule.

func Characters

func Characters(chars string, min int) Rule

Characters validates that a word contains any unicode point if chars at least min times.

func CharsAndDigits

func CharsAndDigits() Rule

CharsAndDigits validates that a word does contain at least one character and one digit.

func Digits

func Digits(min int) Rule

Digits validates the digit count in a word.

func MinLength

func MinLength(length int) Rule

MinLength validates the length of a word.

func Regexp

func Regexp(expr *regexp.Regexp) Rule

Regexp validates a word against a regular expression.

func Runes

func Runes(runes []rune, min int) Rule

Runes validates that a word contains any of runes at least min times.

func SpecialChars

func SpecialChars(min int) Rule

SpecialChars validates the special character count in a word. Special characters are all charaters that are not latin-alphanumerical (a-z & 0-9).

type RuleFunc

type RuleFunc func(string) error

RuleFunc is a validation rule.

func (RuleFunc) Validate

func (fn RuleFunc) Validate(word string) error

Validate validates the given word.

type RunesError

type RunesError struct {
	Runes    []rune
	Minimum  int
	Provided int
}

RunesError means the provided word does not contain enough runes of a given set.

func (RunesError) Error

func (err RunesError) Error() string

type SpecialCharsError

type SpecialCharsError struct {
	Minimum  int
	Provided int
}

SpecialCharsError means the provided word does not contain enough special characters.

func (SpecialCharsError) Error

func (err SpecialCharsError) Error() string

Jump to

Keyboard shortcuts

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