148 lines
5.3 KiB
Go
148 lines
5.3 KiB
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestSessionPolicyAdminUIAndCookie(t *testing.T) {
|
|
s, token, domain := configFixture(t)
|
|
form := url.Values{"action": {"save_session_policy"}, "session_minutes": {"60"}}
|
|
w := configRequest(s, token, "POST", "/-/sys/config/auth", form)
|
|
if w.Code != http.StatusSeeOther || w.Header().Get("Location") != "/-/sys/config?panel=auth-sessions&saved=1" {
|
|
t.Fatalf("policy save %d %s", w.Code, w.Body.String())
|
|
}
|
|
if s.store.AuthSessionLifetimeMinutes(domain) != 60 {
|
|
t.Fatal("policy not saved")
|
|
}
|
|
w = configRequest(s, token, "GET", "/-/sys/config?panel=auth-sessions", nil)
|
|
if w.Code != 200 || !strings.Contains(w.Body.String(), `name="session_minutes" value="60"`) {
|
|
t.Fatal("policy control missing")
|
|
}
|
|
for _, bad := range []string{"", "abc", "0", "99999999999999999999999"} {
|
|
form.Set("session_minutes", bad)
|
|
if w = configRequest(s, token, "POST", "/-/sys/config/auth", form); w.Code != 400 {
|
|
t.Fatalf("bad lifetime %q accepted", bad)
|
|
}
|
|
}
|
|
user, err := s.store.CreatePasswordAccount(domain, "reader", "a long test password", "Reader", "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
reader, err := s.store.CreateAuthSession(domain, user.UUID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
form.Set("session_minutes", "120")
|
|
if w = configRequest(s, reader, "POST", "/-/sys/config/auth", form); w.Code != 403 {
|
|
t.Fatalf("non-admin policy change: %d", w.Code)
|
|
}
|
|
w = httptest.NewRecorder()
|
|
r := httptest.NewRequest("GET", "https://localhost/", nil)
|
|
s.setAuthCookie(w, r, "fixture-token")
|
|
cookies := w.Result().Cookies()
|
|
if len(cookies) != 1 || cookies[0].MaxAge != 3600 || !cookies[0].HttpOnly || !cookies[0].Secure {
|
|
t.Fatalf("cookie policy %#v", cookies)
|
|
}
|
|
}
|
|
|
|
func TestTrustedSessionPolicyAndRevocation(t *testing.T) {
|
|
s, admin, domain := configFixture(t)
|
|
p := s.mustCLIPrincipal(t, domain, admin)
|
|
for _, days := range []string{"30", "7", "0"} {
|
|
form := url.Values{"action": {"save_trusted_session_policy"}, "trusted_session_days": {days}}
|
|
w := configRequest(s, admin, "POST", "/-/sys/config/auth", form)
|
|
if w.Code != 303 {
|
|
t.Fatalf("policy %s: %d %s", days, w.Code, w.Body.String())
|
|
}
|
|
want := 24 * time.Hour
|
|
if days == "30" {
|
|
want *= 30
|
|
}
|
|
if days == "7" {
|
|
want *= 7
|
|
}
|
|
for _, browser := range []bool{false, true} {
|
|
token, lifetime, err := s.store.CreateLoginSession(domain, p, 0, browser, true)
|
|
if err != nil || lifetime != want {
|
|
t.Fatalf("duration %v %v", lifetime, err)
|
|
}
|
|
if _, err = s.store.PrincipalForSession(domain, token); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = s.store.RevokeAuthSession(token); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = s.store.PrincipalForSession(domain, token); err == nil {
|
|
t.Fatal("revoked trusted session accepted")
|
|
}
|
|
}
|
|
w = httptest.NewRecorder()
|
|
s.handleAuth(w, httptest.NewRequest("GET", "https://localhost/_/auth/login", nil))
|
|
if strings.Contains(w.Body.String(), `id="auth-remember"`) != (days != "0") {
|
|
t.Fatal("login choice does not reflect policy")
|
|
}
|
|
}
|
|
for _, bad := range []string{"", "-1", "31", "abc"} {
|
|
w := configRequest(s, admin, "POST", "/-/sys/config/auth", url.Values{"action": {"save_trusted_session_policy"}, "trusted_session_days": {bad}})
|
|
if w.Code != 400 {
|
|
t.Fatalf("invalid policy %q accepted: %d", bad, w.Code)
|
|
}
|
|
}
|
|
user, err := s.store.CreatePasswordAccount(domain, "trusted-reader", "a long test password", "Reader", "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = s.store.SetConfigInDomain(domain, "auth_trusted_session_days", "30"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
token, _, err := s.store.CreateLoginSession(domain, user.UUID, 0, false, true)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
w := configRequest(s, token, "POST", "/-/sys/config/auth", url.Values{"action": {"save_trusted_session_policy"}, "trusted_session_days": {"7"}})
|
|
if w.Code != 403 {
|
|
t.Fatalf("reader changed trusted policy: %d", w.Code)
|
|
}
|
|
if err = s.store.SetPrincipalState(domain, user.UUID, "suspended"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = s.store.PrincipalForSession(domain, token); err == nil {
|
|
t.Fatal("suspended trusted session accepted")
|
|
}
|
|
}
|
|
|
|
func TestTrustedSessionExpiryIsFixed(t *testing.T) {
|
|
s, admin, domain := configFixture(t)
|
|
p := s.mustCLIPrincipal(t, domain, admin)
|
|
token, lifetime, err := s.store.CreateLoginSession(domain, p, 0, false, true)
|
|
if err != nil || lifetime != 30*24*time.Hour {
|
|
t.Fatalf("trusted default: %v %v", lifetime, err)
|
|
}
|
|
var id string
|
|
var expiry int64
|
|
if err = s.store.DB.QueryRow(`SELECT uuid,expires_at_ms FROM auth_sessions ORDER BY rowid DESC LIMIT 1`).Scan(&id, &expiry); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = s.store.SetConfigInDomain(domain, "auth_trusted_session_days", "0"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = s.store.PrincipalForSession(domain, token); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var after int64
|
|
if err = s.store.DB.QueryRow(`SELECT expires_at_ms FROM auth_sessions WHERE uuid=?`, id).Scan(&after); err != nil || after != expiry {
|
|
t.Fatalf("policy/read changed expiry: %d %d %v", expiry, after, err)
|
|
}
|
|
if _, err = s.store.DB.Exec(`UPDATE auth_sessions SET expires_at_ms=? WHERE uuid=?`, time.Now().Add(-time.Second).UnixMilli(), id); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = s.store.PrincipalForSession(domain, token); err == nil {
|
|
t.Fatal("expired trusted session accepted")
|
|
}
|
|
}
|