Documentation ¶
Overview ¶
Package jwt provides simple wrapper functions for managing basic JWT Authentication with username/password credentials.
The package is designed to be used in conjunction with the net/http package in the Go standard library. It includes functions for handling login, renewal, and authorization of JWT tokens.
Index ¶
- Constants
- type Claims
- type Credentials
- type JWT
- type Option
- func WithAuthorizationHeader(authorizationHeader string) Option
- func WithClaimAudience(audience []string) Option
- func WithClaimIssuer(issuer string) Option
- func WithClaimSubject(subject string) Option
- func WithExpirationTime(expirationTime time.Duration) Option
- func WithRenewTime(renewTime time.Duration) Option
- func WithSendResponseFn(sendResponseFn SendResponseFn) Option
- func WithSigningMethod(signingMethod SigningMethod) Option
- type SendResponseFn
- type SigningMethod
- type UserHashFn
Constants ¶
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.RegisteredClaims }
Claims holds the JWT information to be encoded.
type Credentials ¶
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 ¶
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 ¶
WithAuthorizationHeader sets the authorization header name.
func WithClaimAudience ¶ added in v1.84.1
WithClaimAudience sets the `aud` (Audience) claim. See https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.3
func WithClaimIssuer ¶ added in v1.84.1
WithClaimIssuer sets the `iss` (Issuer) JWT claim. See https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.1
func WithClaimSubject ¶ added in v1.84.1
WithClaimSubject sets the `sub` (Subject) claim. See https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.2
func WithExpirationTime ¶
WithExpirationTime set the JWT expiration time.
func WithRenewTime ¶
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 ¶
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 ¶
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).