Skip to content

Commit f67efda

Browse files
unknwonachilleas-k
authored andcommitted
conf: overhaul auth and user settings (#5942)
* conf: overhaul auth and user settings * ci: update travis Go versions
1 parent 203340c commit f67efda

28 files changed

+236
-194
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
os: linux
22
language: go
33
go:
4-
- 1.12.x
54
- 1.13.x
5+
- 1.14.x
66
go_import_path: gogs.io/gogs
77

88
env:

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@ All notable changes to Gogs are documented in this file.
2222
- Configuration option `[server] LANDING_PAGE` is deprecated and will end support in 0.13.0, please start using `[server] LANDING_URL`.
2323
- Configuration option `[database] DB_TYPE` is deprecated and will end support in 0.13.0, please start using `[database] TYPE`.
2424
- Configuration option `[database] PASSWD` is deprecated and will end support in 0.13.0, please start using `[database] PASSWORD`.
25+
- Configuration option `[security] REVERSE_PROXY_AUTHENTICATION_USER` is deprecated and will end support in 0.13.0, please start using `[auth] REVERSE_PROXY_AUTHENTICATION_HEADER`.
2526
- Configuration section `[mailer]` is deprecated and will end support in 0.13.0, please start using `[email]`.
27+
- Configuration section `[service]` is deprecated and will end support in 0.13.0, please start using `[auth]`.
28+
- Configuration option `[auth] ACTIVE_CODE_LIVE_MINUTES` is deprecated and will end support in 0.13.0, please start using `[auth] ACTIVATE_CODE_LIVES`.
29+
- Configuration option `[auth] RESET_PASSWD_CODE_LIVE_MINUTES` is deprecated and will end support in 0.13.0, please start using `[auth] RESET_PASSWORD_CODE_LIVES`.
30+
- Configuration option `[auth] ENABLE_CAPTCHA` is deprecated and will end support in 0.13.0, please start using `[auth] ENABLE_REGISTRATION_CAPTCHA`.
31+
- Configuration option `[auth] ENABLE_NOTIFY_MAIL` is deprecated and will end support in 0.13.0, please start using `[user] ENABLE_EMAIL_NOTIFICATION`.
2632

2733
### Fixed
2834

conf/app.ini

+26-19
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,6 @@ COOKIE_REMEMBER_NAME = gogs_incredible
172172
COOKIE_USERNAME = gogs_awesome
173173
; Whether to set secure cookie.
174174
COOKIE_SECURE = false
175-
; The HTTP header for reverse proxy authentication via username.
176-
REVERSE_PROXY_AUTHENTICATION_USER = X-WEBAUTH-USER
177175
; Whether to set cookie to indicate user login status.
178176
ENABLE_LOGIN_STATUS_COOKIE = false
179177
; The cookie name to store user login status.
@@ -213,6 +211,32 @@ USE_PLAIN_TEXT = false
213211
; It is used to support older mail clients and make spam filters happier.
214212
ADD_PLAIN_TEXT_ALT = false
215213

214+
[auth]
215+
; The valid duration of activate code in minutes.
216+
ACTIVATE_CODE_LIVES = 180
217+
; The valid duration of reset password code in minutes.
218+
RESET_PASSWORD_CODE_LIVES = 180
219+
; Whether to require email confirmation for adding new email addresses.
220+
; Enable this option will also require user to confirm the email for registration.
221+
REQUIRE_EMAIL_CONFIRMATION = false
222+
; Whether to disallow anonymous users visiting the site.
223+
REQUIRE_SIGNIN_VIEW = false
224+
; Whether to disable self-registration. When disabled, accounts would have to be created by admins.
225+
DISABLE_REGISTRATION = false
226+
; Whether to enable captcha validation for registration
227+
ENABLE_REGISTRATION_CAPTCHA = true
228+
229+
; Whether to enable reverse proxy authentication via HTTP header.
230+
ENABLE_REVERSE_PROXY_AUTHENTICATION = false
231+
; Whether to automatically create new users for reverse proxy authentication.
232+
ENABLE_REVERSE_PROXY_AUTO_REGISTRATION = false
233+
; The HTTP header used as username for reverse proxy authentication.
234+
REVERSE_PROXY_AUTHENTICATION_HEADER = X-WEBAUTH-USER
235+
236+
[user]
237+
; Whether to enable email notifications for users.
238+
ENABLE_EMAIL_NOTIFICATION = false
239+
216240
; Attachment settings for releases
217241
[release.attachment]
218242
; Whether attachments are enabled. Defaults to `true`
@@ -251,23 +275,6 @@ ACCESS_CONTROL_ALLOW_ORIGIN =
251275
; Disable regular (non-admin) users to create organizations
252276
DISABLE_REGULAR_ORG_CREATION = false
253277

254-
[service]
255-
ACTIVE_CODE_LIVE_MINUTES = 180
256-
RESET_PASSWD_CODE_LIVE_MINUTES = 180
257-
; User need to confirm e-mail for registration
258-
REGISTER_EMAIL_CONFIRM = false
259-
; Does not allow register and admin create account only
260-
DISABLE_REGISTRATION = false
261-
; User must sign in to view anything.
262-
REQUIRE_SIGNIN_VIEW = false
263-
; Mail notification
264-
ENABLE_NOTIFY_MAIL = false
265-
; More detail: https://github.com/gogits/gogs/issues/165
266-
ENABLE_REVERSE_PROXY_AUTHENTICATION = false
267-
ENABLE_REVERSE_PROXY_AUTO_REGISTRATION = false
268-
; Enable captcha validation for registration
269-
ENABLE_CAPTCHA = false
270-
271278
[webhook]
272279
; Types are enabled for users to use, can be "gogs", "slack", "discord", "dingtalk"
273280
TYPES = gogs, slack, discord, dingtalk

conf/locale/locale_en-US.ini

+14-10
Original file line numberDiff line numberDiff line change
@@ -1270,21 +1270,25 @@ config.email.send_test_mail = Send test email
12701270
config.email.test_mail_failed = Failed to send test email to '%s': %v
12711271
config.email.test_mail_sent = Test email has been sent to '%s'.
12721272
1273+
config.auth_config = Authentication configuration
1274+
config.auth.activate_code_lives = Activate code lives
1275+
config.auth.reset_password_code_lives = Reset password code lives
1276+
config.auth.require_email_confirm = Require email confirmation
1277+
config.auth.require_sign_in_view = Require sign in view
1278+
config.auth.disable_registration = Disable registration
1279+
config.auth.enable_registration_captcha = Enable registration captcha
1280+
config.auth.enable_reverse_proxy_authentication = Enable reverse proxy authentication
1281+
config.auth.enable_reverse_proxy_auto_registration = Enable reverse proxy auto registration
1282+
config.auth.reverse_proxy_authentication_header = Reverse proxy authentication header
1283+
1284+
config.user_config = User configuration
1285+
config.user.enable_email_notify = Enable email notification
1286+
12731287
config.log_file_root_path = Log File Root Path
12741288
12751289
config.http_config = HTTP Configuration
12761290
config.http_access_control_allow_origin = Access Control Allow Origin
12771291
1278-
config.service_config = Service Configuration
1279-
config.register_email_confirm = Require Email Confirmation
1280-
config.disable_register = Disable Registration
1281-
config.show_registration_button = Show Register Button
1282-
config.require_sign_in_view = Require Sign In View
1283-
config.mail_notify = Mail Notification
1284-
config.disable_key_size_check = Disable Minimum Key Size Check
1285-
config.enable_captcha = Enable Captcha
1286-
config.active_code_lives = Active Code Lives
1287-
config.reset_password_code_lives = Reset Password Code Lives
12881292
12891293
config.webhook_config = Webhook Configuration
12901294
config.queue_length = Queue Length

internal/assets/conf/conf_gen.go

+8-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/assets/templates/templates_gen.go

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/auth/auth.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func SignedInUser(ctx *macaron.Context, sess session.Store) (_ *db.User, isBasic
9090
uid, isTokenAuth := SignedInID(ctx, sess)
9191

9292
if uid <= 0 {
93-
if conf.Service.EnableReverseProxyAuth {
93+
if conf.Auth.EnableReverseProxyAuthentication {
9494
webAuthUser := ctx.Req.Header.Get(conf.Security.ReverseProxyAuthenticationUser)
9595
if len(webAuthUser) > 0 {
9696
u, err := db.GetUserByName(webAuthUser)
@@ -101,7 +101,7 @@ func SignedInUser(ctx *macaron.Context, sess session.Store) (_ *db.User, isBasic
101101
}
102102

103103
// Check if enabled auto-registration.
104-
if conf.Service.EnableReverseProxyAutoRegister {
104+
if conf.Auth.EnableReverseProxyAutoRegistration {
105105
u := &db.User{
106106
Name: webAuthUser,
107107
Email: gouuid.NewV4().String() + "@localhost",

internal/cmd/hook.go

-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,6 @@ func runHookPostReceive(c *cli.Context) error {
198198

199199
// Post-receive hook does more than just gather Git information,
200200
// so we need to setup additional services for email notifications.
201-
conf.NewPostReceiveHookServices()
202201
email.NewContext()
203202

204203
isWiki := strings.Contains(os.Getenv(db.ENV_REPO_CUSTOM_HOOKS_PATH), ".wiki.git/")

internal/cmd/serv.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,11 @@ func runServ(c *cli.Context) error {
235235
}
236236
}
237237
} else {
238-
conf.NewService()
239238
// Check if the key can access to the repository in case of it is a deploy key (a deploy keys != user key).
240-
// A deploy key doesn't represent a signed in user, so in a site with Service.RequireSignInView activated
241-
// we should give read access only in repositories where this deploy key is in use. In other case, a server
242-
// or system using an active deploy key can get read access to all the repositories in a Gogs service.
243-
if key.IsDeployKey() && conf.Service.RequireSignInView {
239+
// A deploy key doesn't represent a signed in user, so in a site with Auth.RequireSignInView enabled,
240+
// we should give read access only in repositories where this deploy key is in use. In other cases,
241+
// a server or system using an active deploy key can get read access to all repositories on a Gogs instace.
242+
if key.IsDeployKey() && conf.Auth.RequireSigninView {
244243
checkDeployKey(key, repo)
245244
}
246245
}

internal/cmd/web.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ func runWeb(c *cli.Context) error {
177177
m := newMacaron()
178178

179179
reqSignIn := context.Toggle(&context.ToggleOptions{SignInRequired: true})
180-
ignSignIn := context.Toggle(&context.ToggleOptions{SignInRequired: conf.Service.RequireSignInView})
180+
ignSignIn := context.Toggle(&context.ToggleOptions{SignInRequired: conf.Auth.RequireSigninView})
181181
ignSignInAndCsrf := context.Toggle(&context.ToggleOptions{DisableCSRF: true})
182182
reqSignOut := context.Toggle(&context.ToggleOptions{SignOutRequired: true})
183183

internal/conf/conf.go

+23-68
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ var File *ini.File
6161
// It is safe to call this function multiple times with desired `customConf`, but it is
6262
// not concurrent safe.
6363
//
64+
// NOTE: The order of loading configuration sections matters as one may depend on another.
65+
//
6466
// ⚠️ WARNING: Do not print anything in this function other than wanrings.
6567
func Init(customConf string) error {
6668
var err error
@@ -232,6 +234,26 @@ func Init(customConf string) error {
232234
Email.FromEmail = parsed.Address
233235
}
234236

237+
// ***********************************
238+
// ----- Authentication settings -----
239+
// ***********************************
240+
241+
if err = File.Section("auth").MapTo(&Auth); err != nil {
242+
return errors.Wrap(err, "mapping [auth] section")
243+
}
244+
// LEGACY [0.13]: In case there are values with old section name.
245+
if err = File.Section("service").MapTo(&Auth); err != nil {
246+
return errors.Wrap(err, "mapping [service] section")
247+
}
248+
249+
// ***********************************
250+
// ----- User settings -----
251+
// ***********************************
252+
253+
if err = File.Section("user").MapTo(&User); err != nil {
254+
return errors.Wrap(err, "mapping [user] section")
255+
}
256+
235257
handleDeprecated()
236258

237259
// TODO
@@ -690,31 +712,6 @@ func InitLogging() {
690712
}
691713
}
692714

693-
var Service struct {
694-
ActiveCodeLives int
695-
ResetPwdCodeLives int
696-
RegisterEmailConfirm bool
697-
DisableRegistration bool
698-
ShowRegistrationButton bool
699-
RequireSignInView bool
700-
EnableNotifyMail bool
701-
EnableReverseProxyAuth bool
702-
EnableReverseProxyAutoRegister bool
703-
EnableCaptcha bool
704-
}
705-
706-
func newService() {
707-
sec := File.Section("service")
708-
Service.ActiveCodeLives = sec.Key("ACTIVE_CODE_LIVE_MINUTES").MustInt(180)
709-
Service.ResetPwdCodeLives = sec.Key("RESET_PASSWD_CODE_LIVE_MINUTES").MustInt(180)
710-
Service.DisableRegistration = sec.Key("DISABLE_REGISTRATION").MustBool()
711-
Service.ShowRegistrationButton = sec.Key("SHOW_REGISTRATION_BUTTON").MustBool(!Service.DisableRegistration)
712-
Service.RequireSignInView = sec.Key("REQUIRE_SIGNIN_VIEW").MustBool()
713-
Service.EnableReverseProxyAuth = sec.Key("ENABLE_REVERSE_PROXY_AUTHENTICATION").MustBool()
714-
Service.EnableReverseProxyAutoRegister = sec.Key("ENABLE_REVERSE_PROXY_AUTO_REGISTRATION").MustBool()
715-
Service.EnableCaptcha = sec.Key("ENABLE_CAPTCHA").MustBool()
716-
}
717-
718715
func newCacheService() {
719716
CacheAdapter = File.Section("cache").Key("ADAPTER").In("memory", []string{"memory", "redis", "memcache"})
720717
switch CacheAdapter {
@@ -744,53 +741,11 @@ func newSessionService() {
744741
log.Trace("Session service is enabled")
745742
}
746743

747-
func newRegisterMailService() {
748-
if !File.Section("service").Key("REGISTER_EMAIL_CONFIRM").MustBool() {
749-
return
750-
} else if !Email.Enabled {
751-
log.Warn("Email confirmation is not enabled due to the mail service is not available")
752-
return
753-
}
754-
Service.RegisterEmailConfirm = true
755-
log.Trace("Email confirmation is enabled")
756-
}
757-
758-
// newNotifyMailService initializes notification email service options from configuration.
759-
// No non-error log will be printed in hook mode.
760-
func newNotifyMailService() {
761-
if !File.Section("service").Key("ENABLE_NOTIFY_MAIL").MustBool() {
762-
return
763-
} else if !Email.Enabled {
764-
log.Warn("Email notification is not enabled due to the mail service is not available")
765-
return
766-
}
767-
Service.EnableNotifyMail = true
768-
769-
if HookMode {
770-
return
771-
}
772-
log.Trace("Email notification is enabled")
773-
}
774-
775-
func NewService() {
776-
newService()
777-
}
778-
779744
func NewServices() {
780-
newService()
781745
newCacheService()
782746
newSessionService()
783-
newRegisterMailService()
784-
newNotifyMailService()
785747
}
786748

787749
// HookMode indicates whether program starts as Git server-side hook callback.
750+
// All operations should be done synchronously to prevent program exits before finishing.
788751
var HookMode bool
789-
790-
// NewPostReceiveHookServices initializes all services that are needed by
791-
// Git server-side post-receive hook callback.
792-
func NewPostReceiveHookServices() {
793-
HookMode = true
794-
newService()
795-
newNotifyMailService()
796-
}

internal/conf/static.go

+66-8
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,17 @@ var (
149149

150150
// Security settings
151151
Security struct {
152-
InstallLock bool
153-
SecretKey string
154-
LoginRememberDays int
155-
CookieRememberName string
156-
CookieUsername string
157-
CookieSecure bool
152+
InstallLock bool
153+
SecretKey string
154+
LoginRememberDays int
155+
CookieRememberName string
156+
CookieUsername string
157+
CookieSecure bool
158+
EnableLoginStatusCookie bool
159+
LoginStatusCookieName string
160+
161+
// Deprecated: Use Auth.ReverseProxyAuthenticationHeader instead, will be removed in 0.13.
158162
ReverseProxyAuthenticationUser string
159-
EnableLoginStatusCookie bool
160-
LoginStatusCookieName string
161163
}
162164

163165
// Email settings
@@ -186,6 +188,36 @@ var (
186188
// Deprecated: Use Password instead, will be removed in 0.13.
187189
Passwd string
188190
}
191+
192+
// Authentication settings
193+
Auth struct {
194+
ActivateCodeLives int
195+
ResetPasswordCodeLives int
196+
RequireEmailConfirmation bool
197+
RequireSigninView bool
198+
DisableRegistration bool
199+
EnableRegistrationCaptcha bool
200+
201+
EnableReverseProxyAuthentication bool
202+
EnableReverseProxyAutoRegistration bool
203+
ReverseProxyAuthenticationHeader string
204+
205+
// Deprecated: Use ActivateCodeLives instead, will be removed in 0.13.
206+
ActiveCodeLiveMinutes int
207+
// Deprecated: Use ResetPasswordCodeLives instead, will be removed in 0.13.
208+
ResetPasswdCodeLiveMinutes int
209+
// Deprecated: Use RequireEmailConfirmation instead, will be removed in 0.13.
210+
RegisterEmailConfirm bool
211+
// Deprecated: Use EnableRegistrationCaptcha instead, will be removed in 0.13.
212+
EnableCaptcha bool
213+
// Deprecated: Use User.EnableEmailNotification instead, will be removed in 0.13.
214+
EnableNotifyMail bool
215+
}
216+
217+
// User settings
218+
User struct {
219+
EnableEmailNotification bool
220+
}
189221
)
190222

191223
// handleDeprecated transfers deprecated values to the new ones when set.
@@ -217,4 +249,30 @@ func handleDeprecated() {
217249
Email.Password = Email.Passwd
218250
Email.Passwd = ""
219251
}
252+
253+
if Auth.ActiveCodeLiveMinutes > 0 {
254+
Auth.ActivateCodeLives = Auth.ActiveCodeLiveMinutes
255+
Auth.ActiveCodeLiveMinutes = 0
256+
}
257+
if Auth.ResetPasswdCodeLiveMinutes > 0 {
258+
Auth.ResetPasswordCodeLives = Auth.ResetPasswdCodeLiveMinutes
259+
Auth.ResetPasswdCodeLiveMinutes = 0
260+
}
261+
if Auth.RegisterEmailConfirm {
262+
Auth.RequireEmailConfirmation = true
263+
Auth.RegisterEmailConfirm = false
264+
}
265+
if Auth.EnableCaptcha {
266+
Auth.EnableRegistrationCaptcha = true
267+
Auth.EnableCaptcha = false
268+
}
269+
if Security.ReverseProxyAuthenticationUser != "" {
270+
Auth.ReverseProxyAuthenticationHeader = Security.ReverseProxyAuthenticationUser
271+
Security.ReverseProxyAuthenticationUser = ""
272+
}
273+
274+
if Auth.EnableNotifyMail {
275+
User.EnableEmailNotification = true
276+
Auth.EnableNotifyMail = false
277+
}
220278
}

0 commit comments

Comments
 (0)