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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2019 The Gitea 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. package setting
  5. import (
  6. "net/http"
  7. "path"
  8. "path/filepath"
  9. "strings"
  10. "code.gitea.io/gitea/modules/log"
  11. jsoniter "github.com/json-iterator/go"
  12. )
  13. var (
  14. // SessionConfig difines Session settings
  15. SessionConfig = struct {
  16. Provider string
  17. // Provider configuration, it's corresponding to provider.
  18. ProviderConfig string
  19. // Cookie name to save session ID. Default is "MacaronSession".
  20. CookieName string
  21. // Cookie path to store. Default is "/".
  22. CookiePath string
  23. // GC interval time in seconds. Default is 3600.
  24. Gclifetime int64
  25. // Max life time in seconds. Default is whatever GC interval time is.
  26. Maxlifetime int64
  27. // Use HTTPS only. Default is false.
  28. Secure bool
  29. // Cookie domain name. Default is empty.
  30. Domain string
  31. // 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"
  32. SameSite http.SameSite
  33. }{
  34. CookieName: "i_like_gitea",
  35. Gclifetime: 86400,
  36. Maxlifetime: 86400,
  37. SameSite: http.SameSiteLaxMode,
  38. }
  39. )
  40. func newSessionService() {
  41. sec := Cfg.Section("session")
  42. SessionConfig.Provider = sec.Key("PROVIDER").In("memory",
  43. []string{"memory", "file", "redis", "mysql", "postgres", "couchbase", "memcache", "db"})
  44. SessionConfig.ProviderConfig = strings.Trim(sec.Key("PROVIDER_CONFIG").MustString(path.Join(AppDataPath, "sessions")), "\" ")
  45. if SessionConfig.Provider == "file" && !filepath.IsAbs(SessionConfig.ProviderConfig) {
  46. SessionConfig.ProviderConfig = path.Join(AppWorkPath, SessionConfig.ProviderConfig)
  47. }
  48. SessionConfig.CookieName = sec.Key("COOKIE_NAME").MustString("i_like_gitea")
  49. SessionConfig.CookiePath = AppSubURL
  50. SessionConfig.Secure = sec.Key("COOKIE_SECURE").MustBool(false)
  51. SessionConfig.Gclifetime = sec.Key("GC_INTERVAL_TIME").MustInt64(86400)
  52. SessionConfig.Maxlifetime = sec.Key("SESSION_LIFE_TIME").MustInt64(86400)
  53. SessionConfig.Domain = sec.Key("DOMAIN").String()
  54. samesiteString := sec.Key("SAME_SITE").In("lax", []string{"none", "lax", "strict"})
  55. switch strings.ToLower(samesiteString) {
  56. case "none":
  57. SessionConfig.SameSite = http.SameSiteNoneMode
  58. case "strict":
  59. SessionConfig.SameSite = http.SameSiteStrictMode
  60. default:
  61. SessionConfig.SameSite = http.SameSiteLaxMode
  62. }
  63. json := jsoniter.ConfigCompatibleWithStandardLibrary
  64. shadowConfig, err := json.Marshal(SessionConfig)
  65. if err != nil {
  66. log.Fatal("Can't shadow session config: %v", err)
  67. }
  68. SessionConfig.ProviderConfig = string(shadowConfig)
  69. SessionConfig.Provider = "VirtualSession"
  70. log.Info("Session Service Enabled")
  71. }