-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: readme examples * fix negroni at readme * Update README.md * add http example readme
- Loading branch information
Showing
3 changed files
with
145 additions
and
86 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# HTTP example | ||
|
||
This is an example of how to use the http middleware. | ||
|
||
# Using it | ||
|
||
To try this out, first install all dependencies with `go install` and then run `go run main.go` to start the app. | ||
|
||
* Call `http://localhost:3000` with a JWT signed with `My Secret` to get a response back. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
jwtmiddleware "github.com/auth0/go-jwt-middleware" | ||
"github.com/form3tech-oss/jwt-go" | ||
) | ||
|
||
var myHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
user := r.Context().Value("user") | ||
fmt.Fprintf(w, "This is an authenticated request") | ||
fmt.Fprintf(w, "Claim content:\n") | ||
for k, v := range user.(*jwt.Token).Claims.(jwt.MapClaims) { | ||
fmt.Fprintf(w, "%s :\t%#v\n", k, v) | ||
} | ||
}) | ||
|
||
func main() { | ||
jwtMiddleware := jwtmiddleware.New(jwtmiddleware.Options{ | ||
ValidationKeyGetter: func(token *jwt.Token) (interface{}, error) { | ||
return []byte("My Secret"), nil | ||
}, | ||
// When set, the middleware verifies that tokens are signed with the specific signing algorithm | ||
// If the signing method is not constant the ValidationKeyGetter callback can be used to implement additional checks | ||
// Important to avoid security issues described here: https://auth0.com/blog/critical-vulnerabilities-in-json-web-token-libraries/ | ||
SigningMethod: jwt.SigningMethodHS256, | ||
}) | ||
|
||
app := jwtMiddleware.Handler(myHandler) | ||
http.ListenAndServe("0.0.0.0:3000", app) | ||
} |