This is an automated email from the ASF dual-hosted git repository.

LinkinStars pushed a commit to branch fix/2.0.3/user
in repository https://gitbox.apache.org/repos/asf/answer.git

commit 3985d20c7e5b85811eb5f009a4de7e14822f0e0e
Author: LinkinStars <[email protected]>
AuthorDate: Tue Sep 8 15:43:49 2026 +0800

    fix(security): preserve User Center session identity
---
 internal/service/auth/user_center_status_test.go   | 111 +++++++++++++++
 .../user_center_login_service.go                   |  28 +++-
 .../user_center_login_service_test.go              | 149 +++++++++++++++++++++
 3 files changed, 284 insertions(+), 4 deletions(-)

diff --git a/internal/service/auth/user_center_status_test.go 
b/internal/service/auth/user_center_status_test.go
new file mode 100644
index 00000000..df8b379e
--- /dev/null
+++ b/internal/service/auth/user_center_status_test.go
@@ -0,0 +1,111 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package auth_test
+
+import (
+       "context"
+       "fmt"
+       "testing"
+
+       "github.com/apache/answer/internal/base/data"
+       "github.com/apache/answer/internal/entity"
+       authrepo "github.com/apache/answer/internal/repo/auth"
+       authservice "github.com/apache/answer/internal/service/auth"
+       "github.com/apache/answer/plugin"
+)
+
+func TestGetUserCacheInfoChecksUserCenterStatus(t *testing.T) {
+       cache, cleanup, err := data.NewCache(&data.CacheConf{})
+       if err != nil {
+               t.Fatalf("create cache: %v", err)
+       }
+       t.Cleanup(cleanup)
+
+       previousCallUserCenter := plugin.CallUserCenter
+       testUserCenter := &authTestUserCenter{}
+       plugin.CallUserCenter = plugin.CallFn[plugin.UserCenter](func(fn 
plugin.Caller[plugin.UserCenter]) error {
+               return fn(testUserCenter)
+       })
+       t.Cleanup(func() { plugin.CallUserCenter = previousCallUserCenter })
+
+       service := 
authservice.NewAuthService(authrepo.NewAuthRepo(&data.Data{Cache: cache}), nil)
+       for _, expectedStatus := range 
[]plugin.UserStatus{plugin.UserStatusSuspended, plugin.UserStatusDeleted} {
+               t.Run(fmt.Sprintf("status_%d", expectedStatus), func(t 
*testing.T) {
+                       testUserCenter.status = expectedStatus
+                       testUserCenter.externalID = ""
+                       accessToken, _, err := 
service.SetUserCacheInfo(context.Background(), &entity.UserCacheInfo{
+                               UserID:      "user-center-user",
+                               UserStatus:  entity.UserStatusAvailable,
+                               EmailStatus: entity.EmailStatusAvailable,
+                               ExternalID:  "central-user-1001",
+                       })
+                       if err != nil {
+                               t.Fatalf("cache user session: %v", err)
+                       }
+
+                       userInfo, err := 
service.GetUserCacheInfo(context.Background(), accessToken)
+                       if err != nil {
+                               t.Fatalf("get user cache info: %v", err)
+                       }
+                       if userInfo.UserStatus != int(expectedStatus) {
+                               t.Fatalf("user status = %d, want %d", 
userInfo.UserStatus, expectedStatus)
+                       }
+                       if testUserCenter.externalID != "central-user-1001" {
+                               t.Fatalf("user center checked external ID = %q, 
want %q", testUserCenter.externalID, "central-user-1001")
+                       }
+               })
+       }
+}
+
+type authTestUserCenter struct {
+       externalID string
+       status     plugin.UserStatus
+}
+
+func (*authTestUserCenter) Info() plugin.Info { return plugin.Info{SlugName: 
"auth-test-user-center"} }
+
+func (*authTestUserCenter) Description() plugin.UserCenterDesc { return 
plugin.UserCenterDesc{} }
+
+func (*authTestUserCenter) ControlCenterItems() []plugin.ControlCenter { 
return nil }
+
+func (*authTestUserCenter) LoginCallback(*plugin.GinContext) 
(*plugin.UserCenterBasicUserInfo, error) {
+       return nil, nil
+}
+
+func (*authTestUserCenter) SignUpCallback(*plugin.GinContext) 
(*plugin.UserCenterBasicUserInfo, error) {
+       return nil, nil
+}
+
+func (*authTestUserCenter) UserInfo(string) (*plugin.UserCenterBasicUserInfo, 
error) { return nil, nil }
+
+func (u *authTestUserCenter) UserStatus(externalID string) plugin.UserStatus {
+       u.externalID = externalID
+       return u.status
+}
+
+func (*authTestUserCenter) UserList([]string) 
([]*plugin.UserCenterBasicUserInfo, error) {
+       return nil, nil
+}
+
+func (*authTestUserCenter) UserSettings(string) (*plugin.SettingInfo, error) { 
return nil, nil }
+
+func (*authTestUserCenter) PersonalBranding(string) []*plugin.PersonalBranding 
{ return nil }
+
+func (*authTestUserCenter) AfterLogin(string, string) {}
diff --git a/internal/service/user_external_login/user_center_login_service.go 
b/internal/service/user_external_login/user_center_login_service.go
index 90895ac4..4dda13b7 100644
--- a/internal/service/user_external_login/user_center_login_service.go
+++ b/internal/service/user_external_login/user_center_login_service.go
@@ -40,11 +40,31 @@ import (
        "github.com/segmentfault/pacman/log"
 )
 
+type userCenterLoginUserRepo interface {
+       AddUser(ctx context.Context, user *entity.User) (err error)
+       GetByUserID(ctx context.Context, userID string) (userInfo *entity.User, 
exist bool, err error)
+       GetByUsername(ctx context.Context, username string) (userInfo 
*entity.User, exist bool, err error)
+       UpdateLastLoginDate(ctx context.Context, userID string) (err error)
+}
+
+type userCenterLoginExternalLoginRepo interface {
+       AddUserExternalLogin(ctx context.Context, user 
*entity.UserExternalLogin) (err error)
+       GetByExternalID(ctx context.Context, provider, externalID string) (
+               userInfo *entity.UserExternalLogin, exist bool, err error)
+       GetUserExternalLoginList(ctx context.Context, userID string) (resp 
[]*entity.UserExternalLogin, err error)
+}
+
+type userCenterLoginUserCommonService interface {
+       MakeUsername(ctx context.Context, username string) (string, error)
+       CacheLoginUserInfo(ctx context.Context, userID string, userStatus, 
emailStatus int, externalID string) (
+               accessToken string, userCacheInfo *entity.UserCacheInfo, err 
error)
+}
+
 // UserCenterLoginService user external login service
 type UserCenterLoginService struct {
-       userRepo              usercommon.UserRepo
-       userExternalLoginRepo UserExternalLoginRepo
-       userCommonService     *usercommon.UserCommon
+       userRepo              userCenterLoginUserRepo
+       userExternalLoginRepo userCenterLoginExternalLoginRepo
+       userCommonService     userCenterLoginUserCommonService
        userActivity          activity.UserActiveActivityRepo
        siteInfoCommonService siteinfo_common.SiteInfoCommonService
 }
@@ -134,7 +154,7 @@ func (us *UserCenterLoginService) ExternalLogin(
        }
 
        accessToken, _, err := us.userCommonService.CacheLoginUserInfo(
-               ctx, oldUserInfo.ID, oldUserInfo.MailStatus, 
oldUserInfo.Status, oldExternalLoginUserInfo.ExternalID)
+               ctx, oldUserInfo.ID, oldUserInfo.MailStatus, 
oldUserInfo.Status, basicUserInfo.ExternalID)
        return &schema.UserExternalLoginResp{AccessToken: accessToken}, err
 }
 
diff --git 
a/internal/service/user_external_login/user_center_login_service_test.go 
b/internal/service/user_external_login/user_center_login_service_test.go
new file mode 100644
index 00000000..95d75660
--- /dev/null
+++ b/internal/service/user_external_login/user_center_login_service_test.go
@@ -0,0 +1,149 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package user_external_login
+
+import (
+       "context"
+       "testing"
+
+       "github.com/apache/answer/internal/entity"
+       "github.com/apache/answer/plugin"
+)
+
+func TestUserCenterFirstLoginCachesExternalID(t *testing.T) {
+       userRepo := &userCenterLoginTestUserRepo{}
+       externalLoginRepo := &userCenterLoginTestExternalLoginRepo{}
+       userCommonService := &userCenterLoginTestUserCommonService{}
+       service := &UserCenterLoginService{
+               userRepo:              userRepo,
+               userExternalLoginRepo: externalLoginRepo,
+               userCommonService:     userCommonService,
+               userActivity:          userCenterLoginTestActivity{},
+       }
+
+       const externalID = "central-user-1001"
+       resp, err := service.ExternalLogin(context.Background(), 
userCenterLoginTestUserCenter{},
+               &plugin.UserCenterBasicUserInfo{ExternalID: externalID, 
Username: "central-user"})
+       if err != nil {
+               t.Fatalf("first user center login failed: %v", err)
+       }
+       if resp.AccessToken == "" {
+               t.Fatal("first user center login did not issue an access token")
+       }
+       if userCommonService.externalID != externalID {
+               t.Fatalf("cached external ID = %q, want %q", 
userCommonService.externalID, externalID)
+       }
+       if externalLoginRepo.added == nil || externalLoginRepo.added.ExternalID 
!= externalID {
+               t.Fatalf("persisted external ID = %#v, want %q", 
externalLoginRepo.added, externalID)
+       }
+}
+
+type userCenterLoginTestUserRepo struct{}
+
+func (*userCenterLoginTestUserRepo) AddUser(_ context.Context, user 
*entity.User) error {
+       user.ID = "answer-user-1"
+       return nil
+}
+
+func (*userCenterLoginTestUserRepo) GetByUserID(context.Context, string) 
(*entity.User, bool, error) {
+       return nil, false, nil
+}
+
+func (*userCenterLoginTestUserRepo) GetByUsername(context.Context, string) 
(*entity.User, bool, error) {
+       return nil, false, nil
+}
+
+func (*userCenterLoginTestUserRepo) UpdateLastLoginDate(context.Context, 
string) error { return nil }
+
+type userCenterLoginTestExternalLoginRepo struct {
+       added *entity.UserExternalLogin
+}
+
+func (*userCenterLoginTestExternalLoginRepo) GetByExternalID(context.Context, 
string, string) (*entity.UserExternalLogin, bool, error) {
+       return &entity.UserExternalLogin{}, false, nil
+}
+
+func (r *userCenterLoginTestExternalLoginRepo) AddUserExternalLogin(_ 
context.Context, user *entity.UserExternalLogin) error {
+       r.added = user
+       return nil
+}
+
+func (*userCenterLoginTestExternalLoginRepo) 
GetUserExternalLoginList(context.Context, string) ([]*entity.UserExternalLogin, 
error) {
+       return nil, nil
+}
+
+type userCenterLoginTestUserCommonService struct {
+       externalID string
+}
+
+func (*userCenterLoginTestUserCommonService) MakeUsername(_ context.Context, 
username string) (string, error) {
+       return username, nil
+}
+
+func (s *userCenterLoginTestUserCommonService) CacheLoginUserInfo(
+       _ context.Context, _ string, _, _ int, externalID string,
+) (string, *entity.UserCacheInfo, error) {
+       s.externalID = externalID
+       return "access-token", &entity.UserCacheInfo{ExternalID: externalID}, 
nil
+}
+
+type userCenterLoginTestActivity struct{}
+
+func (userCenterLoginTestActivity) UserActive(context.Context, string) error { 
return nil }
+
+type userCenterLoginTestUserCenter struct{}
+
+func (userCenterLoginTestUserCenter) Info() plugin.Info {
+       return plugin.Info{SlugName: "test-user-center"}
+}
+
+func (userCenterLoginTestUserCenter) Description() plugin.UserCenterDesc {
+       return plugin.UserCenterDesc{}
+}
+
+func (userCenterLoginTestUserCenter) ControlCenterItems() 
[]plugin.ControlCenter { return nil }
+
+func (userCenterLoginTestUserCenter) LoginCallback(*plugin.GinContext) 
(*plugin.UserCenterBasicUserInfo, error) {
+       return nil, nil
+}
+
+func (userCenterLoginTestUserCenter) SignUpCallback(*plugin.GinContext) 
(*plugin.UserCenterBasicUserInfo, error) {
+       return nil, nil
+}
+
+func (userCenterLoginTestUserCenter) UserInfo(string) 
(*plugin.UserCenterBasicUserInfo, error) {
+       return nil, nil
+}
+
+func (userCenterLoginTestUserCenter) UserStatus(string) plugin.UserStatus {
+       return plugin.UserStatusAvailable
+}
+
+func (userCenterLoginTestUserCenter) UserList([]string) 
([]*plugin.UserCenterBasicUserInfo, error) {
+       return nil, nil
+}
+
+func (userCenterLoginTestUserCenter) UserSettings(string) 
(*plugin.SettingInfo, error) {
+       return nil, nil
+}
+
+func (userCenterLoginTestUserCenter) PersonalBranding(string) 
[]*plugin.PersonalBranding { return nil }
+
+func (userCenterLoginTestUserCenter) AfterLogin(string, string) {}

Reply via email to