auth

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidToken = errors.New("jwt token is invalid")

Functions

func CheckPasswordHash

func CheckPasswordHash(password, hash string) bool

Compares the password with the hash using bcrypt.CompareHashAndPassword

func HashPassword

func HashPassword(password string) (string, error)

Hashes a password string using default cost

Types

type JWT

type JWT struct {
	// contains filtered or unexported fields
}

JWT implements the Tokener interface. Create method takes in id as the payload.

It is important to note that bcrypt is a slow hashing function, which means that it can take a long time to generate and verify hashes. This is a security feature, as it makes it more difficult for attackers to guess or brute-force the hashes. However, it also means that bcrypt may not be the most suitable choice for generating and verifying tokens, especially if you need to do it frequently or in high-concurrency situations.

func (*JWT) Create

func (jwtoken *JWT) Create(id uint) (string, error)

Creates a jwt token that expires after the configured duration.

Payload is the id. Returns a base64 encoded JWT string.

func (*JWT) Verify

func (jwtoken *JWT) Verify(base64Token string) (uint, error)

Verifies a base64 encoded token string Returns the user id from the payload and an error if any or nil. If the base64Token can not be decoded or an error occurs in jwt.Parse, the error is auth.ErrInvalidToken

type JWTOption

type JWTOption func(*JWT)

func ExpiresAfter

func ExpiresAfter(expiresAfter time.Duration) JWTOption

Configure the expiry of the JWT token.

func SigningMethod

func SigningMethod(method jwt.SigningMethod) JWTOption

Configure the JWT signing method. The default is jwt.SigningMethodHS256

type Tokener

type Tokener interface {
	Create(id uint) (string, error)
	Verify(token string) (uint, error)
}

func NewJWT

func NewJWT(secretKey string, options ...JWTOption) Tokener

Creates a new Tokener interface with default expiry of 72 hours. Customize this by passing in functional options of type JWTOption. secretKey is a required secure token.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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