Skip to content

Commit

Permalink
Add token entry/storage functionality to test console (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
efixler authored Jul 16, 2024
1 parent 0e48c95 commit 1720426
Show file tree
Hide file tree
Showing 3 changed files with 269 additions and 114 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
MODULE_NAME := $(shell go list -m)
MODULE_NAME := scrape
ifneq ($(shell command -v go >/dev/null 2>&1 && echo yes),)
MODULE_NAME := $(shell go list -m)
endif
DOCKER_IMAGE_NAME := ${shell basename ${MODULE_NAME}}-bookworm-slim
CWD := $(shell pwd)
BUILD_DIR := build
Expand Down
13 changes: 9 additions & 4 deletions internal/server/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,10 @@ var home embed.FS

func (h scrapeServer) mustHomeTemplate() *template.Template {
tmpl := template.New("home")
var keyF = func() string { return "" }
if len(h.SigningKey) > 0 {
keyF = func() string {
var authTokenF = func() string { return "" }
var authEnabledF = func() bool { return len(h.SigningKey) > 0 }
if authEnabledF() {
authTokenF = func() string {
c, err := auth.NewClaims(
auth.WithSubject("home"),
auth.ExpiresIn(60*time.Minute),
Expand All @@ -126,7 +127,11 @@ func (h scrapeServer) mustHomeTemplate() *template.Template {
return s
}
}
tmpl = tmpl.Funcs(template.FuncMap{"AuthToken": keyF})
funcMap := template.FuncMap{
"AuthToken": authTokenF,
"AuthEnabled": authEnabledF,
}
tmpl = tmpl.Funcs(funcMap)
homeSource, _ := home.ReadFile("templates/index.html")
tmpl = template.Must(tmpl.Parse(string(homeSource)))
return tmpl
Expand Down
Loading

0 comments on commit 1720426

Please sign in to comment.