Skip to content

Commit a229e34

Browse files
authored
Allow only internal registration (#15795)
* Add ALLOW_ONLY_INTERNAL_REGISTRATION into settings * OpenID respect setting too
1 parent e818e91 commit a229e34

File tree

9 files changed

+26
-4
lines changed

9 files changed

+26
-4
lines changed

custom/conf/app.example.ini

Lines changed: 2 additions & 0 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -659,6 +659,8 @@ EMAIL_DOMAIN_WHITELIST =
659
EMAIL_DOMAIN_BLOCKLIST =
659
EMAIL_DOMAIN_BLOCKLIST =
660
; Disallow registration, only allow admins to create accounts.
660
; Disallow registration, only allow admins to create accounts.
661
DISABLE_REGISTRATION = false
661
DISABLE_REGISTRATION = false
662+
; Allow registration only using gitea itself, it works only when DISABLE_REGISTRATION is false
663+
ALLOW_ONLY_INTERNAL_REGISTRATION = false
662
; Allow registration only using third-party services, it works only when DISABLE_REGISTRATION is false
664
; Allow registration only using third-party services, it works only when DISABLE_REGISTRATION is false
663
ALLOW_ONLY_EXTERNAL_REGISTRATION = false
665
ALLOW_ONLY_EXTERNAL_REGISTRATION = false
664
; User must sign in to view anything.
666
; User must sign in to view anything.

docs/content/doc/advanced/config-cheat-sheet.en-us.md

Lines changed: 1 addition & 0 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -497,6 +497,7 @@ relation to port exhaustion.
497
- `AUTO_WATCH_ON_CHANGES`: **false**: Enable this to make users watch a repository after their first commit to it
497
- `AUTO_WATCH_ON_CHANGES`: **false**: Enable this to make users watch a repository after their first commit to it
498
- `DEFAULT_ORG_VISIBILITY`: **public**: Set default visibility mode for organisations, either "public", "limited" or "private".
498
- `DEFAULT_ORG_VISIBILITY`: **public**: Set default visibility mode for organisations, either "public", "limited" or "private".
499
- `DEFAULT_ORG_MEMBER_VISIBLE`: **false** True will make the membership of the users visible when added to the organisation.
499
- `DEFAULT_ORG_MEMBER_VISIBLE`: **false** True will make the membership of the users visible when added to the organisation.
500+
- `ALLOW_ONLY_INTERNAL_REGISTRATION`: **false** Set to true to force registration only via gitea.
500
- `ALLOW_ONLY_EXTERNAL_REGISTRATION`: **false** Set to true to force registration only using third-party services.
501
- `ALLOW_ONLY_EXTERNAL_REGISTRATION`: **false** Set to true to force registration only using third-party services.
501
- `NO_REPLY_ADDRESS`: **noreply.DOMAIN** Value for the domain part of the user's email address in the git log if user has set KeepEmailPrivate to true. DOMAIN resolves to the value in server.DOMAIN.
502
- `NO_REPLY_ADDRESS`: **noreply.DOMAIN** Value for the domain part of the user's email address in the git log if user has set KeepEmailPrivate to true. DOMAIN resolves to the value in server.DOMAIN.
502
The user's email will be replaced with a concatenation of the user name in lower case, "@" and NO_REPLY_ADDRESS.
503
The user's email will be replaced with a concatenation of the user name in lower case, "@" and NO_REPLY_ADDRESS.

modules/setting/service.go

Lines changed: 6 additions & 0 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -23,6 +23,7 @@ var Service struct {
23
EmailDomainWhitelist []string
23
EmailDomainWhitelist []string
24
EmailDomainBlocklist []string
24
EmailDomainBlocklist []string
25
DisableRegistration bool
25
DisableRegistration bool
26+
AllowOnlyInternalRegistration bool
26
AllowOnlyExternalRegistration bool
27
AllowOnlyExternalRegistration bool
27
ShowRegistrationButton bool
28
ShowRegistrationButton bool
28
ShowMilestonesDashboardPage bool
29
ShowMilestonesDashboardPage bool
@@ -73,7 +74,12 @@ func newService() {
73
Service.ActiveCodeLives = sec.Key("ACTIVE_CODE_LIVE_MINUTES").MustInt(180)
74
Service.ActiveCodeLives = sec.Key("ACTIVE_CODE_LIVE_MINUTES").MustInt(180)
74
Service.ResetPwdCodeLives = sec.Key("RESET_PASSWD_CODE_LIVE_MINUTES").MustInt(180)
75
Service.ResetPwdCodeLives = sec.Key("RESET_PASSWD_CODE_LIVE_MINUTES").MustInt(180)
75
Service.DisableRegistration = sec.Key("DISABLE_REGISTRATION").MustBool()
76
Service.DisableRegistration = sec.Key("DISABLE_REGISTRATION").MustBool()
77+
Service.AllowOnlyInternalRegistration = sec.Key("ALLOW_ONLY_INTERNAL_REGISTRATION").MustBool()
76
Service.AllowOnlyExternalRegistration = sec.Key("ALLOW_ONLY_EXTERNAL_REGISTRATION").MustBool()
78
Service.AllowOnlyExternalRegistration = sec.Key("ALLOW_ONLY_EXTERNAL_REGISTRATION").MustBool()
79+
if Service.AllowOnlyExternalRegistration && Service.AllowOnlyInternalRegistration {
80+
log.Warn("ALLOW_ONLY_INTERNAL_REGISTRATION and ALLOW_ONLY_EXTERNAL_REGISTRATION are true - disabling registration")
81+
Service.DisableRegistration = true
82+
}
77
if !sec.Key("REGISTER_EMAIL_CONFIRM").MustBool() {
83
if !sec.Key("REGISTER_EMAIL_CONFIRM").MustBool() {
78
Service.RegisterManualConfirm = sec.Key("REGISTER_MANUAL_CONFIRM").MustBool(false)
84
Service.RegisterManualConfirm = sec.Key("REGISTER_MANUAL_CONFIRM").MustBool(false)
79
} else {
85
} else {

options/locale/locale_en-US.ini

Lines changed: 1 addition & 0 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -2412,6 +2412,7 @@ config.db_path = Path
2412
config.service_config = Service Configuration
2412
config.service_config = Service Configuration
2413
config.register_email_confirm = Require Email Confirmation to Register
2413
config.register_email_confirm = Require Email Confirmation to Register
2414
config.disable_register = Disable Self-Registration
2414
config.disable_register = Disable Self-Registration
2415+
config.allow_only_internal_registration = Allow Registration Only Through Gitea itself
2415
config.allow_only_external_registration = Allow Registration Only Through External Services
2416
config.allow_only_external_registration = Allow Registration Only Through External Services
2416
config.enable_openid_signup = Enable OpenID Self-Registration
2417
config.enable_openid_signup = Enable OpenID Self-Registration
2417
config.enable_openid_signin = Enable OpenID Sign-In
2418
config.enable_openid_signin = Enable OpenID Sign-In

routers/user/auth.go

Lines changed: 3 additions & 2 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -617,7 +617,7 @@ func SignInOAuthCallback(ctx *context.Context) {
617
}
617
}
618

618

619
if u == nil {
619
if u == nil {
620-
if setting.OAuth2Client.EnableAutoRegistration {
620+
if !(setting.Service.DisableRegistration || setting.Service.AllowOnlyInternalRegistration) && setting.OAuth2Client.EnableAutoRegistration {
621
// create new user with details from oauth2 provider
621
// create new user with details from oauth2 provider
622
var missingFields []string
622
var missingFields []string
623
if gothUser.UserID == "" {
623
if gothUser.UserID == "" {
@@ -828,6 +828,7 @@ func LinkAccount(ctx *context.Context) {
828
ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
828
ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
829
ctx.Data["HcaptchaSitekey"] = setting.Service.HcaptchaSitekey
829
ctx.Data["HcaptchaSitekey"] = setting.Service.HcaptchaSitekey
830
ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration
830
ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration
831+
ctx.Data["AllowOnlyInternalRegistration"] = setting.Service.AllowOnlyInternalRegistration
831
ctx.Data["ShowRegistrationButton"] = false
832
ctx.Data["ShowRegistrationButton"] = false
832

833

833
// use this to set the right link into the signIn and signUp templates in the link_account template
834
// use this to set the right link into the signIn and signUp templates in the link_account template
@@ -993,7 +994,7 @@ func LinkAccountPostRegister(ctx *context.Context) {
993
return
994
return
994
}
995
}
995

996

996-
if setting.Service.DisableRegistration {
997+
if setting.Service.DisableRegistration || setting.Service.AllowOnlyInternalRegistration {
997
ctx.Error(http.StatusForbidden)
998
ctx.Error(http.StatusForbidden)
998
return
999
return
999
}
1000
}

routers/user/auth_openid.go

Lines changed: 8 additions & 1 deletion
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -249,7 +249,7 @@ func signInOpenIDVerify(ctx *context.Context) {
249
log.Error("signInOpenIDVerify: Unable to save changes to the session: %v", err)
249
log.Error("signInOpenIDVerify: Unable to save changes to the session: %v", err)
250
}
250
}
251

251

252-
if u != nil || !setting.Service.EnableOpenIDSignUp {
252+
if u != nil || !setting.Service.EnableOpenIDSignUp || setting.Service.AllowOnlyInternalRegistration {
253
ctx.Redirect(setting.AppSubURL + "/user/openid/connect")
253
ctx.Redirect(setting.AppSubURL + "/user/openid/connect")
254
} else {
254
} else {
255
ctx.Redirect(setting.AppSubURL + "/user/openid/register")
255
ctx.Redirect(setting.AppSubURL + "/user/openid/register")
@@ -267,6 +267,7 @@ func ConnectOpenID(ctx *context.Context) {
267
ctx.Data["PageIsSignIn"] = true
267
ctx.Data["PageIsSignIn"] = true
268
ctx.Data["PageIsOpenIDConnect"] = true
268
ctx.Data["PageIsOpenIDConnect"] = true
269
ctx.Data["EnableOpenIDSignUp"] = setting.Service.EnableOpenIDSignUp
269
ctx.Data["EnableOpenIDSignUp"] = setting.Service.EnableOpenIDSignUp
270+
ctx.Data["AllowOnlyInternalRegistration"] = setting.Service.AllowOnlyInternalRegistration
270
ctx.Data["OpenID"] = oid
271
ctx.Data["OpenID"] = oid
271
userName, _ := ctx.Session.Get("openid_determined_username").(string)
272
userName, _ := ctx.Session.Get("openid_determined_username").(string)
272
if userName != "" {
273
if userName != "" {
@@ -328,6 +329,7 @@ func RegisterOpenID(ctx *context.Context) {
328
ctx.Data["PageIsSignIn"] = true
329
ctx.Data["PageIsSignIn"] = true
329
ctx.Data["PageIsOpenIDRegister"] = true
330
ctx.Data["PageIsOpenIDRegister"] = true
330
ctx.Data["EnableOpenIDSignUp"] = setting.Service.EnableOpenIDSignUp
331
ctx.Data["EnableOpenIDSignUp"] = setting.Service.EnableOpenIDSignUp
332+
ctx.Data["AllowOnlyInternalRegistration"] = setting.Service.AllowOnlyInternalRegistration
331
ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
333
ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
332
ctx.Data["Captcha"] = context.GetImageCaptcha()
334
ctx.Data["Captcha"] = context.GetImageCaptcha()
333
ctx.Data["CaptchaType"] = setting.Service.CaptchaType
335
ctx.Data["CaptchaType"] = setting.Service.CaptchaType
@@ -367,6 +369,11 @@ func RegisterOpenIDPost(ctx *context.Context) {
367
ctx.Data["HcaptchaSitekey"] = setting.Service.HcaptchaSitekey
369
ctx.Data["HcaptchaSitekey"] = setting.Service.HcaptchaSitekey
368
ctx.Data["OpenID"] = oid
370
ctx.Data["OpenID"] = oid
369

371

372+
if setting.Service.AllowOnlyInternalRegistration {
373+
ctx.Error(http.StatusForbidden)
374+
return
375+
}
376+
370
if setting.Service.EnableCaptcha {
377
if setting.Service.EnableCaptcha {
371
var valid bool
378
var valid bool
372
var err error
379
var err error

templates/admin/config.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -149,6 +149,8 @@
149
<dd>{{if .Service.RegisterEmailConfirm}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}</dd>
149
<dd>{{if .Service.RegisterEmailConfirm}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}</dd>
150
<dt>{{.i18n.Tr "admin.config.disable_register"}}</dt>
150
<dt>{{.i18n.Tr "admin.config.disable_register"}}</dt>
151
<dd>{{if .Service.DisableRegistration}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}</dd>
151
<dd>{{if .Service.DisableRegistration}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}</dd>
152+
<dt>{{.i18n.Tr "admin.config.allow_only_internal_registration"}}</dt>
153+
<dd>{{if .Service.AllowOnlyInternalRegistration}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}</dd>
152
<dt>{{.i18n.Tr "admin.config.allow_only_external_registration"}}</dt>
154
<dt>{{.i18n.Tr "admin.config.allow_only_external_registration"}}</dt>
153
<dd>{{if .Service.AllowOnlyExternalRegistration}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}</dd>
155
<dd>{{if .Service.AllowOnlyExternalRegistration}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}</dd>
154
<dt>{{.i18n.Tr "admin.config.show_registration_button"}}</dt>
156
<dt>{{.i18n.Tr "admin.config.show_registration_button"}}</dt>

templates/user/auth/link_account.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -3,10 +3,12 @@
3
<div class="ui secondary pointing tabular top attached borderless menu new-menu navbar">
3
<div class="ui secondary pointing tabular top attached borderless menu new-menu navbar">
4
<div class="new-menu-inner">
4
<div class="new-menu-inner">
5
<!-- TODO handle .ShowRegistrationButton once other login bugs are fixed -->
5
<!-- TODO handle .ShowRegistrationButton once other login bugs are fixed -->
6+
{{if not .AllowOnlyInternalRegistration}}
6
<a class="item {{if not .user_exists}}active{{end}}"
7
<a class="item {{if not .user_exists}}active{{end}}"
7
data-tab="auth-link-signup-tab">
8
data-tab="auth-link-signup-tab">
8
{{.i18n.Tr "auth.oauth_signup_tab"}}
9
{{.i18n.Tr "auth.oauth_signup_tab"}}
9
</a>
10
</a>
11+
{{end}}
10
<a class="item {{if .user_exists}}active{{end}}"
12
<a class="item {{if .user_exists}}active{{end}}"
11
data-tab="auth-link-signin-tab">
13
data-tab="auth-link-signin-tab">
12
{{.i18n.Tr "auth.oauth_signin_tab"}}
14
{{.i18n.Tr "auth.oauth_signin_tab"}}

templates/user/auth/signup_openid_navbar.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -3,7 +3,7 @@
3
<a class="{{if .PageIsOpenIDConnect}}active{{end}} item" href="{{AppSubUrl}}/user/openid/connect">
3
<a class="{{if .PageIsOpenIDConnect}}active{{end}} item" href="{{AppSubUrl}}/user/openid/connect">
4
{{.i18n.Tr "auth.openid_connect_title"}}
4
{{.i18n.Tr "auth.openid_connect_title"}}
5
</a>
5
</a>
6-
{{if .EnableOpenIDSignUp}}
6+
{{if and .EnableOpenIDSignUp (not .AllowOnlyInternalRegistration)}}
7
<a class="{{if .PageIsOpenIDRegister}}active{{end}} item" href="{{AppSubUrl}}/user/openid/register">
7
<a class="{{if .PageIsOpenIDRegister}}active{{end}} item" href="{{AppSubUrl}}/user/openid/register">
8
{{.i18n.Tr "auth.openid_register_title"}}
8
{{.i18n.Tr "auth.openid_register_title"}}
9
</a>
9
</a>

0 commit comments

Comments
 (0)