jwt

package
v1.81.0 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package jwt provides JWT Auth handlers.

Index

Constants

View Source
const (
	// DefaultExpirationTime is the default JWT expiration time.
	DefaultExpirationTime = 5 * time.Minute

	// DefaultRenewTime is the default time before the JWT expiration when the renewal is allowed.
	DefaultRenewTime = 30 * time.Second

	// DefaultAuthorizationHeader is the default authorization header name.
	DefaultAuthorizationHeader = "Authorization"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Claims

type Claims struct {
	Username string `json:"username"`
	jwt.StandardClaims
}

Claims holds the JWT information to be encoded.

type Credentials

type Credentials struct {
	Username string `json:"username"`
	Password string `json:"password"`
}

Credentials holds the user name and password from the request body.

type JWT

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

JWT represents an instance of the HTTP retrier.

func New

func New(key []byte, userHashFn UserHashFn, opts ...Option) (*JWT, error)

New creates a new instance.

func (*JWT) IsAuthorized

func (c *JWT) IsAuthorized(w http.ResponseWriter, r *http.Request) bool

IsAuthorized checks if the user is authorized via JWT token.

func (*JWT) LoginHandler

func (c *JWT) LoginHandler(w http.ResponseWriter, r *http.Request)

LoginHandler handles the login endpoint.

func (*JWT) RenewHandler

func (c *JWT) RenewHandler(w http.ResponseWriter, r *http.Request)

RenewHandler handles the JWT renewal endpoint.

type Option

type Option func(c *JWT)

Option is the interface that allows to set the options.

func WithAuthorizationHeader

func WithAuthorizationHeader(authorizationHeader string) Option

WithAuthorizationHeader sets the authorization header name.

func WithExpirationTime

func WithExpirationTime(expirationTime time.Duration) Option

WithExpirationTime set the JWT expiration time.

func WithRenewTime

func WithRenewTime(renewTime time.Duration) Option

WithRenewTime set the time before the JWT expiration when the renewal is allowed.

func WithSendResponseFn

func WithSendResponseFn(sendResponseFn SendResponseFn) Option

WithSendResponseFn set the function used to send back the HTTP responses.

func WithSigningMethod

func WithSigningMethod(signingMethod SigningMethod) Option

WithSigningMethod sets the signing method function.

type SendResponseFn

type SendResponseFn func(ctx context.Context, w http.ResponseWriter, statusCode int, data string)

SendResponseFn is the type of function used to send back the HTTP responses.

type SigningMethod

type SigningMethod jwt.SigningMethod

SigningMethod is a type alias for the Signing Method interface.

type UserHashFn

type UserHashFn func(username string) ([]byte, error)

UserHashFn is the type of function used to retrieve the password hash associated with each user. The hash values should be generated via bcrypt.GenerateFromPassword(pwd, bcrypt.MinCost).

Jump to

Keyboard shortcuts

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