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

Remove Apps and first run logic from Tenant struct #742

Merged
merged 1 commit into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
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
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