Prompt user for auth after service requireing auth added

This commit is contained in:
Philipp Wolfer 2023-12-09 23:17:43 +01:00
parent 9449a29fb1
commit ab0e50f7aa
No known key found for this signature in database
GPG key ID: 8FDF744D4919943B
3 changed files with 106 additions and 53 deletions

View file

@ -27,9 +27,11 @@ import (
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"go.uploadedlobster.com/scotty/internal/backends"
"go.uploadedlobster.com/scotty/internal/cli"
"go.uploadedlobster.com/scotty/internal/config"
"go.uploadedlobster.com/scotty/internal/i18n"
"go.uploadedlobster.com/scotty/internal/models"
)
var serviceAddCmd = &cobra.Command{
@ -71,6 +73,10 @@ var serviceAddCmd = &cobra.Command{
err = service.Save()
cobra.CheckErr(err)
fmt.Println(i18n.Tr("Saved service %v using backend %v", service.Name, service.Backend))
// Check whether authentication is required
err = promptForAuth(service)
cobra.CheckErr(err)
},
}
@ -87,3 +93,25 @@ func init() {
// is called directly, e.g.:
// serviceAddCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
func promptForAuth(service config.ServiceConfig) error {
backend, err := backends.ResolveBackend[models.OAuth2Authenticator](service)
if err != nil {
// No authentication required, return
return nil
}
doAuth, err := cli.PromptYesNo(
i18n.Tr("The backend %v requires authentication. Authenticate now?", service.Backend),
true,
)
if err != nil {
return err
}
if !doAuth {
return nil
}
cli.AuthenticationFlow(service, backend)
return nil
}