15// GitHubProvider represents an GitHub based Identity Provider
16type GitHubProvider struct {
22// NewGitHubProvider initiates a new GitHubProvider
23func NewGitHubProvider(p *ProviderData) *GitHubProvider {
24 p.ProviderName = "GitHub"
25 if p.LoginURL == nil || p.LoginURL.String() == "" {
26 p.LoginURL = &url.URL{
29 Path: "/login/oauth/authorize",
32 if p.RedeemURL == nil || p.RedeemURL.String() == "" {
33 p.RedeemURL = &url.URL{
36 Path: "/login/oauth/access_token",
39 // ValidationURL is the API Base URL
40 if p.ValidateURL == nil || p.ValidateURL.String() == "" {
41 p.ValidateURL = &url.URL{
43 Host: "api.github.com",
48 p.Scope = "user:email"
50 return &GitHubProvider{ProviderData: p}
53// SetOrgTeam adds GitHub org reading parameters to the OAuth2 scope
54func (p *GitHubProvider) SetOrgTeam(org, team string) {
57 if org != "" || team != "" {
58 p.Scope += " read:org"
62func (p *GitHubProvider) hasOrg(accessToken string) (bool, error) {
63 // https://developer.github.com/v3/orgs/#list-your-organizations
66 Login string `json:"login"`
69 type orgsPage []struct {
70 Login string `json:"login"`
77 "page": {strconv.Itoa(pn)},
81 Scheme: p.ValidateURL.Scheme,
82 Host: p.ValidateURL.Host,
83 Path: path.Join(p.ValidateURL.Path, "/user/orgs"),
84 RawQuery: params.Encode(),
86 req, _ := http.NewRequest("GET", endpoint.String(), nil)
87 req.Header.Set("Accept", "application/vnd.github.v3+json")
88 req.Header.Set("Authorization", fmt.Sprintf("token %s", accessToken))
89 resp, err := http.DefaultClient.Do(req)
94 body, err := ioutil.ReadAll(resp.Body)
99 if resp.StatusCode != 200 {
100 return false, fmt.Errorf(
101 "got %d from %q %s", resp.StatusCode, endpoint.String(), body)
105 if err := json.Unmarshal(body, &op); err != nil {
112 orgs = append(orgs, op...)
116 var presentOrgs []string
117 for _, org := range orgs {
118 if p.Org == org.Login {
119 log.Printf("Found Github Organization: %q", org.Login)
122 presentOrgs = append(presentOrgs, org.Login)
125 log.Printf("Missing Organization:%q in %v", p.Org, presentOrgs)
129func (p *GitHubProvider) hasOrgAndTeam(accessToken string) (bool, error) {
130 // https://developer.github.com/v3/orgs/teams/#list-user-teams
133 Name string `json:"name"`
134 Slug string `json:"slug"`
136 Login string `json:"login"`
137 } `json:"organization"`
140 params := url.Values{
144 endpoint := &url.URL{
145 Scheme: p.ValidateURL.Scheme,
146 Host: p.ValidateURL.Host,
147 Path: path.Join(p.ValidateURL.Path, "/user/teams"),
148 RawQuery: params.Encode(),
150 req, _ := http.NewRequest("GET", endpoint.String(), nil)
151 req.Header.Set("Accept", "application/vnd.github.v3+json")
152 req.Header.Set("Authorization", fmt.Sprintf("token %s", accessToken))
153 resp, err := http.DefaultClient.Do(req)
158 body, err := ioutil.ReadAll(resp.Body)
163 if resp.StatusCode != 200 {
164 return false, fmt.Errorf(
165 "got %d from %q %s", resp.StatusCode, endpoint.String(), body)
168 if err := json.Unmarshal(body, &teams); err != nil {
169 return false, fmt.Errorf("%s unmarshaling %s", err, body)
173 presentOrgs := make(map[string]bool)
174 var presentTeams []string
175 for _, team := range teams {
176 presentOrgs[team.Org.Login] = true
177 if p.Org == team.Org.Login {
179 ts := strings.Split(p.Team, ",")
180 for _, t := range ts {
182 log.Printf("Found Github Organization:%q Team:%q (Name:%q)", team.Org.Login, team.Slug, team.Name)
186 presentTeams = append(presentTeams, team.Slug)
190 log.Printf("Missing Team:%q from Org:%q in teams: %v", p.Team, p.Org, presentTeams)
193 for org := range presentOrgs {
194 allOrgs = append(allOrgs, org)
196 log.Printf("Missing Organization:%q in %#v", p.Org, allOrgs)
201// GetEmailAddress returns the Account email address
202func (p *GitHubProvider) GetEmailAddress(s *SessionState) (string, error) {
204 var emails []struct {
205 Email string `json:"email"`
206 Primary bool `json:"primary"`
209 // if we require an Org or Team, check that first
212 if ok, err := p.hasOrgAndTeam(s.AccessToken); err != nil || !ok {
216 if ok, err := p.hasOrg(s.AccessToken); err != nil || !ok {
222 endpoint := &url.URL{
223 Scheme: p.ValidateURL.Scheme,
224 Host: p.ValidateURL.Host,
225 Path: path.Join(p.ValidateURL.Path, "/user/emails"),
227 req, _ := http.NewRequest("GET", endpoint.String(), nil)
228 req.Header.Set("Authorization", fmt.Sprintf("token %s", s.AccessToken))
229 resp, err := http.DefaultClient.Do(req)
233 body, err := ioutil.ReadAll(resp.Body)
239 if resp.StatusCode != 200 {
240 return "", fmt.Errorf("got %d from %q %s",
241 resp.StatusCode, endpoint.String(), body)
244 log.Printf("got %d from %q %s", resp.StatusCode, endpoint.String(), body)
246 if err := json.Unmarshal(body, &emails); err != nil {
247 return "", fmt.Errorf("%s unmarshaling %s", err, body)
250 for _, email := range emails {
252 return email.Email, nil
259// GetUserName returns the Account user name
260func (p *GitHubProvider) GetUserName(s *SessionState) (string, error) {
262 Login string `json:"login"`
263 Email string `json:"email"`
266 endpoint := &url.URL{
267 Scheme: p.ValidateURL.Scheme,
268 Host: p.ValidateURL.Host,
269 Path: path.Join(p.ValidateURL.Path, "/user"),
272 req, err := http.NewRequest("GET", endpoint.String(), nil)
274 return "", fmt.Errorf("could not create new GET request: %v", err)
277 req.Header.Set("Authorization", fmt.Sprintf("token %s", s.AccessToken))
278 resp, err := http.DefaultClient.Do(req)
283 body, err := ioutil.ReadAll(resp.Body)
284 defer resp.Body.Close()
289 if resp.StatusCode != 200 {
290 return "", fmt.Errorf("got %d from %q %s",
291 resp.StatusCode, endpoint.String(), body)
294 log.Printf("got %d from %q %s", resp.StatusCode, endpoint.String(), body)
296 if err := json.Unmarshal(body, &user); err != nil {
297 return "", fmt.Errorf("%s unmarshaling %s", err, body)
300 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