From 529e14158a2fb55169d0094debf2f607ca913bf6 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Wed, 25 Oct 2023 02:41:02 +0000 Subject: [PATCH] forgot to add these --- cmd/help/email.go | 40 ++++++++++++++++++++++++++++++++++++++++ codes/codes.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 cmd/help/email.go create mode 100644 codes/codes.go diff --git a/cmd/help/email.go b/cmd/help/email.go new file mode 100644 index 0000000..df65c1c --- /dev/null +++ b/cmd/help/email.go @@ -0,0 +1,40 @@ +package main + +import ( + "fmt" + + "git.tilde.town/tildetown/town/email" + "git.tilde.town/tildetown/town/towndb" +) + +const emailText = `hello! + +You (hopefully) requested to add a new public key to your tilde.town account. + +If you didn't, feel free to ignore this email (or report it to an admin). + +If you did, here is your auth code: %s + +To use this code, please open a terminal and run: + +ssh help@tilde.town + +Follow the instructions there to add your new key and restore access to your account. + +best, +~vilmibm` + +func sendAuthCodeEmail(ac towndb.AuthCode) error { + pw, err := email.LoadPassword() + if err != nil { + return err + } + + body := fmt.Sprintf(emailText, ac.Code) + + mailer := email.NewExternalMailer(pw) + return mailer.Send( + ac.Email, + "Adding a new tilde.town public key", + body) +} diff --git a/codes/codes.go b/codes/codes.go new file mode 100644 index 0000000..7edbfe2 --- /dev/null +++ b/codes/codes.go @@ -0,0 +1,47 @@ +package codes + +import ( + "crypto/rand" + "encoding/base64" + "math/big" + "strings" +) + +const codeLen = 32 + +func NewCode(email string) string { + charset := "abcdefghijklmnopqrztuvwxyz" + charset += strings.ToUpper(charset) + charset += "0123456789" + charset += "`~!@#$%^&*()-=_+[]{}|;:,./<>?" + + code := []byte{} + + max := big.NewInt(int64(len(charset))) + for len(code) < codeLen { + ix, err := rand.Int(rand.Reader, max) + if err != nil { + // TODO this is bad but I'm just kind of hoping it doesn't happen...often + panic(err) + } + code = append(code, charset[ix.Int64()]) + } + + code = append(code, ' ') + + eb := []byte(email) + for x := 0; x < len(eb); x++ { + code = append(code, eb[x]) + } + + return base64.StdEncoding.EncodeToString(code) +} + +func Decode(code string) ([]string, error) { + decoded, err := base64.StdEncoding.DecodeString(code) + if err != nil { + return nil, err + } + + return strings.Split(string(decoded), " "), nil +}