Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: prompt for login if no token #3130

Merged
merged 3 commits into from
May 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 96 additions & 52 deletions cmd/flipt/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,62 +116,18 @@ func (c *cloudCommand) login(cmd *cobra.Command, args []string) error {
return err
}

// if they didn't attempt login, exit
if !ok {
return nil
}

flow, err := cloud.InitFlow()
if err != nil {
return fmt.Errorf("initializing flow: %w", err)
}

defer flow.Close()

var g errgroup.Group

g.Go(func() error {
if err := flow.StartServer(nil); err != nil && !errors.Is(err, net.ErrClosed) {
return fmt.Errorf("starting server: %w", err)
}
return nil
})

url, err := flow.BrowserURL(fmt.Sprintf("%s/login/device", c.url))
if err != nil {
return fmt.Errorf("creating browser URL: %w", err)
}

if err := util.OpenBrowser(url); err != nil {
return fmt.Errorf("opening browser: %w", err)
}

cloudAuthFile := filepath.Join(userConfigDir, "cloud.json")

tok, err := flow.Wait(ctx)
if err != nil {
return fmt.Errorf("waiting for token: %w", err)
}

if err := flow.Close(); err != nil && !errors.Is(err, net.ErrClosed) {
return fmt.Errorf("closing flow: %w", err)
}

cloudAuth := cloudAuth{
Token: tok,
}

cloudAuthBytes, err := json.Marshal(cloudAuth)
if err != nil {
return fmt.Errorf("marshalling cloud auth token: %w", err)
}

if err := os.WriteFile(cloudAuthFile, cloudAuthBytes, 0600); err != nil {
return fmt.Errorf("writing cloud auth token: %w", err)
if err := c.loginFlow(ctx); err != nil {
return err
}

fmt.Println("\n✓ Authenticated with Flipt Cloud!\nYou can now run commands that require cloud authentication.")

return g.Wait()
return nil
}

func (c *cloudCommand) serve(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -204,12 +160,30 @@ func (c *cloudCommand) serve(cmd *cobra.Command, args []string) error {

// first check for existing of auth token/cloud.json
// if not found, prompt user to login

AUTH:
cloudAuthFile := filepath.Join(userConfigDir, "cloud.json")
f, err := os.ReadFile(cloudAuthFile)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
fmt.Println("\n✗ No cloud authentication token found. Please run 'flipt cloud login' to authenticate.")
return nil
fmt.Println("\n✗ No cloud authentication token found.")

ok, err := util.PromptConfirm("Open browser to authenticate with Flipt Cloud?", false)
if err != nil {
return err
}

// if they didn't attempt login, exit
if !ok {
return nil
}

if err := c.loginFlow(ctx); err != nil {
return err
}

// otherwise, try reading the file again
goto AUTH
}

return fmt.Errorf("reading cloud auth payload %w", err)
Expand All @@ -234,8 +208,24 @@ func (c *cloudCommand) serve(cmd *cobra.Command, args []string) error {
parsed, err := jwt.Parse(auth.Token, k.Keyfunc, jwt.WithExpirationRequired())
if err != nil {
if errors.Is(err, jwt.ErrTokenExpired) {
fmt.Println("✗ Existing cloud authentication token expired. Please run 'flipt cloud login' to re-authenticate.")
return nil
fmt.Println("✗ Existing cloud authentication token expired.")

ok, err := util.PromptConfirm("Open browser to authenticate with Flipt Cloud?", false)
if err != nil {
return err
}

// if they didn't attempt login, exit
if !ok {
return nil
}

if err := c.loginFlow(ctx); err != nil {
return err
}

// otherwise, try reading the file again
goto AUTH
}

return fmt.Errorf("parsing JWT: %w", err)
Expand Down Expand Up @@ -375,3 +365,57 @@ func (c *cloudCommand) serve(cmd *cobra.Command, args []string) error {
fmt.Println("✓ Starting local instance linked with Flipt Cloud.")
return run(ctx, logger, srvConfig(cfg, instance))
}

func (c *cloudCommand) loginFlow(ctx context.Context) error {

flow, err := cloud.InitFlow()
if err != nil {
return fmt.Errorf("initializing flow: %w", err)
}

defer flow.Close()

var g errgroup.Group

g.Go(func() error {
if err := flow.StartServer(nil); err != nil && !errors.Is(err, net.ErrClosed) {
return fmt.Errorf("starting server: %w", err)
}
return nil
})

url, err := flow.BrowserURL(fmt.Sprintf("%s/login/device", c.url))
if err != nil {
return fmt.Errorf("creating browser URL: %w", err)
}

if err := util.OpenBrowser(url); err != nil {
return fmt.Errorf("opening browser: %w", err)
}

cloudAuthFile := filepath.Join(userConfigDir, "cloud.json")

tok, err := flow.Wait(ctx)
if err != nil {
return fmt.Errorf("waiting for token: %w", err)
}

if err := flow.Close(); err != nil && !errors.Is(err, net.ErrClosed) {
return fmt.Errorf("closing flow: %w", err)
}

cloudAuth := cloudAuth{
Token: tok,
}

cloudAuthBytes, err := json.Marshal(cloudAuth)
if err != nil {
return fmt.Errorf("marshalling cloud auth token: %w", err)
}

if err := os.WriteFile(cloudAuthFile, cloudAuthBytes, 0600); err != nil {
return fmt.Errorf("writing cloud auth token: %w", err)
}

return g.Wait()
}
Loading