Skip to content

Commit

Permalink
Merge branch 'main' into cli-135
Browse files Browse the repository at this point in the history
  • Loading branch information
alejofernandez authored Apr 30, 2021
2 parents 8b97b43 + 457d3ff commit 435c222
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 84 deletions.
92 changes: 75 additions & 17 deletions internal/cli/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"net/url"
"strconv"

"github.com/auth0/auth0-cli/internal/ansi"
"github.com/auth0/auth0-cli/internal/prompt"
Expand Down Expand Up @@ -35,9 +36,23 @@ var (
Name: "Scopes",
LongForm: "scopes",
ShortForm: "s",
Help: "Comma-separated list of scopes.",
Help: "Comma-separated list of scopes (permissions).",
IsRequired: true,
}
apiTokenLifetime = Flag{
Name: "Token Lifetime",
LongForm: "token-lifetime",
ShortForm: "l",
Help: "The amount of time in seconds that the token will be valid after being issued. Default value is 86400 seconds (1 day).",
AlwaysPrompt: true,
}
apiOfflineAccess = Flag{
Name: "Allow Offline Access",
LongForm: "offline-access",
ShortForm: "o",
Help: "Whether Refresh Tokens can be issued for this API (true) or not (false).",
AlwaysPrompt: true,
}
)

func apisCmd(cli *cli) *cobra.Command {
Expand Down Expand Up @@ -147,9 +162,11 @@ auth0 apis show <id|audience>`,

func createApiCmd(cli *cli) *cobra.Command {
var inputs struct {
Name string
Identifier string
Scopes []string
Name string
Identifier string
Scopes []string
TokenLifetime int
AllowOfflineAccess bool
}

cmd := &cobra.Command{
Expand All @@ -158,8 +175,10 @@ func createApiCmd(cli *cli) *cobra.Command {
Short: "Create a new API",
Long: "Create a new API.",
Example: `auth0 apis create
auth0 apis create --name myapi
auth0 apis create -n myapi --identifier http://my-api`,
auth0 apis create --name myapi
auth0 apis create -n myapi --identifier http://my-api
auth0 apis create -n myapi --token-expiration 6100
auth0 apis create -n myapi -e 6100 --offline-access=true`,
PreRun: func(cmd *cobra.Command, args []string) {
prepareInteractivity(cmd)
},
Expand All @@ -176,9 +195,19 @@ auth0 apis create -n myapi --identifier http://my-api`,
return err
}

if err := apiTokenLifetime.Ask(cmd, &inputs.TokenLifetime, auth0.String("86400")); err != nil {
return err
}

if err :=apiOfflineAccess.AskBool(cmd, &inputs.AllowOfflineAccess, nil); err != nil {
return err
}

api := &management.ResourceServer{
Name: &inputs.Name,
Identifier: &inputs.Identifier,
Name: &inputs.Name,
Identifier: &inputs.Identifier,
AllowOfflineAccess: &inputs.AllowOfflineAccess,
TokenLifetime: &inputs.TokenLifetime,
}

if len(inputs.Scopes) > 0 {
Expand All @@ -199,15 +228,19 @@ auth0 apis create -n myapi --identifier http://my-api`,
apiName.RegisterString(cmd, &inputs.Name, "")
apiIdentifier.RegisterString(cmd, &inputs.Identifier, "")
apiScopes.RegisterStringSlice(cmd, &inputs.Scopes, nil)
apiOfflineAccess.RegisterBool(cmd, &inputs.AllowOfflineAccess, false)
apiTokenLifetime.RegisterInt(cmd, &inputs.TokenLifetime, 0)

return cmd
}

func updateApiCmd(cli *cli) *cobra.Command {
var inputs struct {
ID string
Name string
Scopes []string
ID string
Name string
Scopes []string
TokenLifetime int
AllowOfflineAccess bool
}

cmd := &cobra.Command{
Expand All @@ -217,7 +250,9 @@ func updateApiCmd(cli *cli) *cobra.Command {
Long: "Update an API.",
Example: `auth0 apis update
auth0 apis update <id|audience>
auth0 apis update <id|audience> --name myapi`,
auth0 apis update <id|audience> --name myapi
auth0 apis update -n myapi --token-expiration 6100
auth0 apis update -n myapi -e 6100 --offline-access=true`,
PreRun: func(cmd *cobra.Command, args []string) {
prepareInteractivity(cmd)
},
Expand Down Expand Up @@ -249,7 +284,22 @@ auth0 apis update <id|audience> --name myapi`,
return err
}

api := &management.ResourceServer{}
currentTokenLifetime := strconv.Itoa(auth0.IntValue(current.TokenLifetime))
if err := apiTokenLifetime.AskU(cmd, &inputs.TokenLifetime, &currentTokenLifetime); err != nil {
return err
}

if !apiOfflineAccess.IsSet(cmd) {
inputs.AllowOfflineAccess = auth0.BoolValue(current.AllowOfflineAccess)
}

if err := apiOfflineAccess.AskBoolU(cmd, &inputs.AllowOfflineAccess, current.AllowOfflineAccess); err != nil {
return err
}

api := &management.ResourceServer{
AllowOfflineAccess: &inputs.AllowOfflineAccess,
}

if len(inputs.Name) == 0 {
api.Name = current.Name
Expand All @@ -263,6 +313,12 @@ auth0 apis update <id|audience> --name myapi`,
api.Scopes = apiScopesFor(inputs.Scopes)
}

if inputs.TokenLifetime == 0 {
api.TokenLifetime = current.TokenLifetime
} else {
api.TokenLifetime = &inputs.TokenLifetime
}

if err := ansi.Waiting(func() error {
return cli.api.ResourceServer.Update(current.GetID(), api)
}); err != nil {
Expand All @@ -276,6 +332,8 @@ auth0 apis update <id|audience> --name myapi`,

apiName.RegisterStringU(cmd, &inputs.Name, "")
apiScopes.RegisterStringSliceU(cmd, &inputs.Scopes, nil)
apiOfflineAccess.RegisterBoolU(cmd, &inputs.AllowOfflineAccess, false)
apiTokenLifetime.RegisterIntU(cmd, &inputs.TokenLifetime, 0)

return cmd
}
Expand Down Expand Up @@ -332,10 +390,10 @@ func openApiCmd(cli *cli) *cobra.Command {
}

cmd := &cobra.Command{
Use: "open",
Args: cobra.MaximumNArgs(1),
Short: "Open API settings page in Auth0 Manage",
Long: "Open API settings page in Auth0 Manage.",
Use: "open",
Args: cobra.MaximumNArgs(1),
Short: "Open API settings page in Auth0 Manage",
Long: "Open API settings page in Auth0 Manage.",
Example: `auth0 apis open
auth0 apis open <id|audience>`,
PreRun: func(cmd *cobra.Command, args []string) {
Expand Down
36 changes: 18 additions & 18 deletions internal/cli/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ auth0 apps create -n myapp -t [native|spa|regular|m2m] -- description <descripti
appIsSPA := apiTypeFor(inputs.Type) == appTypeSPA

// Prompt for callback URLs if app is not m2m
if !appIsM2M {
if !appIsM2M && !appCallbacks.IsSet(cmd) {
var defaultValue string

if !appIsNative {
Expand All @@ -368,7 +368,7 @@ auth0 apps create -n myapp -t [native|spa|regular|m2m] -- description <descripti
}

// Prompt for logout URLs if app is not m2m
if !appIsM2M {
if !appIsM2M && !appLogoutURLs.IsSet(cmd) {
var defaultValue string

if !appIsNative {
Expand All @@ -381,7 +381,7 @@ auth0 apps create -n myapp -t [native|spa|regular|m2m] -- description <descripti
}

// Prompt for allowed origins URLs if app is SPA
if appIsSPA {
if appIsSPA && !appOrigins.IsSet(cmd) {
defaultValue := appDefaultURL

if err := appOrigins.AskMany(cmd, &inputs.AllowedOrigins, &defaultValue); err != nil {
Expand All @@ -390,7 +390,7 @@ auth0 apps create -n myapp -t [native|spa|regular|m2m] -- description <descripti
}

// Prompt for allowed web origins URLs if app is SPA
if appIsSPA {
if appIsSPA && !appWebOrigins.IsSet(cmd) {
defaultValue := appDefaultURL

if err := appWebOrigins.AskMany(cmd, &inputs.AllowedWebOrigins, &defaultValue); err != nil {
Expand Down Expand Up @@ -519,7 +519,7 @@ auth0 apps update <id> -n myapp --type [native|spa|regular|m2m]`,
appIsSPA := apiTypeFor(inputs.Type) == appTypeSPA

// Prompt for callback URLs if app is not m2m
if !appIsM2M {
if !appIsM2M && !appCallbacks.IsSet(cmd) {
var defaultValue string

if !appIsNative {
Expand All @@ -536,7 +536,7 @@ auth0 apps update <id> -n myapp --type [native|spa|regular|m2m]`,
}

// Prompt for logout URLs if app is not m2m
if !appIsM2M {
if !appIsM2M && !appLogoutURLs.IsSet(cmd) {
var defaultValue string

if !appIsNative {
Expand All @@ -553,7 +553,7 @@ auth0 apps update <id> -n myapp --type [native|spa|regular|m2m]`,
}

// Prompt for allowed origins URLs if app is SPA
if appIsSPA {
if appIsSPA && !appOrigins.IsSet(cmd) {
defaultValue := appDefaultURL

if len(current.AllowedOrigins) > 0 {
Expand All @@ -566,7 +566,7 @@ auth0 apps update <id> -n myapp --type [native|spa|regular|m2m]`,
}

// Prompt for allowed web origins URLs if app is SPA
if appIsSPA {
if appIsSPA && !appWebOrigins.IsSet(cmd) {
defaultValue := appDefaultURL

if len(current.WebOrigins) > 0 {
Expand Down Expand Up @@ -745,25 +745,25 @@ func apiGrantsFor(s []string) []interface{} {
for i, v := range s {
switch strings.ToLower(v) {
case "authorization-code", "code":
res[i] = auth0.String("authorization_code")
res[i] = "authorization_code"
case "implicit":
res[i] = auth0.String("implicit")
res[i] = "implicit"
case "refresh-token":
res[i] = auth0.String("refresh_token")
res[i] = "refresh_token"
case "client-credentials", "credentials":
res[i] = auth0.String("client_credentials")
res[i] = "client_credentials"
case "password":
res[i] = auth0.String("password")
res[i] = "password"
case "password-realm":
res[i] = auth0.String("http://auth0.com/oauth/grant-type/password-realm")
res[i] = "http://auth0.com/oauth/grant-type/password-realm"
case "mfa-oob":
res[i] = auth0.String("http://auth0.com/oauth/grant-type/mfa-oob")
res[i] = "http://auth0.com/oauth/grant-type/mfa-oob"
case "mfa-otp":
res[i] = auth0.String("http://auth0.com/oauth/grant-type/mfa-otp")
res[i] = "http://auth0.com/oauth/grant-type/mfa-otp"
case "mfa-recovery-code":
res[i] = auth0.String("http://auth0.com/oauth/grant-type/mfa-recovery-code")
res[i] = "http://auth0.com/oauth/grant-type/mfa-recovery-code"
case "device-code":
res[i] = auth0.String("urn:ietf:params:oauth:grant-type:device_code")
res[i] = "urn:ietf:params:oauth:grant-type:device_code"
default:
}
}
Expand Down
20 changes: 14 additions & 6 deletions internal/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ func (f Flag) GetIsRequired() bool {
return f.IsRequired
}

func (f *Flag) IsSet(cmd *cobra.Command) bool {
return cmd.Flags().Changed(f.LongForm)
}

func (f *Flag) Ask(cmd *cobra.Command, value interface{}, defaultValue *string) error {
return askFlag(cmd, f, value, defaultValue, false)
}
Expand All @@ -49,12 +53,12 @@ func (f *Flag) AskManyU(cmd *cobra.Command, value interface{}, defaultValue *str
return askManyFlag(cmd, f, value, defaultValue, true)
}

func (f *Flag) AskBool(cmd *cobra.Command, value *bool, defaultValue *bool) {
askBoolFlag(cmd, f, value, defaultValue, false)
func (f *Flag) AskBool(cmd *cobra.Command, value *bool, defaultValue *bool) error {
return askBoolFlag(cmd, f, value, defaultValue, false)
}

func (f *Flag) AskBoolU(cmd *cobra.Command, value *bool, defaultValue *bool) {
askBoolFlag(cmd, f, value, defaultValue, true)
func (f *Flag) AskBoolU(cmd *cobra.Command, value *bool, defaultValue *bool) error {
return askBoolFlag(cmd, f, value, defaultValue, true)
}

func (f *Flag) Select(cmd *cobra.Command, value interface{}, options []string, defaultValue *string) error {
Expand Down Expand Up @@ -183,10 +187,14 @@ func askManyFlag(cmd *cobra.Command, f *Flag, value interface{}, defaultValue *s
return nil
}

func askBoolFlag(cmd *cobra.Command, f *Flag, value *bool, defaultValue *bool, isUpdate bool) {
func askBoolFlag(cmd *cobra.Command, f *Flag, value *bool, defaultValue *bool, isUpdate bool) error {
if shouldAsk(cmd, f, isUpdate) {
askBool(cmd, f, value, defaultValue)
if err := askBool(cmd, f, value, defaultValue); err != nil {
return err
}
}

return nil
}

func selectFlag(cmd *cobra.Command, f *Flag, value interface{}, options []string, defaultValue *string, isUpdate bool) error {
Expand Down
9 changes: 6 additions & 3 deletions internal/cli/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ func ask(cmd *cobra.Command, i commandInput, value interface{}, defaultValue *st
return nil
}

func askBool(cmd *cobra.Command, i commandInput, value *bool, defaultValue *bool) {
result := prompt.ConfirmDefault(i.GetLabel(), auth0.BoolValue(defaultValue))
*value = result
func askBool(cmd *cobra.Command, i commandInput, value *bool, defaultValue *bool) error {
if err := prompt.AskBool(i.GetLabel(), value, auth0.BoolValue(defaultValue)); err != nil {
return handleInputError(err)
}

return nil
}

func _select(cmd *cobra.Command, i commandInput, value interface{}, options []string, defaultValue *string, isUpdate bool) error {
Expand Down
8 changes: 7 additions & 1 deletion internal/cli/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,13 @@ auth0 rules update <rule-id> -n "My Updated Rule" --enabled=false`,
return err
}

ruleEnabled.AskBoolU(cmd, &inputs.Enabled, rule.Enabled)
if !ruleEnabled.IsSet(cmd) {
inputs.Enabled = auth0.BoolValue(rule.Enabled)
}

if err := ruleEnabled.AskBoolU(cmd, &inputs.Enabled, rule.Enabled); err != nil {
return err
}

// TODO(cyx): we can re-think this once we have
// `--stdin` based commands. For now we don't have
Expand Down
Loading

0 comments on commit 435c222

Please sign in to comment.