Skip to content
This repository was archived by the owner on Dec 23, 2024. It is now read-only.

Commit cadc967

Browse files
committed
docs and example
1 parent 52e86fb commit cadc967

2 files changed

Lines changed: 15 additions & 4 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,15 +226,15 @@ the example using Gorilla:
226226
| [Access Token](middleware_accesstoken.go) | Provide Access Token validation |
227227
| [CIDR](middleware_cidr.go) | Provide request IP whitelisting |
228228
| [CORS](middleware_cors.go) | Provide CORS functionality for routes |
229-
| [JWT](middleware_jwt.go) | Provide JWT validation |
229+
| [Auth](middleware_auth.go) | Provide Authorization header validation (basic auth, JWT) |
230230
| [Route Logger](middleware_routelogger.go) | Provide basic logging for a specific route |
231231
| [Static File](middleware_static_file.go) | Provides serving a single file |
232232
| [Static Filesystem](middleware_static_filesystem.go) | Provides serving a single file |
233233

234234

235235
### A Note on the JWT Middleware
236236

237-
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:
237+
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:
238238
```go
239239
func getJWTfromContext(rw http.ResponseWriter, r *http.Request) *rye.Response {
240240
// Retrieving the value is easy!

example/rye_example.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77
"net/http"
88

99
"github.com/InVisionApp/rye"
10-
log "github.com/sirupsen/logrus"
1110
"github.com/cactus/go-statsd-client/statsd"
1211
"github.com/gorilla/mux"
12+
log "github.com/sirupsen/logrus"
1313
)
1414

1515
func main() {
@@ -41,11 +41,22 @@ func main() {
4141
homeHandler,
4242
})).Methods("GET", "OPTIONS")
4343

44+
// If you perform an `curl -i http://localhost:8181/jwt \
45+
// -H "Authorization: Basic dXNlcjE6cGFzczEK"
46+
// you will see that we are allowed through to the handler, if the header is changed, you will get a 401
47+
routes.Handle("/basic-auth", middlewareHandler.Handle([]rye.Handler{
48+
rye.NewMiddlewareAuth(rye.NewBasicAuthFunc(map[string]string{
49+
"user1": "pass1",
50+
"user2": "pass2",
51+
})),
52+
getJwtFromContextHandler,
53+
})).Methods("GET")
54+
4455
// If you perform an `curl -i http://localhost:8181/jwt \
4556
// -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ"
4657
// you will see that we are allowed through to the handler, if the sample token is changed, we will get a 401
4758
routes.Handle("/jwt", middlewareHandler.Handle([]rye.Handler{
48-
rye.NewMiddlewareJWT("secret"),
59+
rye.NewMiddlewareAuth(rye.NewJWTAuthFunc("secret")),
4960
getJwtFromContextHandler,
5061
})).Methods("GET")
5162

0 commit comments

Comments
 (0)