Documentation
¶
Index ¶
- Variables
- func ExtractClaims(c *faygo.Context) jwt.MapClaims
- type FaygoJWTMiddleware
- func (mw *FaygoJWTMiddleware) LoginHandler(c *faygo.Context) error
- func (mw *FaygoJWTMiddleware) MiddlewareFunc() faygo.HandlerFunc
- func (mw *FaygoJWTMiddleware) MiddlewareInit() error
- func (mw *FaygoJWTMiddleware) RefreshHandler(c *faygo.Context) error
- func (mw *FaygoJWTMiddleware) TokenGenerator(userID string) (string, time.Time, error)
- type Login
Constants ¶
This section is empty.
Variables ¶
var ( // ErrMissingRealm indicates Realm name is required ErrMissingRealm = errors.New("realm is missing") // 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("FaygoJWTMiddleware.Authenticator func is undefined") // ErrMissingLoginValues indicates a user tried to authenticate without username or password ErrMissingLoginValues = errors.New("missing Usercode or Password") // ErrFailedAuthentication indicates authentication failed, could be faulty username or password ErrFailedAuthentication = errors.New("incorrect Usercode 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") // ErrEmptyAuthHeader can be thrown if authing with a HTTP header, the Auth header needs to be set ErrEmptyAuthHeader = errors.New("auth header is empty") // 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 cokie is empty ErrEmptyCookieToken = errors.New("cookie 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") )
Functions ¶
Types ¶
type FaygoJWTMiddleware ¶
type FaygoJWTMiddleware struct {
// Realm name to display to the user. Required.
Realm string
// signing algorithm - possible values are HS256, HS384, HS512
// Optional, default is HS256.
SigningAlgorithm string
// Secret key used for signing. Required.
Key []byte
// Duration that a jwt token is valid. Optional, defaults to one hour.
Timeout time.Duration
// This field allows clients to refresh their token until MaxRefresh has passed.
// Note that clients can refresh their token in the last moment of MaxRefresh.
// This means that the maximum validity timespan for a token is MaxRefresh + Timeout.
// Optional, defaults to 0 meaning not refreshable.
MaxRefresh time.Duration
// Callback function that should perform the authentication of the user based on userID and
// password. Must return true on success, false on failure. Required.
// Option return user data, if so, user data will be stored in Claim Array.
Authenticator func(userID string, password string, c *faygo.Context) (interface{}, bool)
// Callback function that should perform the authorization of the authenticated user. Called
// only after an authentication success. Must return true on success, false on failure.
// Optional, default to success.
Authorizator func(data interface{}, c *faygo.Context) bool
// Callback function that will be called during Login.
// Using this function it is possible to add additional payload data to the webtoken.
// The data is then made available during requests via c.Get("JWT_PAYLOAD").
// Note that the payload is not encrypted.
// The attributes mentioned on jwt.io can't be used as keys for the map.
// Optional, by default no additional data will be set.
PayloadFunc func(data interface{}) map[string]interface{}
Unauthorized func(*faygo.Context, int, string)
// User can define own LoginResponse func.
LoginResponse func(*faygo.Context, int, string, time.Time) error
// User can define own RefreshResponse func.
RefreshResponse func(*faygo.Context, int, string, time.Time) error
// Set the identity handler function
IdentityHandler func(jwt.MapClaims) interface{}
// TokenLookup is a string in the form of "<source>:<name>" that is used
// to extract token from the request.
// Optional. Default value "header:Authorization".
// Possible values:
// - "header:<name>"
// - "query:<name>"
// - "cookie:<name>"
TokenLookup string
// TokenHeadName is a string in the header. Default value is "Bearer"
TokenHeadName string
// TimeFunc provides the current time. You can override it to use another time value. This is useful for testing or if your server uses a different time zone than your tokens.
TimeFunc func() time.Time
// HTTP Status messages for when something in the JWT middleware fails.
// Check error (e) to determine the appropriate error message.
HTTPStatusMessageFunc func(e error, c *faygo.Context) string
// Private key file for asymmetric algorithms
PrivKeyFile string
// Public key file for asymmetric algorithms
PubKeyFile string
// contains filtered or unexported fields
}
FaygoJWTMiddleware provides a Json-Web-Token authentication implementation. On failure, a 401 HTTP response is returned. On success, the wrapped middleware is called, and the userID is made available as c.Get("userID").(string). Users can get a token by posting a json request to LoginHandler. The token then needs to be passed in the Authentication header. Example: Authorization:Bearer XXX_TOKEN_XXX
func (*FaygoJWTMiddleware) LoginHandler ¶
func (mw *FaygoJWTMiddleware) LoginHandler(c *faygo.Context) error
LoginHandler can be used by clients to get a jwt token. Payload needs to be json in the form of {"username": "USERNAME", "password": "PASSWORD"}. Reply will be of the form {"token": "TOKEN"}.
func (*FaygoJWTMiddleware) MiddlewareFunc ¶
func (mw *FaygoJWTMiddleware) MiddlewareFunc() faygo.HandlerFunc
MiddlewareFunc makes FaygoJWTMiddleware implement the Middleware interface.
func (*FaygoJWTMiddleware) MiddlewareInit ¶
func (mw *FaygoJWTMiddleware) MiddlewareInit() error
MiddlewareInit initialize jwt configs.
func (*FaygoJWTMiddleware) RefreshHandler ¶
func (mw *FaygoJWTMiddleware) RefreshHandler(c *faygo.Context) error
RefreshHandler can be used to refresh a token. The token still needs to be valid on refresh. Shall be put under an endpoint that is using the FaygoJWTMiddleware. Reply will be of the form {"token": "TOKEN"}.
Source Files
¶
- jwt.go