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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. "encoding/json"
  7. "path"
  8. "path/filepath"
  9. "strings"
  10. "code.gitea.io/gitea/modules/log"
  11. )
  12. var (
  13. // SessionConfig difines Session settings
  14. SessionConfig = struct {
  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 "/".
  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. }{
  31. CookieName: "i_like_gitea",
  32. Gclifetime: 86400,
  33. Maxlifetime: 86400,
  34. }
  35. )
  36. func newSessionService() {
  37. sec := Cfg.Section("session")
  38. SessionConfig.Provider = sec.Key("PROVIDER").In("memory",
  39. []string{"memory", "file", "redis", "mysql", "postgres", "couchbase", "memcache", "nodb"})
  40. SessionConfig.ProviderConfig = strings.Trim(sec.Key("PROVIDER_CONFIG").MustString(path.Join(AppDataPath, "sessions")), "\" ")
  41. if SessionConfig.Provider == "file" && !filepath.IsAbs(SessionConfig.ProviderConfig) {
  42. SessionConfig.ProviderConfig = path.Join(AppWorkPath, SessionConfig.ProviderConfig)
  43. }
  44. SessionConfig.CookieName = sec.Key("COOKIE_NAME").MustString("i_like_gitea")
  45. SessionConfig.CookiePath = AppSubURL
  46. SessionConfig.Secure = sec.Key("COOKIE_SECURE").MustBool(false)
  47. SessionConfig.Gclifetime = sec.Key("GC_INTERVAL_TIME").MustInt64(86400)
  48. SessionConfig.Maxlifetime = sec.Key("SESSION_LIFE_TIME").MustInt64(86400)
  49. SessionConfig.Domain = sec.Key("DOMAIN").String()
  50. shadowConfig, err := json.Marshal(SessionConfig)
  51. if err != nil {
  52. log.Fatal("Can't shadow session config: %v", err)
  53. }
  54. SessionConfig.ProviderConfig = string(shadowConfig)
  55. SessionConfig.Provider = "VirtualSession"
  56. log.Info("Session Service Enabled")
  57. }