mirror of
https://github.com/lukaszraczylo/traefikoidc.git
synced 2026-10-12 04:04:26 +00:00
Add session refresh.
This commit is contained in:
@@ -15,6 +15,8 @@ import (
|
|||||||
"golang.org/x/time/rate"
|
"golang.org/x/time/rate"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const ConstSessionTimeout = 86400
|
||||||
|
|
||||||
type TokenVerifier interface {
|
type TokenVerifier interface {
|
||||||
VerifyToken(token string) error
|
VerifyToken(token string) error
|
||||||
}
|
}
|
||||||
@@ -123,7 +125,7 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h
|
|||||||
store := sessions.NewCookieStore([]byte(config.SessionEncryptionKey))
|
store := sessions.NewCookieStore([]byte(config.SessionEncryptionKey))
|
||||||
store.Options = &sessions.Options{
|
store.Options = &sessions.Options{
|
||||||
Path: "/",
|
Path: "/",
|
||||||
MaxAge: 3600,
|
MaxAge: ConstSessionTimeout,
|
||||||
HttpOnly: true,
|
HttpOnly: true,
|
||||||
Secure: true,
|
Secure: true,
|
||||||
SameSite: http.SameSiteLaxMode,
|
SameSite: http.SameSiteLaxMode,
|
||||||
@@ -242,6 +244,7 @@ func (t *TraefikOidc) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
|||||||
|
|
||||||
authenticated, tokenExpired := t.isUserAuthenticated(session)
|
authenticated, tokenExpired := t.isUserAuthenticated(session)
|
||||||
if authenticated {
|
if authenticated {
|
||||||
|
t.refreshSession(rw, req)
|
||||||
t.logger.Debugf("User is authenticated, serving content")
|
t.logger.Debugf("User is authenticated, serving content")
|
||||||
t.next.ServeHTTP(rw, req)
|
t.next.ServeHTTP(rw, req)
|
||||||
return
|
return
|
||||||
@@ -297,9 +300,10 @@ func (t *TraefikOidc) isUserAuthenticated(session *sessions.Session) (bool, bool
|
|||||||
return false, false
|
return false, false
|
||||||
}
|
}
|
||||||
|
|
||||||
if time.Now().Unix() > int64(exp) {
|
gracePeriod := time.Minute * 1
|
||||||
t.logger.Debugf("Session has expired")
|
if time.Now().Add(gracePeriod).Unix() > int64(exp) {
|
||||||
return false, true // Token expired
|
t.logger.Debugf("Session has expired or will expire soon")
|
||||||
|
return false, true // Token expired or will expire soon
|
||||||
}
|
}
|
||||||
|
|
||||||
return t.verifyToken(idToken) == nil, false
|
return t.verifyToken(idToken) == nil, false
|
||||||
@@ -380,3 +384,20 @@ func (t *TraefikOidc) RevokeToken(token string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *TraefikOidc) refreshSession(w http.ResponseWriter, r *http.Request) {
|
||||||
|
session, err := t.store.Get(r, cookieName)
|
||||||
|
if err != nil {
|
||||||
|
t.logger.Errorf("Error getting session: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if auth, ok := session.Values["authenticated"].(bool); ok && auth {
|
||||||
|
// Refresh the session
|
||||||
|
session.Options.MaxAge = ConstSessionTimeout
|
||||||
|
err = session.Save(r, w)
|
||||||
|
if err != nil {
|
||||||
|
t.logger.Errorf("Error saving session: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user