Documentation ¶
Index ¶
- func GetClaims(r *http.Request) (map[string]interface{}, bool)
- func HMAC(k []byte) func(*Options)
- func New(fns ...func(*Options)) janice.MiddlewareFunc
- func Optional(o *Options)
- func RSA(k *rsa.PublicKey) func(*Options)
- func WithClaims(r *http.Request, c map[string]interface{}) *http.Request
- type Options
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func New ¶
func New(fns ...func(*Options)) janice.MiddlewareFunc
New returns new JWT middleware for the specified option funcs
Example ¶
package main import ( "fmt" "net/http" "net/http/httptest" "time" jwtgo "github.com/golang-jwt/jwt" jwt "github.com/stevecallear/janice-jwt" ) func main() { key := []byte("secretkey") claims := map[string]interface{}{ "sub": "test@email.com", "exp": time.Now().UTC().Add(1 * time.Hour), } token, err := jwtgo.NewWithClaims(jwtgo.SigningMethodHS512, jwtgo.MapClaims(claims)).SignedString(key) if err != nil { panic(err) } auth := jwt.New(jwt.HMAC(key)) handler := auth.Then(func(w http.ResponseWriter, r *http.Request) error { c, _ := jwt.GetClaims(r) _, err := fmt.Fprint(w, c["sub"].(string)) return err }) rec := httptest.NewRecorder() req := httptest.NewRequest("GET", "/", nil) req.Header.Add("Authorization", "Bearer "+token) handler.ServeHTTP(rec, req) fmt.Printf("%d %s", rec.Code, rec.Body) }
Output: 200 test@email.com
func Optional ¶
func Optional(o *Options)
Optional configures the middleware to allow unauthorized requests
Types ¶
Click to show internal directories.
Click to hide internal directories.