mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-11-04 16:40:21 +00:00
feat(auth): allow JWT IP changes by configuration (#1236)
* feat(auth): allow JWT IP changes by configuration * fix(auth): make JWT IP change setting concurrency-safe * test(auth): move JWT IP change setting tests out of MCP test file
This commit is contained in:
1 parent
8cb86e3853
commit
d19952a834
8 files changed
+172
-2
No files matched your search
@@ -0,0 +1,56 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestJWTIPChangeAllowedPersistence(t *testing.T) {
|
||||
c := &Config{}
|
||||
if c.JWTIPChangeAllowed() {
|
||||
t.Fatal("zero-value Config must bind JWT sessions to their IP")
|
||||
}
|
||||
// An absent agent secret makes Read save the config during initialization.
|
||||
path := newTempConfig(t, "jwt_secret_key: test\nallow_jwt_ip_change: true\n")
|
||||
if err := c.Read(path, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !c.JWTIPChangeAllowed() {
|
||||
t.Fatal("Read must initialize the atomic mirror before saving")
|
||||
}
|
||||
for _, want := range []bool{true, false} {
|
||||
c.SetJWTIPChangeAllowed(want)
|
||||
if err := c.Save(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
reloaded := &Config{}
|
||||
if err := reloaded.Read(path, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if c.AllowJWTIPChange != want || reloaded.JWTIPChangeAllowed() != want {
|
||||
t.Fatalf("saved and reloaded setting must be %v", want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestJWTIPChangeAllowedConcurrentAccess(t *testing.T) {
|
||||
c := &Config{filePath: filepath.Join(t.TempDir(), "config.yaml")}
|
||||
var wg sync.WaitGroup
|
||||
wg.Go(func() {
|
||||
for i := 0; i < 10000; i++ {
|
||||
c.SetJWTIPChangeAllowed(i%2 == 0)
|
||||
}
|
||||
})
|
||||
wg.Go(func() {
|
||||
for i := 0; i < 10000; i++ {
|
||||
_ = c.JWTIPChangeAllowed()
|
||||
}
|
||||
})
|
||||
for i := 0; i < 20; i++ {
|
||||
if err := c.Save(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
Reference in new issue
Block a user