jwt

package module
v0.0.0-...-8ed5e69 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2025 License: Apache-2.0 Imports: 9 Imported by: 0

README

iris-jwt

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrMissingSecretKey indicates Secret key is required
	ErrMissingSecretKey = errors.New("secret key is required")

	// ErrForbidden when HTTP status 403 is given
	ErrForbidden = errors.New("you don't have permission to access this resource")

	// ErrMissingAuthenticatorFunc indicates Authenticator is required
	ErrMissingAuthenticatorFunc = errors.New("irisJWTMiddleware.Authenticator func is undefined")

	// ErrMissingLoginValues indicates a user tried to authenticate without username or password
	ErrMissingLoginValues = errors.New("missing Username or Password")

	// ErrFailedAuthentication indicates authentication failed, could be faulty username or password
	ErrFailedAuthentication = errors.New("incorrect Username or Password")

	// ErrFailedTokenCreation indicates JWT Token failed to create, reason unknown
	ErrFailedTokenCreation = errors.New("failed to create JWT Token")

	// ErrExpiredToken indicates JWT token has expired. Can't refresh.
	ErrExpiredToken = errors.New("token is expired") // in practice, this is generated from the jwt library not by us

	// ErrEmptyAuthHeader can be thrown if authing with a HTTP header, the Auth header needs to be set
	ErrEmptyAuthHeader = errors.New("auth header is empty")

	// ErrMissingExpField missing exp field in token
	ErrMissingExpField = errors.New("missing exp field")

	// ErrWrongFormatOfExp field must be float64 format
	ErrWrongFormatOfExp = errors.New("exp must be float64 format")

	// ErrInvalidAuthHeader indicates auth header is invalid, could for example have the wrong Realm name
	ErrInvalidAuthHeader = errors.New("auth header is invalid")

	// ErrEmptyQueryToken can be thrown if authing with URL Query, the query token variable is empty
	ErrEmptyQueryToken = errors.New("query token is empty")

	// ErrEmptyCookieToken can be thrown if authing with a cookie, the token cookie is empty
	ErrEmptyCookieToken = errors.New("cookie token is empty")

	// ErrEmptyParamToken can be thrown if authing with parameter in path, the parameter in path is empty
	ErrEmptyParamToken = errors.New("parameter token is empty")

	// ErrInvalidSigningAlgorithm indicates signing algorithm is invalid, needs to be HS256, HS384, HS512, RS256, RS384 or RS512
	ErrInvalidSigningAlgorithm = errors.New("invalid signing algorithm")

	// ErrNoPrivKeyFile indicates that the given private key is unreadable
	ErrNoPrivKeyFile = errors.New("private key file unreadable")

	// ErrNoPubKeyFile indicates that the given public key is unreadable
	ErrNoPubKeyFile = errors.New("public key file unreadable")

	// ErrInvalidPrivKey indicates that the given private key is invalid
	ErrInvalidPrivKey = errors.New("private key invalid")

	// ErrInvalidPubKey indicates the the given public key is invalid
	ErrInvalidPubKey = errors.New("public key invalid")

	// IdentityKey default identity key
	IdentityKey = "identity"
)

Functions

This section is empty.

Types

type IrisJWTMiddleware

type IrisJWTMiddleware struct {
	Realm                 string
	SigningAlgorithm      string
	Key                   []byte
	KeyFunc               func(token *jwt.Token) (interface{}, error)
	Timeout               time.Duration
	TimeoutFunc           func(data interface{}) time.Duration
	MaxRefresh            time.Duration
	Authenticator         func(ctx iris.Context) (interface{}, error)
	Authorizator          func(data interface{}, ctx iris.Context) bool
	PayloadFunc           func(data interface{}) MapClaims
	Unauthorized          func(ctx iris.Context, code int, message string)
	LoginResponse         func(ctx iris.Context, code int, message string, time time.Time)
	LogoutResponse        func(ctx iris.Context, code int)
	RefreshResponse       func(ctx iris.Context, code int, message string, time time.Time)
	IdentityHandler       func(ctx iris.Context) interface{}
	IdentityKey           string
	TokenLookup           string
	TokenHeadName         string
	TimeFunc              func() time.Time
	HTTPStatusMessageFunc func(e error, ctx iris.Context) string
	PrivKeyFile           string
	PrivKeyBytes          []byte
	PubKeyFile            string
	PrivateKeyPassphrase  string
	PubKeyBytes           []byte

	SendCookie        bool
	CookieMaxAge      time.Duration
	SecureCookie      bool
	CookieHTTPOnly    bool
	CookieDomain      string
	SendAuthorization bool
	DisabledAbort     bool
	CookieName        string
	CookieSameSite    http.SameSite
	ParseOptions      []jwt.ParserOption
	// contains filtered or unexported fields
}

IrisJWTMiddleware provides a JWT authentication implementation for Iris.

func New

New for check error with IrisJWTMiddleware

func (*IrisJWTMiddleware) CheckIfTokenExpire

func (mw *IrisJWTMiddleware) CheckIfTokenExpire(ctx iris.Context) (jwt.MapClaims, error)

CheckIfTokenExpire check if token expire

func (*IrisJWTMiddleware) GetClaimsFromJWT

func (mw *IrisJWTMiddleware) GetClaimsFromJWT(ctx iris.Context) (MapClaims, error)

GetClaimsFromJWT get claims from JWT token

func (*IrisJWTMiddleware) LoginHandler

func (mw *IrisJWTMiddleware) LoginHandler(ctx iris.Context)

LoginHandler can be used by clients to get a jwt token.

func (*IrisJWTMiddleware) LogoutHandler

func (mw *IrisJWTMiddleware) LogoutHandler(ctx iris.Context)

LogoutHandler can be used by clients to remove the jwt cookie (if set)

func (*IrisJWTMiddleware) MiddlewareFunc

func (mw *IrisJWTMiddleware) MiddlewareFunc() iris.Handler

MiddlewareFunc makes IrisJWTMiddleware implement the Middleware interface.

func (*IrisJWTMiddleware) MiddlewareInit

func (mw *IrisJWTMiddleware) MiddlewareInit() error

MiddlewareInit initialize jwt configs.

func (*IrisJWTMiddleware) ParseToken

func (mw *IrisJWTMiddleware) ParseToken(ctx iris.Context) (*jwt.Token, error)

ParseToken parse jwt token from iris context

func (*IrisJWTMiddleware) ParseTokenString

func (mw *IrisJWTMiddleware) ParseTokenString(token string) (*jwt.Token, error)

ParseTokenString parse jwt token string

func (*IrisJWTMiddleware) RefreshHandler

func (mw *IrisJWTMiddleware) RefreshHandler(ctx iris.Context)

RefreshHandler can be used to refresh a token.

func (*IrisJWTMiddleware) RefreshToken

func (mw *IrisJWTMiddleware) RefreshToken(ctx iris.Context) (string, time.Time, error)

RefreshToken refresh token and check if token is expired

func (*IrisJWTMiddleware) SetCookie

func (mw *IrisJWTMiddleware) SetCookie(ctx iris.Context, tokenString string)

SetCookie sets the JWT token as a cookie

func (*IrisJWTMiddleware) TokenGenerator

func (mw *IrisJWTMiddleware) TokenGenerator(data interface{}) (string, time.Time, error)

TokenGenerator method that clients can use to get a jwt token.

type MapClaims

type MapClaims map[string]interface{}

MapClaims type that uses the map[string]interface{} for JSON decoding

func ExtractClaims

func ExtractClaims(ctx iris.Context) MapClaims

ExtractClaims help to extract the JWT claims

func ExtractClaimsFromToken

func ExtractClaimsFromToken(token *jwt.Token) MapClaims

ExtractClaimsFromToken helps to extract the JWT claims from a token

Jump to

Keyboard shortcuts

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