@@ -3,6 +3,7 @@ package auth
33import (
44 "context"
55 "fmt"
6+ "net/url"
67
78 "github.com/spf13/cobra"
89
@@ -18,6 +19,7 @@ type AuthCmd struct {
1819 Port int
1920 LocalBindAddress string
2021 RedirectURLHostname string
22+ RedirectURL string
2123}
2224
2325func NewCommand (globalFlags * flags.GlobalFlags ) * cobra.Command {
@@ -33,7 +35,14 @@ func NewCommand(globalFlags *flags.GlobalFlags) *cobra.Command {
3335
3436 authCmd .Flags ().IntVar (& cmd .Port , "port" , 0 , "port on which the auth server will listen (default 0)" )
3537 authCmd .Flags ().StringVar (& cmd .LocalBindAddress , "local-bind-address" , "127.0.0.1" , "local address on which the auth server will listen" )
36- authCmd .Flags ().StringVar (& cmd .RedirectURLHostname , "redirect-url-hostname" , "localhost" , "hostname of the redirect URL" )
38+
39+ // Declare the version flag and then you can deprecate it.
40+ authCmd .Flags ().StringVar (& cmd .RedirectURLHostname , "redirect-url-hostname" , "" , "hostname of the redirect URL" )
41+ err := authCmd .Flags ().MarkDeprecated ("redirect-url-hostname" , "use --redirect-url instead" )
42+ if err != nil {
43+ panic (fmt .Sprintf ("error marking flag --redirect-url-hostname as deprecated: %v" , err ))
44+ }
45+ authCmd .Flags ().StringVar (& cmd .RedirectURL , "redirect-url" , "" , "URL of the redirect URL to use for authentication, (e.g. http://localhost:12345)" )
3746
3847 return authCmd
3948}
@@ -48,12 +57,43 @@ func (cmd *AuthCmd) Run(cobraCmd *cobra.Command, args []string) error {
4857 _ = cli .Stop ()
4958 }()
5059
60+ // TODO: Remove this check in the v6 release.
61+ // --redirect-url-hostname is deprecated, so we keep it for backward compatibility.
62+ if cmd .RedirectURLHostname != "" && cmd .Port == 0 {
63+ return fmt .Errorf ("--port is required when using --redirect-url-hostname" )
64+ }
65+ if cmd .RedirectURL != "" && cmd .RedirectURLHostname != "" {
66+ return fmt .Errorf ("--redirect-url and --redirect-url-hostname cannot be used together" )
67+ }
68+ // End of TODO
69+
70+ // Validate the redirect URL if it is set.
71+ if cmd .RedirectURL != "" {
72+ _ , err := url .ParseRequestURI (cmd .RedirectURL )
73+ if err != nil {
74+ return fmt .Errorf ("invalid redirect URL '%s'" , cmd .RedirectURL )
75+ }
76+ }
77+
78+ // If redirect URL is set, we require the port to be specified as well.
79+ if cmd .RedirectURL != "" && cmd .Port == 0 {
80+ return fmt .Errorf ("--port is required when using --redirect-url" )
81+ }
82+
5183 // customize authentication options based on the command line parameters
5284 authOptions := app.AuthenticationOptions {}
5385 authOptions .LocalServerBindAddress = fmt .Sprintf ("%s:%d" , cmd .LocalBindAddress , cmd .Port )
5486
87+ // TODO: Remove this check in the v6 release.
88+ // If redirect URL hostname is set, we use it to construct the redirect URL.
5589 if cmd .RedirectURLHostname != "" {
56- authOptions .RedirectURLHostname = cmd .RedirectURLHostname
90+ authOptions .RedirectURL = fmt .Sprintf ("http://%s:%d" , cmd .RedirectURLHostname , cmd .Port )
91+ }
92+ // End of TODO
93+
94+ // If redirect URL is set, we use it to construct the redirect URL.
95+ if cmd .RedirectURL != "" {
96+ authOptions .RedirectURL = cmd .RedirectURL
5797 }
5898
5999 _ , err = cli .AuthenticateFromWeb (ctx , authOptions )
0 commit comments