we are going to use Gin Web framework to create simple HTTP server that we could query against, passing JWT in the header and then using secret or public key to validate the signature.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package main
import(
"github.com/gin-gonic/gin"
)
funcAuthMiddleware() gin.HandlerFunc{
// In a real-world application, you would perform proper authentication here.
// For the sake of this example, we'll just check if an API key is present.
package main
import (
"github.com/gin-gonic/gin"
)
func AuthMiddleware() gin.HandlerFunc {
// In a real-world application, you would perform proper authentication here.
// For the sake of this example, we'll just check if an API key is present.
return func(c *gin.Context) {
apiKey := c.GetHeader("X-Auth-Token")
if apiKey == "" {
c.AbortWithStatusJSON(401, gin.H{"error": "Unauthorized"})
return
}
c.Next()
}
}
func main() {
// Create a new Gin router
router := gin.Default()
// Public routes (no authentication required)
public := router.Group("/public")
{
public.GET("/info", func(c *gin.Context) {
c.String(200, "Public information")
})
public.GET("/products", func(c *gin.Context) {
c.String(200, "Public product list")
})
}
// Private routes (require authentication)
private := router.Group("/private")
private.Use(AuthMiddleware())
{
private.GET("/data", func(c *gin.Context) {
c.String(200, "Private data accessible after authentication")
})
private.POST("/create", func(c *gin.Context) {
c.String(200, "Create a new resource")
})
}
router.POST("query", AuthMiddleware(), validateSession, returnData)
// Run the server on port 8080
router.Run(":8080")
}
package main
import (
"github.com/gin-gonic/gin"
)
func AuthMiddleware() gin.HandlerFunc {
// In a real-world application, you would perform proper authentication here.
// For the sake of this example, we'll just check if an API key is present.
return func(c *gin.Context) {
apiKey := c.GetHeader("X-Auth-Token")
if apiKey == "" {
c.AbortWithStatusJSON(401, gin.H{"error": "Unauthorized"})
return
}
c.Next()
}
}
func main() {
// Create a new Gin router
router := gin.Default()
// Public routes (no authentication required)
public := router.Group("/public")
{
public.GET("/info", func(c *gin.Context) {
c.String(200, "Public information")
})
public.GET("/products", func(c *gin.Context) {
c.String(200, "Public product list")
})
}
// Private routes (require authentication)
private := router.Group("/private")
private.Use(AuthMiddleware())
{
private.GET("/data", func(c *gin.Context) {
c.String(200, "Private data accessible after authentication")
})
private.POST("/create", func(c *gin.Context) {
c.String(200, "Create a new resource")
})
}
router.POST("query", AuthMiddleware(), validateSession, returnData)
// Run the server on port 8080
router.Run(":8080")
}
We added validateSession middleware that will decode the token and verify the signature.
Creating JWT services to decode and validate signature
We are using jwt GoLang library to decode the token, and validate the signature.
There are two ways to encode JWT: using symmetric encryption (meaning that the same secret is used to sign and validate the signature. This is done in retreiveTokenWithSymmetrikKey and retreiveTokenWithAsymmetrikKey is used to validate signature using the public key from the private/public key pair used to sign the token.
Make sure that you comment and uncomment the right type of token that you want to use: asymmetric vs symmetric.
Run the project yarn start end copy the long string printed right after SIGNED JWT.
Create new postman POST request
Open Postman and create new POST request. In the url put http://localhost:8080/query this is where our Gin Web server running.
Add X-Auth-Token JWT
Open header section, and add X-Auth-Token key with the value the JWT copied from Sign, Verify and decode JWT
Add query parameters and variables.
We are going to pass dummy parameters just for testing except for product
We are going to use product parameter to distinguish between symmetric and asymmetric tokens.
Let’s assume that our app will except symmetric tokens for web and asymmetric for app so make sure that you will pass the right JWT.
Navigate to the GraphQL section of the request, and add the query and the variables.