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.

session.go 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package setting
  4. import (
  5. "net/http"
  6. "path"
  7. "path/filepath"
  8. "strings"
  9. "code.gitea.io/gitea/modules/json"
  10. "code.gitea.io/gitea/modules/log"
  11. )
  12. // SessionConfig defines Session settings
  13. var SessionConfig = struct {
  14. OriginalProvider string
  15. Provider string
  16. // Provider configuration, it's corresponding to provider.
  17. ProviderConfig string
  18. // Cookie name to save session ID. Default is "MacaronSession".
  19. CookieName string
  20. // Cookie path to store. Default is "/". HINT: there was a bug, the old value doesn't have trailing slash, and could be empty "".
  21. CookiePath string
  22. // GC interval time in seconds. Default is 3600.
  23. Gclifetime int64
  24. // Max life time in seconds. Default is whatever GC interval time is.
  25. Maxlifetime int64
  26. // Use HTTPS only. Default is false.
  27. Secure bool
  28. // Cookie domain name. Default is empty.
  29. Domain string
  30. // SameSite declares if your cookie should be restricted to a first-party or same-site context. Valid strings are "none", "lax", "strict". Default is "lax"
  31. SameSite http.SameSite
  32. }{
  33. CookieName: "i_like_gitea",
  34. Gclifetime: 86400,
  35. Maxlifetime: 86400,
  36. SameSite: http.SameSiteLaxMode,
  37. }
  38. func loadSessionFrom(rootCfg ConfigProvider) {
  39. sec := rootCfg.Section("session")
  40. SessionConfig.Provider = sec.Key("PROVIDER").In("memory",
  41. []string{"memory", "file", "redis", "mysql", "postgres", "couchbase", "memcache", "db"})
  42. SessionConfig.ProviderConfig = strings.Trim(sec.Key("PROVIDER_CONFIG").MustString(path.Join(AppDataPath, "sessions")), "\" ")
  43. if SessionConfig.Provider == "file" && !filepath.IsAbs(SessionConfig.ProviderConfig) {
  44. SessionConfig.ProviderConfig = path.Join(AppWorkPath, SessionConfig.ProviderConfig)
  45. }
  46. SessionConfig.CookieName = sec.Key("COOKIE_NAME").MustString("i_like_gitea")
  47. SessionConfig.CookiePath = AppSubURL + "/" // there was a bug, old code only set CookePath=AppSubURL, no trailing slash
  48. SessionConfig.Secure = sec.Key("COOKIE_SECURE").MustBool(strings.HasPrefix(strings.ToLower(AppURL), "https://"))
  49. SessionConfig.Gclifetime = sec.Key("GC_INTERVAL_TIME").MustInt64(86400)
  50. SessionConfig.Maxlifetime = sec.Key("SESSION_LIFE_TIME").MustInt64(86400)
  51. SessionConfig.Domain = sec.Key("DOMAIN").String()
  52. samesiteString := sec.Key("SAME_SITE").In("lax", []string{"none", "lax", "strict"})
  53. switch strings.ToLower(samesiteString) {
  54. case "none":
  55. SessionConfig.SameSite = http.SameSiteNoneMode
  56. case "strict":
  57. SessionConfig.SameSite = http.SameSiteStrictMode
  58. default:
  59. SessionConfig.SameSite = http.SameSiteLaxMode
  60. }
  61. shadowConfig, err := json.Marshal(SessionConfig)
  62. if err != nil {
  63. log.Fatal("Can't shadow session config: %v", err)
  64. }
  65. SessionConfig.ProviderConfig = string(shadowConfig)
  66. SessionConfig.OriginalProvider = SessionConfig.Provider
  67. SessionConfig.Provider = "VirtualSession"
  68. log.Info("Session Service Enabled")
  69. }