@@ -3,10 +3,13 @@ package providers
8 oidc "github.com/coreos/go-oidc"
9 oidc "github.com/coreos/go-oidc"
9 "github.com/pusher/oauth2_proxy/pkg/apis/sessions"
10 "github.com/pusher/oauth2_proxy/pkg/apis/sessions"
11+ "github.com/pusher/oauth2_proxy/pkg/requests"
10 "golang.org/x/oauth2"
13 "golang.org/x/oauth2"
@@ -117,8 +120,31 @@ func (p *OIDCProvider) createSessionState(ctx context.Context, token *oauth2.Tok
119 if claims.Email == "" {
122 if claims.Email == "" {
120− // TODO: Try getting email from /userinfo before falling back to Subject
123+ if p.ProfileURL.String() == "" {
124+ return nil, fmt.Errorf("id_token did not contain an email")
127+ // If the userinfo endpoint profileURL is defined, then there is a chance the userinfo
128+ // contents at the profileURL contains the email.
129+ // Make a query to the userinfo endpoint, and attempt to locate the email from there.
131+ req, err := http.NewRequest("GET", p.ProfileURL.String(), nil)
135+ req.Header = getOIDCHeader(token.AccessToken)
137+ respJSON, err := requests.Request(req)
142+ email, err := respJSON.Get("email").String()
144+ return nil, fmt.Errorf("Neither id_token nor userinfo endpoint contained an email")
121− claims.Email = claims.Subject
147+ claims.Email = email
123 if !p.AllowUnverifiedEmail && claims.Verified != nil && !*claims.Verified {
149 if !p.AllowUnverifiedEmail && claims.Verified != nil && !*claims.Verified {
124 return nil, fmt.Errorf("email in id_token (%s) isn't verified", claims.Email)
150 return nil, fmt.Errorf("email in id_token (%s) isn't verified", claims.Email)
@@ -145,3 +171,10 @@ func (p *OIDCProvider) ValidateSessionState(s *sessions.SessionState) bool {
146 return true
172 return true
175+func getOIDCHeader(accessToken string) http.Header {
176+ header := make(http.Header)
177+ header.Set("Accept", "application/json")
178+ header.Set("Authorization", fmt.Sprintf("Bearer %s", accessToken))