pubkey validation

pull/1/head
vilmibm 2023-03-01 05:22:09 +00:00
parent cec7ee4a82
commit f28da14d98
1 changed files with 44 additions and 13 deletions

View File

@ -9,6 +9,7 @@ import (
"strings" "strings"
"git.tilde.town/tildetown/town/invites" "git.tilde.town/tildetown/town/invites"
"git.tilde.town/tildetown/town/sshkey"
"git.tilde.town/tildetown/town/stats" "git.tilde.town/tildetown/town/stats"
"github.com/AlecAivazis/survey/v2" "github.com/AlecAivazis/survey/v2"
"github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss"
@ -34,10 +35,9 @@ func surveyIconSet(icons *survey.IconSet) {
} }
func promptCode() (code string, err error) { func promptCode() (code string, err error) {
codePrompt := &survey.Input{ err = survey.AskOne(&survey.Input{
Message: "invite code?", Message: "invite code?",
} }, &code,
err = survey.AskOne(codePrompt, &code,
survey.WithValidator(survey.Required), survey.WithValidator(survey.Required),
survey.WithIcons(surveyIconSet)) survey.WithIcons(surveyIconSet))
code = strings.TrimSpace(code) code = strings.TrimSpace(code)
@ -45,11 +45,12 @@ func promptCode() (code string, err error) {
} }
func promptUsername(townData stats.TildeData) (un string, err error) { func promptUsername(townData stats.TildeData) (un string, err error) {
// copied from /etc/adduser.conf
usernameRE := regexp.MustCompile(`^[a-z][-a-z0-9_]*$`) usernameRE := regexp.MustCompile(`^[a-z][-a-z0-9_]*$`)
unPrompt := &survey.Input{ err = survey.AskOne(
&survey.Input{
Message: "desired username?", Message: "desired username?",
} }, &un,
err = survey.AskOne(unPrompt, &un,
survey.WithValidator(survey.Required), survey.WithValidator(survey.Required),
survey.WithIcons(surveyIconSet), survey.WithIcons(surveyIconSet),
survey.WithValidator(func(val interface{}) error { survey.WithValidator(func(val interface{}) error {
@ -82,11 +83,11 @@ func promptUsername(townData stats.TildeData) (un string, err error) {
} }
func promptEmail(defaultEmail string) (email string, err error) { func promptEmail(defaultEmail string) (email string, err error) {
emailPrompt := &survey.Input{ err = survey.AskOne(
&survey.Input{
Message: "e-mail (for account recovery only)?", Message: "e-mail (for account recovery only)?",
Default: defaultEmail, Default: defaultEmail,
} }, &email,
err = survey.AskOne(emailPrompt, &email,
survey.WithValidator(survey.Required), survey.WithValidator(survey.Required),
survey.WithIcons(surveyIconSet), survey.WithIcons(surveyIconSet),
survey.WithValidator(func(val interface{}) error { survey.WithValidator(func(val interface{}) error {
@ -106,6 +107,30 @@ func promptEmail(defaultEmail string) (email string, err error) {
return "", nil return "", nil
} }
func promptKey() (key string, err error) {
err = survey.AskOne(
&survey.Input{
Message: "SSH public key?",
}, &key,
survey.WithValidator(survey.Required),
survey.WithIcons(surveyIconSet),
survey.WithValidator(func(v interface{}) error {
key := v.(string)
valid, err := sshkey.ValidKey(key)
if err != nil {
return fmt.Errorf("failed to validate key: %w", err)
}
if !valid {
return errors.New("that doesn't seem like a valid SSH key. try another public key?")
}
return nil
}))
return
}
func _main() error { func _main() error {
townData, err := stats.Stats() townData, err := stats.Stats()
if err != nil { if err != nil {
@ -156,7 +181,13 @@ func _main() error {
return err return err
} }
// TODO collect public key data.PubKey, err = promptKey()
if err != nil {
return err
}
// TODO should I allow a review+edit step?
// TODO have enough to make account; can now do that // TODO have enough to make account; can now do that
// TODO assuming account creation succeeded, mark invite as used // TODO assuming account creation succeeded, mark invite as used