-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
OAuth2 auto-register #5123
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
OAuth2 auto-register #5123
Changes from 39 commits
Commits
Show all changes
46 commits
Select commit
Hold shift + click to select a range
a3366c4
Refactored handleOAuth2SignIn in routers/user/auth.go
mgjm 6e2ece4
Refactored user creation
mgjm a473815
Added auto-register for OAuth2 users
mgjm a700b02
Moved oauth2 settings to new section in app.ini
mgjm f0124c4
Added oauth2 use nickname setting
mgjm 9504a74
Merge branch 'master' into oauth2-auto-register
lafriks 688afbd
Merge branch 'master' into oauth2-auto-register
mgjm aeb833c
Moved oauth2_client settings to service file
mgjm 527c7e6
Updated comments on auth helpers
mgjm 6a3a0e7
Renamed oauth2_client settings
mgjm 7a84789
Uncommented setting in app.ini.sample
mgjm b57cb72
Merge branch 'master' into oauth2-auto-register
mgjm 3f93e42
Merge branch 'master' into oauth2-auto-register
6543 6fe0f39
fix conflict resolve relict
6543 c39ed5a
Merge branch 'master' into oauth2-auto-register
6543 bcd3fb6
Merge branch 'master' into oauth2-auto-register
6543 9d1e1f2
Added error for missing fields in OAuth2 response
mgjm 8a64dfe
Fixed error handling in createUserInContext
mgjm aece370
Merge branch 'master' into oauth2-auto-register
6543 1373b78
Merge branch 'master' into oauth2-auto-register
6543 8ee4097
Merge branch 'master' into oauth2-auto-register
6543 7b2ff29
Merge branch 'master' into oauth2-auto-register
6543 e166d49
Merge branch 'master' into oauth2-auto-register
6543 4cc2d8f
Merge branch 'master' into oauth2-auto-register
kvaster 3edad74
Linking and auto linking on oauth2 registration
kvaster 7a811af
Code cleanup
kvaster 150163e
Fix lint problems
kvaster 586dff7
Fix bugs in validating config options
kvaster fb464db
Convert oauth2 client types to string enums
kvaster d62cf15
Fix ioutil.ReadAll
mgjm f4924a3
Set default username source to nickname
mgjm 28b696c
Merge branch 'master' into oauth2-auto-register
mgjm 491610a
Add copyright and empty line
mgjm cd7241e
Move oauth2 client settings to new file
mgjm a3cf72b
Add automatic oauth2 scopes for github and google
mgjm 3d99ee8
Add hint to change the openid connect scopes if fields are missing
mgjm 917ed54
Merge branch 'master' into oauth2-auto-register
mgjm ebc0d0b
OAuth2 sign in is not handled properly after all merges
kvaster 7795946
Merge branch 'master' into oauth2-auto-register
mgjm d448ed9
Merge branch 'master' into oauth2-auto-register
6543 1d4d7d3
Merge branch 'master' into oauth2-auto-register
6543 8f44610
Merge branch 'master' into oauth2-auto-register
6543 f320e34
More detailed description of options in cheat sheet
kvaster 2987081
Correct info about auto linking security risk
kvaster 7b96396
Extend info about auto linking security risk
mgjm 4de0b71
Merge branch 'master' into oauth2-auto-register
6543 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2021 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package migrations | ||
mgjm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import ( | ||
"xorm.io/xorm" | ||
"xorm.io/xorm/schemas" | ||
) | ||
|
||
func convertAvatarURLToText(x *xorm.Engine) error { | ||
dbType := x.Dialect().URI().DBType | ||
if dbType == schemas.SQLITE { // For SQLITE, varchar or char will always be represented as TEXT | ||
return nil | ||
} | ||
|
||
// Some oauth2 providers may give very long avatar urls (i.e. Google) | ||
return modifyColumn(x, "external_login_user", &schemas.Column{ | ||
Name: "avatar_url", | ||
SQLType: schemas.SQLType{ | ||
Name: schemas.Text, | ||
}, | ||
Nullable: true, | ||
}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright 2021 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package setting | ||
|
||
import ( | ||
"code.gitea.io/gitea/modules/log" | ||
|
||
"gopkg.in/ini.v1" | ||
) | ||
|
||
// OAuth2UsernameType is enum describing the way gitea 'name' should be generated from oauth2 data | ||
type OAuth2UsernameType string | ||
|
||
const ( | ||
// OAuth2UsernameUserid oauth2 userid field will be used as gitea name | ||
OAuth2UsernameUserid OAuth2UsernameType = "userid" | ||
// OAuth2UsernameNickname oauth2 nickname field will be used as gitea name | ||
OAuth2UsernameNickname OAuth2UsernameType = "nickname" | ||
// OAuth2UsernameEmail username of oauth2 email filed will be used as gitea name | ||
OAuth2UsernameEmail OAuth2UsernameType = "email" | ||
) | ||
|
||
func (username OAuth2UsernameType) isValid() bool { | ||
switch username { | ||
case OAuth2UsernameUserid, OAuth2UsernameNickname, OAuth2UsernameEmail: | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
// OAuth2AccountLinkingType is enum describing behaviour of linking with existing account | ||
type OAuth2AccountLinkingType string | ||
|
||
const ( | ||
// OAuth2AccountLinkingDisabled error will be displayed if account exist | ||
OAuth2AccountLinkingDisabled OAuth2AccountLinkingType = "disabled" | ||
// OAuth2AccountLinkingLogin account linking login will be displayed if account exist | ||
OAuth2AccountLinkingLogin OAuth2AccountLinkingType = "login" | ||
// OAuth2AccountLinkingAuto account will be automatically linked if account exist | ||
OAuth2AccountLinkingAuto OAuth2AccountLinkingType = "auto" | ||
) | ||
|
||
func (accountLinking OAuth2AccountLinkingType) isValid() bool { | ||
switch accountLinking { | ||
case OAuth2AccountLinkingDisabled, OAuth2AccountLinkingLogin, OAuth2AccountLinkingAuto: | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
// OAuth2Client settings | ||
var OAuth2Client struct { | ||
RegisterEmailConfirm bool | ||
OpenIDConnectScopes []string | ||
EnableAutoRegistration bool | ||
Username OAuth2UsernameType | ||
UpdateAvatar bool | ||
AccountLinking OAuth2AccountLinkingType | ||
} | ||
|
||
func newOAuth2Client() { | ||
sec := Cfg.Section("oauth2_client") | ||
OAuth2Client.RegisterEmailConfirm = sec.Key("REGISTER_EMAIL_CONFIRM").MustBool(Service.RegisterEmailConfirm) | ||
OAuth2Client.OpenIDConnectScopes = parseScopes(sec, "OPENID_CONNECT_SCOPES") | ||
OAuth2Client.EnableAutoRegistration = sec.Key("ENABLE_AUTO_REGISTRATION").MustBool() | ||
OAuth2Client.Username = OAuth2UsernameType(sec.Key("USERNAME").MustString(string(OAuth2UsernameNickname))) | ||
if !OAuth2Client.Username.isValid() { | ||
log.Warn("Username setting is not valid: '%s', will fallback to '%s'", OAuth2Client.Username, OAuth2UsernameNickname) | ||
OAuth2Client.Username = OAuth2UsernameNickname | ||
} | ||
OAuth2Client.UpdateAvatar = sec.Key("UPDATE_AVATAR").MustBool() | ||
OAuth2Client.AccountLinking = OAuth2AccountLinkingType(sec.Key("ACCOUNT_LINKING").MustString(string(OAuth2AccountLinkingDisabled))) | ||
if !OAuth2Client.AccountLinking.isValid() { | ||
log.Warn("Account linking setting is not valid: '%s', will fallback to '%s'", OAuth2Client.AccountLinking, OAuth2AccountLinkingDisabled) | ||
OAuth2Client.AccountLinking = OAuth2AccountLinkingDisabled | ||
} | ||
} | ||
|
||
func parseScopes(sec *ini.Section, name string) []string { | ||
parts := sec.Key(name).Strings(" ") | ||
scopes := make([]string, 0, len(parts)) | ||
for _, scope := range parts { | ||
if scope != "" { | ||
scopes = append(scopes, scope) | ||
} | ||
} | ||
return scopes | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.