From bf7d9c79cae53f64fcd04527248987bd4e7ca3c4 Mon Sep 17 00:00:00 2001 From: Mitchell Riedstra Date: Sun, 19 Jun 2022 23:57:04 -0400 Subject: 0.0.17a / Alpha. Introduce users and page editing. Breaking changes: inc/base.html is now tpl/base.md by default. This can be overridden on the command line. 404.md is now tpl/404.md. This can be overridden with templatedirectory in the configuration file. Additional files: `auth.json` file that stores credentials and settings for authorization cookie. Further notes: This will likely receive some major updates and changes over the next few commits. The scaffolidng is now in place for user accounts, login handling, and page editing. It's all extremely basic at the moment, on the idea list: Listing of all markdown files File uploader and general content management Flags to turn on/off git integration for edits. Download / Upload of all markdown files as a backup/restore. It's of course, all subject to change. --- users/main.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 users/main.go (limited to 'users') diff --git a/users/main.go b/users/main.go new file mode 100644 index 0000000..edb306f --- /dev/null +++ b/users/main.go @@ -0,0 +1,43 @@ +package users + +import ( + "fmt" + + "golang.org/x/crypto/bcrypt" +) + +type SiteUser struct { + Username string `yaml:"username"` + PasswordHash string `yaml:"password-hash"` + Password string `yaml:"password"` +} + +func (su *SiteUser) SetPasswordHashIfNecessary() error { + if su.Password == "" { + return nil + } + + res, err := bcrypt.GenerateFromPassword([]byte(su.Password), bcrypt.DefaultCost) + if err != nil { + return fmt.Errorf("SetPasswordHashIfNecessary: %w", err) + } + + su.Password = "" + su.PasswordHash = string(res) + + return nil +} + +func (su *SiteUser) CheckPassword(pass string) error { + err := su.SetPasswordHashIfNecessary() + if err != nil { + return err + } + + err = bcrypt.CompareHashAndPassword([]byte(su.PasswordHash), []byte(pass)) + if err != nil { + return fmt.Errorf("CheckPassword: %w", err) + } + + return nil +} -- cgit v1.2.3