-
-
Save durango/6c4ba0600ef6f60641ebd790110b71e9 to your computer and use it in GitHub Desktop.
JWT middleware
This file contains hidden or 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