modulepackage
1.0.4
Repository: https://github.com/fghpdf/gin-jwt-cognito.git
Documentation: pkg.go.dev
# README
Gin Cognito JWT Authentication Middleware
This is a JWT auth Gin middleware to validate JWT token issued by AWS Cognito identity manager. The implementation of this middleware is based on the AWS documentation on how to verify the JWT token
Here is an example of how can this be invoked. It should be attached to all endpoint you would want to authenticate against the user.
package main
import (
ginjwt "github.com/akhettar/gin-jwt-cognito"
"github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt"
)
func main() {
// Creates a gin router with default middleware:
router := gin.Default()
// Create Cognito JWT auth middleware and set it in all authenticated endpoints
mw, err := ginjwt.AuthJWTMiddleware("<some_userpool_id>", "region")
if err != nil {
panic(err)
}
router.GET("/someGet", mw.MiddlewareFunc(), func(c *gin.Context) {
token := c.MustGet(ginjwt.ContextToken)
claims := token.(*jwt.Token).Claims.(jwt.MapClaims)
user := make([]string, 0)
if email, ok := claims["email"]; ok {
user = append(user, email.(string))
}
if username, ok := claims["username"]; ok {
user = append(user, username.(string))
}
c.JSON(200, gin.H{"user": user})
})
router.POST("/somePost", mw.MiddlewareFunc(), func(c *gin.Context) {
// some implementation
})
router.PUT("/somePut", mw.MiddlewareFunc(), func(c *gin.Context) {
// some implementation
})
// By default, it serves on :8080 unless a
// PORT environment variable was defined.
router.Run()
}
License
# Functions
AuthJWTMiddleware create an instance of the middle ware function.
# Constants
AuthenticateHeader the Gin authenticate header.
AuthorizationHeader the auth header that gets past to all services.
ContextToken the token key.
ForwardSlash Forward slash character.
HEADER used by the JWT middle ware.
IssuerFieldName the issuer field name.
# Variables
AuthHeaderEmptyError thrown when an empty Authorization header is received.
Error logger.
Info logger.
InvalidAuthHeaderError thrown when an invalid Authorization header is received.
Trace logger.
Warning logger.
# Structs
AuthError auth error response.
AuthMiddleware middleware.
JWK is json data struct for JSON Web Key.
JWKKey is json data struct for cognito jwk key.