forgot to add these
parent
bf244101e6
commit
529e14158a
|
@ -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)
|
||||||
|
}
|
|
@ -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
|
||||||
|
}
|
Loading…
Reference in New Issue