tweak reauth code and request a new scope

This commit is contained in:
vilmibm 2020-05-13 14:24:29 -05:00
parent ed1a3a60fd
commit 3a7f56456e
3 changed files with 10 additions and 8 deletions

View file

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

View file

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

View file

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