Skip to content

Commit d78241f

Browse files
committed
fix
1 parent 3531e9d commit d78241f

File tree

10 files changed

+108
-59
lines changed

10 files changed

+108
-59
lines changed

modules/session/mem.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package session
5+
6+
import (
7+
"bytes"
8+
"encoding/gob"
9+
"net/http"
10+
11+
"gitea.com/go-chi/session"
12+
)
13+
14+
type MemStore struct {
15+
s *session.MemStore
16+
}
17+
18+
var _ session.RawStore = (*MemStore)(nil)
19+
20+
func (m *MemStore) Set(k, v any) error {
21+
var buf bytes.Buffer
22+
if err := gob.NewEncoder(&buf).Encode(v); err != nil {
23+
return err
24+
}
25+
return m.s.Set(k, buf.Bytes())
26+
}
27+
28+
func (m *MemStore) Get(k any) (ret any) {
29+
v, ok := m.s.Get(k).([]byte)
30+
if !ok {
31+
return nil
32+
}
33+
_ = gob.NewDecoder(bytes.NewBuffer(v)).Decode(&ret)
34+
return ret
35+
}
36+
37+
func (m *MemStore) Delete(k any) error {
38+
return m.s.Delete(k)
39+
}
40+
41+
func (m *MemStore) ID() string {
42+
return m.s.ID()
43+
}
44+
45+
func (m *MemStore) Release() error {
46+
return m.s.Release()
47+
}
48+
49+
func (m *MemStore) Flush() error {
50+
return m.s.Flush()
51+
}
52+
53+
type mockMemStore struct {
54+
*MemStore
55+
}
56+
57+
var _ Store = (*mockMemStore)(nil)
58+
59+
func (m mockMemStore) Destroy(writer http.ResponseWriter, request *http.Request) error {
60+
return nil
61+
}
62+
63+
func NewMockStore(sid string) Store {
64+
return &mockMemStore{&MemStore{session.NewMemStore(sid)}}
65+
}

modules/session/mock.go

Lines changed: 0 additions & 26 deletions
This file was deleted.

modules/session/store.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,34 @@ import (
1111
"gitea.com/go-chi/session"
1212
)
1313

14-
// Store represents a session store
14+
type RawStore = session.RawStore
15+
1516
type Store interface {
16-
Get(any) any
17-
Set(any, any) error
18-
Delete(any) error
19-
ID() string
20-
Release() error
21-
Flush() error
17+
RawStore
2218
Destroy(http.ResponseWriter, *http.Request) error
2319
}
2420

21+
type mockStoreContextKeyStruct struct{}
22+
23+
var MockStoreContextKey = mockStoreContextKeyStruct{}
24+
2525
// RegenerateSession regenerates the underlying session and returns the new store
2626
func RegenerateSession(resp http.ResponseWriter, req *http.Request) (Store, error) {
2727
for _, f := range BeforeRegenerateSession {
2828
f(resp, req)
2929
}
3030
if setting.IsInTesting {
31-
if store, ok := req.Context().Value(MockStoreContextKey).(*MockStore); ok {
32-
return store, nil
31+
if store := req.Context().Value(MockStoreContextKey); store != nil {
32+
return store.(Store), nil
3333
}
3434
}
3535
return session.RegenerateSession(resp, req)
3636
}
3737

3838
func GetContextSession(req *http.Request) Store {
3939
if setting.IsInTesting {
40-
if store, ok := req.Context().Value(MockStoreContextKey).(*MockStore); ok {
41-
return store
40+
if store := req.Context().Value(MockStoreContextKey); store != nil {
41+
return store.(Store)
4242
}
4343
}
4444
return session.GetSession(req)

modules/session/virtual.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ type VirtualSessionProvider struct {
2222
provider session.Provider
2323
}
2424

25-
// Init initializes the cookie session provider with given root path.
26-
func (o *VirtualSessionProvider) Init(gclifetime int64, config string) error {
25+
// Init initializes the cookie session provider with the given config.
26+
func (o *VirtualSessionProvider) Init(gcLifetime int64, config string) error {
2727
var opts session.Options
2828
if err := json.Unmarshal([]byte(config), &opts); err != nil {
2929
return err
@@ -52,7 +52,7 @@ func (o *VirtualSessionProvider) Init(gclifetime int64, config string) error {
5252
default:
5353
return fmt.Errorf("VirtualSessionProvider: Unknown Provider: %s", opts.Provider)
5454
}
55-
return o.provider.Init(gclifetime, opts.ProviderConfig)
55+
return o.provider.Init(gcLifetime, opts.ProviderConfig)
5656
}
5757

5858
// Read returns raw session store by session ID.

routers/web/auth/auth.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ func createUserInContext(ctx *context.Context, tpl templates.TplName, form any,
565565
oauth2LinkAccount(ctx, user, possibleLinkAccountData, true)
566566
return false // user is already created here, all redirects are handled
567567
case setting.OAuth2AccountLinkingLogin:
568-
showLinkingLogin(ctx, &possibleLinkAccountData.AuthSource, possibleLinkAccountData.GothUser)
568+
showLinkingLogin(ctx, possibleLinkAccountData.AuthSourceID, possibleLinkAccountData.GothUser)
569569
return false // user will be created only after linking login
570570
}
571571
}
@@ -633,7 +633,7 @@ func handleUserCreated(ctx *context.Context, u *user_model.User, possibleLinkAcc
633633

634634
// update external user information
635635
if possibleLinkAccountData != nil {
636-
if err := externalaccount.EnsureLinkExternalToUser(ctx, possibleLinkAccountData.AuthSource.ID, u, possibleLinkAccountData.GothUser); err != nil {
636+
if err := externalaccount.EnsureLinkExternalToUser(ctx, possibleLinkAccountData.AuthSourceID, u, possibleLinkAccountData.GothUser); err != nil {
637637
log.Error("EnsureLinkExternalToUser failed: %v", err)
638638
}
639639
}

routers/web/auth/linkaccount.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func LinkAccountPostSignIn(ctx *context.Context) {
170170
}
171171

172172
func oauth2LinkAccount(ctx *context.Context, u *user_model.User, linkAccountData *LinkAccountData, remember bool) {
173-
oauth2SignInSync(ctx, &linkAccountData.AuthSource, u, linkAccountData.GothUser)
173+
oauth2SignInSync(ctx, linkAccountData.AuthSourceID, u, linkAccountData.GothUser)
174174
if ctx.Written() {
175175
return
176176
}
@@ -185,7 +185,7 @@ func oauth2LinkAccount(ctx *context.Context, u *user_model.User, linkAccountData
185185
return
186186
}
187187

188-
err = externalaccount.LinkAccountToUser(ctx, linkAccountData.AuthSource.ID, u, linkAccountData.GothUser)
188+
err = externalaccount.LinkAccountToUser(ctx, linkAccountData.AuthSourceID, u, linkAccountData.GothUser)
189189
if err != nil {
190190
ctx.ServerError("UserLinkAccount", err)
191191
return
@@ -295,7 +295,7 @@ func LinkAccountPostRegister(ctx *context.Context) {
295295
Email: form.Email,
296296
Passwd: form.Password,
297297
LoginType: auth.OAuth2,
298-
LoginSource: linkAccountData.AuthSource.ID,
298+
LoginSource: linkAccountData.AuthSourceID,
299299
LoginName: linkAccountData.GothUser.UserID,
300300
}
301301

@@ -304,7 +304,12 @@ func LinkAccountPostRegister(ctx *context.Context) {
304304
return
305305
}
306306

307-
source := linkAccountData.AuthSource.Cfg.(*oauth2.Source)
307+
authSource, err := auth.GetSourceByID(ctx, linkAccountData.AuthSourceID)
308+
if err != nil {
309+
ctx.ServerError("GetSourceByID", err)
310+
return
311+
}
312+
source := authSource.Cfg.(*oauth2.Source)
308313
if err := syncGroupsToTeams(ctx, source, &linkAccountData.GothUser, u); err != nil {
309314
ctx.ServerError("SyncGroupsToTeams", err)
310315
return
@@ -318,5 +323,5 @@ func linkAccountFromContext(ctx *context.Context, user *user_model.User) error {
318323
if linkAccountData == nil {
319324
return errors.New("not in LinkAccount session")
320325
}
321-
return externalaccount.LinkAccountToUser(ctx, linkAccountData.AuthSource.ID, user, linkAccountData.GothUser)
326+
return externalaccount.LinkAccountToUser(ctx, linkAccountData.AuthSourceID, user, linkAccountData.GothUser)
322327
}

routers/web/auth/oauth.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func SignInOAuthCallback(ctx *context.Context) {
171171
gothUser.RawData = make(map[string]any)
172172
}
173173
gothUser.RawData["__giteaAutoRegMissingFields"] = missingFields
174-
showLinkingLogin(ctx, authSource, gothUser)
174+
showLinkingLogin(ctx, authSource.ID, gothUser)
175175
return
176176
}
177177
u = &user_model.User{
@@ -192,7 +192,7 @@ func SignInOAuthCallback(ctx *context.Context) {
192192
u.IsAdmin = isAdmin.ValueOrDefault(user_service.UpdateOptionField[bool]{FieldValue: false}).FieldValue
193193
u.IsRestricted = isRestricted.ValueOrDefault(setting.Service.DefaultUserIsRestricted)
194194

195-
linkAccountData := &LinkAccountData{*authSource, gothUser}
195+
linkAccountData := &LinkAccountData{authSource.ID, gothUser}
196196
if setting.OAuth2Client.AccountLinking == setting.OAuth2AccountLinkingDisabled {
197197
linkAccountData = nil
198198
}
@@ -207,7 +207,7 @@ func SignInOAuthCallback(ctx *context.Context) {
207207
}
208208
} else {
209209
// no existing user is found, request attach or new account
210-
showLinkingLogin(ctx, authSource, gothUser)
210+
showLinkingLogin(ctx, authSource.ID, gothUser)
211211
return
212212
}
213213
}
@@ -272,8 +272,8 @@ func getUserAdminAndRestrictedFromGroupClaims(source *oauth2.Source, gothUser *g
272272
}
273273

274274
type LinkAccountData struct {
275-
AuthSource auth.Source
276-
GothUser goth.User
275+
AuthSourceID int64
276+
GothUser goth.User
277277
}
278278

279279
func oauth2GetLinkAccountData(ctx *context.Context) *LinkAccountData {
@@ -284,9 +284,9 @@ func oauth2GetLinkAccountData(ctx *context.Context) *LinkAccountData {
284284
return &v
285285
}
286286

287-
func showLinkingLogin(ctx *context.Context, authSource *auth.Source, gothUser goth.User) {
287+
func showLinkingLogin(ctx *context.Context, authSourceID int64, gothUser goth.User) {
288288
if err := updateSession(ctx, nil, map[string]any{
289-
"linkAccountData": LinkAccountData{*authSource, gothUser},
289+
"linkAccountData": LinkAccountData{authSourceID, gothUser},
290290
}); err != nil {
291291
ctx.ServerError("updateSession", err)
292292
return
@@ -313,7 +313,7 @@ func oauth2UpdateAvatarIfNeed(ctx *context.Context, url string, u *user_model.Us
313313
}
314314

315315
func handleOAuth2SignIn(ctx *context.Context, authSource *auth.Source, u *user_model.User, gothUser goth.User) {
316-
oauth2SignInSync(ctx, authSource, u, gothUser)
316+
oauth2SignInSync(ctx, authSource.ID, u, gothUser)
317317
if ctx.Written() {
318318
return
319319
}

routers/web/auth/oauth_signin_sync.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@ import (
1818
"github.com/markbates/goth"
1919
)
2020

21-
func oauth2SignInSync(ctx *context.Context, authSource *auth.Source, u *user_model.User, gothUser goth.User) {
21+
func oauth2SignInSync(ctx *context.Context, authSourceID int64, u *user_model.User, gothUser goth.User) {
2222
oauth2UpdateAvatarIfNeed(ctx, gothUser.AvatarURL, u)
2323

24+
authSource, err := auth.GetSourceByID(ctx, authSourceID)
25+
if err != nil {
26+
ctx.ServerError("GetSourceByID", err)
27+
return
28+
}
2429
oauth2Source, _ := authSource.Cfg.(*oauth2.Source)
2530
if !authSource.IsOAuth2() || oauth2Source == nil {
2631
ctx.ServerError("oauth2SignInSync", fmt.Errorf("source %s is not an OAuth2 source", gothUser.Provider))
@@ -45,7 +50,7 @@ func oauth2SignInSync(ctx *context.Context, authSource *auth.Source, u *user_mod
4550
}
4651
}
4752

48-
err := oauth2UpdateSSHPubIfNeed(ctx, authSource, &gothUser, u)
53+
err = oauth2UpdateSSHPubIfNeed(ctx, authSource, &gothUser, u)
4954
if err != nil {
5055
log.Error("Unable to sync OAuth2 SSH public key %s: %v", gothUser.Provider, err)
5156
}

services/contexttest/context_tests.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func mockRequest(t *testing.T, reqPath string) *http.Request {
4949

5050
type MockContextOption struct {
5151
Render context.Render
52-
SessionStore *session.MockStore
52+
SessionStore session.Store
5353
}
5454

5555
// MockContext mock context for unit tests

tests/integration/signin_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func TestEnablePasswordSignInFormAndEnablePasskeyAuth(t *testing.T) {
107107
mockLinkAccount := func(ctx *context.Context) {
108108
authSource := auth_model.Source{ID: 1}
109109
gothUser := goth.User{Email: "invalid-email", Name: "."}
110-
_ = ctx.Session.Set("linkAccountData", auth.LinkAccountData{AuthSource: authSource, GothUser: gothUser})
110+
_ = ctx.Session.Set("linkAccountData", auth.LinkAccountData{AuthSourceID: authSource.ID, GothUser: gothUser})
111111
}
112112

113113
t.Run("EnablePasswordSignInForm=false", func(t *testing.T) {

0 commit comments

Comments
 (0)