diff --git a/pkg/module/auth/http/local_handler.go b/pkg/module/auth/http/local_handler.go index d7fe8ef..3b86c66 100644 --- a/pkg/module/auth/http/local_handler.go +++ b/pkg/module/auth/http/local_handler.go @@ -30,10 +30,12 @@ func init() { } type LocalHandler struct { - router chi.Router - algo jwa.KeyAlgorithm - key jwk.Key - accounts map[string]LocalAccount + router chi.Router + algo jwa.KeyAlgorithm + key jwk.Key + cookieDomain string + cookieDuration time.Duration + accounts map[string]LocalAccount } func (h *LocalHandler) initRouter(prefix string) { @@ -119,7 +121,9 @@ func (h *LocalHandler) handleForm(w http.ResponseWriter, r *http.Request) { cookie := http.Cookie{ Name: auth.CookieName, Value: string(token), + Domain: h.cookieDomain, HttpOnly: false, + Expires: time.Now().Add(h.cookieDuration), Path: "/", } @@ -134,6 +138,7 @@ func (h *LocalHandler) handleLogout(w http.ResponseWriter, r *http.Request) { Value: "", HttpOnly: false, Expires: time.Unix(0, 0), + Domain: h.cookieDomain, Path: "/", }) @@ -165,9 +170,11 @@ func NewLocalHandler(algo jwa.KeyAlgorithm, key jwk.Key, funcs ...LocalHandlerOp } handler := &LocalHandler{ - algo: algo, - key: key, - accounts: toAccountsMap(opts.Accounts), + algo: algo, + key: key, + accounts: toAccountsMap(opts.Accounts), + cookieDomain: opts.CookieDomain, + cookieDuration: opts.CookieDuration, } handler.initRouter(opts.RoutePrefix) diff --git a/pkg/module/auth/http/options.go b/pkg/module/auth/http/options.go index 70f55bd..84c6cf7 100644 --- a/pkg/module/auth/http/options.go +++ b/pkg/module/auth/http/options.go @@ -1,16 +1,22 @@ package http +import "time" + type LocalHandlerOptions struct { - RoutePrefix string - Accounts []LocalAccount + RoutePrefix string + Accounts []LocalAccount + CookieDomain string + CookieDuration time.Duration } type LocalHandlerOptionFunc func(*LocalHandlerOptions) func defaultLocalHandlerOptions() *LocalHandlerOptions { return &LocalHandlerOptions{ - RoutePrefix: "", - Accounts: make([]LocalAccount, 0), + RoutePrefix: "", + Accounts: make([]LocalAccount, 0), + CookieDomain: "", + CookieDuration: 24 * time.Hour, } } @@ -25,3 +31,10 @@ func WithRoutePrefix(prefix string) LocalHandlerOptionFunc { opts.RoutePrefix = prefix } } + +func WithCookieOptions(domain string, duration time.Duration) LocalHandlerOptionFunc { + return func(opts *LocalHandlerOptions) { + opts.CookieDomain = domain + opts.CookieDuration = duration + } +}