api

package
v0.0.0-...-db524d0 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2023 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrUnauthorized unauhorized token error
	ErrUnauthorized = errors.New("jwtauth: unauthorized token")
	// ErrExpired expired token error
	ErrExpired = errors.New("jwtauth: expired token")
)

Functions

func Authenticator

func Authenticator(next http.Handler) http.Handler

Authenticator validate that user has a valid user auth token before letting him access.

func EpochNow

func EpochNow() int64

EpochNow Helper function that returns the NumericDate time value used by the spec

func ExpireIn

func ExpireIn(tm time.Duration) int64

ExpireIn Helper function to return calculated time in the future for "exp" claim.

func StartAPI

func StartAPI(hostname string, port string, DbConnectionInfo *configs.DbConnection)

StartAPI initialise the api with provided host and port.

Types

type Claims

type Claims map[string]interface{}

Claims is a convenience type to manage a JWT claims hash.

func (Claims) Get

func (c Claims) Get(k string) (interface{}, bool)

Get get claim value

func (Claims) Set

func (c Claims) Set(k string, v interface{}) Claims

Set set claim value

func (Claims) SetExpiry

func (c Claims) SetExpiry(tm time.Time) Claims

SetExpiry Set expiry ("exp") in the claims and return itself so it can be chained

func (Claims) SetExpiryIn

func (c Claims) SetExpiryIn(tm time.Duration) Claims

SetExpiryIn Set expiry ("exp") in the claims to some duration from the present time and return itself so it can be chained

func (Claims) SetIssuedAt

func (c Claims) SetIssuedAt(tm time.Time) Claims

SetIssuedAt Set issued at ("iat") to specified time in the claims

func (Claims) SetIssuedNow

func (c Claims) SetIssuedNow() Claims

SetIssuedNow Set issued at ("iat") to present time in the claims

func (Claims) Valid

func (c Claims) Valid() error

Valid check if claims map is valid

type JwtAuth

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

JwtAuth struct to store JWT auth informations

func New

func New(alg string, signKey []byte, verifyKey []byte) *JwtAuth

New creates a JwtAuth authenticator instance that provides middleware handlers and encoding/decoding functions for JWT signing.

func NewWithParser

func NewWithParser(alg string, parser *jwt.Parser, signKey []byte, verifyKey []byte) *JwtAuth

NewWithParser is the same as New, except it supports custom parser settings introduced in ver. 2.4.0 of jwt-go

func (*JwtAuth) Decode

func (ja *JwtAuth) Decode(tokenString string) (t *jwt.Token, err error)

Decode decode claims

func (*JwtAuth) Encode

func (ja *JwtAuth) Encode(claims Claims) (t *jwt.Token, tokenString string, err error)

Encode encode claims

func (*JwtAuth) IsExpired

func (ja *JwtAuth) IsExpired(t *jwt.Token) bool

IsExpired check if token is expired

func (*JwtAuth) SetContext

func (ja *JwtAuth) SetContext(ctx context.Context, t *jwt.Token, err error) context.Context

SetContext set jwt && jwt.err in context

func (*JwtAuth) Verifier

func (ja *JwtAuth) Verifier(next http.Handler) http.Handler

Verifier middleware will verify a JWT passed by a client request. The Verifier will look for a JWT token from: 1. 'jwt' URI query parameter 2. 'Authorization: BEARER T' request header 3. Cookie 'jwt' value

The verification processes finishes here and sets the token and a error in the request context and calls the next handler.

Make sure to have your own handler following the Validator that will check the value of the "jwt" and "jwt.err" in the context and respond to the client accordingly. A generic Authenticator middleware is provided by this package, that will return a 401 message for all unverified tokens, see jwtauth.Authenticator.

func (*JwtAuth) Verify

func (ja *JwtAuth) Verify(paramAliases ...string) func(http.Handler) http.Handler

Verify get token from context

Jump to

Keyboard shortcuts

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