From 3a7f56456e671c003aef12a7712ba706f00d07af Mon Sep 17 00:00:00 2001 From: vilmibm Date: Wed, 13 May 2020 14:24:29 -0500 Subject: [PATCH] tweak reauth code and request a new scope --- api/client.go | 7 ++++--- command/gist.go | 4 ++-- command/root.go | 7 ++++--- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/api/client.go b/api/client.go index d7b981bee..6d8fcb34c 100644 --- a/api/client.go +++ b/api/client.go @@ -76,11 +76,11 @@ func ReplaceTripper(tr http.RoundTripper) ClientOption { var issuedScopesWarning bool // CheckScopes checks whether an OAuth scope is present in a response -func CheckScopes(wantedScope string, cb func(string) error) ClientOption { +func CheckScopes(wantedScope string, cb func(string, string) error) ClientOption { return func(tr http.RoundTripper) http.RoundTripper { return &funcTripper{roundTrip: func(req *http.Request) (*http.Response, error) { res, err := tr.RoundTrip(req) - if err != nil || res.StatusCode > 299 || issuedScopesWarning { + if err != nil || (res.StatusCode > 299 && req.URL.Path == "/graphql") || issuedScopesWarning { return res, err } @@ -96,9 +96,10 @@ func CheckScopes(wantedScope string, cb func(string) error) ClientOption { } if !hasWanted { - if err := cb(appID); err != nil { + if err := cb(wantedScope, appID); err != nil { return res, err } + // TODO seems like we should call RoundTrip again? I tried but it didn't work issuedScopesWarning = true } diff --git a/command/gist.go b/command/gist.go index 5ed223997..1816b2746 100644 --- a/command/gist.go +++ b/command/gist.go @@ -1,4 +1,4 @@ -package command +package command import ( "fmt" @@ -69,4 +69,4 @@ func gistCreate(cmd *cobra.Command, args []string) error { fmt.Fprintln(cmd.OutOrStdout(), gist.HTMLURL) return nil -} \ No newline at end of file +} diff --git a/command/root.go b/command/root.go index 403bb6141..e492a895c 100644 --- a/command/root.go +++ b/command/root.go @@ -141,10 +141,11 @@ var apiClientForContext = func(ctx context.Context) (*api.Client, error) { } getAuthValue := func() string { + fmt.Println("GITTIN MY AUTH", token) return fmt.Sprintf("token %s", token) } - checkScopesFunc := func(appID string) error { + checkScopesFunc := func(wantedScope, appID string) error { if config.IsGitHubApp(appID) && utils.IsTerminal(os.Stdin) && utils.IsTerminal(os.Stderr) { newToken, loginHandle, err := config.AuthFlow("Notice: additional authorization required") if err != nil { @@ -165,8 +166,7 @@ var apiClientForContext = func(ctx context.Context) (*api.Client, error) { token = newToken config.AuthFlowComplete() } else { - // TODO for gist - fmt.Fprintln(os.Stderr, "Warning: gh now requires the `read:org` OAuth scope.") + fmt.Fprintln(os.Stderr, fmt.Sprintf("Warning: gh now requires the `%s` OAuth scope.", wantedScope)) fmt.Fprintln(os.Stderr, "Visit https://github.com/settings/tokens and edit your token to enable `read:org`") fmt.Fprintln(os.Stderr, "or generate a new token and paste it via `gh config set -h github.com oauth_token MYTOKEN`") } @@ -175,6 +175,7 @@ var apiClientForContext = func(ctx context.Context) (*api.Client, error) { opts = append(opts, api.CheckScopes("read:org", checkScopesFunc), + api.CheckScopes("gist", checkScopesFunc), api.AddHeaderFunc("Authorization", getAuthValue), api.AddHeader("User-Agent", fmt.Sprintf("GitHub CLI %s", Version)), // antiope-preview: Checks