Skip to content

Store OAuth2 session data in database #3660

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 29, 2018

Conversation

lafriks
Copy link
Member

@lafriks lafriks commented Mar 12, 2018

Currently OAuth2 sessions are saved only on filesystem in sessions directory independently of where gitea session is actually saved

@lafriks lafriks added the type/enhancement An improvement of existing functionality label Mar 12, 2018
@lafriks lafriks added this to the 1.5.0 milestone Mar 12, 2018
@codecov-io
Copy link

codecov-io commented Mar 12, 2018

Codecov Report

Merging #3660 into master will decrease coverage by <.01%.
The diff coverage is 0%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3660      +/-   ##
==========================================
- Coverage    23.3%   23.29%   -0.01%     
==========================================
  Files         126      126              
  Lines       24999    25002       +3     
==========================================
  Hits         5825     5825              
- Misses      18286    18289       +3     
  Partials      888      888
Impacted Files Coverage Δ
models/oauth2.go 0% <0%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8d5f58d...542efa7. Read the comment docs.

@tboerger tboerger added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Mar 12, 2018
}

if !st.opts.SkipCreateTable {
st.e.Sync2(&xormSession{tableName: st.opts.TableName})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error should be handled.

@lunny
Copy link
Member

lunny commented Mar 13, 2018

@lafriks why save OAuth session to database not in file system?

@lafriks
Copy link
Member Author

lafriks commented Mar 13, 2018

@lunny probably currently there is no real reason. I wrote this code when trying to fix oath2 bug but error that file session storage was throwing about trying to delete unexisting file was just a consequence not a real source for that bug.
But more generally speaking this could be useful in future as currently we can specify to store Gitea session data in redis for example but oauth2 sessions will still be saved in file system so Gitea can not be load balanced because of that as if first request before redirecting to auth2 authorization service would hit one node and create session, if second request after authorization would hit other node, it would not know about such session and would fail with error.

@lunny
Copy link
Member

lunny commented Mar 13, 2018

@lafriks seems good enough. Please follow my review, otherwise LGTM.

@tboerger tboerger added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Mar 13, 2018
Copy link
Member

@jonasfranz jonasfranz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get the following error if I try to login via github oauth at my local gitea instance:

2018/03/18 20:49:52 [...routers/user/auth.go:411 handleOAuth2SignIn()] [E] UserSignIn: could not find a matching session for this request

store := sessions.NewFilesystemStore(sessionDir, []byte(sessionUsersStoreKey))
func Init(x *xorm.Engine) {
store := xormstore.NewOptions(x, xormstore.Options{
TableName: "oauth2_sessions",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO we should use oauth2_session instead of oauth2_sessions because we currently use the singular for table names.

@lafriks
Copy link
Member Author

lafriks commented Mar 18, 2018

@JonasFranzDEV ~~~did you clear cookies (could be that some old cookie still be left that was stored in file system)?~~~ try again, there was not updated goth libs that actually fixed github authorization

}

type xormSession struct {
ID string `xorm:"VARCHAR(400) PK NAME 'id'"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VARCHAR(400) results in an error when I use mysql:

panic: Error 1071: Specified key was too long; max key length is 767 bytes

goroutine 1 [running]:
code.gitea.io/gitea/vendor/github.com/lafriks/xormstore.NewOptions(0xc4206680b0, 0x113ce57, 0xf, 0xc42067d600, 0xc4206fb8f0, 0x1, 0x1, 0x0)
	/mnt/local_storage/jonasfranz/Go/src/code.gitea.io/gitea/vendor/github.com/lafriks/xormstore/xormstore.go:108 +0x2d2
code.gitea.io/gitea/modules/auth/oauth2.Init(0xc4206680b0)
	/mnt/local_storage/jonasfranz/Go/src/code.gitea.io/gitea/modules/auth/oauth2/oauth2.go:44 +0xb9
code.gitea.io/gitea/models.InitOAuth2()
	/mnt/local_storage/jonasfranz/Go/src/code.gitea.io/gitea/models/oauth2.go:101 +0x3f
code.gitea.io/gitea/routers.GlobalInit()
	/mnt/local_storage/jonasfranz/Go/src/code.gitea.io/gitea/routers/init.go:63 +0x56e
code.gitea.io/gitea/cmd.runWeb(0xc420344000, 0x0, 0x0)
	/mnt/local_storage/jonasfranz/Go/src/code.gitea.io/gitea/cmd/web.go:83 +0xa8
code.gitea.io/gitea/vendor/github.com/urfave/cli.HandleAction(0xfb0900, 0x117e690, 0xc420344000, 0xc42020f440, 0x0)
	/mnt/local_storage/jonasfranz/Go/src/code.gitea.io/gitea/vendor/github.com/urfave/cli/app.go:471 +0xad
code.gitea.io/gitea/vendor/github.com/urfave/cli.(*App).Run(0xc4200d5040, 0xc420030190, 0x1, 0x1, 0x0, 0x0)
	/mnt/local_storage/jonasfranz/Go/src/code.gitea.io/gitea/vendor/github.com/urfave/cli/app.go:246 +0x542
main.main()
	/mnt/local_storage/jonasfranz/Go/src/code.gitea.io/gitea/main.go:52 +0x40e

The error is returned at line 106 and is actually not handled!

When I change this to VARCHAR(200) for example, no error is thrown.

@jonasfranz
Copy link
Member

@lafriks Now the authentication works (except the error reported (mysql)) but the table is empty. Is that an excepted behaviour?

@lafriks
Copy link
Member Author

lafriks commented Mar 19, 2018

@JonasFranzDEV it depends on goth lib usage and in how it is currently done in gitea it seams to store session only between when you redirect to oauth2 provider and when you get session back.

@lafriks lafriks force-pushed the feat/xormstore_oath2_sessions branch from eb61efd to 6858da3 Compare April 9, 2018 10:48
@lafriks
Copy link
Member Author

lafriks commented Apr 9, 2018

@lunny @JonasFranzDEV fixed

@bkcsoft bkcsoft added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Apr 10, 2018
@lafriks
Copy link
Member Author

lafriks commented Apr 29, 2018

Make LG-TM work

@lunny
Copy link
Member

lunny commented Apr 29, 2018

@lafriks this pr is update to latest master commit. Code Review is passed already.

@lafriks lafriks merged commit 5a62eb3 into go-gitea:master Apr 29, 2018
@lafriks lafriks deleted the feat/xormstore_oath2_sessions branch April 29, 2018 06:09
@go-gitea go-gitea locked and limited conversation to collaborators Nov 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. type/enhancement An improvement of existing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants