2021-09-08 18:04:56 +00:00
|
|
|
package application
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-12-13 12:33:20 +00:00
|
|
|
"math"
|
2022-05-11 08:08:38 +00:00
|
|
|
"net/url"
|
2021-12-12 16:59:31 +00:00
|
|
|
"os"
|
2021-09-08 18:04:56 +00:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/gorilla/sessions"
|
2022-03-03 09:40:07 +00:00
|
|
|
"goauthentik.io/api/v3"
|
2021-09-08 18:04:56 +00:00
|
|
|
"goauthentik.io/internal/config"
|
|
|
|
"gopkg.in/boj/redistore.v1"
|
|
|
|
)
|
|
|
|
|
2022-05-11 08:08:38 +00:00
|
|
|
func (a *Application) getStore(p api.ProxyOutpostConfig, externalHost *url.URL) sessions.Store {
|
2021-09-08 18:04:56 +00:00
|
|
|
var store sessions.Store
|
2022-07-26 09:33:35 +00:00
|
|
|
if config.Get().Redis.Host != "" {
|
2022-11-15 13:31:29 +00:00
|
|
|
rs, err := redistore.NewRediStoreWithDB(10, "tcp", fmt.Sprintf("%s:%d", config.Get().Redis.Host, config.Get().Redis.Port), config.Get().Redis.Password, strconv.Itoa(config.Get().Redis.DB), []byte(*p.CookieSecret))
|
2021-09-08 18:04:56 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-01-21 22:11:17 +00:00
|
|
|
rs.SetMaxLength(math.MaxInt)
|
2022-11-15 13:31:29 +00:00
|
|
|
rs.SetKeyPrefix("authentik_proxy_session_")
|
2021-09-25 14:12:59 +00:00
|
|
|
if p.TokenValidity.IsSet() {
|
|
|
|
t := p.TokenValidity.Get()
|
|
|
|
// Add one to the validity to ensure we don't have a session with indefinite length
|
2021-12-21 12:10:57 +00:00
|
|
|
rs.SetMaxAge(int(*t) + 1)
|
|
|
|
} else {
|
|
|
|
rs.SetMaxAge(0)
|
2021-09-25 14:12:59 +00:00
|
|
|
}
|
2021-09-08 18:04:56 +00:00
|
|
|
rs.Options.Domain = *p.CookieDomain
|
2022-05-21 11:18:06 +00:00
|
|
|
a.log.Trace("using redis session backend")
|
2021-09-08 18:04:56 +00:00
|
|
|
store = rs
|
|
|
|
} else {
|
2021-12-13 12:33:20 +00:00
|
|
|
dir := os.TempDir()
|
|
|
|
cs := sessions.NewFilesystemStore(dir, []byte(*p.CookieSecret))
|
|
|
|
// https://github.com/markbates/goth/commit/7276be0fdf719ddff753f3574ef0f967e4a5a5f7
|
|
|
|
// set the maxLength of the cookies stored on the disk to a larger number to prevent issues with:
|
|
|
|
// securecookie: the value is too long
|
|
|
|
// when using OpenID Connect , since this can contain a large amount of extra information in the id_token
|
|
|
|
|
|
|
|
// Note, when using the FilesystemStore only the session.ID is written to a browser cookie, so this is explicit for the storage on disk
|
2022-01-21 22:11:17 +00:00
|
|
|
cs.MaxLength(math.MaxInt)
|
2021-09-25 14:12:59 +00:00
|
|
|
if p.TokenValidity.IsSet() {
|
|
|
|
t := p.TokenValidity.Get()
|
|
|
|
// Add one to the validity to ensure we don't have a session with indefinite length
|
2021-12-21 12:10:57 +00:00
|
|
|
cs.MaxAge(int(*t) + 1)
|
|
|
|
} else {
|
|
|
|
cs.MaxAge(0)
|
2021-09-25 14:12:59 +00:00
|
|
|
}
|
2021-12-21 12:10:57 +00:00
|
|
|
cs.Options.Domain = *p.CookieDomain
|
2022-05-21 11:18:06 +00:00
|
|
|
a.log.WithField("dir", dir).Trace("using filesystem session backend")
|
2021-09-08 18:04:56 +00:00
|
|
|
store = cs
|
|
|
|
}
|
|
|
|
return store
|
|
|
|
}
|