@@ -2,7 +2,6 @@ package providers
8 "io/ioutil"
7 "io/ioutil"
@@ -15,6 +14,7 @@ import (
16 "github.com/oauth2-proxy/oauth2-proxy/pkg/apis/sessions"
15 "github.com/oauth2-proxy/oauth2-proxy/pkg/apis/sessions"
17 "github.com/oauth2-proxy/oauth2-proxy/pkg/logger"
16 "github.com/oauth2-proxy/oauth2-proxy/pkg/logger"
17+ "github.com/oauth2-proxy/oauth2-proxy/pkg/requests"
20// GitHubProvider represents an GitHub based Identity Provider
20// GitHubProvider represents an GitHub based Identity Provider
@@ -111,27 +111,16 @@ func (p *GitHubProvider) hasOrg(ctx context.Context, accessToken string) (bool,
111 Path: path.Join(p.ValidateURL.Path, "/user/orgs"),
111 Path: path.Join(p.ValidateURL.Path, "/user/orgs"),
112 RawQuery: params.Encode(),
112 RawQuery: params.Encode(),
114− req, _ := http.NewRequestWithContext(ctx, "GET", endpoint.String(), nil)
115− req.Header = getGitHubHeader(accessToken)
116− resp, err := http.DefaultClient.Do(req)
121− body, err := ioutil.ReadAll(resp.Body)
115+ var op orgsPage
122− resp.Body.Close()
116+ err := requests.New(endpoint.String()).
118+ WithHeaders(getGitHubHeader(accessToken)).
123 if err != nil {
120 if err != nil {
124 return false, err
121 return false, err
126− if resp.StatusCode != 200 {
127− return false, fmt.Errorf(
128− "got %d from %q %s", resp.StatusCode, endpoint.String(), body)
132− if err := json.Unmarshal(body, &op); err != nil {
135 if len(op) == 0 {
124 if len(op) == 0 {
@@ -187,9 +176,13 @@ func (p *GitHubProvider) hasOrgAndTeam(ctx context.Context, accessToken string)
187 RawQuery: params.Encode(),
176 RawQuery: params.Encode(),
190− req, _ := http.NewRequestWithContext(ctx, "GET", endpoint.String(), nil)
179+ // bodyclose cannot detect that the body is being closed later in requests.Into,
180+ // so have to skip the linting for the next line.
182+ resp, err := requests.New(endpoint.String()).
191− req.Header = getGitHubHeader(accessToken)
184+ WithHeaders(getGitHubHeader(accessToken)).
192− resp, err := http.DefaultClient.Do(req)
185+ Do()
193 if err != nil {
186 if err != nil {
194 return false, err
187 return false, err
@@ -217,21 +210,9 @@ func (p *GitHubProvider) hasOrgAndTeam(ctx context.Context, accessToken string)
220− body, err := ioutil.ReadAll(resp.Body)
227− if resp.StatusCode != 200 {
228− return false, fmt.Errorf(
229− "got %d from %q %s", resp.StatusCode, endpoint.String(), body)
232 var tp teamsPage
213 var tp teamsPage
233− if err := json.Unmarshal(body, &tp); err != nil {
214+ if err := requests.UnmarshalInto(resp, &tp); err != nil {
234− return false, fmt.Errorf("%s unmarshaling %s", err, body)
215+ return false, err
236 if len(tp) == 0 {
217 if len(tp) == 0 {
@@ -297,25 +278,12 @@ func (p *GitHubProvider) hasRepo(ctx context.Context, accessToken string) (bool,
297 Path: path.Join(p.ValidateURL.Path, "/repo/", p.Repo),
278 Path: path.Join(p.ValidateURL.Path, "/repo/", p.Repo),
300− req, _ := http.NewRequestWithContext(ctx, "GET", endpoint.String(), nil)
301− req.Header = getGitHubHeader(accessToken)
302− resp, err := http.DefaultClient.Do(req)
307− body, err := ioutil.ReadAll(resp.Body)
312− if resp.StatusCode != 200 {
313− return false, fmt.Errorf(
314− "got %d from %q %s", resp.StatusCode, endpoint.String(), body)
317 var repo repository
281 var repo repository
318− if err := json.Unmarshal(body, &repo); err != nil {
282+ err := requests.New(endpoint.String()).
284+ WithHeaders(getGitHubHeader(accessToken)).
285+ UnmarshalInto(&repo)
319 return false, err
287 return false, err
@@ -337,26 +305,14 @@ func (p *GitHubProvider) hasUser(ctx context.Context, accessToken string) (bool,
337 Host: p.ValidateURL.Host,
305 Host: p.ValidateURL.Host,
338 Path: path.Join(p.ValidateURL.Path, "/user"),
306 Path: path.Join(p.ValidateURL.Path, "/user"),
340− req, _ := http.NewRequestWithContext(ctx, "GET", endpoint.String(), nil)
341− req.Header = getGitHubHeader(accessToken)
342− resp, err := http.DefaultClient.Do(req)
346− defer resp.Body.Close()
348− body, err := ioutil.ReadAll(resp.Body)
309+ err := requests.New(endpoint.String()).
311+ WithHeaders(getGitHubHeader(accessToken)).
312+ UnmarshalInto(&user)
349 if err != nil {
313 if err != nil {
350 return false, err
314 return false, err
352− if resp.StatusCode != 200 {
353− return false, fmt.Errorf("got %d from %q %s",
354− resp.StatusCode, stripToken(endpoint.String()), body)
357− if err := json.Unmarshal(body, &user); err != nil {
361 if p.isVerifiedUser(user.Login) {
317 if p.isVerifiedUser(user.Login) {
362 return true, nil
318 return true, nil
@@ -372,12 +328,14 @@ func (p *GitHubProvider) isCollaborator(ctx context.Context, username, accessTok
372 Host: p.ValidateURL.Host,
328 Host: p.ValidateURL.Host,
373 Path: path.Join(p.ValidateURL.Path, "/repos/", p.Repo, "/collaborators/", username),
329 Path: path.Join(p.ValidateURL.Path, "/repos/", p.Repo, "/collaborators/", username),
375− req, _ := http.NewRequestWithContext(ctx, "GET", endpoint.String(), nil)
331+ resp, err := requests.New(endpoint.String()).
376− req.Header = getGitHubHeader(accessToken)
333+ WithHeaders(getGitHubHeader(accessToken)).
377− resp, err := http.DefaultClient.Do(req)
334+ Do()
378 if err != nil {
335 if err != nil {
379 return false, err
336 return false, err
381 body, err := ioutil.ReadAll(resp.Body)
339 body, err := ioutil.ReadAll(resp.Body)
382 resp.Body.Close()
340 resp.Body.Close()
383 if err != nil {
341 if err != nil {
@@ -440,29 +398,14 @@ func (p *GitHubProvider) GetEmailAddress(ctx context.Context, s *sessions.Sessio
440 Host: p.ValidateURL.Host,
398 Host: p.ValidateURL.Host,
441 Path: path.Join(p.ValidateURL.Path, "/user/emails"),
399 Path: path.Join(p.ValidateURL.Path, "/user/emails"),
443− req, _ := http.NewRequestWithContext(ctx, "GET", endpoint.String(), nil)
401+ err := requests.New(endpoint.String()).
444− req.Header = getGitHubHeader(s.AccessToken)
403+ WithHeaders(getGitHubHeader(s.AccessToken)).
445− resp, err := http.DefaultClient.Do(req)
404+ UnmarshalInto(&emails)
449− body, err := ioutil.ReadAll(resp.Body)
451 if err != nil {
405 if err != nil {
452 return "", err
406 return "", err
455− if resp.StatusCode != 200 {
456− return "", fmt.Errorf("got %d from %q %s",
457− resp.StatusCode, endpoint.String(), body)
460− logger.Printf("got %d from %q %s", resp.StatusCode, endpoint.String(), body)
462− if err := json.Unmarshal(body, &emails); err != nil {
463− return "", fmt.Errorf("%s unmarshaling %s", err, body)
466 returnEmail := ""
409 returnEmail := ""
467 for _, email := range emails {
410 for _, email := range emails {
468 if email.Verified {
411 if email.Verified {
@@ -489,34 +432,14 @@ func (p *GitHubProvider) GetUserName(ctx context.Context, s *sessions.SessionSta
489 Path: path.Join(p.ValidateURL.Path, "/user"),
432 Path: path.Join(p.ValidateURL.Path, "/user"),
492− req, err := http.NewRequestWithContext(ctx, "GET", endpoint.String(), nil)
435+ err := requests.New(endpoint.String()).
493− if err != nil {
436+ WithContext(ctx).
494− return "", fmt.Errorf("could not create new GET request: %v", err)
497− req.Header = getGitHubHeader(s.AccessToken)
437+ WithHeaders(getGitHubHeader(s.AccessToken)).
498− resp, err := http.DefaultClient.Do(req)
438+ UnmarshalInto(&user)
503− body, err := ioutil.ReadAll(resp.Body)
504− defer resp.Body.Close()
505 if err != nil {
439 if err != nil {
506 return "", err
440 return "", err
509− if resp.StatusCode != 200 {
510− return "", fmt.Errorf("got %d from %q %s",
511− resp.StatusCode, endpoint.String(), body)
514− logger.Printf("got %d from %q %s", resp.StatusCode, endpoint.String(), body)
516− if err := json.Unmarshal(body, &user); err != nil {
517− return "", fmt.Errorf("%s unmarshaling %s", err, body)
520 // Now that we have the username we can check collaborator status
443 // Now that we have the username we can check collaborator status
521 if !p.isVerifiedUser(user.Login) && p.Org == "" && p.Repo != "" && p.Token != "" {
444 if !p.isVerifiedUser(user.Login) && p.Org == "" && p.Repo != "" && p.Token != "" {
522 if ok, err := p.isCollaborator(ctx, user.Login, p.Token); err != nil || !ok {
445 if ok, err := p.isCollaborator(ctx, user.Login, p.Token); err != nil || !ok {