Skip to content

Commit 3d566dc

Browse files
committed
Detect and warn about read:org OAuth scope being missing
1 parent 14ce1f9 commit 3d566dc

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

api/client.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io"
88
"io/ioutil"
99
"net/http"
10+
"os"
1011
"regexp"
1112
"strings"
1213

@@ -63,6 +64,46 @@ func ReplaceTripper(tr http.RoundTripper) ClientOption {
6364
}
6465
}
6566

67+
var issuedScopesWarning bool
68+
69+
// CheckScopes checks whether an OAuth scope is present in a response
70+
func CheckScopes(wantedScope string) ClientOption {
71+
return func(tr http.RoundTripper) http.RoundTripper {
72+
return &funcTripper{roundTrip: func(req *http.Request) (*http.Response, error) {
73+
res, err := tr.RoundTrip(req)
74+
if err != nil || issuedScopesWarning {
75+
return res, err
76+
}
77+
78+
isApp := res.Header.Get("X-Oauth-Client-Id") != ""
79+
hasScopes := strings.Split(res.Header.Get("X-Oauth-Scopes"), ",")
80+
81+
hasWanted := false
82+
for _, s := range hasScopes {
83+
if wantedScope == strings.TrimSpace(s) {
84+
hasWanted = true
85+
break
86+
}
87+
}
88+
89+
if !hasWanted {
90+
fmt.Fprintln(os.Stderr, "Warning: gh now requires the `read:org` OAuth scope.")
91+
// TODO: offer to take the person through the authentication flow again?
92+
// TODO: retry the original request if it was a read?
93+
if isApp {
94+
fmt.Fprintln(os.Stderr, "To re-authenticate, please `rm ~/.config/gh/config.yml` and try again.")
95+
} else {
96+
// the person has pasted a Personal Access Token
97+
fmt.Fprintln(os.Stderr, "Re-generate your token in `rm ~/.config/gh/config.yml` and try again.")
98+
}
99+
issuedScopesWarning = true
100+
}
101+
102+
return res, nil
103+
}}
104+
}
105+
}
106+
66107
type funcTripper struct {
67108
roundTrip func(*http.Request) (*http.Response, error)
68109
}

command/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ var apiClientForContext = func(ctx context.Context) (*api.Client, error) {
131131
opts = append(opts, apiVerboseLog())
132132
}
133133
opts = append(opts,
134+
api.CheckScopes("read:org"),
134135
api.AddHeader("Authorization", fmt.Sprintf("token %s", token)),
135136
api.AddHeader("User-Agent", fmt.Sprintf("GitHub CLI %s", Version)),
136137
// antiope-preview: Checks

0 commit comments

Comments
 (0)