auth

package
v0.0.0-...-015e340 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTokenExpired means that the JWT is valid, but it has expired
	ErrTokenExpired = errors.New("token has expired")
	// ErrInvalidSignature means that the JWT has been tampered with
	ErrInvalidSignature = errors.New("invalid signature")
	// ErrInvalidFormat means that the token is not a valid JWT token
	ErrInvalidFormat = errors.New("invalid token format")
)

Functions

This section is empty.

Types

type Authenticator

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

Authenticator handles the logic around generating and validating JWT tokens

func NewAutheniticator

func NewAutheniticator(secret string) *Authenticator

NewAutheniticator creates new Authenticator from the given parameters.

func (*Authenticator) DecodeToken

func (a *Authenticator) DecodeToken(token string) (*models.User, error)

DecodeToken accepts a token, and if valid and unexpired returns the username of the user the token belongs to, otherwise it returns an error. If the token is expired, a ErrTokenExpired is returned. If the JWT has been tampered with, a ErrInvalidSignature is returned.

func (*Authenticator) NewTokenForUser

func (a *Authenticator) NewTokenForUser(user *models.User) (string, error)

NewTokenForUser generates a new JWT for the given username, with the default expiration of 50 minutes, signs it with a secret and returns it.

func (*Authenticator) NewTokenForUserWithExpiration

func (a *Authenticator) NewTokenForUserWithExpiration(user *models.User, d time.Duration) (string, error)

NewTokenForUserWithExpiration generates a new JWT for the given username, with expiration now + d, signs it with a secret and returns it.

Jump to

Keyboard shortcuts

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