all repos — felt @ faccff3fb22052b9048f7960127048a0914ce0fe

virtual tabletop for dungeons and dragons (and similar) using Go, MongoDB, and websockets

config, register: gofmt and make sure secret is 32 bytes
Iris Lightshard nilix@nilfm.cc
commit

faccff3fb22052b9048f7960127048a0914ce0fe

parent

e7caa27a353012e2ac7ce891b54417a01b793ae9

2 files changed, 37 insertions(+), 28 deletions(-)

jump to
M config/config.goconfig/config.go

@@ -83,11 +83,21 @@ fmt.Printf("Max file upload size (MB)? ")

self.UploadMaxMB = ensureNumberOption(&inputBuf) fmt.Printf("Encryption secret for admin invite codes? ") - ensureNonEmptyOption(&inputBuf) + ensure32BytePassphrase(&inputBuf) self.RegistrationSecret = inputBuf fmt.Printf("Configuration complete!\n") self.Write() +} + +func ensure32BytePassphrase(buffer *string) { + for { + fmt.Scanln(buffer) + if len([]byte(strings.TrimSpace(*buffer))) == 32 { + break + } + fmt.Println("Please enter a 32-byte string") + } } func ensureNonEmptyOption(buffer *string) {
M register/register.goregister/register.go

@@ -4,17 +4,16 @@ import (

"crypto/aes" "crypto/cipher" "encoding/hex" + "fmt" "html/template" "net/http" - "time" "strconv" - "fmt" - + "time" "hacklab.nilfm.cc/quartzgun/auth" "hacklab.nilfm.cc/quartzgun/renderer" "hacklab.nilfm.cc/quartzgun/router" - "hacklab.nilfm.cc/quartzgun/util" + "hacklab.nilfm.cc/quartzgun/util" ) type SymmetricCrypto interface {

@@ -29,20 +28,20 @@ type SymmetricCrypt struct {

Secret string } -var iv []byte = []byte {107, 53, 46, 249, 52, 70, 36, 185, - 168, 139, 144, 249, 242, 2, 125, 183 } +var iv []byte = []byte{107, 53, 46, 249, 52, 70, 36, 185, + 168, 139, 144, 249, 242, 2, 125, 183} func (self *SymmetricCrypt) IsValid(cipher string) bool { - stringTimestamp, err := self.Decrypt(cipher) - if err != nil { - return false - } - int64Timestamp, err := strconv.ParseInt(stringTimestamp, 10, 64) - if err != nil { - return false - } - then := time.UnixMicro(int64Timestamp) - return time.Since(then).Minutes() <= 15 + stringTimestamp, err := self.Decrypt(cipher) + if err != nil { + return false + } + int64Timestamp, err := strconv.ParseInt(stringTimestamp, 10, 64) + if err != nil { + return false + } + then := time.UnixMicro(int64Timestamp) + return time.Since(then).Minutes() <= 15 } func (self *SymmetricCrypt) Encode(b []byte) string {

@@ -58,7 +57,7 @@ return data

} func (self *SymmetricCrypt) Encrypt(text string) (string, error) { - fmt.Println(text) + fmt.Println(text) block, err := aes.NewCipher([]byte(self.Secret)) if err != nil { return "", err

@@ -84,7 +83,7 @@ }

func WithCrypto(next http.Handler, crypto SymmetricCrypto) http.Handler { handlerFunc := func(w http.ResponseWriter, req *http.Request) { - util.AddContextValue(req, "crypto", crypto); + util.AddContextValue(req, "crypto", crypto) next.ServeHTTP(w, req) }

@@ -93,15 +92,15 @@ }

func WithUserStoreAndCrypto(next http.Handler, udb auth.UserStore, crypto SymmetricCrypto) http.Handler { handlerFunc := func(w http.ResponseWriter, req *http.Request) { - urlParams := req.Context().Value("params").(map[string]string) - success := false - cipher := urlParams["cipher"] - username := req.FormValue("username") - password := req.FormValue("password") - if crypto.IsValid(cipher) && len(username) > 0 && len(password) > 0 { - success = udb.AddUser(username, password) == nil - } - util.AddContextValue(req, "success", success); + urlParams := req.Context().Value("params").(map[string]string) + success := false + cipher := urlParams["cipher"] + username := req.FormValue("username") + password := req.FormValue("password") + if crypto.IsValid(cipher) && len(username) > 0 && len(password) > 0 { + success = udb.AddUser(username, password) == nil + } + util.AddContextValue(req, "success", success) next.ServeHTTP(w, req) }