Created
June 9, 2015 11:34
-
-
Save denro/9e190d6b59923b9d0338 to your computer and use it in GitHub Desktop.
JWT middleware
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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