Skip to content

Commit

Permalink
add disable-pop-up flag + add logout command (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkim committed Nov 1, 2022
1 parent 252dafe commit f878b4e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 25 deletions.
61 changes: 36 additions & 25 deletions app/login.go
Expand Up @@ -23,6 +23,32 @@ const (

var (
tokenFileName = "tokens.json"
domainFlag = &cli.StringFlag{
Name: "domain",
Value: "login.tmprl.cloud",
Aliases: []string{"d"},
Required: false,
Hidden: true,
}
audienceFlag = &cli.StringFlag{
Name: "audience",
Value: "https://saas-api.tmprl.cloud",
Aliases: []string{"a"},
Required: false,
Hidden: true,
}
clientIDFlag = &cli.StringFlag{
Name: "client-id",
Value: "d7V5bZMLCbRLfRVpqC567AqjAERaWHhl",
Aliases: []string{"id"},
Required: false,
Hidden: true,
}
disablePopUpFlag = &cli.BoolFlag{
Name: "disable-pop-up",
Usage: "disable browser pop-up",
Required: false,
}
)

func GetLoginClient() *LoginClient {
Expand Down Expand Up @@ -101,7 +127,7 @@ func getURLFromDomain(domain string) (string, error) {
return domain, nil
}

func (c *LoginClient) login(ctx *cli.Context, domain string, audience string, clientID string) error {
func (c *LoginClient) login(ctx *cli.Context, domain string, audience string, clientID string, disablePopUp bool) error {
// Get device code
oauthDeviceCodeResponse := OauthDeviceCodeResponse{}
domain, err := getURLFromDomain(domain)
Expand All @@ -118,8 +144,10 @@ func (c *LoginClient) login(ctx *cli.Context, domain string, audience string, cl

fmt.Printf("Login via this url: %s\n", oauthDeviceCodeResponse.VerificationURIComplete)

if err := c.loginService.OpenBrowser(oauthDeviceCodeResponse.VerificationURIComplete); err != nil {
fmt.Println("Unable to open browser, please open url manually.")
if !disablePopUp {
if err := c.loginService.OpenBrowser(oauthDeviceCodeResponse.VerificationURIComplete); err != nil {
fmt.Println("Unable to open browser, please open url manually.")
}
}

// Get access token
Expand Down Expand Up @@ -162,30 +190,13 @@ func NewLoginCommand(c *LoginClient) (CommandOut, error) {
return err
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "domain",
Value: "login.tmprl.cloud",
Aliases: []string{"d"},
Required: false,
Hidden: true,
},
&cli.StringFlag{
Name: "audience",
Value: "https://saas-api.tmprl.cloud",
Aliases: []string{"a"},
Required: false,
Hidden: true,
},
&cli.StringFlag{
Name: "client-id",
Value: "d7V5bZMLCbRLfRVpqC567AqjAERaWHhl",
Aliases: []string{"id"},
Required: false,
Hidden: true,
},
domainFlag,
audienceFlag,
clientIDFlag,
disablePopUpFlag,
},
Action: func(ctx *cli.Context) error {
return c.login(ctx, ctx.String("domain"), ctx.String("audience"), ctx.String("client-id"))
return c.login(ctx, ctx.String("domain"), ctx.String("audience"), ctx.String("client-id"), ctx.Bool("disable-pop-up"))
},
}}, nil
}
Expand Down
28 changes: 28 additions & 0 deletions app/logout.go
@@ -0,0 +1,28 @@
package app

import (
"fmt"
"github.com/urfave/cli/v2"
)

func NewLogoutCommand(c *LoginClient) (CommandOut, error) {
return CommandOut{Command: &cli.Command{
Name: "logout",
Usage: "Logout current user",
Aliases: []string{"lo"},
Flags: []cli.Flag{
domainFlag,
disablePopUpFlag,
},
Action: func(ctx *cli.Context) error {
logoutURL := fmt.Sprintf("https://%s/v2/logout", ctx.String("domain"))
fmt.Printf("Logout via this url: %s\n", logoutURL)
if !ctx.Bool("disable-pop-up") {
if err := c.loginService.OpenBrowser(logoutURL); err != nil {
return fmt.Errorf("Unable to open browser, please open url manually.")
}
}
return nil
},
}}, nil
}
1 change: 1 addition & 0 deletions cmd/tcld/fx.go
Expand Up @@ -19,6 +19,7 @@ func fxOptions() fx.Option {
app.NewRequestCommand,
app.GetLoginClient,
app.NewLoginCommand,
app.NewLogoutCommand,
func() app.GetNamespaceClientFn {
return app.GetNamespaceClient
},
Expand Down

0 comments on commit f878b4e

Please sign in to comment.