diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-01-27 22:56:54 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-27 22:56:54 +0800 |
commit | 41c0776568b3fa1bf825439103085146260e16a8 (patch) | |
tree | 84237872a82c15ec666a760dd648474fd6ff9ccb /modules | |
parent | 669ff8e9b1c15d24dd30852588c2dbb3d82e0cd9 (diff) | |
download | gitea-41c0776568b3fa1bf825439103085146260e16a8.tar.gz gitea-41c0776568b3fa1bf825439103085146260e16a8.zip |
Fix captcha (#14488)
Co-authored-by: 6543 <6543@obermui.de>
Co-authored-by: Lauris BH <lauris@nix.lv>
Diffstat (limited to 'modules')
-rw-r--r-- | modules/cache/cache.go | 20 | ||||
-rw-r--r-- | modules/context/captcha.go | 2 | ||||
-rw-r--r-- | modules/context/context.go | 38 | ||||
-rw-r--r-- | modules/setting/cache.go | 4 |
4 files changed, 18 insertions, 46 deletions
diff --git a/modules/cache/cache.go b/modules/cache/cache.go index 3f8885ee30..609f5a242b 100644 --- a/modules/cache/cache.go +++ b/modules/cache/cache.go @@ -27,24 +27,6 @@ func newCache(cacheConfig setting.Cache) (mc.Cache, error) { }) } -// Cache is the interface that operates the cache data. -type Cache interface { - // Put puts value into cache with key and expire time. - Put(key string, val interface{}, timeout int64) error - // Get gets cached value by given key. - Get(key string) interface{} - // Delete deletes cached value by given key. - Delete(key string) error - // Incr increases cached int-type value by given key as a counter. - Incr(key string) error - // Decr decreases cached int-type value by given key as a counter. - Decr(key string) error - // IsExist returns true if cached value exists. - IsExist(key string) bool - // Flush deletes all cached data. - Flush() error -} - // NewContext start cache service func NewContext() error { var err error @@ -59,7 +41,7 @@ func NewContext() error { } // GetCache returns the currently configured cache -func GetCache() Cache { +func GetCache() mc.Cache { return conn } diff --git a/modules/context/captcha.go b/modules/context/captcha.go index 956380ed73..b8540136a1 100644 --- a/modules/context/captcha.go +++ b/modules/context/captcha.go @@ -7,6 +7,7 @@ package context import ( "sync" + "code.gitea.io/gitea/modules/cache" "code.gitea.io/gitea/modules/setting" "gitea.com/go-chi/captcha" @@ -21,6 +22,7 @@ func GetImageCaptcha() *captcha.Captcha { cpt = captcha.NewCaptcha(captcha.Options{ SubURL: setting.AppSubURL, }) + cpt.Store = cache.GetCache() }) return cpt } diff --git a/modules/context/context.go b/modules/context/context.go index 630129b8c1..e5025205c9 100644 --- a/modules/context/context.go +++ b/modules/context/context.go @@ -23,6 +23,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/auth/sso" "code.gitea.io/gitea/modules/base" + mc "code.gitea.io/gitea/modules/cache" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/middlewares" "code.gitea.io/gitea/modules/setting" @@ -499,23 +500,8 @@ func getCsrfOpts() CsrfOptions { // Contexter initializes a classic context for a request. func Contexter() func(next http.Handler) http.Handler { - rnd := templates.HTMLRenderer() - - var c cache.Cache - var err error - if setting.CacheService.Enabled { - c, err = cache.NewCacher(cache.Options{ - Adapter: setting.CacheService.Adapter, - AdapterConfig: setting.CacheService.Conn, - Interval: setting.CacheService.Interval, - }) - if err != nil { - panic(err) - } - } - + var rnd = templates.HTMLRenderer() var csrfOpts = getCsrfOpts() - //var flashEncryptionKey, _ = NewSecret() return func(next http.Handler) http.Handler { return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) { @@ -524,7 +510,7 @@ func Contexter() func(next http.Handler) http.Handler { var link = setting.AppSubURL + strings.TrimSuffix(req.URL.EscapedPath(), "/") var ctx = Context{ Resp: NewResponse(resp), - Cache: c, + Cache: mc.GetCache(), Locale: locale, Link: link, Render: rnd, @@ -571,16 +557,14 @@ func Contexter() func(next http.Handler) http.Handler { } ctx.Resp.Before(func(resp ResponseWriter) { if flash := f.Encode(); len(flash) > 0 { - if err == nil { - middlewares.SetCookie(resp, "macaron_flash", flash, 0, - setting.SessionConfig.CookiePath, - middlewares.Domain(setting.SessionConfig.Domain), - middlewares.HTTPOnly(true), - middlewares.Secure(setting.SessionConfig.Secure), - //middlewares.SameSite(opt.SameSite), FIXME: we need a samesite config - ) - return - } + middlewares.SetCookie(resp, "macaron_flash", flash, 0, + setting.SessionConfig.CookiePath, + middlewares.Domain(setting.SessionConfig.Domain), + middlewares.HTTPOnly(true), + middlewares.Secure(setting.SessionConfig.Secure), + //middlewares.SameSite(opt.SameSite), FIXME: we need a samesite config + ) + return } ctx.SetCookie("macaron_flash", "", -1, diff --git a/modules/setting/cache.go b/modules/setting/cache.go index af47bd085a..618be2482a 100644 --- a/modules/setting/cache.go +++ b/modules/setting/cache.go @@ -68,6 +68,10 @@ func newCacheService() { if CacheService.Enabled { log.Info("Cache Service Enabled") + } else { + log.Warn("Cache Service Disabled so that captcha disabled too") + // captcha depends on cache service + Service.EnableCaptcha = false } sec = Cfg.Section("cache.last_commit") |