Skip to content

show token scopes on auth status - #6546

Merged
mislav merged 9 commits into
trunkfrom
mntlty/user_scopes
Jan 25, 2023
Merged

mislav merged 9 commits into
trunkfrom
mntlty/user_scopes

Conversation

@mntlty

@mntlty mntlty commented Oct 31, 2022 •

Copy link
Copy Markdown
Contributor

Closes #6520

The current query to get the logged in user uses the graphql package https://pkg.go.dev/github.com/cli/shurcooL-graphql which does not expose the http headers in the response, which is where the scopes information is found. I also looked at

ghAPI "github.com/cli/go-gh/pkg/api"
but ran into the same issue, the response struct is not returned to the caller.

I also added some documentation to the httpmock.REST command.

Output looks like:

Screen Shot 2022-10-31 at 4 40 49 PM

In the case that no scopes are found, the output will say

X Token Scopes: None found

There are different prefixes for token types https://github.blog/2022-10-18-introducing-fine-grained-personal-access-tokens-for-github/

Server-to-server and Fine grained permissions tokens do not contain the "X-Oauth-Scopes" header, which would make the output above confusing. In those cases, we show a warning to the user.

For a fine-grained token (prefixed with github_pat_):

github.com
  X github.com: api call failed: non-200 OK status code: 401 Unauthorized body: "{\"message\":\"Personal access tokens with fine grained access do not support the GraphQL API\",\"documentation_url\":\"https://docs.github.com/graphql/guides/forming-calls-with-graphql#authenticating-with-graphql\"}"
  ✓ Logged in to github.com as  (GH_TOKEN)
  ✓ Git operations for github.com configured to use ssh protocol.
  ✓ Token: *******************
  ! Cannot determine scopes for fine-grained token.

For a server-to-server token (prefixed with ghs_):

github.com
  ✓ Logged in to github.com as mntlty-test[bot] (GH_TOKEN)
  ✓ Git operations for github.com configured to use ssh protocol.
  ✓ Token: *******************
  ! Cannot determine scopes for server-to-server token.

Comment thread pkg/cmd/auth/status/status_test.go Resolved
@mntlty
mntlty marked this pull request as ready for review October 31, 2022 23:54
@mntlty
mntlty requested a review from a team as a code owner October 31, 2022 23:54
@mntlty
mntlty requested review from mislav and removed request for a team October 31, 2022 23:54
@cliAutomation cliAutomation added the external pull request originating outside of the CLI core team label Oct 31, 2022
Comment thread pkg/cmd/auth/status/status.go Resolved
@mntlty
mntlty requested a review from mislav November 15, 2022 15:43
Comment thread pkg/cmd/auth/status/status.go Resolved
Comment thread pkg/cmd/auth/status/status.go Resolved
@mntlty
mntlty requested a review from samcoe December 5, 2022 22:57

@samcoe samcoe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks for the contribution. I pushed a small 💅 commit to cleanup some small style nits I had.

@samcoe samcoe assigned samcoe and unassigned mislav Dec 6, 2022
@samcoe
samcoe enabled auto-merge (squash) December 6, 2022 15:58

@mislav mislav left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost there! Thank you @mntlty

Comment thread pkg/cmd/auth/status/status.go Outdated
if scopesHeader != "" {
addMsg("%s Token Scopes: %s", cs.SuccessIcon(), scopesHeader)
} else {
addMsg("%s Token Scopes: None found", cs.Red("X"))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rendering this as an error in red color might be misleading for Integration tokens and PAT v2, which have a concept of "permissions" but not scopes, and thus do not render any x-oauth-scopes response header.

Maybe it's best to just not render information about "scopes" in that case and instead print the type of token used?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mislav thanks, I made the changes and updated the PR body to reflect if we don't know the token type and offer more information. If you think it's clearer, I can remove it entirely in those cases.

Thinking about this, I wonder if

  • the current output is perhaps not correct with a server-to-server-token, as we can't know if the token has the scopes needed for cli operations
  • we should modify the message when we don't know the user's login (see the output from a fine-grained token)
  • we should modify some of the other output when using a fine-grained token, where we also show an error message

I feel like these may be out of scope from the original issue, but wanted to bring them up.

@mntlty
mntlty disabled auto-merge December 9, 2022 15:24
@samcoe
samcoe requested a review from mislav January 3, 2023 19:45
- show path to config file
- display token prefix
- refine how missing scopes are shown

@mislav mislav left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @mntlty for the contribution and @samcoe for reviewing.

I've pushed some changes:

  • When a token for a specific host is invalid (i.e. results in a HTTP 4xx), this restores the original error message that prints: The {host} token in {source} is no longer valid.
  • Unless the token comes from environment variables, the value for {source} is now a file path to hosts.yml instead of a hardcoded string oauth_token (which might be a bug in go-gh)
  • When printing a masked token, print the token prefix like ghp_ unmasked so that the user can inspect which type of token they are using
  • Only print scopes information for “classic” PATs and OAuth tokens
  • gh auth status tests now assert exact command output instead of matching with regexp

@mntlty

mntlty commented Jan 25, 2023

Copy link
Copy Markdown
Contributor Author

@mislav can this be merged?

@mislav

mislav commented Jan 25, 2023

Copy link
Copy Markdown
Contributor

Yes. I guess that because the scope of my added changes, I wanted to give you a chance to weigh in.

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

Labels

external pull request originating outside of the CLI core team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Show token scopes in gh auth status

4 participants