Skip to content

Instantly share code, notes, and snippets.

@denro
Created June 9, 2015 11:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save denro/9e190d6b59923b9d0338 to your computer and use it in GitHub Desktop.
Save denro/9e190d6b59923b9d0338 to your computer and use it in GitHub Desktop.
JWT middleware
func authenticateBearer(c *echo.Context) error {
// Look for an Authorization header
if ah := c.Request().Header.Get("Authorization"); ah != "" {
// Should be a bearer token
if len(ah) > 6 && ah[0:6] == "Bearer" {
// Parse rest of header and verify with the secret
if token, err := jwt.Parse(ah[7:], tokenSecret); err == nil && token.Valid {
c.Set("token", token)
return nil
}
}
}
return echo.NewHTTPError(401)
}
e:= echo.New()
e.Use(authenticateBearer)
e.Get("/endpoint", handler)
e.Run(":1234")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment