mark code used

trunk
vilmibm 2023-10-25 03:03:36 +00:00
parent 7255ee691e
commit 2a07a0e200
2 changed files with 52 additions and 4 deletions

View File

@ -1,10 +1,12 @@
package main package main
import ( import (
"bytes"
"database/sql" "database/sql"
"errors" "errors"
"fmt" "fmt"
"os" "os"
"os/exec"
"strconv" "strconv"
"strings" "strings"
@ -16,7 +18,7 @@ import (
"github.com/mattn/go-tty" "github.com/mattn/go-tty"
) )
// TODO consider local-only help command for renaming and adding emails to account // TODO consider local-only help command for renaming, email mgmt, deleting account
// TODO put colorscheme, prompting stuff into own packages for use in the other commands. would be good to get off of survey. // TODO put colorscheme, prompting stuff into own packages for use in the other commands. would be good to get off of survey.
@ -262,9 +264,27 @@ func redeemCode(db *sql.DB, cs colorScheme, p *Prompter) error {
return nil return nil
} }
// TODO need to create a new helper: appendkeyfile and use sudoers to allow help to call it. also need to add help user and add it to /etc/ssh/sshd_config cmd := exec.Command("sudo", "--user", user.Username, "/town/bin/appendkeyfile", user.Username)
cmd.Stdin = bytes.NewBufferString(key)
stdoutBuff := bytes.NewBuffer([]byte{})
cmd.Stdout = stdoutBuff
if err = cmd.Run(); err != nil {
// TODO log error
//return fmt.Errorf("appendkeyfile failed with '%s': %w", string(stdoutBuff.Bytes()), err)
return errors.New("adding to keys file failed")
}
// TODO add help user
// TODO update sshd_config
// TODO update sudoers
// TODO compile appendkeyfile and add to /town/bin/
err = code.MarkUsed(db)
if err != nil {
// TODO log err
return errors.New("database was sad")
}
// TODO mark used
return nil return nil
} }

View File

@ -221,4 +221,32 @@ func (c *AuthCode) Hydrate(db *sql.DB) error {
return stmt.QueryRow(c.Code).Scan(&c.ID, &c.Used, &c.Created) return stmt.QueryRow(c.Code).Scan(&c.ID, &c.Used, &c.Created)
} }
// TODO other auth code as needed 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
}