cerrado @ 3784181e4fad3c947dfa95081d8a0b34f5be12d4

feat: Disable auth if passphrase is empty

Disable all auth mechanism when passphrase is empty. That will allow for
a simpler setup.
diff --git a/pkg/config/config.go b/pkg/config/config.go
index da6e0e7044745edf7ef9a5b3e720f97b2ce62df0..c17e6df4ca913f882912747f4e76aa240976a158 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 bb6c0a2c07f23394ead3809aa54390326fd0fcd0..b57e86ae552fb056a7b8d3af16fca672e906cd03 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 ee4081b504b8291e24926062dfb9bc1765119874..82ee8fdc32a068b111399bd610fddc7496dd90d2 100644
--- a/pkg/handler/router.go
+++ b/pkg/handler/router.go
@@ -33,12 +33,17 @@ 	}
 
 	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 2a42cb889be45ffeb308c0aae56cd37666f9e2f8..db9deeed4f226872ce7a32728f8c5d9db0898b5f 100644
--- a/templates/base.qtpl
+++ b/templates/base.qtpl
@@ -38,6 +38,13 @@         return v
     }
 %}
 
+
+{% 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 5bb45323a0d55b6106c12d8370671c6e3e909553..796538ee9e81af6a386f75a825c5c8c13bbd27e6 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 @@ }
 
 // 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(`
 <!DOCTYPE html>
 <html lang="en">
@@ -97,64 +103,64 @@   <head>
     <meta charset="utf-8">
     <link rel="icon" href="data:,">
     <title>`)
-//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(`</title> 
     <link rel="stylesheet" href="/static/main`)
-//line templates/base.qtpl:55
+//line templates/base.qtpl:62
 	qw422016.E().S(Slug)
-//line templates/base.qtpl:55
+//line templates/base.qtpl:62
 	qw422016.N().S(`.css">
     <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
     <meta name="viewport" content="width=device-width, initial-scale=1" />
   </head>
   <body>
     `)
-//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(`
     <div class="container">
       `)
-//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(`
     </div>
   </body>
   `)
-//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(`
 </html>
 `)
-//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 ae26a51bdb76657f64787e6af32195200292c4ed..dc6eae466e552b01fe1c0bb2f113a5b904281c72 100644
--- a/templates/commit.qtpl
+++ b/templates/commit.qtpl
@@ -4,7 +4,7 @@ {% func Commit(name string, c *object.Commit, showTar bool) %}
   <div class="row event">
     <div class="row">
       <div class="col-md">
-       <a title="{%s c.Hash.String() %}" href="/{%s name %}/commit/{%s c.Hash.String() %}">{%s c.Hash.String()[0:8] %}</a>
+       <a title="{%s c.Hash.String() %}" href="/{%s name %}/commit/{%s c.Hash.String() %}/">{%s c.Hash.String()[0:8] %}</a>
       </div>
       {% if showTar %}
       <div class="col-md text-md-center">
diff --git a/templates/commit.qtpl.go b/templates/commit.qtpl.go
index fac2e882055fa6c6b9f3356ed0d506185d778eb0..ea8f0200e57a3c54646da90fb859fabfbfc2b240 100644
--- a/templates/commit.qtpl.go
+++ b/templates/commit.qtpl.go
@@ -39,7 +39,7 @@ 	qw422016.N().S(`/commit/`)
 //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 e24edd8eae963dcadb8655f198749a96dceb902c..c222171ddfc0bb551a4021c7a2da1600fdd80dc6 100644
--- a/templates/navbar.qtpl
+++ b/templates/navbar.qtpl
@@ -30,10 +30,12 @@           <div class="navbar-nav">
             <a class="nav-link{%= insertIfEqual(s, Git) %}" href="/">git</a>
           </div>
           <div class="navbar-nav ms-auto">
+          {% if !IsAuthenticationDisabled(ctx) %}
           {% if IsLoggedIn(ctx) %}
             <a class="nav-link{%= insertIfEqual(s, Login) %}" href="/logout/">logout</a>
           {% else %}
             <a class="nav-link{%= insertIfEqual(s, Login) %}" href="/login/">login</a>
+          {% endif %}
           {% endif %}
 {% comment %}
 Add this back once needed
diff --git a/templates/navbar.qtpl.go b/templates/navbar.qtpl.go
index b359ffb4c480166426bacd72268fe00eb7780c06..47573a70617e22f27872a0cf186414ecd7867dad 100644
--- a/templates/navbar.qtpl.go
+++ b/templates/navbar.qtpl.go
@@ -94,181 +94,191 @@           </div>
           <div class="navbar-nav ms-auto">
           `)
 //line templates/navbar.qtpl:33
-	if IsLoggedIn(ctx) {
+	if !IsAuthenticationDisabled(ctx) {
 //line templates/navbar.qtpl:33
 		qw422016.N().S(`
-            <a class="nav-link`)
+          `)
 //line templates/navbar.qtpl:34
-		streaminsertIfEqual(qw422016, s, Login)
+		if IsLoggedIn(ctx) {
 //line templates/navbar.qtpl:34
-		qw422016.N().S(`" href="/logout/">logout</a>
-          `)
+			qw422016.N().S(`
+            <a class="nav-link`)
 //line templates/navbar.qtpl:35
-	} else {
+			streaminsertIfEqual(qw422016, s, Login)
 //line templates/navbar.qtpl:35
-		qw422016.N().S(`
-            <a class="nav-link`)
+			qw422016.N().S(`" href="/logout/">logout</a>
+          `)
 //line templates/navbar.qtpl:36
-		streaminsertIfEqual(qw422016, s, Login)
+		} else {
 //line templates/navbar.qtpl:36
-		qw422016.N().S(`" href="/login/">login</a>
+			qw422016.N().S(`
+            <a class="nav-link`)
+//line templates/navbar.qtpl:37
+			streaminsertIfEqual(qw422016, s, Login)
+//line templates/navbar.qtpl:37
+			qw422016.N().S(`" href="/login/">login</a>
+          `)
+//line templates/navbar.qtpl:38
+		}
+//line templates/navbar.qtpl:38
+		qw422016.N().S(`
           `)
-//line templates/navbar.qtpl:37
+//line templates/navbar.qtpl:39
 	}
-//line templates/navbar.qtpl:37
+//line templates/navbar.qtpl:39
 	qw422016.N().S(`
 `)
-//line templates/navbar.qtpl:41
+//line templates/navbar.qtpl:43
 	qw422016.N().S(`
 `)
-//line templates/navbar.qtpl:45
+//line templates/navbar.qtpl:47
 	qw422016.N().S(`
           </div>
         </nav>
 `)
-//line templates/navbar.qtpl:48
+//line templates/navbar.qtpl:50
 }
 
-//line templates/navbar.qtpl:48
+//line templates/navbar.qtpl:50
 func WriteNavbar(qq422016 qtio422016.Writer, ctx context.Context, s Selection) {
-//line templates/navbar.qtpl:48
+//line templates/navbar.qtpl:50
 	qw422016 := qt422016.AcquireWriter(qq422016)
-//line templates/navbar.qtpl:48
+//line templates/navbar.qtpl:50
 	StreamNavbar(qw422016, ctx, s)
-//line templates/navbar.qtpl:48
+//line templates/navbar.qtpl:50
 	qt422016.ReleaseWriter(qw422016)
-//line templates/navbar.qtpl:48
+//line templates/navbar.qtpl:50
 }
 
-//line templates/navbar.qtpl:48
+//line templates/navbar.qtpl:50
 func Navbar(ctx context.Context, s Selection) string {
-//line templates/navbar.qtpl:48
+//line templates/navbar.qtpl:50
 	qb422016 := qt422016.AcquireByteBuffer()
-//line templates/navbar.qtpl:48
+//line templates/navbar.qtpl:50
 	WriteNavbar(qb422016, ctx, s)
-//line templates/navbar.qtpl:48
+//line templates/navbar.qtpl:50
 	qs422016 := string(qb422016.B)
-//line templates/navbar.qtpl:48
+//line templates/navbar.qtpl:50
 	qt422016.ReleaseByteBuffer(qb422016)
-//line templates/navbar.qtpl:48
+//line templates/navbar.qtpl:50
 	return qs422016
-//line templates/navbar.qtpl:48
+//line templates/navbar.qtpl:50
 }
 
-//line templates/navbar.qtpl:50
+//line templates/navbar.qtpl:52
 func StreamGitItemNav(qw422016 *qt422016.Writer, name, ref string, s GitSelection) {
-//line templates/navbar.qtpl:50
+//line templates/navbar.qtpl:52
 	qw422016.N().S(`
 <div class="row">
     <h3 id="name">`)
-//line templates/navbar.qtpl:52
+//line templates/navbar.qtpl:54
 	qw422016.E().S(name)
-//line templates/navbar.qtpl:52
+//line templates/navbar.qtpl:54
 	qw422016.N().S(` `)
-//line templates/navbar.qtpl:52
+//line templates/navbar.qtpl:54
 	if ref != "" && (s == Log || s == Tree) {
-//line templates/navbar.qtpl:52
+//line templates/navbar.qtpl:54
 		qw422016.N().S(`@ `)
-//line templates/navbar.qtpl:52
+//line templates/navbar.qtpl:54
 		qw422016.E().S(ref)
-//line templates/navbar.qtpl:52
+//line templates/navbar.qtpl:54
 	}
-//line templates/navbar.qtpl:52
+//line templates/navbar.qtpl:54
 	qw422016.N().S(`</h3>
 </div>
 <div class="row">
   <ul class="nav">
     <li class="nav-item">
       <a class="nav-link`)
-//line templates/navbar.qtpl:57
+//line templates/navbar.qtpl:59
 	streaminsertIfEqual(qw422016, s, Readme)
-//line templates/navbar.qtpl:57
+//line templates/navbar.qtpl:59
 	qw422016.N().S(`" aria-current="page" href="/`)
-//line templates/navbar.qtpl:57
+//line templates/navbar.qtpl:59
 	qw422016.E().S(name)
-//line templates/navbar.qtpl:57
+//line templates/navbar.qtpl:59
 	qw422016.N().S(`/about/">about</a>
     </li>
     <li class="nav-item">
       <a class="nav-link`)
-//line templates/navbar.qtpl:60
+//line templates/navbar.qtpl:62
 	streaminsertIfEqual(qw422016, s, Summary)
-//line templates/navbar.qtpl:60
+//line templates/navbar.qtpl:62
 	qw422016.N().S(`" aria-current="page" href="/`)
-//line templates/navbar.qtpl:60
+//line templates/navbar.qtpl:62
 	qw422016.E().S(name)
-//line templates/navbar.qtpl:60
+//line templates/navbar.qtpl:62
 	qw422016.N().S(`/">summary</a>
     </li>
     <li class="nav-item">
       <a class="nav-link`)
-//line templates/navbar.qtpl:63
+//line templates/navbar.qtpl:65
 	streaminsertIfEqual(qw422016, s, Refs)
-//line templates/navbar.qtpl:63
+//line templates/navbar.qtpl:65
 	qw422016.N().S(`" aria-current="page" href="/`)
-//line templates/navbar.qtpl:63
+//line templates/navbar.qtpl:65
 	qw422016.E().S(name)
-//line templates/navbar.qtpl:63
+//line templates/navbar.qtpl:65
 	qw422016.N().S(`/refs/">refs</a>
     </li>
     <li class="nav-item">
       <a class="nav-link`)
-//line templates/navbar.qtpl:66
+//line templates/navbar.qtpl:68
 	streaminsertIfEqual(qw422016, s, Log)
-//line templates/navbar.qtpl:66
+//line templates/navbar.qtpl:68
 	qw422016.N().S(`" aria-current="page" href="/`)
-//line templates/navbar.qtpl:66
+//line templates/navbar.qtpl:68
 	qw422016.E().S(name)
-//line templates/navbar.qtpl:66
+//line templates/navbar.qtpl:68
 	qw422016.N().S(`/log/`)
-//line templates/navbar.qtpl:66
+//line templates/navbar.qtpl:68
 	qw422016.E().S(ref)
-//line templates/navbar.qtpl:66
+//line templates/navbar.qtpl:68
 	qw422016.N().S(`/">log</a>
     </li>
     <li class="nav-item">
       <a class="nav-link`)
-//line templates/navbar.qtpl:69
+//line templates/navbar.qtpl:71
 	streaminsertIfEqual(qw422016, s, Tree)
-//line templates/navbar.qtpl:69
+//line templates/navbar.qtpl:71
 	qw422016.N().S(`" aria-current="page" href="/`)
-//line templates/navbar.qtpl:69
+//line templates/navbar.qtpl:71
 	qw422016.E().S(name)
-//line templates/navbar.qtpl:69
+//line templates/navbar.qtpl:71
 	qw422016.N().S(`/tree/`)
-//line templates/navbar.qtpl:69
+//line templates/navbar.qtpl:71
 	qw422016.E().S(ref)
-//line templates/navbar.qtpl:69
+//line templates/navbar.qtpl:71
 	qw422016.N().S(`/">tree</a>
     </li>
   </ul>
 </div>
 `)
-//line templates/navbar.qtpl:73
+//line templates/navbar.qtpl:75
 }
 
-//line templates/navbar.qtpl:73
+//line templates/navbar.qtpl:75
 func WriteGitItemNav(qq422016 qtio422016.Writer, name, ref string, s GitSelection) {
-//line templates/navbar.qtpl:73
+//line templates/navbar.qtpl:75
 	qw422016 := qt422016.AcquireWriter(qq422016)
-//line templates/navbar.qtpl:73
+//line templates/navbar.qtpl:75
 	StreamGitItemNav(qw422016, name, ref, s)
-//line templates/navbar.qtpl:73
+//line templates/navbar.qtpl:75
 	qt422016.ReleaseWriter(qw422016)
-//line templates/navbar.qtpl:73
+//line templates/navbar.qtpl:75
 }
 
-//line templates/navbar.qtpl:73
+//line templates/navbar.qtpl:75
 func GitItemNav(name, ref string, s GitSelection) string {
-//line templates/navbar.qtpl:73
+//line templates/navbar.qtpl:75
 	qb422016 := qt422016.AcquireByteBuffer()
-//line templates/navbar.qtpl:73
+//line templates/navbar.qtpl:75
 	WriteGitItemNav(qb422016, name, ref, s)
-//line templates/navbar.qtpl:73
+//line templates/navbar.qtpl:75
 	qs422016 := string(qb422016.B)
-//line templates/navbar.qtpl:73
+//line templates/navbar.qtpl:75
 	qt422016.ReleaseByteBuffer(qb422016)
-//line templates/navbar.qtpl:73
+//line templates/navbar.qtpl:75
 	return qs422016
-//line templates/navbar.qtpl:73
+//line templates/navbar.qtpl:75
 }