Skip to content

Additional or alternative authentication method #297

Description

@DannyBen

A browser is not always available for authentication.

I am working on a vagrant linux guest, which cannot open browser in the Windows host.

I would love to see a gh login command (or similar) to store credentials as appropriate by the system without opening a browser.

At the very least - if this is not changed, or until it is - I would love to see the URL it wants to open so I can open it manually.

Activity

  1. vilmibm commented on Feb 3, 2020

    @vilmibm
    Contributor

    Thanks for the feedback! Our current suggested work-around is to authenticate in a GUI environment and then copy gh's config file to the headless environment.

    Looking at the code, it seems like it is supposed to print out the URL if opening the browser fails:

    fmt.Fprintf(os.Stderr, "Please open the following URL manually:\n%s\n", startURL)

    did this not happen for you? if so, we should capture that as a bug.

  2. DannyBen commented on Feb 3, 2020

    @DannyBen
    Author

    Looking at the code, it seems like it is supposed to print out the URL if opening the browser fails:

    Did not happen for me. After pressing Enter to open the browser, it just hangs there waiting for my inevitable Ctrl+C.

  3. vilmibm commented on Feb 3, 2020

    @vilmibm
    Contributor

    oof, thanks for the clarification. I'll mark this as a bug, then.

  4. added
    bugSomething isn't working
    priority-2Affects more than a few users but doesn't prevent core functions
    priority-3Affects a small number of users or is largely cosmetic
    and removed
    priority-2Affects more than a few users but doesn't prevent core functions
    on Feb 3, 2020
  5. vilmibm commented on Feb 3, 2020

    @vilmibm
    Contributor

    Marking as a p3 since there is a workaround (manually copying gh's config). We should at a minimum ensure that the URL is being printed.

  6. genotrance commented on Feb 12, 2020

    @genotrance

    Making life easy for folks who already have an oauth token.

    $HOME/.config/gh/config.yml

    github.com:
      - user: USERNAME
        oauth_token: TOKEN
  7. dapi commented on Feb 12, 2020

    @dapi

    Could you print URL should be opened? To open it manually.

  8. jonasrauber commented on Feb 12, 2020

    @jonasrauber

    For me, the URL is shown and I can open it manually.
    But then manually calling the callback results in a "the access token could not be read from HTTP response" error

  9. eplodn commented on Feb 13, 2020

    @eplodn
    $ ssh [email protected] -T                                                                              
    Hi eplodn! You've successfully authenticated, but GitHub does not provide shell access.
    

    I don't understand. If GitHub can successfully authenticate me by my private key (which is inherently more secure than passwords or tokens in files), why does it need any further authentication?

  10. tdumitrescu commented on Feb 13, 2020

    @tdumitrescu

    A more detailed step-by-step workaround for authenticating on headless environments without a browser:

    • when you get the "Please open the following URL manually:" prompt, open that URL on a different system that does have a browser (e.g. the one you're reading this on right now), and authorize in the UI
    • the UI will then attempt to redirect to a localhost URL, which will fail because the temporary gh auth server is running on your headless system, not the one with the browser
    • copy the URL that it failed to open from your browser address bar (it will look something like http://localhost:35399/callback?browser_session_id=...)
    • in another terminal session on your headless system, use cURL or another http client to send the request, e.g. curl 'http://localhost:35399/callback?browser_session_id=...' (single-quotes around the address to avoid shell interpreting special chars like &).
    • the original terminal where you were trying to run gh should now display "Authentication complete. Press Enter to continue..."
    • you now have valid credentials stored on the headless system in ~/.config/gh/config.yml and can run gh without re-authenticating. fun times
  11. DannyBen commented on Feb 13, 2020

    @DannyBen
    Author

    A more detailed step-by-step workaround for authenticating on headless environments

    Thanks, this is appreciated, but.... this lack of simple authentication in the CLI is a great deterrent.
    There are at least 10 one/two-liners for doing things the CLI does, without all this complication.

    I have been working my GitHub account day and night though my own CLI scripts and tools, having nothing but a GITHUB_ACCESS_TOKEN in my environment. Headless should be a first class citizen for CLI, not an afterthought.

    Although I see this "bug" progressing in the project columns - which is appreciated - I think the authentication method should be changed (or expanded), and not fixed to just show the URL for headless.

  12. odinuge commented on Feb 14, 2020

    @odinuge
    Contributor

    What about something like the one used in the kubernetes cli? https://kubernetes.io/docs/reference/access-authn-authz/authentication/#client-go-credential-plugins. It makes it simple to use custom auth logic based on running shell commands. I don't like storing my credentials in plain text, so I am using pass for everything. Using such a method allows the user to use everything from webhooks, to pass, to plain env vars.

    Here is a simple proof-of-concept (that I am currently using): https://github.com/cli/cli/compare/master...odinuge:exec-auth?expand=1

    Any thoughts?

    cc @vilmibm

  13. 24 remaining items

  14. Lewiscowles1986 commented on Jun 1, 2020

    @Lewiscowles1986

    The last paragraph would seem to me to fall into unreasonable asks, but I don't work for GitHub so I'll leave them to make that determination.

    The first paragraph hints at needing a default timeout and possible detection of non-interactive session.

    The middle paragraph needs secrets to work, which is what tokens and using an environment variable will solve for us. It's a one-time effort to retrieve / create the token, and then all your environments can use a secrets manager to ensure that the oauth token is protected.

    Part of the problem is that much of this isn't protecting GitHub, it's protecting your team members and you.

  15. felipecrs commented on Jun 2, 2020

    @felipecrs

    Issue #1074 can help this.

  16. gwestersf commented on Jun 17, 2020

    @gwestersf

    In late July, GitHub is releasing support for the OAuth Device Authorization Flow in public beta. This allows any CLI client or developer tool to authenticate using a secondary system with a browser. We developed this because sometimes tools running virtual machines or containers can't accept callbacks on localhost ports even if they have an acceptable browser with javascript enabled. And it would be neat if someone wanted to build a GitHub app for a smart television!

    The CLI team is evaluating the device flow as a potential solution for this issue, as part the broader authentication-related work in #957.

    83147646-f8b78680-a0c5-11ea-9b07-189a2760a4ba

  17. Lewiscowles1986 commented on Jun 19, 2020

    @Lewiscowles1986

    While it might be part of a solution for people who have an interactive session. I think after #1245 drops, it should be possible to then use that token for non-interactive sessions like those in CI. It could actually be really cool documentation for how to use the gh tool, to look at tests and see, oh look they supply a token via ENV (of course the token would be in secrets)

  18. nanaluz commented on Jul 12, 2020

    @nanaluz
  19. mathieusama commented on Sep 4, 2020

    @mathieusama

    Based on @tdumitrescu's instructions, I created the following bash script to streamline the whole process of authenticating on headless environments without a browser, all within a single terminal session.

    #!/bin/bash
    
    if [[ "$1" == "-f" ]]; then
      echo "Deleting existing credentials"
      rm -f $HOME/.config/gh/*
    fi
    
    if [[ -f $HOME/.config/gh/config.yml ]]; then
      echo "Already authenticated."
      exit 0
    fi
    
    set -euo pipefail
    
    echo -e "Browse to the following URL and authenticate:\n"
    echo -e "\n" \
      | gh repo view dummy/dummy 2>&1 >/dev/null \
      | grep 'https://' &
    
    sleep 0.1s
    echo -e "\nYou will then be redirected to an invalid URL, which you should cut and paste here:"
    read -r URL
    curl -s "$URL" \
      | grep -o "Successfully authenticated GitHub CLI" || echo "Authentication failed"

    Note that you can pass a -f flag to the script to force recreating credentials file, otherwise if the file already exists, it will skip the whole process.

    Enjoy! =)

  20. felipecrs commented on Sep 4, 2020

    @felipecrs

    @mathieusama Awesome work!

    But I hope we won't need this anymore with gh auth login.

  21. mislav commented on Sep 4, 2020

    @mislav
    Contributor

    @mathieusama That's really nifty; thank you for sharing!

    I also hope that this won't be necessary in gh v0.12.0+ (coming next week!)

  22. mathieusama commented on Sep 4, 2020

    @mathieusama

    Really looking forward to that v0.12.0 release! (especially for the gh repo create -y flag to prevent prompting user!)

  23. mislav commented on Sep 8, 2020

    @mislav
    Contributor

    The v0.12 release with improved authentication is out! https://github.com/cli/cli/releases/tag/v0.12.0

    Try gh auth login on a headless host. When no graphical browser is detected, the flow prints out a URL that you can now open in a graphical browser on any other machine to complete the flow.

    Additionally, you may pass in a token to skip browser authentication via gh auth login --with-token < mytoken-file.txt

  24. jamesbraza commented on Aug 2, 2024

    @jamesbraza

    If you want to see the URL gh auth login uses, use BROWSER=nonexistent gh auth login, and it ultimately prints https://github.com/login/device

  25. Lewiscowles1986 commented on Aug 3, 2024

    @Lewiscowles1986

    Is this device-mode OAuth?

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

    bugSomething isn't workingpriority-3Affects a small number of users or is largely cosmetic

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions