Skip to content

itzamna314/gin-jwt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gin-jwt

JWT Middleware for Gin http framework. Validates the token, and adds the claims payload into the request context.

Usage

package main

import (
	"github.com/dgrijalva/jwt-go"
	"github.com/gin-gonic/gin"
	"github.com/itzamna314/gin-jwt/jwtauth"
)

const superSecretKey = "CAFEBEEF"

func main() {
	validator := jwtauth.Validator{
		Key:      []byte(superSecretKey),
		Method:   jwt.SigningMethodHS256,
		Location: new(string),
	}
	*validator.Location = realm
	r := gin.New()

	r.POST("/tokens", makeToken)
	r.GET("/private", validator.Middleware(), privateHandler)

	r.Run()
}

func privateHandler(c *gin.Context) {
	claims := c.MustGet("claims").(jwt.MapClaims)
	
	// Verify that the claims grant access to this endpoint...

	c.JSON(200, gin.H{
		"secretData": "Illuminati!",
	})
}

Get

  • go get github.com/itzamna314/gin-jwt

Docs

https://godoc.org/github.com/itzamna314/gin-jwt

Based On:

About

JWT Middleware for Gin http framework

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages