finish code validation, start on key handling
parent
6fa11aba8e
commit
bf244101e6
|
@ -2,12 +2,14 @@ package main
|
|||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"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"
|
||||
|
@ -24,6 +26,7 @@ type colorScheme struct {
|
|||
Prompt func(string) string
|
||||
Email func(string) string
|
||||
Option func(string) string
|
||||
Error func(string) string
|
||||
}
|
||||
|
||||
func newColorScheme() colorScheme {
|
||||
|
@ -40,6 +43,7 @@ func newColorScheme() colorScheme {
|
|||
Email: s2r(s().Bold(true).Underline(true)),
|
||||
Prompt: s2r(s().Bold(true).Foreground(c("#00752d"))),
|
||||
Option: s2r(s().Bold(true).Foreground(c("#38747a"))),
|
||||
Error: s2r(s().Bold(true).Foreground(c("#f43124"))),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -101,13 +105,12 @@ func (p *Prompter) Select(prompt string, opts []string) (int, error) {
|
|||
return chosen, nil
|
||||
}
|
||||
|
||||
func _main() error {
|
||||
func _main(cs colorScheme) error {
|
||||
db, err := towndb.ConnectDB()
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not connect to database. please let root@tilde.town know about this.")
|
||||
}
|
||||
|
||||
cs := newColorScheme()
|
||||
fmt.Println(cs.Header("Hi, you have reached the tilde town help desk."))
|
||||
fmt.Println()
|
||||
fmt.Println("Please check out the options below.")
|
||||
|
@ -204,19 +207,18 @@ func collectEmail(db *sql.DB, cs colorScheme, p *Prompter) error {
|
|||
func redeemCode(db *sql.DB, cs colorScheme, p *Prompter) error {
|
||||
fmt.Println(cs.Header("redeem an auth code and add a new public key"))
|
||||
fmt.Println()
|
||||
c, err := p.String("paste your auth code:")
|
||||
// TODO add Error to cs
|
||||
c, err := p.String("paste your auth code and hit enter to submit:")
|
||||
if err != nil {
|
||||
// TODO log
|
||||
// TODO print an error, return nil
|
||||
return err
|
||||
fmt.Println(cs.Error("sorry, I couldn't read that."))
|
||||
return nil
|
||||
}
|
||||
|
||||
parts, err := codes.Decode(c)
|
||||
if err != nil {
|
||||
// TODO log
|
||||
// TODO print an error, return nil
|
||||
return err
|
||||
fmt.Println(cs.Error("sorry, that doesn't look like an auth code..."))
|
||||
return nil
|
||||
}
|
||||
|
||||
code := &towndb.AuthCode{
|
||||
|
@ -227,27 +229,51 @@ func redeemCode(db *sql.DB, cs colorScheme, p *Prompter) error {
|
|||
err = code.Hydrate(db)
|
||||
if err != nil {
|
||||
// TODO log
|
||||
// TODO print an error, return an opaque error about db
|
||||
return err
|
||||
return errors.New("the database is sad")
|
||||
}
|
||||
|
||||
if code.Used {
|
||||
fmt.Println("That code has already been redeemed. You'll have to request a new one.")
|
||||
fmt.Println(cs.Error("That code has already been redeemed. You'll have to request a new one."))
|
||||
return nil
|
||||
}
|
||||
|
||||
// TODO verify code
|
||||
// TODO accept key
|
||||
// TODO verify key
|
||||
// TODO add key to .authorized_keys2
|
||||
user, err := towndb.UserForEmail(db, code.Email)
|
||||
if err != nil || user == nil {
|
||||
fmt.Println(cs.Error("That code doesn't seem to match an account."))
|
||||
// TODO log
|
||||
return nil
|
||||
}
|
||||
|
||||
key, err := p.String("paste your new public key and hit enter to submit:")
|
||||
if err != nil {
|
||||
// TODO log
|
||||
fmt.Println(cs.Error("sorry, I couldn't read that."))
|
||||
return nil
|
||||
}
|
||||
|
||||
valid, err := sshkey.ValidKey(key)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to validate key: %w", err)
|
||||
}
|
||||
|
||||
if !valid {
|
||||
errMsg := fmt.Sprintf("that key is invalid: %s", err.Error())
|
||||
fmt.Println(cs.Error(errMsg))
|
||||
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
|
||||
|
||||
// TODO mark used
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
err := _main()
|
||||
cs := newColorScheme()
|
||||
err := _main(cs)
|
||||
if err != nil {
|
||||
fmt.Printf("sorry, something went wrong: %s\n", err.Error())
|
||||
fmt.Println(
|
||||
cs.Error(fmt.Sprintf("sorry, something went wrong: %s", err.Error())))
|
||||
fmt.Println("Please let an admin know by emailing a copy of this error to root@tilde.town")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue