GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — show token scopes on auth status by mntlty · Pull Request #6546 · cli/cli

Conversation

@mntlty

@mntlty mntlty commented Oct 31, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

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

cli/api/client.go

Line 14 in 37e343a

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@mntlty
show token scopes on auth status
37e343a
mntlty
mntlty commented Oct 31, 2022
View reviewed changes
Comment thread pkg/cmd/auth/status/status_test.go Resolved

Uh oh!

There was an error while loading. Please reload this page.

@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
mislav
mislav suggested changes Nov 2, 2022
View reviewed changes
Comment thread pkg/cmd/auth/status/status.go Resolved

Uh oh!

There was an error while loading. Please reload this page.

@mntlty
use shared.GetScopes
6bf194f
@mntlty
mntlty requested a review from mislav November 15, 2022 15:43
@samcoe samcoe assigned mislav Nov 16, 2022
mntlty added 2 commits December 5, 2022 14:05
@mntlty
only make a single api call to get scopes
c30439f
@mntlty
add defer to reg.Verify(t) and update tests
f9e1a42
mntlty
mntlty commented Dec 5, 2022
View reviewed changes
Comment thread pkg/cmd/auth/shared/oauth_scopes_test.go Resolved

Uh oh!

There was an error while loading. Please reload this page.

@mntlty
do not return error mark it as failed
36fc119
mntlty
mntlty commented Dec 5, 2022
View reviewed changes
Comment thread pkg/cmd/auth/status/status.go Resolved

Uh oh!

There was an error while loading. Please reload this page.

mntlty
mntlty commented Dec 5, 2022
View reviewed changes
Comment thread pkg/cmd/auth/status/status.go Resolved

Uh oh!

There was an error while loading. Please reload this page.

@mntlty
mntlty requested a review from samcoe December 5, 2022 22:57
@samcoe
Small polish/style changes
cd959bc
samcoe
samcoe approved these changes Dec 6, 2022
View reviewed changes

@samcoe samcoe left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@samcoe samcoe assigned samcoe and unassigned mislav Dec 6, 2022
@samcoe
samcoe enabled auto-merge (squash) December 6, 2022 15:58
mislav
mislav suggested changes Dec 8, 2022
View reviewed changes

@mislav mislav left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
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"))

@mislav mislav Dec 8, 2022

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

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?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@mntlty mntlty Dec 9, 2022

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@mntlty
show scopes warning message for fine-grained and server-to-server tokens
70de1dc
@mntlty
mntlty disabled auto-merge December 9, 2022 15:24
@samcoe
samcoe requested a review from mislav January 3, 2023 19:45
mislav added 2 commits January 10, 2023 18:17
@mislav
auth status tweaks
8c28f9f
- show path to config file
- display token prefix
- refine how missing scopes are shown
@mislav
Merge remote-tracking branch 'origin' into mntlty/user_scopes
e710636
mislav
mislav approved these changes Jan 10, 2023
View reviewed changes

@mislav mislav left a comment •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@mntlty

mntlty commented Jan 25, 2023

Copy link
Copy Markdown
Contributor Author

@mislav can this be merged?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mislav
mislav merged commit a5edd8f into trunk Jan 25, 2023
@mislav
mislav deleted the mntlty/user_scopes branch January 25, 2023 15:25
@SCS-CBO SCS-CBO mentioned this pull request Mar 9, 2023
Building local DevEnv, downloading dependencies: Authentication fails when using "gh auth status" in AL-Go-Helper.ps1 microsoft/AL-Go#434
Closed
@renovate renovate Bot mentioned this pull request Jul 24, 2023
chore(deps): update dependency cli/cli to v2.37.0 () - closed suzuki-shunsuke/tfaction-getting-started#140
Closed
1 task
@coderabbitai coderabbitai Bot mentioned this pull request Jun 6, 2026
fix: support fine-grained tokens in pr-review scope validation combsmith/.github-private#443
Merged
@coderabbitai coderabbitai Bot mentioned this pull request Jul 27, 2026
fix(server): treat forwarded loopback ports as loopback lidge-jun/opencodex#573
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

2 more reviewers
@mislav mislav mislav approved these changes
@samcoe samcoe samcoe approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@samcoe samcoe

Labels

external pull request originating outside of the CLI core team

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Show token scopes in gh auth status

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@mntlty @mislav @samcoe @cliAutomation
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.