Beispiel #1
0
func testUser() *models.Person {
	p := &models.Person{
		AboutMe: "nut cracker",
	}
	query.Create(p)
	return p
}
Beispiel #2
0
func (db *PGStore) save(session *sessions.Session) error {
	encoded, err := securecookie.EncodeMulti(session.Name(), session.Values,
		db.Codecs...)

	if err != nil {
		return err
	}

	var expiresOn time.Time

	exOn := session.Values["expires_on"]

	if exOn == nil {
		expiresOn = time.Now().Add(time.Second * time.Duration(session.Options.MaxAge))
	} else {
		expiresOn = exOn.(time.Time)
		if expiresOn.Sub(time.Now().Add(time.Second*time.Duration(session.Options.MaxAge))) < 0 {
			expiresOn = time.Now().Add(time.Second * time.Duration(session.Options.MaxAge))
		}
	}
	s := &models.Session{
		Key:       session.ID,
		Data:      encoded,
		ExpiresOn: expiresOn,
	}
	if session.IsNew {
		return query.Create(s)
	}
	return query.UpdateSession(s)
}
Beispiel #3
0
func TestNewJWTAuth(t *testing.T) {
	e := echo.New()
	privateKey, err := ioutil.ReadFile("test/sample_key")
	if err != nil {
		t.Fatal(err)
	}
	publicKey, err := ioutil.ReadFile("test/sample_key.pub")
	if err != nil {
		t.Fatal(err)
	}
	holder := NewJWTKeys(privateKey, publicKey)
	e.Use(middleware.JWTAuth(NewJWTAuth(holder)))
	e.Get("/", home)

	claims := map[string]string{
		"user":       "******",
		"occupation": "dreamer",
	}
	tok, err := NewToken(holder, claims)
	if err != nil {
		t.Error(err)
	}
	err = query.Create(tok)
	if err != nil {
		t.Error(err)
	}
	r, err := http.NewRequest("GET", "/", nil)
	if err != nil {
		t.Error(err)
	}
	r.Header.Set("Authorization", fmt.Sprintf("Bearer %v", tok.Key))

	w := httptest.NewRecorder()
	e.ServeHTTP(w, r)
	if !strings.Contains(w.Body.String(), "dreamer") {
		t.Errorf("expected %s to contain dreamer", w.Body.String())
	}
}