diff --git a/README.md b/README.md index b0b88d3..d8dcb00 100644 --- a/README.md +++ b/README.md @@ -226,7 +226,7 @@ the example using Gorilla: | [Access Token](middleware_accesstoken.go) | Provide Access Token validation | | [CIDR](middleware_cidr.go) | Provide request IP whitelisting | | [CORS](middleware_cors.go) | Provide CORS functionality for routes | -| [JWT](middleware_jwt.go) | Provide JWT validation | +| [Auth](middleware_auth.go) | Provide Authorization header validation (basic auth, JWT) | | [Route Logger](middleware_routelogger.go) | Provide basic logging for a specific route | | [Static File](middleware_static_file.go) | Provides serving a single file | | [Static Filesystem](middleware_static_filesystem.go) | Provides serving a single file | @@ -234,7 +234,7 @@ the example using Gorilla: ### A Note on the JWT Middleware -The [JWT Middleware](middleware_jwt.go) pushes the JWT token onto the Context for use by other middlewares in the chain. This is a convenience that allows any part of your middleware chain quick access to the JWT. Example usage might include a middleware that needs access to your user id or email address stored in the JWT. To access this `Context` variable, the code is very simple: +The [JWT Middleware](middleware_auth.go) pushes the JWT token onto the Context for use by other middlewares in the chain. This is a convenience that allows any part of your middleware chain quick access to the JWT. Example usage might include a middleware that needs access to your user id or email address stored in the JWT. To access this `Context` variable, the code is very simple: ```go func getJWTfromContext(rw http.ResponseWriter, r *http.Request) *rye.Response { // Retrieving the value is easy! diff --git a/example/rye_example.go b/example/rye_example.go index a08b8a0..2a00927 100644 --- a/example/rye_example.go +++ b/example/rye_example.go @@ -7,9 +7,9 @@ import ( "net/http" "github.com/InVisionApp/rye" - log "github.com/sirupsen/logrus" "github.com/cactus/go-statsd-client/statsd" "github.com/gorilla/mux" + log "github.com/sirupsen/logrus" ) func main() { @@ -41,11 +41,22 @@ func main() { homeHandler, })).Methods("GET", "OPTIONS") + // If you perform an `curl -i http://localhost:8181/jwt \ + // -H "Authorization: Basic dXNlcjE6cGFzczEK" + // you will see that we are allowed through to the handler, if the header is changed, you will get a 401 + routes.Handle("/basic-auth", middlewareHandler.Handle([]rye.Handler{ + rye.NewMiddlewareAuth(rye.NewBasicAuthFunc(map[string]string{ + "user1": "pass1", + "user2": "pass2", + })), + getJwtFromContextHandler, + })).Methods("GET") + // If you perform an `curl -i http://localhost:8181/jwt \ // -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ" // you will see that we are allowed through to the handler, if the sample token is changed, we will get a 401 routes.Handle("/jwt", middlewareHandler.Handle([]rye.Handler{ - rye.NewMiddlewareJWT("secret"), + rye.NewMiddlewareAuth(rye.NewJWTAuthFunc("secret")), getJwtFromContextHandler, })).Methods("GET")