auth

package module
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2020 License: Apache-2.0 Imports: 18 Imported by: 0

README

auth

Authentication Package

Documentation

Overview

Package auth is a authentication helper that supports multiple types of applications.

Index

Constants

View Source
const (
	// BcryptDefaultCost is the Bcrypt Default Cost where the performace is optimal
	BcryptDefaultCost int = bcrypt.DefaultCost
	// BcryptMaxCost is the Bcrypt Maximum Cost with Longest Time
	BcryptMaxCost int = bcrypt.MaxCost
	// BcryptMinCost is the Bcrypt Minimum Cost with Shortest time
	BcryptMinCost int = bcrypt.MinCost
)
View Source
const (
	// Pbkdf2DefaultRounds Default number of rounds to be used for PBKDF2
	Pbkdf2DefaultRounds int = 4096
	// Pbkdf2MaxRounds Maximum number of rounds to be used for PBKDF2
	Pbkdf2MaxRounds int = 8192
	// Pbkdf2MinRounds Minimum number of rounds to be used for PBKDF2
	Pbkdf2MinRounds int = 1024
)
View Source
const JWTDefaultDuration = 1 * time.Minute

JWTDefaultDuration defines the default duration for a Token

View Source
const JWTDefaultIDSize int = 32

JWTDefaultIDSize defines the default size for the JWT ID

View Source
const MethodPasswordHashBcrypt = "Password Hash using bcrypt"

MethodPasswordHashBcrypt defines password hash generation using bcrypt

View Source
const MethodPasswordHashPbkdf2HS256 = "Password Hash using pbkdf2 and HMAC-SHA256"

MethodPasswordHashPbkdf2HS256 defines password hash generation using pbkdf2 with HMAC-SHA256

View Source
const Pbkdf2RoundsSize int = 8

Pbkdf2RoundsSize is the Size of the Encoded number of rounds in a byte array

View Source
const Pbkdf2SaltSize int = 8

Pbkdf2SaltSize is the minimum recommended size of the Salt used for PBKDF2

Variables

View Source
var (
	Md5    *HashFn
	Sha1   *HashFn
	Sha224 *HashFn
	Sha256 *HashFn
	Sha384 *HashFn
	Sha512 *HashFn
	Bcrypt *BcryptFn
)

List of Hash Functions

View Source
var ErrNotImplemented = fmt.Errorf("error functionality not implemented yet")

ErrNotImplemented occurs when an Un-implemented feature is called on

View Source
var ErrNotInitialized = fmt.Errorf("error this construct is not initialized")

ErrNotInitialized occurs when an Authentication process or function tries to access an un-initialized data parameter or construct.

View Source
var ErrParameter = fmt.Errorf("error in supplied parameters")

ErrParameter occurs when there are issues with the supplied parameter in any function.

Functions

func CheckPbkdf2HS1 added in v0.0.5

func CheckPbkdf2HS1(key []byte, derivedKey []byte, salt []byte, rounds int) error

CheckPbkdf2HS1 check the PBKDF2 HMAC-SHA1 Key with an derived key

func CheckPbkdf2HS256 added in v0.0.5

func CheckPbkdf2HS256(key []byte, derivedKey []byte, salt []byte, rounds int) error

CheckPbkdf2HS256 check the PBKDF2 HMAC-SHA256 Key with an derived key

func CheckPbkdf2HS512 added in v0.0.5

func CheckPbkdf2HS512(key []byte, derivedKey []byte, salt []byte, rounds int) error

CheckPbkdf2HS512 check the PBKDF2 HMAC-SHA512 Key with an derived key

func Decode added in v0.0.6

func Decode(f *EncodeIt, value string) ([]byte, error)

Decode is the Generic DecodeFromString function for all encoded values supported

func Digest added in v0.0.6

func Digest(d DigestIt, data []byte, opts ...DigestOptions) ([]byte, error)

Digest is the generic function for obtaining various type of HASH function operations on the data

func Encode added in v0.0.6

func Encode(f *EncodeIt, data []byte) string

Encode is Generic EncodeToString function for All byte Arrays

func GetRandom added in v0.0.5

func GetRandom(size int) ([]byte, error)

GetRandom returns a cryptographically safe randome numbers byte array with the size specified

func Pbkdf2HS1 added in v0.0.5

func Pbkdf2HS1(key []byte, rounds int) (derivedKey []byte, salt []byte, err error)

Pbkdf2HS1 perform PBKDF2 Key Derivation using HMAC-SHA1

func Pbkdf2HS256 added in v0.0.5

func Pbkdf2HS256(key []byte, rounds int) (derivedKey []byte, salt []byte, err error)

Pbkdf2HS256 perform PBKDF2 Key Derivation using HMAC-SHA256

func Pbkdf2HS512 added in v0.0.5

func Pbkdf2HS512(key []byte, rounds int) (derivedKey []byte, salt []byte, err error)

Pbkdf2HS512 perform PBKDF2 Key Derivation using HMAC-SHA512

func RegisterDigestFunction added in v0.0.6

func RegisterDigestFunction(name string, f DigestIt)

RegisterDigestFunction adds the specific Hash generation functions in the global list. It is typically run during init() stage.

func RegisterEncoder added in v0.0.6

func RegisterEncoder(f *EncodeIt)

RegisterEncoder updates the list of Encoders available. This is typically called during init() stage.

func RegisterJwtMethod added in v0.0.6

func RegisterJwtMethod(name string, f func() JwtMethod)

RegisterJwtMethod registers a given Method name and a factory function for registering into the list JWT processing methods. This is only called during init.

func UUIDv4 added in v0.0.6

func UUIDv4() (string, error)

UUIDv4 function helps to generate UUID using V4 algorithm

Types

type Auth

type Auth interface {

	// Create function generates the Authentication Entity by processing
	// incoming data and the specific 'bias'.
	Create(data []byte, bias interface{}) ([]byte, error)

	// Verify function checks the Authentication Entity by processing
	// it with the optional incoming 'bias'. It also recovers the original
	// 'data' and 'bias' used to create the Authentication Entity.
	Verify(value []byte, bias interface{}) ([]byte, interface{}, error)

	// Set function configures the Authentication Entity creation and
	// verification process. It also accepts the static "Key" that needs
	// to be employed while processing the Authentication Entity.
	Set(method string, key interface{}) error
}

Auth is the generic interface that would be implemented by the various authentication algorithms and classifications.

The term "Authentication Entity" refers to a token, pass, or a Unique piece of information that provides Identity, and Authorization status of the bearer.

func NewPasswordHash added in v0.0.2

func NewPasswordHash(method string, key interface{}) (Auth, error)

NewPasswordHash Configures the respective password Hashing algorithm and returns the instance which implements the Auth Interface

type BcryptFn added in v0.0.6

type BcryptFn struct {
	FnName string
}

BcryptFn implements the DigestIt interface but needs a special option to work

func (*BcryptFn) Name added in v0.0.6

func (b *BcryptFn) Name() string

Name method of the DigestIt Interface

func (*BcryptFn) Proc added in v0.0.6

func (b *BcryptFn) Proc(data []byte) ([]byte, error)

Proc method implementation for DigestIt

type DigestIt added in v0.0.6

type DigestIt interface {

	// Name Returns the Name in String for the given Hash Function
	Name() string

	// Proc function takes in the byte array of arbitrary Size and
	// process it into a digest of fix size
	Proc([]byte) ([]byte, error)
}

DigestIt interface defines the way by which Hash function coverts byte array of arbitrary size to a byte array of a fixed size called the "hash value", "hash", or "message digest"

func GetDigestFunction added in v0.0.6

func GetDigestFunction(name string) (f DigestIt)

GetDigestFunction fetches the respective Hash generation function using its pre registed name.

type DigestOptions added in v0.0.6

type DigestOptions func(DigestIt) DigestIt

DigestOptions provides a functional Option for attribute modification functions

func WithBcryptCost added in v0.0.6

func WithBcryptCost(cost int) DigestOptions

WithBcryptCost helps to implement Alternative Bcrypt operation

func WithBcryptDigest added in v0.0.6

func WithBcryptDigest(digest []byte) DigestOptions

WithBcryptDigest helps to implement Verification as part of Digest

func WithHMACKey added in v0.0.6

func WithHMACKey(key []byte) DigestOptions

WithHMACKey helps to implement HMAC operation

type EncodeIt added in v0.0.6

type EncodeIt struct {
	Name string
	// To(EncodeToString) converts the Supplied byte array to the specific
	// encode Format string
	To func(src []byte) string

	// From(DecodeString) converts the Supplied string back to its byte array form
	From func(s string) ([]byte, error)
}

EncodeIt is the String format encode function for byte Array encoders

var (
	Hex       *EncodeIt
	Base64    *EncodeIt
	Base64URL *EncodeIt
)

List of Encoders Supported

func GetEncoder added in v0.0.6

func GetEncoder(name string) (f *EncodeIt)

GetEncoder fetches the specific encoder from the List of Encoders

type HashFn added in v0.0.6

type HashFn struct {
	FnName string
	Hash   crypto.Hash
}

HashFn function implements the DigestIt interface

func (*HashFn) HashFunc added in v0.0.6

func (h *HashFn) HashFunc() crypto.Hash

HashFunc method returns the internal Hash Function unit of Crypto

func (*HashFn) Name added in v0.0.6

func (h *HashFn) Name() string

Name method of the DigestIt Interface

func (*HashFn) New added in v0.0.6

func (h *HashFn) New() hash.Hash

New method creates a new instance of the Internal New Method

func (*HashFn) Proc added in v0.0.6

func (h *HashFn) Proc(data []byte) ([]byte, error)

Proc method implementation for DigestIt

func (*HashFn) Size added in v0.0.6

func (h *HashFn) Size() int

Size method exposes the internal Size Method

type JwtAlgorithm added in v0.0.6

type JwtAlgorithm interface {

	// CreateToken helps to Create a token using supplied data in the from of map of string
	// and supplied options.
	CreateToken(string, ...func(JwtAlgorithm) JwtAlgorithm) (string, error)
	VerifyToken(string, ...func(JwtAlgorithm) JwtAlgorithm) (string, error)
}

JwtAlgorithm defines the way in which the JWT is calculated and verified

type JwtAlgorithmOptions added in v0.0.6

type JwtAlgorithmOptions func(JwtAlgorithm) JwtAlgorithm

JwtAlgorithmOptions is used to implement the JWT Option for Functional parameters of each Algorithm

type JwtMethod added in v0.0.6

type JwtMethod Auth

JwtMethod defines the way in which the JWT is Calculated

func GetJwtMethod added in v0.0.6

func GetJwtMethod(name string) (method JwtMethod)

GetJwtMethod helps to fetch the JWT Method via its name

Jump to

Keyboard shortcuts

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