Skip to content

Commit

Permalink
align code of getTeams with getOrgs to support Github Enterprise Serv…
Browse files Browse the repository at this point in the history
…er instances with different domain
  • Loading branch information
hoax authored and tuunit committed Aug 27, 2023
1 parent 0395e3c commit a9c31a5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 42 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
- [#1839](https://github.com/oauth2-proxy/oauth2-proxy/pull/1839) Add readiness checks for deeper health checks (@kobim)
- [#1927](https://github.com/oauth2-proxy/oauth2-proxy/pull/1927) Fix default scope settings for none oidc providers
- [#1713](https://github.com/oauth2-proxy/oauth2-proxy/pull/1713) Add session cookie support (@t-katsumura @tanuki884)
- [#1928](https://github.com/oauth2-proxy/oauth2-proxy/pull/1928) Add GitHub groups (orgs/teams) support. Including `X-Forwarded-Groups` header
- [#2196](https://github.com/oauth2-proxy/oauth2-proxy/pull/2196) Add GitHub groups (orgs/teams) support. Including `X-Forwarded-Groups` header
- [#1951](https://github.com/oauth2-proxy/oauth2-proxy/pull/1951) Fix validate URL, check if query string marker (?) or separator (&) needs to be appended (@miguelborges99)
- [#1920](https://github.com/oauth2-proxy/oauth2-proxy/pull/1920) Make sure emailClaim is not overriden if userIDClaim is not set
- [#2010](https://github.com/oauth2-proxy/oauth2-proxy/pull/2010) Log the difference between invalid email and not authorized session
Expand Down
50 changes: 9 additions & 41 deletions providers/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net/http"
"net/url"
"path"
"regexp"
"strconv"
"strings"

Expand Down Expand Up @@ -482,7 +481,7 @@ func (p *GitHubProvider) getTeams(ctx context.Context, s *sessions.SessionState)
} `json:"organization"`
}

pn, last := 1, 0
pn := 1
for {
params := url.Values{
"per_page": {"100"},
Expand All @@ -496,55 +495,24 @@ func (p *GitHubProvider) getTeams(ctx context.Context, s *sessions.SessionState)
RawQuery: params.Encode(),
}

// bodyclose cannot detect that the body is being closed later in requests.Into,
// so have to skip the linting for the next line.
// nolint:bodyclose
result := requests.New(endpoint.String()).
var teams []Team
err := requests.New(endpoint.String()).
WithContext(ctx).
WithHeaders(makeGitHubHeader(s.AccessToken)).
Do()

if result.Error() != nil {
return result.Error()
}

if last == 0 {
// link header may not be obtained
// When paging is not required and all data can be retrieved with a single call

// Conditions for obtaining the link header.
// 1. When paging is required (Example: When the data size is 100 and the page size is 99 or less)
// 2. When it exceeds the paging frame (Example: When there is only 10 records but the second page is called with a page size of 100)

// link header at not last page
// <https://api.github.com/user/teams?page=1&per_page=100>; rel="prev", <https://api.github.com/user/teams?page=1&per_page=100>; rel="last", <https://api.github.com/user/teams?page=1&per_page=100>; rel="first"
// link header at last page (doesn't exist last info)
// <https://api.github.com/user/teams?page=3&per_page=10>; rel="prev", <https://api.github.com/user/teams?page=1&per_page=10>; rel="first"

link := result.Headers().Get("Link")
rep1 := regexp.MustCompile(`(?s).*\<https://api.github.com/user/teams\?page=(.)&per_page=[0-9]+\>; rel="last".*`)
i, converr := strconv.Atoi(rep1.ReplaceAllString(link, "$1"))

// If the last page cannot be taken from the link in the http header, the last variable remains zero
if converr == nil {
last = i
}
Do().
UnmarshalInto(&teams)
if err != nil {
return err
}

var teams []Team
if err := result.UnmarshalInto(&teams); err != nil {
return err
if len(teams) == 0 {
break
}

for _, team := range teams {
logger.Printf("Member of Github Organization/Team:%q/%q", team.Org.Login, team.Slug)
s.Groups = append(s.Groups, team.Org.Login+orgTeamSeparator+team.Slug)
}

if len(teams) == 0 || pn == last || last == 0 {
break
}

pn++
}

Expand Down

0 comments on commit a9c31a5

Please sign in to comment.