mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-12-22 06:44:27 +03:00
3b12ff2cc2
Updates #5829. Squashed commit of the following: commit 8a93b30d5bd1c40c30bd10cd3fc77c3a3a64cb71 Merge: 8e4429c4854f77c010
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Mar 20 19:15:07 2024 +0300 Merge branch 'master' into 5829-trusted-ip commit 8e4429c483c0fd6fffdc93fa808adcca6678bc3e Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Mar 20 18:37:26 2024 +0300 all: upd chlog commit b598a8d1ea239cc574bfdfdd6a2da47792582589 Merge: 1f58bf8fd054233962
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Mar 20 18:34:13 2024 +0300 Merge branch 'master' into 5829-trusted-ip commit 1f58bf8fd1bc3b3790475651cb87494885cadf66 Merge: ffb4b9a65c64a36c94
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Mar 20 17:09:09 2024 +0300 Merge branch 'master' into 5829-trusted-ip commit ffb4b9a65fea5555d0d401194d3fc3820b2e6766 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Mar 14 17:40:07 2024 +0300 home: fix alignment commit 7f11807ff13eff286be1d3bd4b796273454bdbda Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Mar 14 17:35:13 2024 +0300 all: imp code commit 2aee9a66c70af929e28653245eb73c0f29a46e97 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Mar 11 18:17:58 2024 +0300 home: real ip in logs
89 lines
2 KiB
Go
89 lines
2 KiB
Go
package home
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestNewSessionToken(t *testing.T) {
|
|
// Successful case.
|
|
token, err := newSessionToken()
|
|
require.NoError(t, err)
|
|
assert.Len(t, token, sessionTokenSize)
|
|
|
|
// Break the rand.Reader.
|
|
prevReader := rand.Reader
|
|
t.Cleanup(func() { rand.Reader = prevReader })
|
|
rand.Reader = &bytes.Buffer{}
|
|
|
|
// Unsuccessful case.
|
|
token, err = newSessionToken()
|
|
require.Error(t, err)
|
|
assert.Empty(t, token)
|
|
}
|
|
|
|
func TestAuth(t *testing.T) {
|
|
dir := t.TempDir()
|
|
fn := filepath.Join(dir, "sessions.db")
|
|
|
|
users := []webUser{{
|
|
Name: "name",
|
|
PasswordHash: "$2y$05$..vyzAECIhJPfaQiOK17IukcQnqEgKJHy0iETyYqxn3YXJl8yZuo2",
|
|
}}
|
|
a := InitAuth(fn, nil, 60, nil, nil)
|
|
s := session{}
|
|
|
|
user := webUser{Name: "name"}
|
|
err := a.addUser(&user, "password")
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, checkSessionNotFound, a.checkSession("notfound"))
|
|
a.removeSession("notfound")
|
|
|
|
sess, err := newSessionToken()
|
|
require.NoError(t, err)
|
|
sessStr := hex.EncodeToString(sess)
|
|
|
|
now := time.Now().UTC().Unix()
|
|
// check expiration
|
|
s.expire = uint32(now)
|
|
a.addSession(sess, &s)
|
|
assert.Equal(t, checkSessionExpired, a.checkSession(sessStr))
|
|
|
|
// add session with TTL = 2 sec
|
|
s = session{}
|
|
s.expire = uint32(time.Now().UTC().Unix() + 2)
|
|
a.addSession(sess, &s)
|
|
assert.Equal(t, checkSessionOK, a.checkSession(sessStr))
|
|
|
|
a.Close()
|
|
|
|
// load saved session
|
|
a = InitAuth(fn, users, 60, nil, nil)
|
|
|
|
// the session is still alive
|
|
assert.Equal(t, checkSessionOK, a.checkSession(sessStr))
|
|
// reset our expiration time because checkSession() has just updated it
|
|
s.expire = uint32(time.Now().UTC().Unix() + 2)
|
|
a.storeSession(sess, &s)
|
|
a.Close()
|
|
|
|
u, ok := a.findUser("name", "password")
|
|
assert.True(t, ok)
|
|
assert.NotEmpty(t, u.Name)
|
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
// load and remove expired sessions
|
|
a = InitAuth(fn, users, 60, nil, nil)
|
|
assert.Equal(t, checkSessionNotFound, a.checkSession(sessStr))
|
|
|
|
a.Close()
|
|
}
|