git.iko.soy/typeable/oauth2-proxy
on master help sign in
Merge pull request #128 from jehiah/github_debug_128

provider github not work with scope read:org

commit5ff8aa35819d985221c8046adab2ddd45df7ebbb authorJehiah Czebotar <jehiah@gmail.com> dateTue 28 Jul 2015, 07:58 (11 years ago) parent221c99b0692c37
5 files changed+34−10
options.go+3 options_test.go+2 providers/github.go+25−5 providers/linkedin.go−3 validator.go+4−2
options.go+3
@@ -103,6 +103,9 @@ func (o *Options) Validate() error {
103103 if o.ClientSecret == "" {
104104 msgs = append(msgs, "missing setting: client-secret")
105105 }
106+ if o.AuthenticatedEmailsFile == "" && len(o.EmailDomains) == 0 && o.HtpasswdFile == "" {
107+ msgs = append(msgs, "missing setting for email validation: email-domain or authenticated-emails-file required.\n use email-domain=* to authorize all email addresses")
108+ }
106109
107110 o.redirectUrl, msgs = parseUrl(o.RedirectUrl, "redirect", msgs)
108111
options_test.go+2
@@ -15,6 +15,7 @@ func testOptions() *Options {
1515 o.CookieSecret = "foobar"
1616 o.ClientID = "bazquux"
1717 o.ClientSecret = "xyzzyplugh"
18+ o.EmailDomains = []string{"*"}
1819 return o
1920}
2021
@@ -27,6 +28,7 @@ func errorMsg(msgs []string) string {
2728
2829func TestNewOptions(t *testing.T) {
2930 o := NewOptions()
31+ o.EmailDomains = []string{"*"}
3032 err := o.Validate()
3133 assert.NotEqual(t, nil, err)
3234
providers/github.go+25−5
@@ -2,7 +2,6 @@ package providers
22
33import (
44 "encoding/json"
5− "errors"
65 "fmt"
76 "io/ioutil"
87 "log"
@@ -66,7 +65,7 @@ func (p *GitHubProvider) hasOrg(accessToken string) (bool, error) {
6665
6766 endpoint := "https://api.github.com/user/orgs?" + params.Encode()
6867 req, _ := http.NewRequest("GET", endpoint, nil)
69− req.Header.Set("Accept", "application/vnd.github.moondragon+json")
68+ req.Header.Set("Accept", "application/vnd.github.v3+json")
7069 resp, err := http.DefaultClient.Do(req)
7170 if err != nil {
7271 return false, err
@@ -85,11 +84,16 @@ func (p *GitHubProvider) hasOrg(accessToken string) (bool, error) {
8584 return false, err
8685 }
8786
87+ var presentOrgs []string
8888 for _, org := range orgs {
8989 if p.Org == org.Login {
90+ log.Printf("Found Github Organization: %q", org.Login)
9091 return true, nil
9192 }
93+ presentOrgs = append(presentOrgs, org.Login)
9294 }
95+
96+ log.Printf("Missing Organization:%q in %v", p.Org, presentOrgs)
9397 return false, nil
9498}
9599
@@ -111,7 +115,7 @@ func (p *GitHubProvider) hasOrgAndTeam(accessToken string) (bool, error) {
111115
112116 endpoint := "https://api.github.com/user/teams?" + params.Encode()
113117 req, _ := http.NewRequest("GET", endpoint, nil)
114− req.Header.Set("Accept", "application/vnd.github.moondragon+json")
118+ req.Header.Set("Accept", "application/vnd.github.v3+json")
115119 resp, err := http.DefaultClient.Do(req)
116120 if err != nil {
117121 return false, err
@@ -130,12 +134,28 @@ func (p *GitHubProvider) hasOrgAndTeam(accessToken string) (bool, error) {
130134 return false, fmt.Errorf("%s unmarshaling %s", err, body)
131135 }
132136
137+ var hasOrg bool
138+ presentOrgs := make(map[string]bool)
139+ var presentTeams []string
133140 for _, team := range teams {
141+ presentOrgs[team.Org.Login] = true
134142 if p.Org == team.Org.Login {
135− if p.Team == "" || p.Team == team.Slug {
143+ hasOrg = true
144+ if p.Team == team.Slug {
145+ log.Printf("Found Github Organization:%q Team:%q (Name:%q)", team.Org.Login, team.Slug, team.Name)
136146 return true, nil
137147 }
148+ presentTeams = append(presentTeams, team.Slug)
149+ }
150+ }
151+ if hasOrg {
152+ log.Printf("Missing Team:%q from Org:%q in teams: %v", p.Team, p.Org, presentTeams)
153+ } else {
154+ var allOrgs []string
155+ for org, _ := range presentOrgs {
156+ allOrgs = append(allOrgs, org)
138157 }
158+ log.Printf("Missing Organization:%q in %#v", p.Org, allOrgs)
139159 }
140160 return false, nil
141161}
@@ -190,5 +210,5 @@ func (p *GitHubProvider) GetEmailAddress(s *SessionState) (string, error) {
190210 }
191211 }
192212
193− return "", errors.New("no email address found")
213+ return "", nil
194214}
providers/linkedin.go−3
@@ -3,7 +3,6 @@ package providers
33import (
44 "errors"
55 "fmt"
6− "log"
76 "net/http"
87 "net/url"
98
@@ -60,13 +59,11 @@ func (p *LinkedInProvider) GetEmailAddress(s *SessionState) (string, error) {
6059
6160 json, err := api.Request(req)
6261 if err != nil {
63− log.Printf("failed making request %s", err)
6462 return "", err
6563 }
6664
6765 email, err := json.String()
6866 if err != nil {
69− log.Printf("failed making request %s", err)
7067 return "", err
7168 }
7269 return email, nil
validator.go+4−2
@@ -71,9 +71,11 @@ func newValidatorImpl(domains []string, usersFile string,
7171 domains[i] = fmt.Sprintf("@%s", strings.ToLower(domain))
7272 }
7373
74− validator := func(email string) bool {
74+ validator := func(email string) (valid bool) {
75+ if email == "" {
76+ return
77+ }
7578 email = strings.ToLower(email)
76− valid := false
7779 for _, domain := range domains {
7880 valid = valid || strings.HasSuffix(email, domain)
7981 }