You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

service.go 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package setting
  4. import (
  5. "regexp"
  6. "strings"
  7. "time"
  8. "code.gitea.io/gitea/modules/log"
  9. "code.gitea.io/gitea/modules/structs"
  10. "github.com/gobwas/glob"
  11. )
  12. // enumerates all the types of captchas
  13. const (
  14. ImageCaptcha = "image"
  15. ReCaptcha = "recaptcha"
  16. HCaptcha = "hcaptcha"
  17. MCaptcha = "mcaptcha"
  18. CfTurnstile = "cfturnstile"
  19. )
  20. // Service settings
  21. var Service = struct {
  22. DefaultUserVisibility string
  23. DefaultUserVisibilityMode structs.VisibleType
  24. AllowedUserVisibilityModes []string
  25. AllowedUserVisibilityModesSlice AllowedVisibility `ini:"-"`
  26. DefaultOrgVisibility string
  27. DefaultOrgVisibilityMode structs.VisibleType
  28. ActiveCodeLives int
  29. ResetPwdCodeLives int
  30. RegisterEmailConfirm bool
  31. RegisterManualConfirm bool
  32. EmailDomainAllowList []glob.Glob
  33. EmailDomainBlockList []glob.Glob
  34. DisableRegistration bool
  35. AllowOnlyInternalRegistration bool
  36. AllowOnlyExternalRegistration bool
  37. ShowRegistrationButton bool
  38. ShowMilestonesDashboardPage bool
  39. RequireSignInView bool
  40. EnableNotifyMail bool
  41. EnableBasicAuth bool
  42. EnableReverseProxyAuth bool
  43. EnableReverseProxyAutoRegister bool
  44. EnableReverseProxyEmail bool
  45. EnableReverseProxyFullName bool
  46. EnableCaptcha bool
  47. RequireCaptchaForLogin bool
  48. RequireExternalRegistrationCaptcha bool
  49. RequireExternalRegistrationPassword bool
  50. CaptchaType string
  51. RecaptchaSecret string
  52. RecaptchaSitekey string
  53. RecaptchaURL string
  54. CfTurnstileSecret string
  55. CfTurnstileSitekey string
  56. HcaptchaSecret string
  57. HcaptchaSitekey string
  58. McaptchaSecret string
  59. McaptchaSitekey string
  60. McaptchaURL string
  61. DefaultKeepEmailPrivate bool
  62. DefaultAllowCreateOrganization bool
  63. DefaultUserIsRestricted bool
  64. EnableTimetracking bool
  65. DefaultEnableTimetracking bool
  66. DefaultEnableDependencies bool
  67. AllowCrossRepositoryDependencies bool
  68. DefaultAllowOnlyContributorsToTrackTime bool
  69. NoReplyAddress string
  70. EnableUserHeatmap bool
  71. AutoWatchNewRepos bool
  72. AutoWatchOnChanges bool
  73. DefaultOrgMemberVisible bool
  74. UserDeleteWithCommentsMaxTime time.Duration
  75. ValidSiteURLSchemes []string
  76. // OpenID settings
  77. EnableOpenIDSignIn bool
  78. EnableOpenIDSignUp bool
  79. OpenIDWhitelist []*regexp.Regexp
  80. OpenIDBlacklist []*regexp.Regexp
  81. // Explore page settings
  82. Explore struct {
  83. RequireSigninView bool `ini:"REQUIRE_SIGNIN_VIEW"`
  84. DisableUsersPage bool `ini:"DISABLE_USERS_PAGE"`
  85. } `ini:"service.explore"`
  86. }{
  87. AllowedUserVisibilityModesSlice: []bool{true, true, true},
  88. }
  89. // AllowedVisibility store in a 3 item bool array what is allowed
  90. type AllowedVisibility []bool
  91. // IsAllowedVisibility check if a AllowedVisibility allow a specific VisibleType
  92. func (a AllowedVisibility) IsAllowedVisibility(t structs.VisibleType) bool {
  93. if int(t) >= len(a) {
  94. return false
  95. }
  96. return a[t]
  97. }
  98. // ToVisibleTypeSlice convert a AllowedVisibility into a VisibleType slice
  99. func (a AllowedVisibility) ToVisibleTypeSlice() (result []structs.VisibleType) {
  100. for i, v := range a {
  101. if v {
  102. result = append(result, structs.VisibleType(i))
  103. }
  104. }
  105. return result
  106. }
  107. func CompileEmailGlobList(sec ConfigSection, keys ...string) (globs []glob.Glob) {
  108. for _, key := range keys {
  109. list := sec.Key(key).Strings(",")
  110. for _, s := range list {
  111. if g, err := glob.Compile(s); err == nil {
  112. globs = append(globs, g)
  113. } else {
  114. log.Error("Skip invalid email allow/block list expression %q: %v", s, err)
  115. }
  116. }
  117. }
  118. return globs
  119. }
  120. func loadServiceFrom(rootCfg ConfigProvider) {
  121. sec := rootCfg.Section("service")
  122. Service.ActiveCodeLives = sec.Key("ACTIVE_CODE_LIVE_MINUTES").MustInt(180)
  123. Service.ResetPwdCodeLives = sec.Key("RESET_PASSWD_CODE_LIVE_MINUTES").MustInt(180)
  124. Service.DisableRegistration = sec.Key("DISABLE_REGISTRATION").MustBool()
  125. Service.AllowOnlyInternalRegistration = sec.Key("ALLOW_ONLY_INTERNAL_REGISTRATION").MustBool()
  126. Service.AllowOnlyExternalRegistration = sec.Key("ALLOW_ONLY_EXTERNAL_REGISTRATION").MustBool()
  127. if Service.AllowOnlyExternalRegistration && Service.AllowOnlyInternalRegistration {
  128. log.Warn("ALLOW_ONLY_INTERNAL_REGISTRATION and ALLOW_ONLY_EXTERNAL_REGISTRATION are true - disabling registration")
  129. Service.DisableRegistration = true
  130. }
  131. if !sec.Key("REGISTER_EMAIL_CONFIRM").MustBool() {
  132. Service.RegisterManualConfirm = sec.Key("REGISTER_MANUAL_CONFIRM").MustBool(false)
  133. } else {
  134. Service.RegisterManualConfirm = false
  135. }
  136. if sec.HasKey("EMAIL_DOMAIN_WHITELIST") {
  137. deprecatedSetting(rootCfg, "service", "EMAIL_DOMAIN_WHITELIST", "service", "EMAIL_DOMAIN_ALLOWLIST", "1.21")
  138. }
  139. Service.EmailDomainAllowList = CompileEmailGlobList(sec, "EMAIL_DOMAIN_WHITELIST", "EMAIL_DOMAIN_ALLOWLIST")
  140. Service.EmailDomainBlockList = CompileEmailGlobList(sec, "EMAIL_DOMAIN_BLOCKLIST")
  141. Service.ShowRegistrationButton = sec.Key("SHOW_REGISTRATION_BUTTON").MustBool(!(Service.DisableRegistration || Service.AllowOnlyExternalRegistration))
  142. Service.ShowMilestonesDashboardPage = sec.Key("SHOW_MILESTONES_DASHBOARD_PAGE").MustBool(true)
  143. Service.RequireSignInView = sec.Key("REQUIRE_SIGNIN_VIEW").MustBool()
  144. Service.EnableBasicAuth = sec.Key("ENABLE_BASIC_AUTHENTICATION").MustBool(true)
  145. Service.EnableReverseProxyAuth = sec.Key("ENABLE_REVERSE_PROXY_AUTHENTICATION").MustBool()
  146. Service.EnableReverseProxyAutoRegister = sec.Key("ENABLE_REVERSE_PROXY_AUTO_REGISTRATION").MustBool()
  147. Service.EnableReverseProxyEmail = sec.Key("ENABLE_REVERSE_PROXY_EMAIL").MustBool()
  148. Service.EnableReverseProxyFullName = sec.Key("ENABLE_REVERSE_PROXY_FULL_NAME").MustBool()
  149. Service.EnableCaptcha = sec.Key("ENABLE_CAPTCHA").MustBool(false)
  150. Service.RequireCaptchaForLogin = sec.Key("REQUIRE_CAPTCHA_FOR_LOGIN").MustBool(false)
  151. Service.RequireExternalRegistrationCaptcha = sec.Key("REQUIRE_EXTERNAL_REGISTRATION_CAPTCHA").MustBool(Service.EnableCaptcha)
  152. Service.RequireExternalRegistrationPassword = sec.Key("REQUIRE_EXTERNAL_REGISTRATION_PASSWORD").MustBool()
  153. Service.CaptchaType = sec.Key("CAPTCHA_TYPE").MustString(ImageCaptcha)
  154. Service.RecaptchaSecret = sec.Key("RECAPTCHA_SECRET").MustString("")
  155. Service.RecaptchaSitekey = sec.Key("RECAPTCHA_SITEKEY").MustString("")
  156. Service.RecaptchaURL = sec.Key("RECAPTCHA_URL").MustString("https://www.google.com/recaptcha/")
  157. Service.CfTurnstileSecret = sec.Key("CF_TURNSTILE_SECRET").MustString("")
  158. Service.CfTurnstileSitekey = sec.Key("CF_TURNSTILE_SITEKEY").MustString("")
  159. Service.HcaptchaSecret = sec.Key("HCAPTCHA_SECRET").MustString("")
  160. Service.HcaptchaSitekey = sec.Key("HCAPTCHA_SITEKEY").MustString("")
  161. Service.McaptchaURL = sec.Key("MCAPTCHA_URL").MustString("https://demo.mcaptcha.org/")
  162. Service.McaptchaSecret = sec.Key("MCAPTCHA_SECRET").MustString("")
  163. Service.McaptchaSitekey = sec.Key("MCAPTCHA_SITEKEY").MustString("")
  164. Service.DefaultKeepEmailPrivate = sec.Key("DEFAULT_KEEP_EMAIL_PRIVATE").MustBool()
  165. Service.DefaultAllowCreateOrganization = sec.Key("DEFAULT_ALLOW_CREATE_ORGANIZATION").MustBool(true)
  166. Service.DefaultUserIsRestricted = sec.Key("DEFAULT_USER_IS_RESTRICTED").MustBool(false)
  167. Service.EnableTimetracking = sec.Key("ENABLE_TIMETRACKING").MustBool(true)
  168. if Service.EnableTimetracking {
  169. Service.DefaultEnableTimetracking = sec.Key("DEFAULT_ENABLE_TIMETRACKING").MustBool(true)
  170. }
  171. Service.DefaultEnableDependencies = sec.Key("DEFAULT_ENABLE_DEPENDENCIES").MustBool(true)
  172. Service.AllowCrossRepositoryDependencies = sec.Key("ALLOW_CROSS_REPOSITORY_DEPENDENCIES").MustBool(true)
  173. Service.DefaultAllowOnlyContributorsToTrackTime = sec.Key("DEFAULT_ALLOW_ONLY_CONTRIBUTORS_TO_TRACK_TIME").MustBool(true)
  174. Service.NoReplyAddress = sec.Key("NO_REPLY_ADDRESS").MustString("noreply." + Domain)
  175. Service.EnableUserHeatmap = sec.Key("ENABLE_USER_HEATMAP").MustBool(true)
  176. Service.AutoWatchNewRepos = sec.Key("AUTO_WATCH_NEW_REPOS").MustBool(true)
  177. Service.AutoWatchOnChanges = sec.Key("AUTO_WATCH_ON_CHANGES").MustBool(false)
  178. modes := sec.Key("ALLOWED_USER_VISIBILITY_MODES").Strings(",")
  179. if len(modes) != 0 {
  180. Service.AllowedUserVisibilityModes = []string{}
  181. Service.AllowedUserVisibilityModesSlice = []bool{false, false, false}
  182. for _, sMode := range modes {
  183. if tp, ok := structs.VisibilityModes[sMode]; ok { // remove unsupported modes
  184. Service.AllowedUserVisibilityModes = append(Service.AllowedUserVisibilityModes, sMode)
  185. Service.AllowedUserVisibilityModesSlice[tp] = true
  186. } else {
  187. log.Warn("ALLOWED_USER_VISIBILITY_MODES %s is unsupported", sMode)
  188. }
  189. }
  190. }
  191. if len(Service.AllowedUserVisibilityModes) == 0 {
  192. Service.AllowedUserVisibilityModes = []string{"public", "limited", "private"}
  193. Service.AllowedUserVisibilityModesSlice = []bool{true, true, true}
  194. }
  195. Service.DefaultUserVisibility = sec.Key("DEFAULT_USER_VISIBILITY").String()
  196. if Service.DefaultUserVisibility == "" {
  197. Service.DefaultUserVisibility = Service.AllowedUserVisibilityModes[0]
  198. } else if !Service.AllowedUserVisibilityModesSlice[structs.VisibilityModes[Service.DefaultUserVisibility]] {
  199. log.Warn("DEFAULT_USER_VISIBILITY %s is wrong or not in ALLOWED_USER_VISIBILITY_MODES, using first allowed", Service.DefaultUserVisibility)
  200. Service.DefaultUserVisibility = Service.AllowedUserVisibilityModes[0]
  201. }
  202. Service.DefaultUserVisibilityMode = structs.VisibilityModes[Service.DefaultUserVisibility]
  203. Service.DefaultOrgVisibility = sec.Key("DEFAULT_ORG_VISIBILITY").In("public", structs.ExtractKeysFromMapString(structs.VisibilityModes))
  204. Service.DefaultOrgVisibilityMode = structs.VisibilityModes[Service.DefaultOrgVisibility]
  205. Service.DefaultOrgMemberVisible = sec.Key("DEFAULT_ORG_MEMBER_VISIBLE").MustBool()
  206. Service.UserDeleteWithCommentsMaxTime = sec.Key("USER_DELETE_WITH_COMMENTS_MAX_TIME").MustDuration(0)
  207. sec.Key("VALID_SITE_URL_SCHEMES").MustString("http,https")
  208. Service.ValidSiteURLSchemes = sec.Key("VALID_SITE_URL_SCHEMES").Strings(",")
  209. schemes := make([]string, len(Service.ValidSiteURLSchemes))
  210. for _, scheme := range Service.ValidSiteURLSchemes {
  211. scheme = strings.ToLower(strings.TrimSpace(scheme))
  212. if scheme != "" {
  213. schemes = append(schemes, scheme)
  214. }
  215. }
  216. Service.ValidSiteURLSchemes = schemes
  217. mustMapSetting(rootCfg, "service.explore", &Service.Explore)
  218. loadOpenIDSetting(rootCfg)
  219. }
  220. func loadOpenIDSetting(rootCfg ConfigProvider) {
  221. sec := rootCfg.Section("openid")
  222. Service.EnableOpenIDSignIn = sec.Key("ENABLE_OPENID_SIGNIN").MustBool(!InstallLock)
  223. Service.EnableOpenIDSignUp = sec.Key("ENABLE_OPENID_SIGNUP").MustBool(!Service.DisableRegistration && Service.EnableOpenIDSignIn)
  224. pats := sec.Key("WHITELISTED_URIS").Strings(" ")
  225. if len(pats) != 0 {
  226. Service.OpenIDWhitelist = make([]*regexp.Regexp, len(pats))
  227. for i, p := range pats {
  228. Service.OpenIDWhitelist[i] = regexp.MustCompilePOSIX(p)
  229. }
  230. }
  231. pats = sec.Key("BLACKLISTED_URIS").Strings(" ")
  232. if len(pats) != 0 {
  233. Service.OpenIDBlacklist = make([]*regexp.Regexp, len(pats))
  234. for i, p := range pats {
  235. Service.OpenIDBlacklist[i] = regexp.MustCompilePOSIX(p)
  236. }
  237. }
  238. }