Skip to content

Commit

Permalink
Remove Apps and first run logic from Tenant struct
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiught committed Apr 15, 2023
1 parent 4f88a7d commit 1078c79
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 85 deletions.
64 changes: 7 additions & 57 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,13 @@ type config struct {
// The fields are tailor fit specifically for
// interacting with the management API.
type Tenant struct {
Name string `json:"name"`
Domain string `json:"domain"`
AccessToken string `json:"access_token,omitempty"`
Scopes []string `json:"scopes,omitempty"`
ExpiresAt time.Time `json:"expires_at"`
Apps map[string]app `json:"apps,omitempty"`
DefaultAppID string `json:"default_app_id,omitempty"`
ClientID string `json:"client_id"`
}

type app struct {
FirstRuns map[string]bool `json:"first_runs"`
Name string `json:"name"`
Domain string `json:"domain"`
AccessToken string `json:"access_token,omitempty"`
Scopes []string `json:"scopes,omitempty"`
ExpiresAt time.Time `json:"expires_at"`
DefaultAppID string `json:"default_app_id,omitempty"`
ClientID string `json:"client_id"`
}

var errUnauthenticated = errors.New("Not logged in. Try 'auth0 login'.")
Expand Down Expand Up @@ -301,10 +296,6 @@ func (c *cli) getTenant() (Tenant, error) {
return Tenant{}, fmt.Errorf("Unable to find tenant: %s; run 'auth0 tenants use' to see your configured tenants or run 'auth0 login' to configure a new tenant", c.tenant)
}

if t.Apps == nil {
t.Apps = map[string]app{}
}

return t, nil
}

Expand Down Expand Up @@ -390,22 +381,6 @@ func (c *cli) removeTenant(ten string) error {
return nil
}

func (c *cli) isFirstCommandRun(clientID string, command string) (bool, error) {
tenant, err := c.getTenant()

if err != nil {
return false, err
}

if a, found := tenant.Apps[clientID]; found {
if a.FirstRuns[command] {
return false, nil
}
}

return true, nil
}

func (c *cli) setDefaultAppID(id string) error {
tenant, err := c.getTenant()
if err != nil {
Expand All @@ -422,31 +397,6 @@ func (c *cli) setDefaultAppID(id string) error {
return nil
}

func (c *cli) setFirstCommandRun(clientID string, command string) error {
tenant, err := c.getTenant()
if err != nil {
return err
}

if a, found := tenant.Apps[clientID]; found {
if a.FirstRuns == nil {
a.FirstRuns = map[string]bool{}
}
a.FirstRuns[command] = true
tenant.Apps[clientID] = a
} else {
tenant.Apps[clientID] = app{
FirstRuns: map[string]bool{
command: true,
},
}
}

c.config.Tenants[tenant.Domain] = tenant

return nil
}

func checkInstallID(c *cli) error {
if c.config.InstallID == "" {
c.config.InstallID = uuid.NewString()
Expand Down
27 changes: 2 additions & 25 deletions internal/cli/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,37 +152,14 @@ func testLoginCmd(cli *cli) *cobra.Command {
}

var userInfo *authutil.UserInfo
if err := ansi.Spinner("Fetching user metadata", func() error {
// Use the access token to fetch user information from the /userinfo endpoint.
if err := ansi.Spinner("Fetching user metadata", func() (err error) {
userInfo, err = authutil.FetchUserInfo(http.DefaultClient, tenant.Domain, tokenResponse.AccessToken)
return err
}); err != nil {
return fmt.Errorf("failed to fetch user info: %w", err)
}

cli.renderer.Newline()
cli.renderer.TestLogin(userInfo, tokenResponse)
cli.renderer.Newline()

const commandKey = "test_login"
isFirstRun, err := cli.isFirstCommandRun(inputs.ClientID, commandKey)
if err != nil {
return err
}

if isFirstRun {
cli.renderer.Infof("Login flow is working!")
cli.renderer.Infof(
"%s Consider downloading and running a quickstart next by running `auth0 quickstarts download %s`",
ansi.Faint("Hint:"),
inputs.ClientID,
)

if err := cli.setFirstCommandRun(inputs.ClientID, commandKey); err != nil {
return err
}
}

cli.renderer.TestLogin(userInfo, tokenResponse, inputs.ClientID)
return nil
},
}
Expand Down
14 changes: 11 additions & 3 deletions internal/display/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,20 @@ type userInfoAndTokens struct {
Tokens *authutil.TokenResponse `json:"tokens"`
}

func (r *Renderer) TestLogin(u *authutil.UserInfo, t *authutil.TokenResponse) {
r.Heading("/userinfo")
func (r *Renderer) TestLogin(u *authutil.UserInfo, t *authutil.TokenResponse, clientID string) {
r.Heading("user metadata and token")

data := &userInfoAndTokens{UserInfo: u, Tokens: t}

r.JSONResult(data)

r.Newline()
r.Newline()
r.Infof(
"%s Login flow is working! If this is the first time you are testing the login flow for this "+
"application, consider downloading and running a quickstart next by running %s",
ansi.Faint("Hint:"),
fmt.Sprintf("`auth0 quickstarts download %s`", clientID),
)
}

func (r *Renderer) TestToken(client *management.Client, t *authutil.TokenResponse) {
Expand Down

0 comments on commit 1078c79

Please sign in to comment.