2019-08-29 02:34:07 -07:00
|
|
|
package home
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
2019-10-11 02:41:01 -07:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2019-08-29 02:34:07 -07:00
|
|
|
"os"
|
2019-10-02 07:02:16 -07:00
|
|
|
"path/filepath"
|
2019-08-29 02:34:07 -07:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2019-10-11 02:41:01 -07:00
|
|
|
func prepareTestDir() string {
|
|
|
|
const dir = "./agh-test"
|
|
|
|
_ = os.RemoveAll(dir)
|
|
|
|
_ = os.MkdirAll(dir, 0755)
|
|
|
|
return dir
|
|
|
|
}
|
2019-10-02 07:02:16 -07:00
|
|
|
|
2019-10-11 02:41:01 -07:00
|
|
|
func TestAuth(t *testing.T) {
|
|
|
|
dir := prepareTestDir()
|
|
|
|
defer func() { _ = os.RemoveAll(dir) }()
|
|
|
|
fn := filepath.Join(dir, "sessions.db")
|
2019-10-02 07:02:16 -07:00
|
|
|
|
2019-08-29 02:34:07 -07:00
|
|
|
users := []User{
|
|
|
|
User{Name: "name", PasswordHash: "$2y$05$..vyzAECIhJPfaQiOK17IukcQnqEgKJHy0iETyYqxn3YXJl8yZuo2"},
|
|
|
|
}
|
2019-11-12 04:23:00 -07:00
|
|
|
a := InitAuth(fn, nil, 60)
|
2019-10-21 07:44:07 -07:00
|
|
|
s := session{}
|
2019-08-29 02:34:07 -07:00
|
|
|
|
2019-10-11 02:41:01 -07:00
|
|
|
user := User{Name: "name"}
|
|
|
|
a.UserAdd(&user, "password")
|
2019-08-29 02:34:07 -07:00
|
|
|
|
|
|
|
assert.True(t, a.CheckSession("notfound") == -1)
|
|
|
|
a.RemoveSession("notfound")
|
|
|
|
|
|
|
|
sess := getSession(&users[0])
|
|
|
|
sessStr := hex.EncodeToString(sess)
|
|
|
|
|
2019-10-21 07:44:07 -07:00
|
|
|
now := time.Now().UTC().Unix()
|
2019-08-29 02:34:07 -07:00
|
|
|
// check expiration
|
2019-10-21 07:44:07 -07:00
|
|
|
s.expire = uint32(now)
|
|
|
|
a.addSession(sess, &s)
|
2019-08-29 02:34:07 -07:00
|
|
|
assert.True(t, a.CheckSession(sessStr) == 1)
|
|
|
|
|
|
|
|
// add session with TTL = 2 sec
|
2019-10-21 07:44:07 -07:00
|
|
|
s = session{}
|
2020-04-24 04:04:36 -07:00
|
|
|
s.expire = uint32(time.Now().UTC().Unix() + 2)
|
2019-10-21 07:44:07 -07:00
|
|
|
a.addSession(sess, &s)
|
2019-08-29 02:34:07 -07:00
|
|
|
assert.True(t, a.CheckSession(sessStr) == 0)
|
|
|
|
|
|
|
|
a.Close()
|
|
|
|
|
|
|
|
// load saved session
|
2019-11-12 04:23:00 -07:00
|
|
|
a = InitAuth(fn, users, 60)
|
2019-08-29 02:34:07 -07:00
|
|
|
|
|
|
|
// the session is still alive
|
|
|
|
assert.True(t, a.CheckSession(sessStr) == 0)
|
2019-10-21 07:44:07 -07:00
|
|
|
// reset our expiration time because CheckSession() has just updated it
|
2020-04-24 04:04:36 -07:00
|
|
|
s.expire = uint32(time.Now().UTC().Unix() + 2)
|
2019-10-21 07:44:07 -07:00
|
|
|
a.storeSession(sess, &s)
|
2019-08-29 02:34:07 -07:00
|
|
|
a.Close()
|
|
|
|
|
|
|
|
u := a.UserFind("name", "password")
|
|
|
|
assert.True(t, len(u.Name) != 0)
|
|
|
|
|
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
|
|
|
|
// load and remove expired sessions
|
2019-11-12 04:23:00 -07:00
|
|
|
a = InitAuth(fn, users, 60)
|
2019-08-29 02:34:07 -07:00
|
|
|
assert.True(t, a.CheckSession(sessStr) == -1)
|
|
|
|
|
|
|
|
a.Close()
|
|
|
|
os.Remove(fn)
|
|
|
|
}
|
2019-10-11 02:41:01 -07:00
|
|
|
|
|
|
|
// implements http.ResponseWriter
|
|
|
|
type testResponseWriter struct {
|
|
|
|
hdr http.Header
|
|
|
|
statusCode int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *testResponseWriter) Header() http.Header {
|
|
|
|
return w.hdr
|
|
|
|
}
|
|
|
|
func (w *testResponseWriter) Write([]byte) (int, error) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
func (w *testResponseWriter) WriteHeader(statusCode int) {
|
|
|
|
w.statusCode = statusCode
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthHTTP(t *testing.T) {
|
|
|
|
dir := prepareTestDir()
|
|
|
|
defer func() { _ = os.RemoveAll(dir) }()
|
|
|
|
fn := filepath.Join(dir, "sessions.db")
|
|
|
|
|
|
|
|
users := []User{
|
|
|
|
User{Name: "name", PasswordHash: "$2y$05$..vyzAECIhJPfaQiOK17IukcQnqEgKJHy0iETyYqxn3YXJl8yZuo2"},
|
|
|
|
}
|
2020-02-13 08:42:07 -07:00
|
|
|
Context.auth = InitAuth(fn, users, 60)
|
2019-10-11 02:41:01 -07:00
|
|
|
|
|
|
|
handlerCalled := false
|
|
|
|
handler := func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
handlerCalled = true
|
|
|
|
}
|
|
|
|
handler2 := optionalAuth(handler)
|
|
|
|
w := testResponseWriter{}
|
|
|
|
w.hdr = make(http.Header)
|
|
|
|
r := http.Request{}
|
|
|
|
r.Header = make(http.Header)
|
|
|
|
r.Method = "GET"
|
|
|
|
|
|
|
|
// get / - we're redirected to login page
|
|
|
|
r.URL = &url.URL{Path: "/"}
|
|
|
|
handlerCalled = false
|
|
|
|
handler2(&w, &r)
|
|
|
|
assert.True(t, w.statusCode == http.StatusFound)
|
|
|
|
assert.True(t, w.hdr.Get("Location") != "")
|
|
|
|
assert.True(t, !handlerCalled)
|
|
|
|
|
|
|
|
// go to login page
|
|
|
|
loginURL := w.hdr.Get("Location")
|
|
|
|
r.URL = &url.URL{Path: loginURL}
|
|
|
|
handlerCalled = false
|
|
|
|
handler2(&w, &r)
|
|
|
|
assert.True(t, handlerCalled)
|
|
|
|
|
|
|
|
// perform login
|
2020-02-13 08:42:07 -07:00
|
|
|
cookie := Context.auth.httpCookie(loginJSON{Name: "name", Password: "password"})
|
2019-10-11 02:41:01 -07:00
|
|
|
assert.True(t, cookie != "")
|
|
|
|
|
|
|
|
// get /
|
|
|
|
handler2 = optionalAuth(handler)
|
|
|
|
w.hdr = make(http.Header)
|
|
|
|
r.Header.Set("Cookie", cookie)
|
|
|
|
r.URL = &url.URL{Path: "/"}
|
|
|
|
handlerCalled = false
|
|
|
|
handler2(&w, &r)
|
|
|
|
assert.True(t, handlerCalled)
|
|
|
|
r.Header.Del("Cookie")
|
|
|
|
|
|
|
|
// get / with basic auth
|
|
|
|
handler2 = optionalAuth(handler)
|
|
|
|
w.hdr = make(http.Header)
|
|
|
|
r.URL = &url.URL{Path: "/"}
|
|
|
|
r.SetBasicAuth("name", "password")
|
|
|
|
handlerCalled = false
|
|
|
|
handler2(&w, &r)
|
|
|
|
assert.True(t, handlerCalled)
|
|
|
|
r.Header.Del("Authorization")
|
|
|
|
|
|
|
|
// get login page with a valid cookie - we're redirected to /
|
|
|
|
handler2 = optionalAuth(handler)
|
|
|
|
w.hdr = make(http.Header)
|
|
|
|
r.Header.Set("Cookie", cookie)
|
|
|
|
r.URL = &url.URL{Path: loginURL}
|
|
|
|
handlerCalled = false
|
|
|
|
handler2(&w, &r)
|
|
|
|
assert.True(t, w.hdr.Get("Location") != "")
|
|
|
|
assert.True(t, !handlerCalled)
|
|
|
|
r.Header.Del("Cookie")
|
|
|
|
|
|
|
|
// get login page with an invalid cookie
|
|
|
|
handler2 = optionalAuth(handler)
|
|
|
|
w.hdr = make(http.Header)
|
|
|
|
r.Header.Set("Cookie", "bad")
|
|
|
|
r.URL = &url.URL{Path: loginURL}
|
|
|
|
handlerCalled = false
|
|
|
|
handler2(&w, &r)
|
|
|
|
assert.True(t, handlerCalled)
|
|
|
|
r.Header.Del("Cookie")
|
|
|
|
|
2020-02-13 08:42:07 -07:00
|
|
|
Context.auth.Close()
|
2019-10-11 02:41:01 -07:00
|
|
|
}
|