alkasir: github.com/alkasir/alkasir/pkg/shared/jwtmw Index | Files

package jwtmw

import "github.com/alkasir/alkasir/pkg/shared/jwtmw"

Package jwt provides Json-Web-Token authentication for the go-json-rest framework

Index

Package Files

auth_jwt.go

func ExtractClaims Uses

func ExtractClaims(request *rest.Request) map[string]interface{}

Helper function to extract the JWT claims

type JWTMiddleware Uses

type JWTMiddleware 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.
    Authenticator func(userId string, password string) 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(userId string, request *rest.Request) 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 request.Env["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(userId string) map[string]interface{}
}

JWTMiddleware 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 request.Env["REMOTE_USER"].(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 (*JWTMiddleware) LoginHandler Uses

func (mw *JWTMiddleware) LoginHandler(writer rest.ResponseWriter, request *rest.Request)

Handler that clients can use 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 (*JWTMiddleware) MiddlewareFunc Uses

func (mw *JWTMiddleware) MiddlewareFunc(handler rest.HandlerFunc) rest.HandlerFunc

MiddlewareFunc makes JWTMiddleware implement the Middleware interface.

func (*JWTMiddleware) RefreshHandler Uses

func (mw *JWTMiddleware) RefreshHandler(writer rest.ResponseWriter, request *rest.Request)

Handler that clients can use to refresh their token. The token still needs to be valid on refresh. Shall be put under an endpoint that is using the JWTMiddleware. Reply will be of the form {"token": "TOKEN"}.

Package jwtmw imports 9 packages (graph) and is imported by 1 packages. Updated 2016-08-14. Refresh now. Tools for package owners.