mirror of
https://git.vectorsigma.ru/public/AdGuardHome.git
synced 2026-08-03 20:49:21 +00:00
Merge in DNS/adguard-home from AGDNS-2743-auth-middleware-glinet to master
Squashed commit of the following:
commit 66b6b3d94ec2e19c9fd7dc07c2496a7600d4395a
Merge: 7adae7e2a e1f87ac8f
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Thu Jun 5 16:08:23 2025 +0300
Merge branch 'master' into AGDNS-2743-auth-middleware-glinet
commit 7adae7e2a58bba2d224057e4079b4d1330d33b5d
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Thu Jun 5 15:02:21 2025 +0300
all: imp code
commit fbdc43cde06368db591e29618d0b7281f8342ad5
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Wed Jun 4 21:50:38 2025 +0300
home: add todo
commit 891db077a7bfc41ace66ec593e57253f50500737
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Wed Jun 4 20:36:29 2025 +0300
home: imp docs
commit c706ab6fc4293ce504a2e7b04393296e34cec03e
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Tue Jun 3 22:40:07 2025 +0300
home: imp code
commit 5c75efd020f94a72f6ed48a78e12409a98d2bdc7
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Mon Jun 2 21:06:30 2025 +0300
home: auth middleware glinet config
commit f537365ca40ac75046c0ca31746af139572fde84
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Mon Jun 2 15:46:26 2025 +0300
home: auth middleware glinet
103 lines
2.4 KiB
Go
103 lines
2.4 KiB
Go
package home
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/AdguardTeam/golibs/timeutil"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestAuthMiddlewareGLiNet(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
const (
|
|
testTTL = 60 * time.Second
|
|
|
|
glTokenFileSuffix = "test"
|
|
)
|
|
|
|
tempDir := t.TempDir()
|
|
glFilePrefix = tempDir + "/gl_token_"
|
|
glTokenFile := glFilePrefix + glTokenFileSuffix
|
|
|
|
glFileData := make([]byte, 4)
|
|
binary.NativeEndian.PutUint32(glFileData, uint32(time.Now().Add(testTTL).Unix()))
|
|
|
|
err := os.WriteFile(glTokenFile, glFileData, 0o644)
|
|
require.NoError(t, err)
|
|
|
|
mw := newAuthMiddlewareGLiNet(&authMiddlewareGLiNetConfig{
|
|
logger: testLogger,
|
|
clock: timeutil.SystemClock{},
|
|
tokenFilePrefix: glFilePrefix,
|
|
maxTokenSize: MaxFileSize,
|
|
ttl: testTTL,
|
|
})
|
|
|
|
h := &testAuthHandler{}
|
|
wrapped := mw.Wrap(h)
|
|
|
|
reqValidCookie := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
reqValidCookie.AddCookie(&http.Cookie{Name: glCookieName, Value: glTokenFileSuffix})
|
|
|
|
reqInvalidCookie := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
reqInvalidCookie.AddCookie(&http.Cookie{Name: glCookieName, Value: "invalid_cookie"})
|
|
|
|
testCases := []struct {
|
|
req *http.Request
|
|
name string
|
|
wantCode int
|
|
}{{
|
|
req: httptest.NewRequest(http.MethodGet, "/", nil),
|
|
name: "no_cookie",
|
|
wantCode: http.StatusUnauthorized,
|
|
}, {
|
|
req: reqValidCookie,
|
|
name: "valid_cookie",
|
|
wantCode: http.StatusOK,
|
|
}, {
|
|
req: reqInvalidCookie,
|
|
name: "invalid_cookie",
|
|
wantCode: http.StatusUnauthorized,
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
w := httptest.NewRecorder()
|
|
wrapped.ServeHTTP(w, tc.req)
|
|
|
|
assert.Equal(t, tc.wantCode, w.Code)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestAuthGL(t *testing.T) {
|
|
dir := t.TempDir()
|
|
|
|
GLMode = true
|
|
t.Cleanup(func() { GLMode = false })
|
|
glFilePrefix = dir + "/gl_token_"
|
|
|
|
data := make([]byte, 4)
|
|
binary.NativeEndian.PutUint32(data, 1)
|
|
|
|
require.NoError(t, os.WriteFile(glFilePrefix+"test", data, 0o644))
|
|
assert.False(t, glCheckToken("test"))
|
|
|
|
data = make([]byte, 4)
|
|
binary.NativeEndian.PutUint32(data, uint32(time.Now().UTC().Unix()+60))
|
|
|
|
require.NoError(t, os.WriteFile(glFilePrefix+"test", data, 0o644))
|
|
r, _ := http.NewRequest(http.MethodGet, "http://localhost/", nil)
|
|
r.AddCookie(&http.Cookie{Name: glCookieName, Value: "test"})
|
|
assert.True(t, glProcessCookie(r))
|
|
}
|