13 "github.com/pusher/oauth2_proxy/logger"
14 "github.com/pusher/oauth2_proxy/pkg/apis/sessions"
17// GitHubProvider represents an GitHub based Identity Provider
18type GitHubProvider struct {
24// NewGitHubProvider initiates a new GitHubProvider
25func NewGitHubProvider(p *ProviderData) *GitHubProvider {
26 p.ProviderName = "GitHub"
27 if p.LoginURL == nil || p.LoginURL.String() == "" {
28 p.LoginURL = &url.URL{
31 Path: "/login/oauth/authorize",
34 if p.RedeemURL == nil || p.RedeemURL.String() == "" {
35 p.RedeemURL = &url.URL{
38 Path: "/login/oauth/access_token",
41 // ValidationURL is the API Base URL
42 if p.ValidateURL == nil || p.ValidateURL.String() == "" {
43 p.ValidateURL = &url.URL{
45 Host: "api.github.com",
50 p.Scope = "user:email"
52 return &GitHubProvider{ProviderData: p}
55// SetOrgTeam adds GitHub org reading parameters to the OAuth2 scope
56func (p *GitHubProvider) SetOrgTeam(org, team string) {
59 if org != "" || team != "" {
60 p.Scope += " read:org"
64func (p *GitHubProvider) hasOrg(accessToken string) (bool, error) {
65 // https://developer.github.com/v3/orgs/#list-your-organizations
68 Login string `json:"login"`
71 type orgsPage []struct {
72 Login string `json:"login"`
79 "page": {strconv.Itoa(pn)},
83 Scheme: p.ValidateURL.Scheme,
84 Host: p.ValidateURL.Host,
85 Path: path.Join(p.ValidateURL.Path, "/user/orgs"),
86 RawQuery: params.Encode(),
88 req, _ := http.NewRequest("GET", endpoint.String(), nil)
89 req.Header.Set("Accept", "application/vnd.github.v3+json")
90 req.Header.Set("Authorization", fmt.Sprintf("token %s", accessToken))
91 resp, err := http.DefaultClient.Do(req)
96 body, err := ioutil.ReadAll(resp.Body)
101 if resp.StatusCode != 200 {
102 return false, fmt.Errorf(
103 "got %d from %q %s", resp.StatusCode, endpoint.String(), body)
107 if err := json.Unmarshal(body, &op); err != nil {
114 orgs = append(orgs, op...)
118 var presentOrgs []string
119 for _, org := range orgs {
120 if p.Org == org.Login {
121 logger.Printf("Found Github Organization: %q", org.Login)
124 presentOrgs = append(presentOrgs, org.Login)
127 logger.Printf("Missing Organization:%q in %v", p.Org, presentOrgs)
131func (p *GitHubProvider) hasOrgAndTeam(accessToken string) (bool, error) {
132 // https://developer.github.com/v3/orgs/teams/#list-user-teams
135 Name string `json:"name"`
136 Slug string `json:"slug"`
138 Login string `json:"login"`
139 } `json:"organization"`
142 params := url.Values{
146 endpoint := &url.URL{
147 Scheme: p.ValidateURL.Scheme,
148 Host: p.ValidateURL.Host,
149 Path: path.Join(p.ValidateURL.Path, "/user/teams"),
150 RawQuery: params.Encode(),
152 req, _ := http.NewRequest("GET", endpoint.String(), nil)
153 req.Header.Set("Accept", "application/vnd.github.v3+json")
154 req.Header.Set("Authorization", fmt.Sprintf("token %s", accessToken))
155 resp, err := http.DefaultClient.Do(req)
160 body, err := ioutil.ReadAll(resp.Body)
165 if resp.StatusCode != 200 {
166 return false, fmt.Errorf(
167 "got %d from %q %s", resp.StatusCode, endpoint.String(), body)
170 if err := json.Unmarshal(body, &teams); err != nil {
171 return false, fmt.Errorf("%s unmarshaling %s", err, body)
175 presentOrgs := make(map[string]bool)
176 var presentTeams []string
177 for _, team := range teams {
178 presentOrgs[team.Org.Login] = true
179 if p.Org == team.Org.Login {
181 ts := strings.Split(p.Team, ",")
182 for _, t := range ts {
184 logger.Printf("Found Github Organization:%q Team:%q (Name:%q)", team.Org.Login, team.Slug, team.Name)
188 presentTeams = append(presentTeams, team.Slug)
192 logger.Printf("Missing Team:%q from Org:%q in teams: %v", p.Team, p.Org, presentTeams)
195 for org := range presentOrgs {
196 allOrgs = append(allOrgs, org)
198 logger.Printf("Missing Organization:%q in %#v", p.Org, allOrgs)
203// GetEmailAddress returns the Account email address
204func (p *GitHubProvider) GetEmailAddress(s *sessions.SessionState) (string, error) {
206 var emails []struct {
207 Email string `json:"email"`
208 Primary bool `json:"primary"`
209 Verified bool `json:"verified"`
212 // if we require an Org or Team, check that first
215 if ok, err := p.hasOrgAndTeam(s.AccessToken); err != nil || !ok {
219 if ok, err := p.hasOrg(s.AccessToken); err != nil || !ok {
225 endpoint := &url.URL{
226 Scheme: p.ValidateURL.Scheme,
227 Host: p.ValidateURL.Host,
228 Path: path.Join(p.ValidateURL.Path, "/user/emails"),
230 req, _ := http.NewRequest("GET", endpoint.String(), nil)
231 req.Header.Set("Authorization", fmt.Sprintf("token %s", s.AccessToken))
232 resp, err := http.DefaultClient.Do(req)
236 body, err := ioutil.ReadAll(resp.Body)
242 if resp.StatusCode != 200 {
243 return "", fmt.Errorf("got %d from %q %s",
244 resp.StatusCode, endpoint.String(), body)
247 logger.Printf("got %d from %q %s", resp.StatusCode, endpoint.String(), body)
249 if err := json.Unmarshal(body, &emails); err != nil {
250 return "", fmt.Errorf("%s unmarshaling %s", err, body)
253 for _, email := range emails {
254 if email.Primary && email.Verified {
255 return email.Email, nil
262// GetUserName returns the Account user name
263func (p *GitHubProvider) GetUserName(s *sessions.SessionState) (string, error) {
265 Login string `json:"login"`
266 Email string `json:"email"`
269 endpoint := &url.URL{
270 Scheme: p.ValidateURL.Scheme,
271 Host: p.ValidateURL.Host,
272 Path: path.Join(p.ValidateURL.Path, "/user"),
275 req, err := http.NewRequest("GET", endpoint.String(), nil)
277 return "", fmt.Errorf("could not create new GET request: %v", err)
280 req.Header.Set("Authorization", fmt.Sprintf("token %s", s.AccessToken))
281 resp, err := http.DefaultClient.Do(req)
286 body, err := ioutil.ReadAll(resp.Body)
287 defer resp.Body.Close()
292 if resp.StatusCode != 200 {
293 return "", fmt.Errorf("got %d from %q %s",
294 resp.StatusCode, endpoint.String(), body)
297 logger.Printf("got %d from %q %s", resp.StatusCode, endpoint.String(), body)
299 if err := json.Unmarshal(body, &user); err != nil {
300 return "", fmt.Errorf("%s unmarshaling %s", err, body)
303 return user.Login, nil
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list