fix: preserve SSO identities and polish sharing interface
This commit is contained in:
1 parent
2e3b56f250
commit
bd9bf33bc9
22 files changed
+430
-106
No files matched your search
+45
-2
@@ -29,6 +29,7 @@ func TestOIDCSessionLifecycle(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var issuer, nonce, challenge string
|
||||
profileSubject := "subject-123"
|
||||
roles := []string{"FS Admin"}
|
||||
enabled := true
|
||||
provider := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -51,7 +52,7 @@ func TestOIDCSessionLifecycle(t *testing.T) {
|
||||
t.Error("client authentication mismatch")
|
||||
}
|
||||
hash := sha256.Sum256([]byte("access"))
|
||||
claims := map[string]any{"iss": issuer, "aud": "client", "sub": "subject-123", "preferred_username": "alice", "roles": roles, "nonce": nonce, "exp": time.Now().Add(5 * time.Minute).Unix(), "iat": time.Now().Unix(), "at_hash": base64.RawURLEncoding.EncodeToString(hash[:16])}
|
||||
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])}
|
||||
b, _ := json.Marshal(claims)
|
||||
signed, _ := signer.Sign(b)
|
||||
token, _ := signed.CompactSerialize()
|
||||
@@ -61,7 +62,10 @@ func TestOIDCSessionLifecycle(t *testing.T) {
|
||||
w.WriteHeader(401)
|
||||
return
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(ssoClaims{Subject: "subject-123", Username: "alice", Roles: roles})
|
||||
if r.Header.Get("Authorization") != "Bearer access" {
|
||||
t.Error("missing UserInfo bearer")
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(ssoClaims{Subject: profileSubject, Username: "alice", Name: "Alice Example", Email: "alice@example.test", Roles: []string{"User"}})
|
||||
default:
|
||||
w.WriteHeader(404)
|
||||
}
|
||||
@@ -116,6 +120,25 @@ func TestOIDCSessionLifecycle(t *testing.T) {
|
||||
if check.Code != 200 || !strings.Contains(check.Body.String(), `"admin":true`) {
|
||||
t.Fatalf("session: %d %s", check.Code, check.Body.String())
|
||||
}
|
||||
if !strings.Contains(check.Body.String(), `"username":"alice"`) || !strings.Contains(check.Body.String(), `"email":"alice@example.test"`) {
|
||||
t.Fatalf("UserInfo profile not loaded: %s", check.Body.String())
|
||||
}
|
||||
// Repeated logins after changing preferences retain one local identity.
|
||||
var firstID uint
|
||||
for i := 0; i < 3; i++ {
|
||||
identity, err := s.provisionUser(ssoClaims{Subject: "subject-123", Username: "alice"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if i == 0 {
|
||||
firstID = identity.ID
|
||||
} else if identity.ID != firstID {
|
||||
t.Fatal("new user on repeated login")
|
||||
}
|
||||
if err = st.Users.Update(&users.User{ID: identity.ID, Username: "alice", Locale: "ru"}, "Locale"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
roles = []string{"User"}
|
||||
check = request("GET", "/api/session")
|
||||
if check.Code != 200 || !strings.Contains(check.Body.String(), `"admin":true`) {
|
||||
@@ -143,7 +166,27 @@ func TestOIDCSessionLifecycle(t *testing.T) {
|
||||
if w.Code != 400 {
|
||||
t.Fatal("callback replay accepted")
|
||||
}
|
||||
login = httptest.NewRecorder()
|
||||
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"
|
||||
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")
|
||||
}
|
||||
// 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"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
identity, err := restarted.provisionUser(ssoClaims{Subject: "subject-123", Username: "alice"})
|
||||
if err != nil || identity.ID != firstID {
|
||||
t.Fatalf("identity changed after restart: %+v %v", identity, 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()
|
||||
|
||||
Reference in new issue
Block a user