diff options
Diffstat (limited to 'web/command/handler')
| -rw-r--r-- | web/command/handler/authenticate.go | 8 | ||||
| -rw-r--r-- | web/command/handler/password.go | 56 |
2 files changed, 60 insertions, 4 deletions
diff --git a/web/command/handler/authenticate.go b/web/command/handler/authenticate.go index 1c7d312..a463e7f 100644 --- a/web/command/handler/authenticate.go +++ b/web/command/handler/authenticate.go @@ -14,7 +14,7 @@ import ( func GetAuthenticate() func(http.ResponseWriter, *http.Request) { return ghttp.Adapt(func(w http.ResponseWriter, r *http.Request) (Node, error) { - return html.AuthenticatePage(""), nil + return html.AuthenticateScrapbookPage(""), nil }) } @@ -22,20 +22,20 @@ func PostAuthenticate(mainConfig *config.MainConfig, authenticator *auth.Authent return func(w http.ResponseWriter, r *http.Request) { err := r.ParseForm() if err != nil { - html.AuthenticatePage(err.Error()).Render(w) + html.AuthenticateScrapbookPage(err.Error()).Render(w) return } token := r.Form.Get("token") if len(mainConfig.Command.Secret) == 0 || subtle.ConstantTimeCompare([]byte(token), []byte(mainConfig.Command.Secret)) != 1 { - html.AuthenticatePage("The secret key is incorrect").Render(w) + html.AuthenticateScrapbookPage("The secret key is incorrect").Render(w) return } jwt, err := authenticator.NewJwt() if err != nil { - html.AuthenticatePage(fmt.Errorf("Failed to create jwt: %w", err).Error()).Render(w) + html.AuthenticateScrapbookPage(fmt.Errorf("Failed to create jwt: %w", err).Error()).Render(w) return } diff --git a/web/command/handler/password.go b/web/command/handler/password.go new file mode 100644 index 0000000..7f74a78 --- /dev/null +++ b/web/command/handler/password.go @@ -0,0 +1,56 @@ +package handler + +import ( + "fmt" + "net/http" + "path" + + "github.com/LMBishop/scrapbook/pkg/config" + "github.com/LMBishop/scrapbook/pkg/index" + "github.com/LMBishop/scrapbook/web/command/html" + . "maragu.dev/gomponents" + ghttp "maragu.dev/gomponents/http" +) + +func GetPassword(index *index.SiteIndex) func(http.ResponseWriter, *http.Request) { + return ghttp.Adapt(func(w http.ResponseWriter, r *http.Request) (Node, error) { + siteName := r.PathValue("site") + if siteName == "" { + return html.ErrorPage("Unknown site: " + siteName), nil + } + site := index.GetSite(siteName) + if site == nil { + return html.ErrorPage("Unknown site: " + siteName), nil + } + + return html.PasswordPage("", "", siteName, site.SiteConfig.Password), nil + }) +} + +func PostPassword(mainConfig *config.MainConfig, index *index.SiteIndex) func(http.ResponseWriter, *http.Request) { + return ghttp.Adapt(func(w http.ResponseWriter, r *http.Request) (Node, error) { + siteName := r.PathValue("site") + if siteName == "" { + return html.ErrorPage("Unknown site: " + siteName), nil + } + site := index.GetSite(siteName) + if site == nil { + return html.ErrorPage("Unknown site: " + siteName), nil + } + + err := r.ParseForm() + if err != nil { + return html.PasswordPage("", fmt.Errorf("Could not parse form: %w", err).Error(), siteName, site.SiteConfig.Password), nil + } + + password := r.FormValue("password") + + site.SiteConfig.Password = password + err = config.WriteSiteConfig(path.Join(site.Path, "site.toml"), site.SiteConfig) + if err != nil { + return html.PasswordPage("", fmt.Errorf("Failed to persist site: %w", err).Error(), siteName, password), nil + } + + return html.PasswordPage(fmt.Sprintf("Successfully set password for %s to '%s'", siteName, password), "", siteName, password), nil + }) +} |
