use helper

vilmibm 2023-10-25 08:33:25 +00:00
parent b1ff57ba58
commit f53b2721cb
2 changed files with 93 additions and 95 deletions

View File

@ -12,7 +12,6 @@ import (
"git.tilde.town/tildetown/town/codes" "git.tilde.town/tildetown/town/codes"
"git.tilde.town/tildetown/town/sshkey" "git.tilde.town/tildetown/town/sshkey"
"git.tilde.town/tildetown/town/towndb"
"github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
"github.com/mattn/go-tty" "github.com/mattn/go-tty"
@ -158,10 +157,17 @@ func _main(cs colorScheme) error {
return nil return nil
} }
func userToEmail(email string) (string, error) { func emailToUsername(email string) (string, error) {
// TODO shell out to /town/src/town/cmd/usertoemail cmd := exec.Command("sudo", "/town/bin/emailtouser", email)
// TODO add to sudoers stderrBuff := bytes.NewBuffer([]byte{})
return "", nil 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 collectEmail(db *sql.DB, cs colorScheme, p *Prompter) error { func collectEmail(db *sql.DB, cs colorScheme, p *Prompter) error {
@ -188,22 +194,14 @@ func collectEmail(db *sql.DB, cs colorScheme, p *Prompter) error {
return nil return nil
} }
user, err := towndb.UserForEmail(db, email) if _, err = emailToUsername(email); err != nil {
if err != nil {
// TODO log
return err
}
if user == nil {
// TODO log // TODO log
return nil return nil
} }
code := codes.NewCode(email) code := codes.NewCode(email)
fmt.Println(code) ac := &AuthCode{
ac := &towndb.AuthCode{
Code: code, Code: code,
Email: email, Email: email,
} }
@ -238,7 +236,7 @@ func redeemCode(db *sql.DB, cs colorScheme, p *Prompter) error {
return nil return nil
} }
code := &towndb.AuthCode{ code := &AuthCode{
Code: parts[0], Code: parts[0],
Email: parts[1], Email: parts[1],
} }
@ -254,8 +252,8 @@ func redeemCode(db *sql.DB, cs colorScheme, p *Prompter) error {
return nil return nil
} }
user, err := towndb.UserForEmail(db, code.Email) username, err := emailToUsername(code.Email)
if err != nil || user == nil { if err != nil {
fmt.Println(cs.Error("That code doesn't seem to match an account.")) fmt.Println(cs.Error("That code doesn't seem to match an account."))
// TODO log // TODO log
return nil return nil
@ -279,7 +277,7 @@ func redeemCode(db *sql.DB, cs colorScheme, p *Prompter) error {
return nil return nil
} }
cmd := exec.Command("sudo", "--user", user.Username, "/town/bin/appendkeyfile", user.Username) cmd := exec.Command("sudo", "--user", username, "/town/bin/appendkeyfile", username)
cmd.Stdin = bytes.NewBufferString(key) cmd.Stdin = bytes.NewBufferString(key)
stdoutBuff := bytes.NewBuffer([]byte{}) stdoutBuff := bytes.NewBuffer([]byte{})
cmd.Stdout = stdoutBuff cmd.Stdout = stdoutBuff
@ -315,3 +313,79 @@ func main() {
os.Exit(1) 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,79 +174,3 @@ func ConnectDB() (*sql.DB, error) {
return db, nil 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
}