diff options
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/config/config.go | 4 | ||||
-rw-r--r-- | pkg/ext/auth.go | 8 | ||||
-rw-r--r-- | pkg/handler/router.go | 11 |
3 files changed, 20 insertions, 3 deletions
diff --git a/pkg/config/config.go b/pkg/config/config.go index da6e0e7..c17e6df 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -113,6 +113,10 @@ func (c *ConfigurationRepository) GetBase64AesKey() []byte { return c.aesKey } +func (c *ConfigurationRepository) IsAuthEnabled() bool { + return len(c.passphrase) != 0 +} + // GetByName returns configuration of repository for a given name. // It returns nil if there is not match for it. func (c *ConfigurationRepository) GetByName(name string) *GitRepositoryConfiguration { diff --git a/pkg/ext/auth.go b/pkg/ext/auth.go index bb6c0a2..b57e86a 100644 --- a/pkg/ext/auth.go +++ b/pkg/ext/auth.go @@ -11,6 +11,14 @@ type authService interface { ValidateToken(token []byte) (bool, error) } +func DisableAuthentication(next http.HandlerFunc) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + ctx = context.WithValue(ctx, "disableAuthentication", true) + next(w, r.WithContext(ctx)) + } +} + func Authenticate(auth authService) func(next http.HandlerFunc) http.HandlerFunc { return func(next http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { diff --git a/pkg/handler/router.go b/pkg/handler/router.go index ee4081b..82ee8fd 100644 --- a/pkg/handler/router.go +++ b/pkg/handler/router.go @@ -33,12 +33,17 @@ func MountHandler( mux := ext.NewRouter() mux.AddMiddleware(ext.Compress) - mux.AddMiddleware(ext.Authenticate(authService)) mux.AddMiddleware(ext.Log) + if configRepo.IsAuthEnabled() { + mux.AddMiddleware(ext.Authenticate(authService)) + mux.HandleFunc("/login/{$}", loginHandler.Login) + mux.HandleFunc("/logout/{$}", loginHandler.Logout) + } else { + mux.AddMiddleware(ext.DisableAuthentication) + } + mux.HandleFunc("/static/{file}", staticHandler) - mux.HandleFunc("/login/{$}", loginHandler.Login) - mux.HandleFunc("/logout/{$}", loginHandler.Logout) mux.HandleFunc("/{name}/about/{$}", gitHandler.About) mux.HandleFunc("/{name}/", gitHandler.Summary) mux.HandleFunc("/{name}/refs/{$}", gitHandler.Refs) |