use helper
parent
b1ff57ba58
commit
f53b2721cb
112
cmd/help/main.go
112
cmd/help/main.go
|
@ -12,7 +12,6 @@ import (
|
|||
|
||||
"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 +157,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 +194,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 +236,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 +252,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 +277,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 +313,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