Skip to content

Commit 5a62eb3

Browse files
authored
Store OAuth2 session data in database (#3660)
* Store OAuth2 session data in database * Rename table to `oauth2_session` and do not skip xormstorage initialization error
1 parent 8d5f58d commit 5a62eb3

File tree

11 files changed

+603
-12
lines changed

11 files changed

+603
-12
lines changed

models/oauth2.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,17 @@ func GetActiveOAuth2Providers() ([]string, map[string]OAuth2Provider, error) {
9797
}
9898

9999
// InitOAuth2 initialize the OAuth2 lib and register all active OAuth2 providers in the library
100-
func InitOAuth2() {
101-
oauth2.Init()
100+
func InitOAuth2() error {
101+
if err := oauth2.Init(x); err != nil {
102+
return err
103+
}
102104
loginSources, _ := GetActiveOAuth2ProviderLoginSources()
103105

104106
for _, source := range loginSources {
105107
oAuth2Config := source.OAuth2()
106108
oauth2.RegisterProvider(source.Name, oAuth2Config.Provider, oAuth2Config.ClientID, oAuth2Config.ClientSecret, oAuth2Config.OpenIDConnectAutoDiscoveryURL, oAuth2Config.CustomURLMapping)
107109
}
110+
return nil
108111
}
109112

110113
// wrapOpenIDConnectInitializeError is used to wrap the error but this cannot be done in modules/auth/oauth2

modules/auth/oauth2/oauth2.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ package oauth2
77
import (
88
"math"
99
"net/http"
10-
"os"
11-
"path/filepath"
1210

1311
"code.gitea.io/gitea/modules/log"
1412
"code.gitea.io/gitea/modules/setting"
1513

16-
"github.com/gorilla/sessions"
14+
"github.com/go-xorm/xorm"
15+
"github.com/lafriks/xormstore"
1716
"github.com/markbates/goth"
1817
"github.com/markbates/goth/gothic"
1918
"github.com/markbates/goth/providers/bitbucket"
@@ -41,13 +40,14 @@ type CustomURLMapping struct {
4140
}
4241

4342
// Init initialize the setup of the OAuth2 library
44-
func Init() {
45-
sessionDir := filepath.Join(setting.AppDataPath, "sessions", "oauth2")
46-
if err := os.MkdirAll(sessionDir, 0700); err != nil {
47-
log.Fatal(4, "Fail to create dir %s: %v", sessionDir, err)
48-
}
43+
func Init(x *xorm.Engine) error {
44+
store, err := xormstore.NewOptions(x, xormstore.Options{
45+
TableName: "oauth2_session",
46+
}, []byte(sessionUsersStoreKey))
4947

50-
store := sessions.NewFilesystemStore(sessionDir, []byte(sessionUsersStoreKey))
48+
if err != nil {
49+
return err
50+
}
5151
// according to the Goth lib:
5252
// set the maxLength of the cookies stored on the disk to a larger number to prevent issues with:
5353
// securecookie: the value is too long
@@ -65,6 +65,7 @@ func Init() {
6565
return req.Header.Get(providerHeaderKey), nil
6666
}
6767

68+
return nil
6869
}
6970

7071
// Auth OAuth2 auth service

routers/init.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ func GlobalInit() {
6060
log.Fatal(4, "Failed to initialize ORM engine: %v", err)
6161
}
6262
models.HasEngine = true
63-
models.InitOAuth2()
63+
if err := models.InitOAuth2(); err != nil {
64+
log.Fatal(4, "Failed to initialize OAuth2 support: %v", err)
65+
}
6466

6567
models.LoadRepoConfig()
6668
models.NewRepoContext()

vendor/github.com/lafriks/xormstore/Gopkg.lock

Lines changed: 75 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/lafriks/xormstore/Gopkg.toml

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/lafriks/xormstore/LICENSE

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/lafriks/xormstore/README.md

Lines changed: 48 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/lafriks/xormstore/test

Lines changed: 70 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)