Documentation ¶
Index ¶
Examples ¶
Constants ¶
View Source
const ( // AuthenticateHeader the Gin authenticate header AuthenticateHeader = "WWW-Authenticate" // AuthorizationHeader the auth header that gets past to all services AuthorizationHeader = "Authentication" // ForwardSlash Forward slash character ForwardSlash = "/" // HEADER used by the JWT middle ware HEADER = "header" // IssuerFieldName the issuer field name IssuerFieldName = "iss" // ContextToken the token key ContextToken = "JWT_TOKEN" )
Variables ¶
View Source
var ( // AuthHeaderEmptyError thrown when an empty Authorization header is received AuthHeaderEmptyError = errors.New("auth header empty") // InvalidAuthHeaderError thrown when an invalid Authorization header is received InvalidAuthHeaderError = errors.New("invalid auth header") )
Functions ¶
This section is empty.
Types ¶
type AuthMiddleware ¶
type AuthMiddleware struct { gin.Context, int, string) Timeout time.Duration // TokenLookup the header name of the token TokenLookup string // TimeFunc TimeFunc func() time.Time // Realm name to display to the user. Required. Realm string // to verify issuer VerifyIssuer bool // Region aws region Region string // UserPoolID the cognito user pool id UserPoolID string // The issuer Iss string // JWK public JSON Web Key (JWK) for your user pool JWK map[string]JWKKey }Unauthorized func(*
AuthMiddleware middleware
Example ¶
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() }
Output:
func AuthJWTMiddleware ¶
func AuthJWTMiddleware(userPoolID, region string) (*AuthMiddleware, error)
AuthJWTMiddleware create an instance of the middle ware function
func (*AuthMiddleware) MiddlewareFunc ¶
func (mw *AuthMiddleware) MiddlewareFunc() gin.HandlerFunc
MiddlewareFunc implements the Middleware interface.
func (*AuthMiddleware) MiddlewareInit ¶
func (mw *AuthMiddleware) MiddlewareInit()
MiddlewareInit initialize jwt configs.
Click to show internal directories.
Click to hide internal directories.