Skip to content

Assymmetric key validation should have to use private key file #236

@JamesPiggott

Description

@JamesPiggott

I am trying to implement an asymmetric validation middleware that uses RS256. In theory this should only need the public key to verify the JWT, but not including the private key does not work. I do not want to include the private key with the project. Below is my implementation.
`
authMiddleware, _ := app.New(&app.GinJWTMiddleware {

	Realm:            "zone",
	SigningAlgorithm: "RS256",
	PrivKeyFile:      "keys/jwtRS256.key",
	PubKeyFile:       "keys/jwtRS256.key.pub",

	PayloadFunc: func(data interface{}) app.MapClaims {
		if v, ok := data.(*JWTToken); ok {
			return app.MapClaims{
				userName: v.UserName,
			}
		}
		return app.MapClaims{}
	},

	IdentityHandler: func(c *gin.Context) interface{} {
		claims := app.ExtractClaims(c)
		return &JWTToken{
			UserName:  claims[userName].(string),
			TokenType: claims[tokenType].(string),
		}
	},

	Authorizator: func(data interface{}, c *gin.Context) bool {
		if v, ok := data.(*JWTToken); ok && v.UserName == "admin" {
			return true
		}

		return false
	},

	Unauthorized: func(c *gin.Context, code int, message string) {
		c.JSON(code, gin.H{
			"code":    code,
			"message": message,
		})
	},

	TokenLookup:   "header: Authorization, query: token, cookie: jwt",
	TokenHeadName: "Bearer",
	TimeFunc:      time.Now,
})`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions