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

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