Compare commits
3 Commits
b1ff57ba58
...
cbc868ae35
Author | SHA1 | Date |
---|---|---|
vilmibm | cbc868ae35 | |
vilmibm | 940779876c | |
vilmibm | f53b2721cb |
|
@ -4,7 +4,6 @@ import (
|
|||
"fmt"
|
||||
|
||||
"git.tilde.town/tildetown/town/email"
|
||||
"git.tilde.town/tildetown/town/towndb"
|
||||
)
|
||||
|
||||
const emailText = `hello!
|
||||
|
@ -24,7 +23,7 @@ Follow the instructions there to add your new key and restore access to your acc
|
|||
best,
|
||||
~vilmibm`
|
||||
|
||||
func sendAuthCodeEmail(ac towndb.AuthCode) error {
|
||||
func sendAuthCodeEmail(ac AuthCode) error {
|
||||
pw, err := email.LoadPassword()
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
113
cmd/help/main.go
113
cmd/help/main.go
|
@ -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,10 +158,17 @@ func _main(cs colorScheme) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func userToEmail(email string) (string, error) {
|
||||
// TODO shell out to /town/src/town/cmd/usertoemail
|
||||
// TODO add to sudoers
|
||||
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 collectEmail(db *sql.DB, cs colorScheme, p *Prompter) error {
|
||||
|
@ -188,22 +195,14 @@ func collectEmail(db *sql.DB, cs colorScheme, p *Prompter) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
user, err := towndb.UserForEmail(db, email)
|
||||
if err != nil {
|
||||
// TODO log
|
||||
return err
|
||||
}
|
||||
|
||||
if user == nil {
|
||||
if _, err = emailToUsername(email); err != nil {
|
||||
// TODO log
|
||||
return nil
|
||||
}
|
||||
|
||||
code := codes.NewCode(email)
|
||||
|
||||
fmt.Println(code)
|
||||
|
||||
ac := &towndb.AuthCode{
|
||||
ac := &AuthCode{
|
||||
Code: code,
|
||||
Email: email,
|
||||
}
|
||||
|
@ -238,7 +237,7 @@ func redeemCode(db *sql.DB, cs colorScheme, p *Prompter) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
code := &towndb.AuthCode{
|
||||
code := &AuthCode{
|
||||
Code: parts[0],
|
||||
Email: parts[1],
|
||||
}
|
||||
|
@ -254,8 +253,8 @@ func redeemCode(db *sql.DB, cs colorScheme, p *Prompter) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
user, err := towndb.UserForEmail(db, code.Email)
|
||||
if err != nil || user == nil {
|
||||
username, err := emailToUsername(code.Email)
|
||||
if err != nil {
|
||||
fmt.Println(cs.Error("That code doesn't seem to match an account."))
|
||||
// TODO log
|
||||
return nil
|
||||
|
@ -279,7 +278,7 @@ func redeemCode(db *sql.DB, cs colorScheme, p *Prompter) error {
|
|||
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)
|
||||
stdoutBuff := bytes.NewBuffer([]byte{})
|
||||
cmd.Stdout = stdoutBuff
|
||||
|
@ -315,3 +314,79 @@ 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
|
||||
}
|
||||
|
|
|
@ -174,79 +174,3 @@ 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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue