Skip to content

Commit e6293e7

Browse files
6543ashimokawa
authored andcommitted
Allow only internal registration (go-gitea#15795)
* Add ALLOW_ONLY_INTERNAL_REGISTRATION into settings * OpenID respect setting too
1 parent c647722 commit e6293e7

File tree

9 files changed

+31
-8
lines changed

9 files changed

+31
-8
lines changed

custom/conf/app.example.ini

+2
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,8 @@ EMAIL_DOMAIN_WHITELIST =
633633
EMAIL_DOMAIN_BLOCKLIST =
634634
; Disallow registration, only allow admins to create accounts.
635635
DISABLE_REGISTRATION = false
636+
; Allow registration only using gitea itself, it works only when DISABLE_REGISTRATION is false
637+
ALLOW_ONLY_INTERNAL_REGISTRATION = false
636638
; Allow registration only using third-party services, it works only when DISABLE_REGISTRATION is false
637639
ALLOW_ONLY_EXTERNAL_REGISTRATION = false
638640
; User must sign in to view anything.

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

+1
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,7 @@ relation to port exhaustion.
483483
- `AUTO_WATCH_ON_CHANGES`: **false**: Enable this to make users watch a repository after their first commit to it
484484
- `DEFAULT_ORG_VISIBILITY`: **public**: Set default visibility mode for organisations, either "public", "limited" or "private".
485485
- `DEFAULT_ORG_MEMBER_VISIBLE`: **false** True will make the membership of the users visible when added to the organisation.
486+
- `ALLOW_ONLY_INTERNAL_REGISTRATION`: **false** Set to true to force registration only via gitea.
486487
- `ALLOW_ONLY_EXTERNAL_REGISTRATION`: **false** Set to true to force registration only using third-party services.
487488
- `NO_REPLY_ADDRESS`: **DOMAIN** Default value for the domain part of the user's email address in the git log if he has set KeepEmailPrivate to true.
488489
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

+6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var Service struct {
2323
EmailDomainWhitelist []string
2424
EmailDomainBlocklist []string
2525
DisableRegistration bool
26+
AllowOnlyInternalRegistration bool
2627
AllowOnlyExternalRegistration bool
2728
ShowRegistrationButton bool
2829
ShowMilestonesDashboardPage bool
@@ -73,7 +74,12 @@ func newService() {
7374
Service.ActiveCodeLives = sec.Key("ACTIVE_CODE_LIVE_MINUTES").MustInt(180)
7475
Service.ResetPwdCodeLives = sec.Key("RESET_PASSWD_CODE_LIVE_MINUTES").MustInt(180)
7576
Service.DisableRegistration = sec.Key("DISABLE_REGISTRATION").MustBool()
77+
Service.AllowOnlyInternalRegistration = sec.Key("ALLOW_ONLY_INTERNAL_REGISTRATION").MustBool()
7678
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+
}
7783
if !sec.Key("REGISTER_EMAIL_CONFIRM").MustBool() {
7884
Service.RegisterManualConfirm = sec.Key("REGISTER_MANUAL_CONFIRM").MustBool(false)
7985
} else {

options/locale/locale_en-US.ini

+1
Original file line numberDiff line numberDiff line change
@@ -2386,6 +2386,7 @@ config.db_path = Path
23862386
config.service_config = Service Configuration
23872387
config.register_email_confirm = Require Email Confirmation to Register
23882388
config.disable_register = Disable Self-Registration
2389+
config.allow_only_internal_registration = Allow Registration Only Through Gitea itself
23892390
config.allow_only_external_registration = Allow Registration Only Through External Services
23902391
config.enable_openid_signup = Enable OpenID Self-Registration
23912392
config.enable_openid_signin = Enable OpenID Sign-In

routers/user/auth.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,7 @@ func LinkAccount(ctx *context.Context) {
754754
ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
755755
ctx.Data["HcaptchaSitekey"] = setting.Service.HcaptchaSitekey
756756
ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration
757+
ctx.Data["AllowOnlyInternalRegistration"] = setting.Service.AllowOnlyInternalRegistration
757758
ctx.Data["ShowRegistrationButton"] = false
758759

759760
// use this to set the right link into the signIn and signUp templates in the link_account template
@@ -912,8 +913,8 @@ func LinkAccountPostRegister(ctx *context.Context) {
912913
return
913914
}
914915

915-
if setting.Service.DisableRegistration {
916-
ctx.Error(403)
916+
if setting.Service.DisableRegistration || setting.Service.AllowOnlyInternalRegistration {
917+
ctx.Error(http.StatusForbidden)
917918
return
918919
}
919920

routers/user/auth_openid.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package user
66

77
import (
88
"fmt"
9+
"net/http"
910
"net/url"
1011

1112
"code.gitea.io/gitea/models"
@@ -250,7 +251,7 @@ func signInOpenIDVerify(ctx *context.Context) {
250251
log.Error("signInOpenIDVerify: Unable to save changes to the session: %v", err)
251252
}
252253

253-
if u != nil || !setting.Service.EnableOpenIDSignUp {
254+
if u != nil || !setting.Service.EnableOpenIDSignUp || setting.Service.AllowOnlyInternalRegistration {
254255
ctx.Redirect(setting.AppSubURL + "/user/openid/connect")
255256
} else {
256257
ctx.Redirect(setting.AppSubURL + "/user/openid/register")
@@ -268,6 +269,7 @@ func ConnectOpenID(ctx *context.Context) {
268269
ctx.Data["PageIsSignIn"] = true
269270
ctx.Data["PageIsOpenIDConnect"] = true
270271
ctx.Data["EnableOpenIDSignUp"] = setting.Service.EnableOpenIDSignUp
272+
ctx.Data["AllowOnlyInternalRegistration"] = setting.Service.AllowOnlyInternalRegistration
271273
ctx.Data["OpenID"] = oid
272274
userName, _ := ctx.Session.Get("openid_determined_username").(string)
273275
if userName != "" {
@@ -329,6 +331,7 @@ func RegisterOpenID(ctx *context.Context) {
329331
ctx.Data["PageIsSignIn"] = true
330332
ctx.Data["PageIsOpenIDRegister"] = true
331333
ctx.Data["EnableOpenIDSignUp"] = setting.Service.EnableOpenIDSignUp
334+
ctx.Data["AllowOnlyInternalRegistration"] = setting.Service.AllowOnlyInternalRegistration
332335
ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
333336
ctx.Data["Captcha"] = context.GetImageCaptcha()
334337
ctx.Data["CaptchaType"] = setting.Service.CaptchaType
@@ -368,6 +371,11 @@ func RegisterOpenIDPost(ctx *context.Context) {
368371
ctx.Data["HcaptchaSitekey"] = setting.Service.HcaptchaSitekey
369372
ctx.Data["OpenID"] = oid
370373

374+
if setting.Service.AllowOnlyInternalRegistration {
375+
ctx.Error(http.StatusForbidden)
376+
return
377+
}
378+
371379
if setting.Service.EnableCaptcha {
372380
var valid bool
373381
var err error

templates/admin/config.tmpl

+2
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@
149149
<dd>{{if .Service.RegisterEmailConfirm}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}</dd>
150150
<dt>{{.i18n.Tr "admin.config.disable_register"}}</dt>
151151
<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>
152154
<dt>{{.i18n.Tr "admin.config.allow_only_external_registration"}}</dt>
153155
<dd>{{if .Service.AllowOnlyExternalRegistration}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}</dd>
154156
<dt>{{.i18n.Tr "admin.config.show_registration_button"}}</dt>

templates/user/auth/link_account.tmpl

+6-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
<div class="ui secondary pointing tabular top attached borderless menu new-menu navbar">
44
<div class="new-menu-inner">
55
<!-- TODO handle .ShowRegistrationButton once other login bugs are fixed -->
6-
<a class="item {{if not .user_exists}}active{{end}}"
7-
data-tab="auth-link-signup-tab">
8-
{{.i18n.Tr "auth.oauth_signup_tab"}}
9-
</a>
6+
{{if not .AllowOnlyInternalRegistration}}
7+
<a class="item {{if not .user_exists}}active{{end}}"
8+
data-tab="auth-link-signup-tab">
9+
{{.i18n.Tr "auth.oauth_signup_tab"}}
10+
</a>
11+
{{end}}
1012
<a class="item {{if .user_exists}}active{{end}}"
1113
data-tab="auth-link-signin-tab">
1214
{{.i18n.Tr "auth.oauth_signin_tab"}}

templates/user/auth/signup_openid_navbar.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<a class="{{if .PageIsOpenIDConnect}}active{{end}} item" href="{{AppSubUrl}}/user/openid/connect">
44
{{.i18n.Tr "auth.openid_connect_title"}}
55
</a>
6-
{{if .EnableOpenIDSignUp}}
6+
{{if and .EnableOpenIDSignUp (not .AllowOnlyInternalRegistration)}}
77
<a class="{{if .PageIsOpenIDRegister}}active{{end}} item" href="{{AppSubUrl}}/user/openid/register">
88
{{.i18n.Tr "auth.openid_register_title"}}
99
</a>

0 commit comments

Comments
 (0)