README
¶
JWT Middleware for Gin Framework
This is a middleware for Gin framework.
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.
Install
v2 version
Install gin-gwt v2 version for jwt-go
v3 version. To get the package, execute:
$ go get gopkg.in/appleboy/gin-jwt.v2
To import this package, add the following line to your code:
import "gopkg.in/appleboy/gin-jwt.v2"
v1 version
Install gin-gwt v1 version for jwt-go
v2 version. To get the package, execute:
$ go get gopkg.in/appleboy/gin-jwt.v1
To import this package, add the following line to your code:
import "gopkg.in/appleboy/gin-jwt.v1"
Example
Please see server example file.
package main
import (
"net/http"
"os"
"time"
"github.com/appleboy/gin-jwt"
"github.com/gin-gonic/gin"
)
func helloHandler(c *gin.Context) {
c.JSON(200, gin.H{
"text": "Hello World.",
})
}
func main() {
port := os.Getenv("PORT")
r := gin.New()
r.Use(gin.Logger())
r.Use(gin.Recovery())
if port == "" {
port = "8000"
}
// the jwt middleware
authMiddleware := &jwt.GinJWTMiddleware{
Realm: "test zone",
Key: []byte("secret key"),
Timeout: time.Hour,
MaxRefresh: time.Hour,
Authenticator: func(userId string, password string, c *gin.Context) (string, bool) {
if (userId == "admin" && password == "admin") || (userId == "test" && password == "test") {
return userId, true
}
return userId, false
},
Authorizator: func(userId string, c *gin.Context) bool {
if userId == "admin" {
return true
}
return false
},
Unauthorized: func(c *gin.Context, code int, message string) {
c.JSON(code, gin.H{
"code": code,
"message": message,
})
},
// 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: "header:Authorization",
// TokenLookup: "query:token",
// TokenLookup: "cookie:token",
// TokenHeadName is a string in the header. Default value is "Bearer"
TokenHeadName: "Bearer",
// 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: time.Now,
}
r.POST("/login", authMiddleware.LoginHandler)
auth := r.Group("/auth")
auth.Use(authMiddleware.MiddlewareFunc())
{
auth.GET("/hello", helloHandler)
auth.GET("/refresh_token", authMiddleware.RefreshHandler)
}
http.ListenAndServe(":"+port, r)
}
Demo
Please run example/server.go file and listen 8000
port.
$ go run example/server.go
Download and install httpie CLI HTTP client.
Login API:
$ http -v --json POST localhost:8000/auth/login username=admin password=admin
Output screenshot
Refresh token API:
$ http -v -f GET localhost:8000/auth/refresh_token "Authorization:Bearer xxxxxxxxx" "Content-Type: application/json"
Output screenshot
Hello world
Please login as admin
and password as admin
$ http -f GET localhost:8000/auth/hello "Authorization:Bearer xxxxxxxxx" "Content-Type: application/json"
Response message 200 OK
:
HTTP/1.1 200 OK
Content-Length: 24
Content-Type: application/json; charset=utf-8
Date: Sat, 19 Mar 2016 03:02:57 GMT
{
"text": "Hello World."
}
Authorization
Please login as test
and password as test
$ http -f GET localhost:8000/auth/hello "Authorization:Bearer xxxxxxxxx" "Content-Type: application/json"
Response message 403 Forbidden
:
HTTP/1.1 403 Forbidden
Content-Length: 62
Content-Type: application/json; charset=utf-8
Date: Sat, 19 Mar 2016 03:05:40 GMT
Www-Authenticate: JWT realm=test zone
{
"code": 403,
"message": "You don't have permission to access."
}
Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExtractClaims ¶
ExtractClaims help to extract the JWT claims
Types ¶
type GinJWTMiddleware ¶
type GinJWTMiddleware 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 id, if so, user id will be stored in Claim Array. Authenticator func(userID string, password string, c *gin.Context) (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, c *gin.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(userID string) map[string]interface{} Unauthorized func(*gin.Context, int, string) // Set the identity handler function IdentityHandler func(jwt.MapClaims) string // 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 }
GinJWTMiddleware 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 (*GinJWTMiddleware) LoginHandler ¶
func (mw *GinJWTMiddleware) LoginHandler(c *gin.Context)
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 (*GinJWTMiddleware) MiddlewareFunc ¶
func (mw *GinJWTMiddleware) MiddlewareFunc() gin.HandlerFunc
MiddlewareFunc makes GinJWTMiddleware implement the Middleware interface.
func (*GinJWTMiddleware) MiddlewareInit ¶
func (mw *GinJWTMiddleware) MiddlewareInit() error
MiddlewareInit initialize jwt configs.
func (*GinJWTMiddleware) RefreshHandler ¶
func (mw *GinJWTMiddleware) RefreshHandler(c *gin.Context)
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 GinJWTMiddleware. Reply will be of the form {"token": "TOKEN"}.
func (*GinJWTMiddleware) TokenGenerator ¶
func (mw *GinJWTMiddleware) TokenGenerator(userID string) string
TokenGenerator handler that clients can use to get a jwt token.