From 3784181e4fad3c947dfa95081d8a0b34f5be12d4 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Wed, 11 Dec 2024 18:10:12 +0100 Subject: feat: Disable auth if passphrase is empty Disable all auth mechanism when passphrase is empty. That will allow for a simpler setup. --- pkg/config/config.go | 4 ++ pkg/ext/auth.go | 8 +++ pkg/handler/router.go | 11 +++- templates/base.qtpl | 7 +++ templates/base.qtpl.go | 58 ++++++++++-------- templates/commit.qtpl | 2 +- templates/commit.qtpl.go | 2 +- templates/navbar.qtpl | 2 + templates/navbar.qtpl.go | 156 +++++++++++++++++++++++++---------------------- 9 files changed, 146 insertions(+), 104 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) diff --git a/templates/base.qtpl b/templates/base.qtpl index 2a42cb8..db9deee 100644 --- a/templates/base.qtpl +++ b/templates/base.qtpl @@ -38,6 +38,13 @@ Page { } %} + +{% code func IsAuthenticationDisabled(ctx context.Context) bool { + t, ok := ctx.Value("disableAuthentication").(bool) + return ok && t + } +%} + {% code func IsLoggedIn(ctx context.Context) bool { t, ok := ctx.Value("logged").(bool) return ok && t diff --git a/templates/base.qtpl.go b/templates/base.qtpl.go index 5bb4532..796538e 100644 --- a/templates/base.qtpl.go +++ b/templates/base.qtpl.go @@ -79,7 +79,13 @@ func Ignore[T any](v T, _ error) T { return v } -//line templates/base.qtpl:41 +//line templates/base.qtpl:42 +func IsAuthenticationDisabled(ctx context.Context) bool { + t, ok := ctx.Value("disableAuthentication").(bool) + return ok && t +} + +//line templates/base.qtpl:48 func IsLoggedIn(ctx context.Context) bool { t, ok := ctx.Value("logged").(bool) return ok && t @@ -87,9 +93,9 @@ func IsLoggedIn(ctx context.Context) bool { // Page prints a page implementing Page interface. -//line templates/base.qtpl:48 +//line templates/base.qtpl:55 func StreamPageTemplate(qw422016 *qt422016.Writer, p Page, ctx context.Context) { -//line templates/base.qtpl:48 +//line templates/base.qtpl:55 qw422016.N().S(` @@ -97,64 +103,64 @@ func StreamPageTemplate(qw422016 *qt422016.Writer, p Page, ctx context.Context) `) -//line templates/base.qtpl:54 +//line templates/base.qtpl:61 p.StreamTitle(qw422016, ctx) -//line templates/base.qtpl:54 +//line templates/base.qtpl:61 qw422016.N().S(` `) -//line templates/base.qtpl:60 +//line templates/base.qtpl:67 p.StreamNavbar(qw422016, ctx) -//line templates/base.qtpl:60 +//line templates/base.qtpl:67 qw422016.N().S(`
`) -//line templates/base.qtpl:62 +//line templates/base.qtpl:69 p.StreamContent(qw422016, ctx) -//line templates/base.qtpl:62 +//line templates/base.qtpl:69 qw422016.N().S(`
`) -//line templates/base.qtpl:65 +//line templates/base.qtpl:72 p.StreamScript(qw422016, ctx) -//line templates/base.qtpl:65 +//line templates/base.qtpl:72 qw422016.N().S(` `) -//line templates/base.qtpl:67 +//line templates/base.qtpl:74 } -//line templates/base.qtpl:67 +//line templates/base.qtpl:74 func WritePageTemplate(qq422016 qtio422016.Writer, p Page, ctx context.Context) { -//line templates/base.qtpl:67 +//line templates/base.qtpl:74 qw422016 := qt422016.AcquireWriter(qq422016) -//line templates/base.qtpl:67 +//line templates/base.qtpl:74 StreamPageTemplate(qw422016, p, ctx) -//line templates/base.qtpl:67 +//line templates/base.qtpl:74 qt422016.ReleaseWriter(qw422016) -//line templates/base.qtpl:67 +//line templates/base.qtpl:74 } -//line templates/base.qtpl:67 +//line templates/base.qtpl:74 func PageTemplate(p Page, ctx context.Context) string { -//line templates/base.qtpl:67 +//line templates/base.qtpl:74 qb422016 := qt422016.AcquireByteBuffer() -//line templates/base.qtpl:67 +//line templates/base.qtpl:74 WritePageTemplate(qb422016, p, ctx) -//line templates/base.qtpl:67 +//line templates/base.qtpl:74 qs422016 := string(qb422016.B) -//line templates/base.qtpl:67 +//line templates/base.qtpl:74 qt422016.ReleaseByteBuffer(qb422016) -//line templates/base.qtpl:67 +//line templates/base.qtpl:74 return qs422016 -//line templates/base.qtpl:67 +//line templates/base.qtpl:74 } diff --git a/templates/commit.qtpl b/templates/commit.qtpl index ae26a51..dc6eae4 100644 --- a/templates/commit.qtpl +++ b/templates/commit.qtpl @@ -4,7 +4,7 @@
{% if showTar %}
diff --git a/templates/commit.qtpl.go b/templates/commit.qtpl.go index fac2e88..ea8f020 100644 --- a/templates/commit.qtpl.go +++ b/templates/commit.qtpl.go @@ -39,7 +39,7 @@ func StreamCommit(qw422016 *qt422016.Writer, name string, c *object.Commit, show //line templates/commit.qtpl:7 qw422016.E().S(c.Hash.String()) //line templates/commit.qtpl:7 - qw422016.N().S(`">`) + qw422016.N().S(`/">`) //line templates/commit.qtpl:7 qw422016.E().S(c.Hash.String()[0:8]) //line templates/commit.qtpl:7 diff --git a/templates/navbar.qtpl b/templates/navbar.qtpl index e24edd8..c222171 100644 --- a/templates/navbar.qtpl +++ b/templates/navbar.qtpl @@ -30,11 +30,13 @@ const ( git