feat: adopt shared internal SSO sessions and add no-sso launch mode

This commit is contained in:
SantaSpeen committed 2026-10-24 15:41:39 +03:00
1 parent c209f1effc
commit c02bf755f2
51 files changed
+2131 -362

No files matched your search

+32 -17
View File
@@ -42,6 +42,8 @@ func TestOIDCSessionLifecycle(t *testing.T) {
profileSubject := "subject-123"
roles := []string{"FS Admin"}
enabled := true
invalidNonce := false
signedOut := false
provider := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch r.URL.Path {
@@ -63,10 +65,31 @@ func TestOIDCSessionLifecycle(t *testing.T) {
}
hash := sha256.Sum256([]byte("access"))
claims := map[string]any{"iss": issuer, "aud": "client", "sub": "subject-123", "roles": roles, "nonce": nonce, "exp": time.Now().Add(5 * time.Minute).Unix(), "iat": time.Now().Unix(), "at_hash": base64.RawURLEncoding.EncodeToString(hash[:16])}
if invalidNonce {
claims["nonce"] = "wrong"
}
b, _ := json.Marshal(claims)
signed, _ := signer.Sign(b)
token, _ := signed.CompactSerialize()
_ = json.NewEncoder(w).Encode(map[string]any{"access_token": "access", "id_token": token, "token_type": "Bearer", "expires_in": 300})
_ = json.NewEncoder(w).Encode(map[string]any{"access_token": "access", "id_token": token, "token_type": "Bearer", "expires_in": 300, "session_handle": "test-browser-handle-123456789", "session_handle_expires_in": 2592000})
case "/internal/session/resolve":
r.ParseForm()
if id, secret, ok := r.BasicAuth(); !ok || id != "client" || secret != "secret" || r.Form.Get("session_handle") != "test-browser-handle-123456789" {
t.Error("invalid internal resolve request")
}
if signedOut {
json.NewEncoder(w).Encode(internalState{Status: "signed_out"})
return
}
if !enabled {
json.NewEncoder(w).Encode(internalState{Status: "access_denied"})
return
}
json.NewEncoder(w).Encode(internalState{Status: "authenticated", Authenticated: true, ClientID: "client", State: "state-1", Expires: time.Now().Add(time.Hour).Unix(), User: &ssoClaims{Subject: profileSubject, Username: "alice", Name: "Alice Example", Email: "alice@example.test", Picture: issuer + "/avatars/alice.png", Roles: roles}})
case "/internal/session/logout":
signedOut = true
w.WriteHeader(204)
case "/userinfo":
if !enabled {
w.WriteHeader(401)
@@ -100,7 +123,7 @@ func TestOIDCSessionLifecycle(t *testing.T) {
u, _ := url.Parse(login.Header().Get("Location"))
nonce = u.Query().Get("nonce")
challenge = u.Query().Get("code_challenge")
if u.Query().Get("code_challenge_method") != "S256" || nonce == "" {
if u.Query().Get("code_challenge_method") != "S256" || nonce == "" || u.Query().Get("internal_sync") != "1" || u.Query().Has("prompt") || u.Query().Has("max_age") {
t.Fatal("missing PKCE or nonce")
}
callback := httptest.NewRequest("GET", "/auth/callback?code=test&state="+u.Query().Get("state"), nil)
@@ -112,11 +135,11 @@ func TestOIDCSessionLifecycle(t *testing.T) {
}
var sessionCookie *http.Cookie
for _, c := range response.Result().Cookies() {
if c.Name == "fb_session" {
if c.Name == internalCookie {
sessionCookie = c
}
}
if sessionCookie == nil || !sessionCookie.HttpOnly {
if sessionCookie == nil || !sessionCookie.HttpOnly || !sessionCookie.Secure || sessionCookie.Path != "/" || sessionCookie.Domain != "" || sessionCookie.SameSite != http.SameSiteLaxMode {
t.Fatal("missing HttpOnly session")
}
request := func(method, path string) *httptest.ResponseRecorder {
@@ -124,6 +147,7 @@ func TestOIDCSessionLifecycle(t *testing.T) {
r.AddCookie(sessionCookie)
if method == "POST" {
r.Header.Set("X-FB-Request", "1")
r.Header.Set("X-FB-Session-State", "state-1")
}
w := httptest.NewRecorder()
h.ServeHTTP(w, r)
@@ -171,8 +195,8 @@ func TestOIDCSessionLifecycle(t *testing.T) {
}
roles = []string{"User"}
check = request("GET", "/api/session")
if check.Code != 200 || !strings.Contains(check.Body.String(), `"admin":true`) {
t.Fatal("login role snapshot changed during local session")
if check.Code != 200 || !strings.Contains(check.Body.String(), `"admin":false`) {
t.Fatal("current roles were not refreshed")
}
for _, p := range []string{"/api/login", "/api/signup", "/api/renew", "/api/command"} {
@@ -200,13 +224,13 @@ func TestOIDCSessionLifecycle(t *testing.T) {
h.ServeHTTP(login, httptest.NewRequest("GET", "/auth/login", nil))
u, _ = url.Parse(login.Header().Get("Location"))
nonce, challenge = u.Query().Get("nonce"), u.Query().Get("code_challenge")
profileSubject = "another-subject"
invalidNonce = true
callback = httptest.NewRequest("GET", "/auth/callback?code=test&state="+u.Query().Get("state"), nil)
callback.AddCookie(login.Result().Cookies()[0])
w = httptest.NewRecorder()
h.ServeHTTP(w, callback)
if w.Code != 401 {
t.Fatal("mismatched UserInfo subject accepted")
t.Fatal("invalid nonce accepted")
}
// Expired/idle sessions are rejected, even with an otherwise valid cookie.
restarted, err := NewSSO(context.Background(), st, &settings.Server{Root: root}, SSOConfig{Issuer: issuer, ClientID: "client", ClientSecret: "secret", PublicURL: "http://127.0.0.1:9111"})
@@ -258,13 +282,4 @@ func TestOIDCSessionLifecycle(t *testing.T) {
t.Fatalf("existing records changed: %d %v", len(all), err)
}
}
sessionKey := sha256.Sum256([]byte(sessionCookie.Value))
for _, tc := range []struct{ expires, touched time.Time }{{time.Now().Add(-time.Second), time.Now()}, {time.Now().Add(time.Hour), time.Now().Add(-31 * time.Minute)}} {
s.mu.Lock()
s.sessions[sessionKey] = &ssoSession{Identity: sessionIdentity{ID: 2, Name: "alice"}, Expires: tc.expires, Touched: tc.touched}
s.mu.Unlock()
if request("GET", "/api/session").Code != 401 {
t.Fatal("expired/idle session accepted")
}
}
}