Skip to content

Enhance gh auth login to copy one-time code into clipboard before launching browser #11354

Closed

Description

@andyfeller

Describe the feature or problem you’d like to solve

When I use gh auth login to login with the GitHub CLI OAuth app, I have to manually select and copy the one-time device code before launching the browser so I can paste it into the GitHub.com device login screen.

! First copy your one-time code: 7701-C5F6
Press Enter to open https://github.com/login/device in your browser... 

I'd like some way I can configure gh to use the clipboard for certain features like gh auth login.

Proposed solution

I can see 3 different ways to provide this ability depending on how broadly this capability might be extended:

  1. Environment variable: GH_CLIPBOARD=1
  2. Configuration setting: gh config set clipboard enabled
  3. Command-specific flag: gh auth login --clipboard

Additional context

One of gh dependencies is already including a library for putting content into the system's copy buffer:

cli/go.mod

Line 73 in 42a8e02

github.com/atotto/clipboard v0.1.4 // indirect

Activity

  1. added
    enhancement
    on Jul 21, 2025
  2. github-actions commented on Jul 21, 2025

    @github-actions
    Contributor

    Thank you for your issue! We have categorized it as a feature request, and it has been added to our backlog. In doing so, we are not committing to implementing this feature at this time, but, we will consider it for future releases based on community feedback and our own product roadmap.

    Unless you see the help wanted Contributions welcome label, we are not currently looking for external contributions for this feature.

    If you come across this issue and would like to see it implemented, please add a thumbs up! This will help us prioritize the feature. Please only comment if you have additional information or viewpoints to contribute.

  3. added
    auth
    gh-auth
    needs-triage
    on Jul 21, 2025
  4. summersdarius16-cmyk commented on Jul 21, 2025

    @summersdarius16-cmyk
  5. summersdarius16-cmyk commented on Jul 21, 2025

    @summersdarius16-cmyk
  6. added
    help wanted candidate
    and removed
    needs-triage
    on Jul 28, 2025
  7. BagToad commented on Jul 28, 2025

    @BagToad
    Member

    Notes from internal meetings: we might want to include a message on stderr when there is a TTY indicating the availability of --clipboard for discoverability reasons. We also discussed adding a press c to copy alternative option to the press enter to open browser prompt, but will likely table that until it makes sense.

  8. andyfeller commented on Jul 28, 2025

    @andyfeller
    ContributorAuthor

    Acceptance Criteria

    1. When a user authenticates with gh auth login --web --clipboard OR
      When a user authenticates with gh auth login --clipboard and selects Login with a web browser
      Then gh auth login will:

      1. Write the one-time OAuth device code to the user's clipboard

      2. Print a message on stderr telling the user that the one-time code has been stored in their clipboard such as:

        One-time code copied to clipboard
        
    2. When a user authenticates with gh auth login --web OR
      When a user authenticates with gh auth login and selects Login with a web browser
      Then gh auth login will display the existing First copy your one-time code message

    Outside of scope

    • no environment variable of gh config setting to control this
    • no assumptions of whether this should change with interactive or non-interactive flows; users who provide this flag will get the behavior despite the modality

    Related code

    func AuthFlow(oauthHost string, IO *iostreams.IOStreams, notice string, additionalScopes []string, isInteractive bool, b browser.Browser) (string, string, error) {
    w := IO.ErrOut
    cs := IO.ColorScheme()
    httpClient := &http.Client{}
    debugEnabled, debugValue := utils.IsDebugEnabled()
    if debugEnabled {
    logTraffic := strings.Contains(debugValue, "api")
    httpClient.Transport = verboseLog(IO.ErrOut, logTraffic, IO.ColorEnabled())(httpClient.Transport)
    }
    minimumScopes := []string{"repo", "read:org", "gist"}
    scopes := append(minimumScopes, additionalScopes...)
    host, err := oauth.NewGitHubHost(ghinstance.HostPrefix(oauthHost))
    if err != nil {
    return "", "", err
    }
    flow := &oauth.Flow{
    Host: host,
    ClientID: oauthClientID,
    ClientSecret: oauthClientSecret,
    CallbackURI: getCallbackURI(oauthHost),
    Scopes: scopes,
    DisplayCode: func(code, verificationURL string) error {
    fmt.Fprintf(w, "%s First copy your one-time code: %s\n", cs.Yellow("!"), cs.Bold(code))
    return nil
    },
    BrowseURL: func(authURL string) error {
    if u, err := url.Parse(authURL); err == nil {
    if u.Scheme != "http" && u.Scheme != "https" {
    return fmt.Errorf("invalid URL: %s", authURL)
    }
    } else {
    return err
    }
    if !isInteractive {
    fmt.Fprintf(w, "%s to continue in your web browser: %s\n", cs.Bold("Open this URL"), authURL)
    return nil
    }
    fmt.Fprintf(w, "%s to open %s in your browser... ", cs.Bold("Press Enter"), authURL)
    _ = waitForEnter(IO.In)
    if err := b.Browse(authURL); err != nil {
    fmt.Fprintf(w, "%s Failed opening a web browser at %s\n", cs.Red("!"), authURL)
    fmt.Fprintf(w, " %s\n", err)
    fmt.Fprint(w, " Please try entering the URL in your browser manually\n")
    }
    return nil
    },
    WriteSuccessHTML: func(w io.Writer) {
    fmt.Fprint(w, oauthSuccessPage)
    },
    HTTPClient: httpClient,
    Stdin: IO.In,
    Stdout: w,
    }

  9. added
    help wanted
    and removed
    help wanted candidate
    on Jul 28, 2025
  10. andyfeller commented on Jul 30, 2025

    @andyfeller
    ContributorAuthor

    @BagToad : Perhaps outside the scope of this issue, but I can also see this being extended to gh auth refresh since a similar experience is present there:

    $ gh auth refresh -s read:project       
    ? What account do you want to refresh auth for? github.com
    
    ! First copy your one-time code: ###X-####
    Press Enter to open https://github.com/login/device in your browser... 
  11. BagToad commented on Jul 30, 2025

    @BagToad
    Member

    @BagToad : Perhaps outside the scope of this issue, but I can also see this being extended to gh auth refresh since a similar experience is present there:

    $ gh auth refresh -s read:project
    ? What account do you want to refresh auth for? github.com

    ! First copy your one-time code: ###X-####
    Press Enter to open https://github.com/login/device in your browser...

    Right! Good call. I think we should make another issue, maybe a sub-issue? They might be closed by the same PR but in case they aren't then it would be good to have two issues.

  12. BagToad commented on Aug 25, 2025

    @BagToad
    Member

    NOTE: the closing PR #11518 implements gh auth login and gh auth refresh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    authenhancementgh-authhelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions