@@ -4,6 +4,7 @@ import (
44 "context"
55 "crypto/tls"
66 b64 "encoding/base64"
7+ "encoding/json"
78 "errors"
89 "fmt"
910 "html/template"
@@ -75,6 +76,7 @@ type OAuthProxy struct {
7576 OAuthStartPath string
7677 OAuthCallbackPath string
7778 AuthOnlyPath string
79+ UserInfoPath string
7880
7981 redirectURL *url.URL // the url to receive requests at
8082 whitelistDomains []string
@@ -266,6 +268,7 @@ func NewOAuthProxy(opts *Options, validator func(string) bool) *OAuthProxy {
266268 OAuthStartPath: fmt.Sprintf("%s/start", opts.ProxyPrefix),
267269 OAuthCallbackPath: fmt.Sprintf("%s/callback", opts.ProxyPrefix),
268270 AuthOnlyPath: fmt.Sprintf("%s/auth", opts.ProxyPrefix),
271+ UserInfoPath: fmt.Sprintf("%s/userinfo", opts.ProxyPrefix),
269272
270273 ProxyPrefix: opts.ProxyPrefix,
271274 provider: opts.provider,
@@ -557,6 +560,8 @@ func (p *OAuthProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
557560 p.OAuthCallback(rw, req)
558561 case path == p.AuthOnlyPath:
559562 p.AuthenticateOnly(rw, req)
563+ case path == p.UserInfoPath:
564+ p.UserInfo(rw, req)
560565 default:
561566 p.Proxy(rw, req)
562567 }
@@ -585,6 +590,22 @@ func (p *OAuthProxy) SignIn(rw http.ResponseWriter, req *http.Request) {
585590 }
586591}
587592
593+//UserInfo endpoint outputs session email in JSON format
594+func (p *OAuthProxy) UserInfo(rw http.ResponseWriter, req *http.Request) {
595+
596+ session, err := p.getAuthenticatedSession(rw, req)
597+ if err != nil {
598+ http.Error(rw, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
599+ return
600+ }
601+ userInfo := struct {
602+ Email string `json:"email"`
603+ }{session.Email}
604+ rw.Header().Set("Content-Type", "application/json")
605+ rw.WriteHeader(http.StatusOK)
606+ json.NewEncoder(rw).Encode(userInfo)
607+}
608+
588609// SignOut sends a response to clear the authentication cookie
589610func (p *OAuthProxy) SignOut(rw http.ResponseWriter, req *http.Request) {
590611 p.ClearSessionCookie(rw, req)