package
0.0.0-20170824085639-9862ebaddb9e
Repository: https://github.com/akagi201/middleware.git
Documentation: pkg.go.dev

# README

jwt middleware

A middleware that will check that a JWT is sent on the Authorization header and will then set the content of the JWT into the user variable of the request.

This module lets you authenticate HTTP requests using JWT tokens in your Go Programming Language applications. JWTs are typically used to protect API endpoints, and are often issued using OpenID Connect.

Key Features

  • Ability to check the Authorization header for a JWT
  • Decode the JWT and set the content of it to the request context

Token Extraction

The default value for the Extractor option is the FromAuthHeader function which assumes that the JWT will be provided as a bearer token in an Authorization header, i.e.,

Authorization: bearer {token}

To extract the token from a query string parameter, you can use the FromParameter function, e.g.,

jwtmiddleware.New(jwtmiddleware.Options{
  Extractor: jwtmiddleware.FromParameter("auth_code"),
})

In this case, the FromParameter function will look for a JWT in the auth_code query parameter.

Or, if you want to allow both, you can use the FromFirst function to try and extract the token first in one way and then in one or more other ways, e.g.,

jwtmiddleware.New(jwtmiddleware.Options{
  Extractor: jwtmiddleware.FromFirst(jwtmiddleware.FromAuthHeader,
                                     jwtmiddleware.FromParameter("auth_code")),
})

# Packages

No description provided by the author

# Functions

FromAuthHeader is a "TokenExtractor" that takes a give request and extracts the JWT token from the Authorization header.
FromCookie returns a function that extracts the token from the specified key in the HTTP cookie, like "access_token".
FromFirst returns a function that runs multiple token extractors and takes the first token it finds.
FromParameter returns a function that extracts the token from the specified query string parameter.
New constructs a new Secure instance with supplied options.
OnError the default function called whenever an error is encountered.

# Structs

JWTMiddleware contains jwt config options.
Options is a struct for specifying configuration options for the middleware.

# Type aliases

TokenExtractor is a function that takes a request as input and returns either a token or an error.