1
0
mirror of https://github.com/oauth2-proxy/oauth2-proxy.git synced 2025-06-02 23:27:22 +02:00

Merge branch 'ap-gh-paginate' of https://github.com/apratina/oauth2_proxy into ap-gh-pagination-with-lastpage

This commit is contained in:
toshi-miura 2019-10-03 22:34:56 +09:00
commit 1c36b5e2e9
2 changed files with 49 additions and 29 deletions

View File

@ -75,7 +75,7 @@ func (p *GitHubProvider) hasOrg(accessToken string) (bool, error) {
pn := 1 pn := 1
for { for {
params := url.Values{ params := url.Values{
"limit": {"200"}, "per_page": {"100"},
"page": {strconv.Itoa(pn)}, "page": {strconv.Itoa(pn)},
} }
@ -139,8 +139,19 @@ func (p *GitHubProvider) hasOrgAndTeam(accessToken string) (bool, error) {
} `json:"organization"` } `json:"organization"`
} }
type teamsPage []struct {
Name string `json:"name"`
Slug string `json:"slug"`
Org struct {
Login string `json:"login"`
} `json:"organization"`
}
pn := 1
for {
params := url.Values{ params := url.Values{
"limit": {"200"}, "per_page": {"100"},
"page": {strconv.Itoa(pn)},
} }
endpoint := &url.URL{ endpoint := &url.URL{
@ -149,6 +160,7 @@ func (p *GitHubProvider) hasOrgAndTeam(accessToken string) (bool, error) {
Path: path.Join(p.ValidateURL.Path, "/user/teams"), Path: path.Join(p.ValidateURL.Path, "/user/teams"),
RawQuery: params.Encode(), RawQuery: params.Encode(),
} }
req, _ := http.NewRequest("GET", endpoint.String(), nil) req, _ := http.NewRequest("GET", endpoint.String(), nil)
req.Header.Set("Accept", "application/vnd.github.v3+json") req.Header.Set("Accept", "application/vnd.github.v3+json")
req.Header.Set("Authorization", fmt.Sprintf("token %s", accessToken)) req.Header.Set("Authorization", fmt.Sprintf("token %s", accessToken))
@ -167,9 +179,17 @@ func (p *GitHubProvider) hasOrgAndTeam(accessToken string) (bool, error) {
"got %d from %q %s", resp.StatusCode, endpoint.String(), body) "got %d from %q %s", resp.StatusCode, endpoint.String(), body)
} }
if err := json.Unmarshal(body, &teams); err != nil { var tp teamsPage
if err := json.Unmarshal(body, &tp); err != nil {
return false, fmt.Errorf("%s unmarshaling %s", err, body) return false, fmt.Errorf("%s unmarshaling %s", err, body)
} }
if len(tp) == 0 {
break
}
teams = append(teams, tp...)
pn++
}
var hasOrg bool var hasOrg bool
presentOrgs := make(map[string]bool) presentOrgs := make(map[string]bool)

View File

@ -32,7 +32,7 @@ func testGitHubBackend(payload []string) *httptest.Server {
pathToQueryMap := map[string][]string{ pathToQueryMap := map[string][]string{
"/user": {""}, "/user": {""},
"/user/emails": {""}, "/user/emails": {""},
"/user/orgs": {"limit=200&page=1", "limit=200&page=2", "limit=200&page=3"}, "/user/orgs": {"page=1&per_page=100", "page=2&per_page=100", "page=3&per_page=100"},
} }
return httptest.NewServer(http.HandlerFunc( return httptest.NewServer(http.HandlerFunc(