1
0
mirror of https://github.com/oauth2-proxy/oauth2-proxy.git synced 2025-05-31 23:19:50 +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,8 +75,8 @@ 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)},
} }
endpoint := &url.URL{ endpoint := &url.URL{
@ -139,36 +139,56 @@ func (p *GitHubProvider) hasOrgAndTeam(accessToken string) (bool, error) {
} `json:"organization"` } `json:"organization"`
} }
params := url.Values{ type teamsPage []struct {
"limit": {"200"}, Name string `json:"name"`
Slug string `json:"slug"`
Org struct {
Login string `json:"login"`
} `json:"organization"`
} }
endpoint := &url.URL{ pn := 1
Scheme: p.ValidateURL.Scheme, for {
Host: p.ValidateURL.Host, params := url.Values{
Path: path.Join(p.ValidateURL.Path, "/user/teams"), "per_page": {"100"},
RawQuery: params.Encode(), "page": {strconv.Itoa(pn)},
} }
req, _ := http.NewRequest("GET", endpoint.String(), nil)
req.Header.Set("Accept", "application/vnd.github.v3+json")
req.Header.Set("Authorization", fmt.Sprintf("token %s", accessToken))
resp, err := http.DefaultClient.Do(req)
if err != nil {
return false, err
}
body, err := ioutil.ReadAll(resp.Body) endpoint := &url.URL{
resp.Body.Close() Scheme: p.ValidateURL.Scheme,
if err != nil { Host: p.ValidateURL.Host,
return false, err Path: path.Join(p.ValidateURL.Path, "/user/teams"),
} RawQuery: params.Encode(),
if resp.StatusCode != 200 { }
return false, fmt.Errorf(
"got %d from %q %s", resp.StatusCode, endpoint.String(), body)
}
if err := json.Unmarshal(body, &teams); err != nil { req, _ := http.NewRequest("GET", endpoint.String(), nil)
return false, fmt.Errorf("%s unmarshaling %s", err, body) req.Header.Set("Accept", "application/vnd.github.v3+json")
req.Header.Set("Authorization", fmt.Sprintf("token %s", accessToken))
resp, err := http.DefaultClient.Do(req)
if err != nil {
return false, err
}
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return false, err
}
if resp.StatusCode != 200 {
return false, fmt.Errorf(
"got %d from %q %s", resp.StatusCode, endpoint.String(), body)
}
var tp teamsPage
if err := json.Unmarshal(body, &tp); err != nil {
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

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(