Skip to content

Commit

Permalink
docs and example
Browse files Browse the repository at this point in the history
  • Loading branch information
talpert committed May 2, 2018
1 parent 52e86fb commit cadc967
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,15 @@ 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 |


### 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!
Expand Down
15 changes: 13 additions & 2 deletions example/rye_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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")

Expand Down

0 comments on commit cadc967

Please sign in to comment.