github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/setting/session.go (about)

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