Compare commits

...

2 Commits

Author SHA1 Message Date
vilmibm 5c2142f6e7 auth code db methods 2023-10-24 19:17:16 +00:00
vilmibm 92faddd079 refactor prompting, start on redeemCode 2023-10-24 19:10:13 +00:00
2 changed files with 84 additions and 17 deletions

View File

@ -16,6 +16,8 @@ import (
// TODO consider local-only help command for renaming and adding emails to account // TODO consider local-only help command for renaming and adding emails to account
// TODO put colorscheme, prompting stuff into own packages for use in the other commands. would be good to get off of survey.
type colorScheme struct { type colorScheme struct {
Header func(string) string Header func(string) string
Subtitle func(string) string Subtitle func(string) string
@ -41,11 +43,23 @@ func newColorScheme() colorScheme {
} }
} }
func stringPrompt(cs colorScheme, tty *tty.TTY, prompt string) (string, error) { type Prompter struct {
cs colorScheme
tty *tty.TTY
}
func NewPrompter(tty *tty.TTY, cs colorScheme) *Prompter {
return &Prompter{
cs: cs,
tty: tty,
}
}
func (p *Prompter) String(prompt string) (string, error) {
fmt.Println("") fmt.Println("")
fmt.Println(cs.Prompt(prompt)) fmt.Println(p.cs.Prompt(prompt))
fmt.Println(cs.Subtitle("(type your answer below and press enter to submit)")) fmt.Println(p.cs.Subtitle("(type your answer below and press enter to submit)"))
s, err := tty.ReadString() s, err := p.tty.ReadString()
if err != nil { if err != nil {
return "", fmt.Errorf("couldn't collect input: %w", err) return "", fmt.Errorf("couldn't collect input: %w", err)
} }
@ -53,18 +67,18 @@ func stringPrompt(cs colorScheme, tty *tty.TTY, prompt string) (string, error) {
return s, nil return s, nil
} }
func numberPrompt(cs colorScheme, tty *tty.TTY, prompt string, opts []string) (int, error) { func (p *Prompter) Select(prompt string, opts []string) (int, error) {
fmt.Println() fmt.Println()
fmt.Println(cs.Prompt(prompt)) fmt.Println(p.cs.Prompt(prompt))
fmt.Println(cs.Subtitle("(pick an option using the corresponding number)")) fmt.Println(p.cs.Subtitle("(pick an option using the corresponding number)"))
chosen := -1 chosen := -1
for chosen < 0 { for chosen < 0 {
fmt.Println() fmt.Println()
for ix, o := range opts { for ix, o := range opts {
fmt.Printf("%s: %s\n", cs.Option(fmt.Sprintf("%d", ix+1)), o) fmt.Printf("%s: %s\n", p.cs.Option(fmt.Sprintf("%d", ix+1)), o)
} }
r, err := tty.ReadRune() r, err := p.tty.ReadRune()
if err != nil { if err != nil {
return -1, fmt.Errorf("could not collect answer for '%s': %w", prompt, err) return -1, fmt.Errorf("could not collect answer for '%s': %w", prompt, err)
} }
@ -104,12 +118,14 @@ func _main() error {
} }
defer tty.Close() defer tty.Close()
p := NewPrompter(tty, cs)
options := []string{ options := []string{
"I need to request that a new SSH key be added to my account.", "I need to request that a new SSH key be added to my account.",
"I have a code from my e-mail to redeem for a new SSH key", "I have a code from my e-mail to redeem for a new SSH key",
"I just want out of here", "I just want out of here",
} }
c, err := numberPrompt(cs, tty, "What do you need help with?", options) c, err := p.Select("What do you need help with?", options)
defer func() { defer func() {
fmt.Println() fmt.Println()
@ -118,9 +134,9 @@ func _main() error {
switch c { switch c {
case 0: case 0:
return collectEmail(db, cs, tty) return collectEmail(db, cs, p)
case 1: case 1:
return redeemCode(tty) return redeemCode(db, cs, p)
case 2: case 2:
return nil return nil
} }
@ -128,9 +144,9 @@ func _main() error {
return nil return nil
} }
func collectEmail(db *sql.DB, cs colorScheme, tty *tty.TTY) error { func collectEmail(db *sql.DB, cs colorScheme, p *Prompter) error {
fmt.Println(cs.Header("We can send a authorization code to an email associated with your town account.")) fmt.Println(cs.Header("We can send a authorization code to an email associated with your town account."))
email, err := stringPrompt(cs, tty, "email to send reset code to?") email, err := p.String("email to send reset code to?")
if err != nil { if err != nil {
return err return err
} }
@ -185,8 +201,25 @@ func collectEmail(db *sql.DB, cs colorScheme, tty *tty.TTY) error {
return nil return nil
} }
func redeemCode(tty *tty.TTY) error { func redeemCode(db *sql.DB, cs colorScheme, p *Prompter) error {
// TODO deserialize fmt.Println(cs.Header("redeem an auth code and add a new public key"))
fmt.Println()
c, err := p.String("paste your auth code:")
if err != nil {
// TODO log
return err
}
parts, err := codes.Decode(c)
if err != nil {
// TODO log
return err
}
code := parts[0]
email := parts[1]
fmt.Println(code, email)
// TODO verify code // TODO verify code
// TODO accept key // TODO accept key
// TODO verify key // TODO verify key

View File

@ -153,6 +153,7 @@ func UserForEmail(db *sql.DB, address string) (*TownUser, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer stmt.Close()
row := stmt.QueryRow(address) row := stmt.QueryRow(address)
u := &TownUser{} u := &TownUser{}
if err = row.Scan(&u.ID, &u.Username); err != nil { if err = row.Scan(&u.ID, &u.Username); err != nil {
@ -183,8 +184,41 @@ type AuthCode struct {
} }
func (c *AuthCode) Insert(db *sql.DB) error { func (c *AuthCode) Insert(db *sql.DB) error {
// TODO 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 return nil
} }
func (c *AuthCode) Hydrate(db *sql.DB) error {
stmt, err := db.Prepare(`
SELECT id, email, used, created
FROM auth_codes
WHERE code = ?`)
if err != nil {
return err
}
defer stmt.Close()
return stmt.QueryRow(c.Code).Scan(&c.ID, &c.Email, &c.Used, &c.Created)
}
// TODO other auth code as needed // TODO other auth code as needed