func TestWithParseAndValidateSuccess(t *testing.T) { jm, err := ctxjwt.NewService() assert.NoError(t, err) theToken, _, err := jm.GenerateToken(map[string]interface{}{ "xfoo": "bar", "zfoo": 4711, }) assert.NoError(t, err) assert.NotEmpty(t, theToken) req, err := http.NewRequest("GET", "http://auth.xyz", nil) assert.NoError(t, err) ctxjwt.SetHeaderAuthorization(req, theToken) finalHandler := ctxhttp.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error { w.WriteHeader(http.StatusTeapot) fmt.Fprintf(w, "I'm more of a coffee pot") ctxToken, err := ctxjwt.FromContext(ctx) assert.NoError(t, err) assert.NotNil(t, ctxToken) assert.Exactly(t, "bar", ctxToken.Claims["xfoo"].(string)) return nil }) authHandler := jm.WithParseAndValidate()(finalHandler) wRec := httptest.NewRecorder() assert.NoError(t, authHandler.ServeHTTPContext(context.Background(), wRec, req)) assert.Equal(t, http.StatusTeapot, wRec.Code) assert.Equal(t, `I'm more of a coffee pot`, wRec.Body.String()) }
func TestWithParseAndValidateInBlackList(t *testing.T) { bl := &testRealBL{} jm, err := ctxjwt.NewService( ctxjwt.WithBlacklist(bl), ) assert.NoError(t, err) theToken, _, err := jm.GenerateToken(nil) bl.token = theToken assert.NoError(t, err) assert.NotEmpty(t, theToken) req, err := http.NewRequest("GET", "http://auth.xyz", nil) assert.NoError(t, err) ctxjwt.SetHeaderAuthorization(req, theToken) finalHandler := ctxhttp.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error { w.WriteHeader(http.StatusTeapot) return nil }) authHandler := jm.WithParseAndValidate()(finalHandler) wRec := httptest.NewRecorder() assert.NoError(t, authHandler.ServeHTTPContext(context.Background(), wRec, req)) assert.NotEqual(t, http.StatusTeapot, wRec.Code) assert.Equal(t, http.StatusUnauthorized, wRec.Code) }
func bmServeHTTP(b *testing.B, opts ...ctxjwt.Option) { service, err := ctxjwt.NewService(opts...) if err != nil { b.Error(err) } token, _, err := service.GenerateToken(map[string]interface{}{ "xfoo": "bar", "zfoo": 4711, }) if err != nil { b.Error(err) } final := ctxhttp.HandlerFunc(func(_ context.Context, w http.ResponseWriter, _ *http.Request) error { w.WriteHeader(http.StatusTeapot) return nil }) jwtHandler := service.WithParseAndValidate()(final) req, err := http.NewRequest("GET", "http://abc.xyz", nil) if err != nil { b.Error(err) } ctxjwt.SetHeaderAuthorization(req, token) w := httptest.NewRecorder() ctx := context.Background() b.ReportAllocs() b.ResetTimer() for i := 0; i < b.N; i++ { if err := jwtHandler.ServeHTTPContext(ctx, w, req); err != nil { b.Error(err) } if w.Code != http.StatusTeapot { b.Errorf("Response Code want %d; have %d", http.StatusTeapot, w.Code) } } }
func ExampleWithInitStoreByToken() { initStore() ctx := store.NewContextReader(context.Background(), testStoreService) jwtService, err := ctxjwt.NewService(ctxjwt.WithPassword([]byte(`GÒph3r`))) finalHandler := ctxhttp.Chain( ctxhttp.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error { _, haveReqStore, err := store.FromContextReader(ctx) if err != nil { return err } // now we know that the current request depends on the store view DE. fmt.Fprintf(w, "StoreCode: %s\n", haveReqStore.StoreCode()) return nil }), // executed 3rd store.WithInitStoreByToken(), // executed 2nd jwtService.WithParseAndValidate(), // executed 1st ) ts := httptest.NewServer(ctxhttp.NewAdapter(ctx, finalHandler)) defer ts.Close() // Setup GET request token, _, err := jwtService.GenerateToken( map[string]interface{}{ // Despite default store for Website ID 1 is AT we are currently // in the store context of DE. store.ParamName: "de", }, ) if err != nil { log.Fatal("jwtService.GenerateToken", "err", err) } req, err := http.NewRequest("GET", ts.URL, nil) if err != nil { log.Fatal("http.Get", "err", err) } ctxjwt.SetHeaderAuthorization(req, token) res, err := http.DefaultClient.Do(req) if err != nil { log.Fatal("http.DefaultClient.Do", "err", err) } response, err := ioutil.ReadAll(res.Body) if errC := res.Body.Close(); errC != nil { log.Fatal("res.Body.Close", "err", errC) } if err != nil { log.Fatal("ioutil.ReadAll", "err", err) } fmt.Printf("Response: %s\n", response) fmt.Printf("Log: %s\n", testDebugLogBuf.String()) // Output: // Response: StoreCode: de // // Log: }