/go-json-rest-middleware-jwt

JWT auth middleware for the go-json-rest framework

Primary LanguageGoMIT LicenseMIT

This is a fork of StephanDollberg/go-json-rest-middleware-jwt with the intent to return more detailed auth errors in the JSON return data, including errors from dgrijalva/jwt-go.

Example return from curl:

>curl -i -H "Authorization:Bearer eyJhbGci ...... wMzM"  http://localhost:8080/somejwt/login

HTTP/1.1 401 Unauthorized
Content-Type: application/json
Www-Authenticate: JWT realm=HolyRealm
X-Powered-By: go-json-rest
Date: Mon, 07 Sep 2015 02:35:41 GMT
Content-Length: 103

{
  "Error": "Not Authorized",
  "JwtValidationCode": 8,
  "JwtValidationMessage": "token is expired"
}

Its considered alpha, the functionality is implemented but no testcases yet. Debug Printf's need to be removed - but if you try it, it should not burn down your house.

Update 18.9.2015

Its considered beta now - debug printfs have been removed, error messages have been moved into

if debug ..

statements.

Example jwt middleware init:

jwt_middleware = &jwt.JWTMiddleware{
	Key:        []byte("THIS SECRET STRING"),
	Realm:      "THISREALM",
	DebugLevel: 3,
	Timeout:    time.Hour,
	MaxRefresh: time.Hour * 24,
	Authenticator: func(username string, password string) bool {
		if username == "admin" && password == "admin" {
			return true
		}
	},
	Authorizator: func(username string, request *rest.Request) bool {
		return true
	},
	// Payload / claims
	PayloadFunc: func(userId string) map[string]interface{} {
		claims := make(map[string]interface{})
		claims["UserLevel"] = "9001"
		return claims
	},
}

The following text is from the original: StephanDollberg/go-json-rest-middleware-jwt

JWT Middleware for Go-Json-Rest

godoc license

This is a middleware for Go-Json-Rest.

It uses jwt-go to provide a jwt authentication middleware. It provides additional handler functions to provide the login api that will generate the token and an additional refresh handler that can be used to refresh tokens.

An example can be found in the Go-Json-Rest Examples repo.