Skip to content

Commit b97af15

Browse files
adelowotechknowlogick
authored andcommitted
Block registration based on email domain (#5157)
* implement email domain whitelist
1 parent 4c1f1f9 commit b97af15

File tree

7 files changed

+106
-0
lines changed

7 files changed

+106
-0
lines changed

custom/conf/app.ini.sample

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,9 @@ ACTIVE_CODE_LIVE_MINUTES = 180
311311
RESET_PASSWD_CODE_LIVE_MINUTES = 180
312312
; Whether a new user needs to confirm their email when registering.
313313
REGISTER_EMAIL_CONFIRM = false
314+
; List of domain names that are allowed to be used to register on a Gitea instance
315+
; gitea.io,example.com
316+
EMAIL_DOMAIN_WHITELIST=
314317
; Disallow registration, only allow admins to create accounts.
315318
DISABLE_REGISTRATION = false
316319
; Allow registration only using third part services, it works only when DISABLE_REGISTRATION is false

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
194194
- `RECAPTCHA_SITEKEY`: **""**: Go to https://www.google.com/recaptcha/admin to get a sitekey for recaptcha.
195195
- `DEFAULT_ENABLE_DEPENDENCIES`: **true** Enable this to have dependencies enabled by default.
196196
- `ENABLE_USER_HEATMAP`: **true** Enable this to display the heatmap on users profiles.
197+
- `EMAIL_DOMAIN_WHITELIST`: **\<empty\>**: If non-empty, list of domain names that can only be used to register
198+
on this instance.
197199

198200
## Webhook (`webhook`)
199201

modules/auth/user_form.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
// Copyright 2014 The Gogs Authors. All rights reserved.
2+
// Copyright 2018 The Gitea Authors. All rights reserved.
23
// Use of this source code is governed by a MIT-style
34
// license that can be found in the LICENSE file.
45

56
package auth
67

78
import (
89
"mime/multipart"
10+
"strings"
11+
12+
"code.gitea.io/gitea/modules/setting"
913

1014
"github.com/go-macaron/binding"
1115
"gopkg.in/macaron.v1"
@@ -84,6 +88,31 @@ func (f *RegisterForm) Validate(ctx *macaron.Context, errs binding.Errors) bindi
8488
return validate(errs, ctx.Data, f, ctx.Locale)
8589
}
8690

91+
// IsEmailDomainWhitelisted validates that the email address
92+
// provided by the user matches what has been configured .
93+
// If the domain whitelist from the config is empty, it marks the
94+
// email as whitelisted
95+
func (f RegisterForm) IsEmailDomainWhitelisted() bool {
96+
if len(setting.Service.EmailDomainWhitelist) == 0 {
97+
return true
98+
}
99+
100+
n := strings.LastIndex(f.Email, "@")
101+
if n <= 0 {
102+
return false
103+
}
104+
105+
domain := strings.ToLower(f.Email[n+1:])
106+
107+
for _, v := range setting.Service.EmailDomainWhitelist {
108+
if strings.ToLower(v) == domain {
109+
return true
110+
}
111+
}
112+
113+
return false
114+
}
115+
87116
// MustChangePasswordForm form for updating your password after account creation
88117
// by an admin
89118
type MustChangePasswordForm struct {

modules/auth/user_form_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright 2018 The Gogs Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package auth
6+
7+
import (
8+
"testing"
9+
10+
"code.gitea.io/gitea/modules/setting"
11+
12+
"github.com/stretchr/testify/assert"
13+
)
14+
15+
func TestRegisterForm_IsDomainWhiteList_Empty(t *testing.T) {
16+
_ = setting.Service
17+
18+
setting.Service.EmailDomainWhitelist = []string{}
19+
20+
form := RegisterForm{}
21+
22+
assert.True(t, form.IsEmailDomainWhitelisted())
23+
}
24+
25+
func TestRegisterForm_IsDomainWhiteList_InvalidEmail(t *testing.T) {
26+
_ = setting.Service
27+
28+
setting.Service.EmailDomainWhitelist = []string{"gitea.io"}
29+
30+
tt := []struct {
31+
email string
32+
}{
33+
{"securitygieqqq"},
34+
{"hdudhdd"},
35+
}
36+
37+
for _, v := range tt {
38+
form := RegisterForm{Email: v.email}
39+
40+
assert.False(t, form.IsEmailDomainWhitelisted())
41+
}
42+
}
43+
44+
func TestRegisterForm_IsDomainWhiteList_ValidEmail(t *testing.T) {
45+
_ = setting.Service
46+
47+
setting.Service.EmailDomainWhitelist = []string{"gitea.io"}
48+
49+
tt := []struct {
50+
email string
51+
valid bool
52+
}{
53+
{"[email protected]", true},
54+
{"[email protected]", true},
55+
{"hdudhdd", false},
56+
{"[email protected]", false},
57+
}
58+
59+
for _, v := range tt {
60+
form := RegisterForm{Email: v.email}
61+
62+
assert.Equal(t, v.valid, form.IsEmailDomainWhitelisted())
63+
}
64+
}

modules/setting/setting.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,6 +1215,7 @@ var Service struct {
12151215
ActiveCodeLives int
12161216
ResetPwdCodeLives int
12171217
RegisterEmailConfirm bool
1218+
EmailDomainWhitelist []string
12181219
DisableRegistration bool
12191220
AllowOnlyExternalRegistration bool
12201221
ShowRegistrationButton bool
@@ -1248,6 +1249,7 @@ func newService() {
12481249
Service.ResetPwdCodeLives = sec.Key("RESET_PASSWD_CODE_LIVE_MINUTES").MustInt(180)
12491250
Service.DisableRegistration = sec.Key("DISABLE_REGISTRATION").MustBool()
12501251
Service.AllowOnlyExternalRegistration = sec.Key("ALLOW_ONLY_EXTERNAL_REGISTRATION").MustBool()
1252+
Service.EmailDomainWhitelist = sec.Key("EMAIL_DOMAIN_WHITELIST").Strings(",")
12511253
Service.ShowRegistrationButton = sec.Key("SHOW_REGISTRATION_BUTTON").MustBool(!(Service.DisableRegistration || Service.AllowOnlyExternalRegistration))
12521254
Service.RequireSignInView = sec.Key("REQUIRE_SIGNIN_VIEW").MustBool()
12531255
Service.EnableReverseProxyAuth = sec.Key("ENABLE_REVERSE_PROXY_AUTHENTICATION").MustBool()

options/locale/locale_en-US.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ openid_register_title = Create new account
242242
openid_register_desc = The chosen OpenID URI is unknown. Associate it with a new account here.
243243
openid_signin_desc = Enter your OpenID URI. For example: https://anne.me, bob.openid.org.cn or gnusocial.net/carry.
244244
disable_forgot_password_mail = Password reset is disabled. Please contact your site administrator.
245+
email_domain_blacklisted = You cannot register with your email address.
245246
246247
[mail]
247248
activate_account = Please activate your account

routers/user/auth.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,11 @@ func SignUpPost(ctx *context.Context, cpt *captcha.Captcha, form auth.RegisterFo
948948
}
949949
}
950950

951+
if !form.IsEmailDomainWhitelisted() {
952+
ctx.RenderWithErr(ctx.Tr("auth.email_domain_blacklisted"), tplSignUp, &form)
953+
return
954+
}
955+
951956
if form.Password != form.Retype {
952957
ctx.Data["Err_Password"] = true
953958
ctx.RenderWithErr(ctx.Tr("form.password_not_match"), tplSignUp, &form)

0 commit comments

Comments
 (0)