Compare commits

..

No commits in common. "cbc868ae35078fa7d4a2496381e156eeb8a36252" and "b1ff57ba58a2ec59198d61f25d9347e62f694021" have entirely different histories.

3 changed files with 97 additions and 95 deletions

View File

@ -4,6 +4,7 @@ import (
"fmt"
"git.tilde.town/tildetown/town/email"
"git.tilde.town/tildetown/town/towndb"
)
const emailText = `hello!
@ -23,7 +24,7 @@ Follow the instructions there to add your new key and restore access to your acc
best,
~vilmibm`
func sendAuthCodeEmail(ac AuthCode) error {
func sendAuthCodeEmail(ac towndb.AuthCode) error {
pw, err := email.LoadPassword()
if err != nil {
return err

View File

@ -9,10 +9,10 @@ import (
"os/exec"
"strconv"
"strings"
"time"
"git.tilde.town/tildetown/town/codes"
"git.tilde.town/tildetown/town/sshkey"
"git.tilde.town/tildetown/town/towndb"
"github.com/charmbracelet/lipgloss"
_ "github.com/mattn/go-sqlite3"
"github.com/mattn/go-tty"
@ -158,17 +158,10 @@ func _main(cs colorScheme) error {
return nil
}
func emailToUsername(email string) (string, error) {
cmd := exec.Command("sudo", "/town/bin/emailtouser", email)
stderrBuff := bytes.NewBuffer([]byte{})
stdoutBuff := bytes.NewBuffer([]byte{})
cmd.Stderr = stderrBuff
cmd.Stdout = stdoutBuff
if err := cmd.Run(); err != nil {
return "", fmt.Errorf("emailtouser failed with '%s': %w", stderrBuff.String(), err)
}
return stdoutBuff.String(), nil
func userToEmail(email string) (string, error) {
// TODO shell out to /town/src/town/cmd/usertoemail
// TODO add to sudoers
return "", nil
}
func collectEmail(db *sql.DB, cs colorScheme, p *Prompter) error {
@ -195,14 +188,22 @@ func collectEmail(db *sql.DB, cs colorScheme, p *Prompter) error {
return nil
}
if _, err = emailToUsername(email); err != nil {
user, err := towndb.UserForEmail(db, email)
if err != nil {
// TODO log
return err
}
if user == nil {
// TODO log
return nil
}
code := codes.NewCode(email)
ac := &AuthCode{
fmt.Println(code)
ac := &towndb.AuthCode{
Code: code,
Email: email,
}
@ -237,7 +238,7 @@ func redeemCode(db *sql.DB, cs colorScheme, p *Prompter) error {
return nil
}
code := &AuthCode{
code := &towndb.AuthCode{
Code: parts[0],
Email: parts[1],
}
@ -253,8 +254,8 @@ func redeemCode(db *sql.DB, cs colorScheme, p *Prompter) error {
return nil
}
username, err := emailToUsername(code.Email)
if err != nil {
user, err := towndb.UserForEmail(db, code.Email)
if err != nil || user == nil {
fmt.Println(cs.Error("That code doesn't seem to match an account."))
// TODO log
return nil
@ -278,7 +279,7 @@ func redeemCode(db *sql.DB, cs colorScheme, p *Prompter) error {
return nil
}
cmd := exec.Command("sudo", "--user", username, "/town/bin/appendkeyfile", username)
cmd := exec.Command("sudo", "--user", user.Username, "/town/bin/appendkeyfile", user.Username)
cmd.Stdin = bytes.NewBufferString(key)
stdoutBuff := bytes.NewBuffer([]byte{})
cmd.Stdout = stdoutBuff
@ -314,79 +315,3 @@ func main() {
os.Exit(1)
}
}
type AuthCode struct {
ID int64
Code string
Email string
Used bool
Created time.Time
}
func (c *AuthCode) Insert(db *sql.DB) error {
stmt, err := db.Prepare(`
INSERT INTO auth_codes (code, email)
VALUES ?, ?`)
if err != nil {
return err
}
defer stmt.Close()
result, err := stmt.Exec(c.Code, c.Email)
if err != nil {
return err
}
liid, err := result.LastInsertId()
if err != nil {
return err
}
c.ID = liid
return nil
}
func (c *AuthCode) Hydrate(db *sql.DB) error {
stmt, err := db.Prepare(`
SELECT id, used, created
FROM auth_codes
WHERE code = ? AND email = ?`)
if err != nil {
return err
}
defer stmt.Close()
return stmt.QueryRow(c.Code).Scan(&c.ID, &c.Used, &c.Created)
}
func (c *AuthCode) MarkUsed(db *sql.DB) error {
if c.ID == 0 {
return errors.New("not hydrated")
}
stmt, err := db.Prepare(`
UPDATE auth_codes SET used = 1 WHERE id = ?`)
if err != nil {
return err
}
defer stmt.Close()
result, err := stmt.Exec(c.ID)
if err != nil {
return err
}
var rowsAffected int64
if rowsAffected, err = result.RowsAffected(); err != nil {
return err
}
if rowsAffected == 0 {
return errors.New("no rows affected")
}
return nil
}

View File

@ -174,3 +174,79 @@ func ConnectDB() (*sql.DB, error) {
return db, nil
}
type AuthCode struct {
ID int64
Code string
Email string
Used bool
Created time.Time
}
func (c *AuthCode) Insert(db *sql.DB) error {
stmt, err := db.Prepare(`
INSERT INTO auth_codes (code, email)
VALUES ?, ?`)
if err != nil {
return err
}
defer stmt.Close()
result, err := stmt.Exec(c.Code, c.Email)
if err != nil {
return err
}
liid, err := result.LastInsertId()
if err != nil {
return err
}
c.ID = liid
return nil
}
func (c *AuthCode) Hydrate(db *sql.DB) error {
stmt, err := db.Prepare(`
SELECT id, used, created
FROM auth_codes
WHERE code = ? AND email = ?`)
if err != nil {
return err
}
defer stmt.Close()
return stmt.QueryRow(c.Code).Scan(&c.ID, &c.Used, &c.Created)
}
func (c *AuthCode) MarkUsed(db *sql.DB) error {
if c.ID == 0 {
return errors.New("not hydrated")
}
stmt, err := db.Prepare(`
UPDATE auth_codes SET used = 1 WHERE id = ?`)
if err != nil {
return err
}
defer stmt.Close()
result, err := stmt.Exec(c.ID)
if err != nil {
return err
}
var rowsAffected int64
if rowsAffected, err = result.RowsAffected(); err != nil {
return err
}
if rowsAffected == 0 {
return errors.New("no rows affected")
}
return nil
}