git.iko.soy/typeable/oauth2-proxy
on master help sign in
Added userinfo endpoint (#300)

* Added userinfo endpoint * Added documentation for the userinfo endpoint * Update oauthproxy.go Co-Authored-By: Dan Bond <pm@danbond.io> * Suggested fixes : Streaming json to rw , header set after error check * Update oauthproxy.go Co-Authored-By: Dan Bond <pm@danbond.io> * fix session.Email * Ported tests and updated changelog

commitfef940da9a9034ff296d999c3e377a33c20359fa authorKonstantine <kbabuadze@yahoo.com> dateFri 8 Nov 2019, 00:38 (7 years ago) parent2a07983
4 files changed+49
CHANGELOG.md+1 docs/5_endpoints.md+1 oauthproxy.go+21 oauthproxy_test.go+26
CHANGELOG.md+1
@@ -11,6 +11,7 @@
1111- [#286](https://github.com/pusher/oauth2_proxy/pull/286) Requests.go updated with useful error messages (@biotom)
1212- [#302](https://github.com/pusher/oauth2_proxy/pull/302) Rewrite dist script (@syscll)
1313- [#304](https://github.com/pusher/oauth2_proxy/pull/304) Add new Logo! :tada: (@JoelSpeed)
14+- [#300](https://github.com/pusher/oauth2_proxy/pull/300) Added userinfo endpoint (@kbabuadze)
1415
1516# v4.0.0
1617
docs/5_endpoints.md+1
@@ -14,4 +14,5 @@ OAuth2 Proxy responds directly to the following endpoints. All other endpoints w
1414- /oauth2/sign_in - the login page, which also doubles as a sign out page (it clears cookies)
1515- /oauth2/start - a URL that will redirect to start the OAuth cycle
1616- /oauth2/callback - the URL used at the end of the OAuth cycle. The oauth app will be configured with this as the callback url.
17+- /oauth2/userinfo - the URL is used to return user's email from the session in JSON format.
1718- /oauth2/auth - only returns a 202 Accepted response or a 401 Unauthorized response; for use with the [Nginx `auth_request` directive](#nginx-auth-request)
oauthproxy.go+21
@@ -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)
oauthproxy_test.go+26
@@ -746,6 +746,32 @@ func TestProcessCookieFailIfRefreshSetAndCookieExpired(t *testing.T) {
746746 }
747747}
748748
749+func NewUserInfoEndpointTest() *ProcessCookieTest {
750+ pcTest := NewProcessCookieTestWithDefaults()
751+ pcTest.req, _ = http.NewRequest("GET",
752+ pcTest.opts.ProxyPrefix+"/userinfo", nil)
753+ return pcTest
754+}
755+
756+func TestUserInfoEndpointAccepted(t *testing.T) {
757+ test := NewUserInfoEndpointTest()
758+ startSession := &sessions.SessionState{
759+ Email: "john.doe@example.com", AccessToken: "my_access_token"}
760+ test.SaveSession(startSession)
761+
762+ test.proxy.ServeHTTP(test.rw, test.req)
763+ assert.Equal(t, http.StatusOK, test.rw.Code)
764+ bodyBytes, _ := ioutil.ReadAll(test.rw.Body)
765+ assert.Equal(t, "{\"email\":\"john.doe@example.com\"}\n", string(bodyBytes))
766+}
767+
768+func TestUserInfoEndpointUnauthorizedOnNoCookieSetError(t *testing.T) {
769+ test := NewUserInfoEndpointTest()
770+
771+ test.proxy.ServeHTTP(test.rw, test.req)
772+ assert.Equal(t, http.StatusUnauthorized, test.rw.Code)
773+}
774+
749775func NewAuthOnlyEndpointTest(modifiers ...OptionsModifier) *ProcessCookieTest {
750776 pcTest := NewProcessCookieTestWithOptionsModifiers(modifiers...)
751777 pcTest.req, _ = http.NewRequest("GET",