Skip to content

Commit

Permalink
Remove username prompt on buf registry login (#3128)
Browse files Browse the repository at this point in the history
This removes the requirement to provide your username when calling `buf
registry login`. On login the token is validated by fetching the current
user. The username is used only to validate that the current token is
associated with the user but is otherwise redundant. The flag has now
been marked deprecated and is hidden.

The output of the login command is also updated to be more user friendly
prompting the user to visit the web UI to create a token if they do not
have one. On successful login the user is informed that they are logged
in as the current user.
  • Loading branch information
emcfarlane authored Jul 8, 2024
1 parent 7adb2a0 commit 2df4a13
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 21 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
- Add `buf generate --clean` flag that will delete the directories, jar files, or zip files that the
plugins will write to, prior to generation. Allows cleaning of existing assets without having
to call `rm -rf`.
- Deprecate `--username` flag on and username prompt on `buf registry login`. A username is no longer
required to log in.

## [v1.34.0] - 2024-06-21

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func NewCommand(
return &appcmd.Command{
Use: name + " <domain>",
Short: `Log in to the Buf Schema Registry`,
Long: fmt.Sprintf(`This prompts for your BSR username and a BSR token and updates your %s file with these credentials.
Long: fmt.Sprintf(`This prompts for your BSR token and updates your %s file with these credentials.
The <domain> argument will default to buf.build if not specified.`, netrc.Filename),
Args: appcmd.MaximumNArgs(1),
Run: builder.NewRunFunc(
Expand All @@ -73,8 +73,10 @@ func (f *flags) Bind(flagSet *pflag.FlagSet) {
&f.Username,
usernameFlagName,
"",
"The username to use. This command prompts for a username by default",
"The username to use.",
)
_ = flagSet.MarkDeprecated(usernameFlagName, "This flag is no longer needed as the username is automatically derived from the token")
_ = flagSet.MarkHidden(usernameFlagName)
flagSet.BoolVar(
&f.TokenStdin,
tokenStdinFlagName,
Expand Down Expand Up @@ -135,26 +137,15 @@ func inner(
remote = container.Arg(0)
}
// Do not print unless we are prompting
if flags.Username == "" && !flags.TokenStdin {
if !flags.TokenStdin {
if _, err := fmt.Fprintf(
container.Stdout(),
"Log in with your Buf Schema Registry username. If you don't have a username, create one at https://%s.\n\n",
"Enter the BSR token created at https://%s/settings/user.\n\n",
remote,
); err != nil {
return err
}
}
username := flags.Username
if username == "" {
var err error
username, err = bufcli.PromptUser(container, "Username: ")
if err != nil {
if errors.Is(err, bufcli.ErrNotATTY) {
return errors.New("cannot perform an interactive login from a non-TTY device")
}
return err
}
}
var token string
if flags.TokenStdin {
data, err := io.ReadAll(container.Stdin())
Expand Down Expand Up @@ -197,14 +188,11 @@ func inner(
if user == nil {
return errors.New("no user found for provided token")
}
if user.Username != username {
return errors.New("the username associated with the provided token does not match the provided username")
}
if err := netrc.PutMachines(
container,
netrc.NewMachine(
remote,
username,
user.Username,
token,
),
); err != nil {
Expand All @@ -217,9 +205,9 @@ func inner(
if err != nil {
return err
}
loggedInMessage := fmt.Sprintf("Credentials saved to %s.\n", netrcFilePath)
loggedInMessage := fmt.Sprintf("Logged in as %s. Credentials saved to %s.\n", user.Username, netrcFilePath)
// Unless we did not prompt at all, print a newline first
if flags.Username == "" || !flags.TokenStdin {
if !flags.TokenStdin {
loggedInMessage = "\n" + loggedInMessage
}
if _, err := container.Stdout().Write([]byte(loggedInMessage)); err != nil {
Expand Down

0 comments on commit 2df4a13

Please sign in to comment.