refactor: move autologin to handler pkg

This commit is contained in:
Trong Huu Nguyen
2022-09-01 08:17:21 +02:00
parent 619ae52d45
commit d9cc60c4cc
2 changed files with 9 additions and 9 deletions

View File

@@ -8,17 +8,17 @@ import (
"github.com/nais/wonderwall/pkg/config"
)
type Options struct {
type AutoLogin struct {
Enabled bool
IgnorePatterns []string
}
func (o *Options) NeedsLogin(r *http.Request, isAuthenticated bool) bool {
if isAuthenticated || !o.Enabled {
func (a *AutoLogin) NeedsLogin(r *http.Request, isAuthenticated bool) bool {
if isAuthenticated || !a.Enabled {
return false
}
for _, pattern := range o.IgnorePatterns {
for _, pattern := range a.IgnorePatterns {
path := r.URL.Path
if !strings.HasPrefix(path, "/") {
path = "/" + path
@@ -33,7 +33,7 @@ func (o *Options) NeedsLogin(r *http.Request, isAuthenticated bool) bool {
return true
}
func NewOptions(cfg *config.Config) (*Options, error) {
func New(cfg *config.Config) (*AutoLogin, error) {
seen := make(map[string]bool)
patterns := make([]string, 0)
@@ -48,7 +48,7 @@ func NewOptions(cfg *config.Config) (*Options, error) {
}
}
return &Options{
return &AutoLogin{
Enabled: cfg.AutoLogin,
IgnorePatterns: patterns,
}, nil

View File

@@ -6,10 +6,10 @@ import (
"net/http/httputil"
"time"
"github.com/nais/wonderwall/pkg/autologin"
"github.com/nais/wonderwall/pkg/config"
"github.com/nais/wonderwall/pkg/cookie"
"github.com/nais/wonderwall/pkg/crypto"
"github.com/nais/wonderwall/pkg/handler/autologin"
"github.com/nais/wonderwall/pkg/loginstatus"
"github.com/nais/wonderwall/pkg/middleware"
"github.com/nais/wonderwall/pkg/openid/client"
@@ -19,7 +19,7 @@ import (
)
type Handler struct {
AutoLogin *autologin.Options
AutoLogin *autologin.AutoLogin
Client client.Client
Config *config.Config
CookieOptions cookie.Options
@@ -42,7 +42,7 @@ func NewHandler(
return nil, err
}
autoLogin, err := autologin.NewOptions(cfg)
autoLogin, err := autologin.New(cfg)
if err != nil {
return nil, err
}