forked from tildetown/town
finish code validation, start on key handling
parent
6fa11aba8e
commit
bf244101e6
|
@ -2,12 +2,14 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"git.tilde.town/tildetown/town/codes"
|
"git.tilde.town/tildetown/town/codes"
|
||||||
|
"git.tilde.town/tildetown/town/sshkey"
|
||||||
"git.tilde.town/tildetown/town/towndb"
|
"git.tilde.town/tildetown/town/towndb"
|
||||||
"github.com/charmbracelet/lipgloss"
|
"github.com/charmbracelet/lipgloss"
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
|
@ -24,6 +26,7 @@ type colorScheme struct {
|
||||||
Prompt func(string) string
|
Prompt func(string) string
|
||||||
Email func(string) string
|
Email func(string) string
|
||||||
Option func(string) string
|
Option func(string) string
|
||||||
|
Error func(string) string
|
||||||
}
|
}
|
||||||
|
|
||||||
func newColorScheme() colorScheme {
|
func newColorScheme() colorScheme {
|
||||||
|
@ -40,6 +43,7 @@ func newColorScheme() colorScheme {
|
||||||
Email: s2r(s().Bold(true).Underline(true)),
|
Email: s2r(s().Bold(true).Underline(true)),
|
||||||
Prompt: s2r(s().Bold(true).Foreground(c("#00752d"))),
|
Prompt: s2r(s().Bold(true).Foreground(c("#00752d"))),
|
||||||
Option: s2r(s().Bold(true).Foreground(c("#38747a"))),
|
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
|
return chosen, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func _main() error {
|
func _main(cs colorScheme) error {
|
||||||
db, err := towndb.ConnectDB()
|
db, err := towndb.ConnectDB()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not connect to database. please let root@tilde.town know about this.")
|
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(cs.Header("Hi, you have reached the tilde town help desk."))
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
fmt.Println("Please check out the options below.")
|
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 {
|
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(cs.Header("redeem an auth code and add a new public key"))
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
c, err := p.String("paste your auth code:")
|
c, err := p.String("paste your auth code and hit enter to submit:")
|
||||||
// TODO add Error to cs
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO log
|
// TODO log
|
||||||
// TODO print an error, return nil
|
fmt.Println(cs.Error("sorry, I couldn't read that."))
|
||||||
return err
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
parts, err := codes.Decode(c)
|
parts, err := codes.Decode(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO log
|
// TODO log
|
||||||
// TODO print an error, return nil
|
fmt.Println(cs.Error("sorry, that doesn't look like an auth code..."))
|
||||||
return err
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
code := &towndb.AuthCode{
|
code := &towndb.AuthCode{
|
||||||
|
@ -227,27 +229,51 @@ func redeemCode(db *sql.DB, cs colorScheme, p *Prompter) error {
|
||||||
err = code.Hydrate(db)
|
err = code.Hydrate(db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO log
|
// TODO log
|
||||||
// TODO print an error, return an opaque error about db
|
return errors.New("the database is sad")
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if code.Used {
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO verify code
|
user, err := towndb.UserForEmail(db, code.Email)
|
||||||
// TODO accept key
|
if err != nil || user == nil {
|
||||||
// TODO verify key
|
fmt.Println(cs.Error("That code doesn't seem to match an account."))
|
||||||
// TODO add key to .authorized_keys2
|
// 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
|
// TODO mark used
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
err := _main()
|
cs := newColorScheme()
|
||||||
|
err := _main(cs)
|
||||||
if err != nil {
|
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")
|
fmt.Println("Please let an admin know by emailing a copy of this error to root@tilde.town")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue