ValidateSufficientOAuthScopes warns about insufficient OAuth scopes
(res *http.Response)
| 1244 | |
| 1245 | // ValidateSufficientOAuthScopes warns about insufficient OAuth scopes |
| 1246 | func ValidateSufficientOAuthScopes(res *http.Response) error { |
| 1247 | if res.StatusCode != 404 && res.StatusCode != 403 { |
| 1248 | return nil |
| 1249 | } |
| 1250 | |
| 1251 | needScopes := newScopeSet(res.Header.Get("X-Accepted-Oauth-Scopes")) |
| 1252 | if len(needScopes) == 0 && isGistWrite(res.Request) { |
| 1253 | // compensate for a GitHub bug: gist APIs omit proper `X-Accepted-Oauth-Scopes` in responses |
| 1254 | needScopes = newScopeSet("gist") |
| 1255 | } |
| 1256 | |
| 1257 | haveScopes := newScopeSet(res.Header.Get("X-Oauth-Scopes")) |
| 1258 | if len(needScopes) == 0 || needScopes.Intersects(haveScopes) { |
| 1259 | return nil |
| 1260 | } |
| 1261 | |
| 1262 | return fmt.Errorf("Your access token may have insufficient scopes. Visit %s://%s/settings/tokens\n"+ |
| 1263 | "to edit the 'hub' token and enable one of the following scopes: %s", |
| 1264 | res.Request.URL.Scheme, |
| 1265 | reverseNormalizeHost(res.Request.Host), |
| 1266 | needScopes) |
| 1267 | } |
| 1268 | |
| 1269 | func isGistWrite(req *http.Request) bool { |
| 1270 | if req.Method == "GET" { |
no test coverage detected
searching dependent graphs…