diff options
-rw-r--r-- | pkg/config/config.go | 4 | ||||
-rw-r--r-- | pkg/ext/auth.go | 8 | ||||
-rw-r--r-- | pkg/handler/router.go | 11 | ||||
-rw-r--r-- | templates/base.qtpl | 7 | ||||
-rw-r--r-- | templates/base.qtpl.go | 58 | ||||
-rw-r--r-- | templates/commit.qtpl | 2 | ||||
-rw-r--r-- | templates/commit.qtpl.go | 2 | ||||
-rw-r--r-- | templates/navbar.qtpl | 2 | ||||
-rw-r--r-- | 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(` <!DOCTYPE html> <html lang="en"> @@ -97,64 +103,64 @@ func StreamPageTemplate(qw422016 *qt422016.Writer, p Page, ctx context.Context) <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 ae26a51..dc6eae4 100644 --- a/templates/commit.qtpl +++ b/templates/commit.qtpl @@ -4,7 +4,7 @@ <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 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 ( <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 <a class="nav-link{%= insertIfEqual(s, List) %}" href="/list/">list</a> diff --git a/templates/navbar.qtpl.go b/templates/navbar.qtpl.go index b359ffb..47573a7 100644 --- a/templates/navbar.qtpl.go +++ b/templates/navbar.qtpl.go @@ -94,181 +94,191 @@ func StreamNavbar(qw422016 *qt422016.Writer, ctx context.Context, s Selection) { <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:39 + } +//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 } |