Example #1
0
// LoginUser TODO: document
func LoginUser(db AbstractDB, user string, expiration int) (string, error) {
	token, err := utils.NewToken()
	if err != nil {
		return "", err
	}
	err = db.SetAuthToken(token, user, expiration)
	if err != nil {
		return "", err
	}
	return token, err
}
Example #2
0
func TestLogoutHandler(t *testing.T) {
	DB = &auth.MockDB{TokenNamespace: make(map[string]string),
		UserNamespace: make(map[string][]byte)}
	token, _ := utils.NewToken()

	DB.SetAuthToken(token, "test user", 6969)
	req, _ := http.NewRequest("GET", "http://example.com/foo", nil)
	req.Header.Set("Authorization", token)
	w := httptest.NewRecorder()
	LogOutHandler(w, req)

	if w.Code != http.StatusOK {
		t.Errorf("expected error code of %d got: %d\n", http.StatusOK, w.Code)
	}

	if usr, _ := DB.GetUserFromToken(token); usr != "" {
		t.Errorf("expected %s to no longer be associated with token: %s", usr, token)
	}
}
Example #3
0
func TestTokenAuth(t *testing.T) {
	// test data
	user := "******"
	token, _ := utils.NewToken()
	// mock databse
	db := &MockDB{TokenNamespace: make(map[string]string),
		UserNamespace: make(map[string][]byte)}
	// inject test data into mock db
	db.TokenNamespace[token] = user
	// check if db contains token
	userBack, err := TokenAuth(db, token)
	if err != nil {
		t.Errorf("TestTokenAuth caused by TokenAuth: %v", err)
		t.FailNow()
	}
	if userBack != user {
		t.Errorf("expected token to return: '%s', got '%s'", user, userBack)
		t.FailNow()
	}
	log.Println("*auth_test.TestTokenAuth passed*")
}
func getUserAndToken() (user, token string) {
	token, _ = utils.NewToken()
	token = "test:" + token
	user = "******"
	return
}