@@ -5,7 +5,6 @@ import (
7 "html/template"
7 "html/template"
10 "net/http"
9 "net/http"
11 "net/http/httputil"
10 "net/http/httputil"
@@ -16,6 +15,7 @@ import (
17 "github.com/mbland/hmacauth"
16 "github.com/mbland/hmacauth"
18 "github.com/pusher/oauth2_proxy/cookie"
17 "github.com/pusher/oauth2_proxy/cookie"
18+ "github.com/pusher/oauth2_proxy/logger"
19 "github.com/pusher/oauth2_proxy/providers"
19 "github.com/pusher/oauth2_proxy/providers"
20 "github.com/yhat/wsutil"
20 "github.com/yhat/wsutil"
@@ -185,7 +185,7 @@ func NewOAuthProxy(opts *Options, validator func(string) bool) *OAuthProxy {
185 path := u.Path
185 path := u.Path
186 switch u.Scheme {
186 switch u.Scheme {
187 case httpScheme, httpsScheme:
187 case httpScheme, httpsScheme:
188− log.Printf("mapping path %q => upstream %q", path, u)
188+ logger.Printf("mapping path %q => upstream %q", path, u)
189 proxy := NewWebSocketOrRestReverseProxy(u, opts, auth)
189 proxy := NewWebSocketOrRestReverseProxy(u, opts, auth)
190 serveMux.Handle(path, proxy)
190 serveMux.Handle(path, proxy)
@@ -193,7 +193,7 @@ func NewOAuthProxy(opts *Options, validator func(string) bool) *OAuthProxy {
193 if u.Fragment != "" {
193 if u.Fragment != "" {
194 path = u.Fragment
194 path = u.Fragment
196− log.Printf("mapping path %q => file system %q", path, u.Path)
196+ logger.Printf("mapping path %q => file system %q", path, u.Path)
197 proxy := NewFileServer(path, u.Path)
197 proxy := NewFileServer(path, u.Path)
198 serveMux.Handle(path, &UpstreamProxy{path, proxy, nil, nil})
198 serveMux.Handle(path, &UpstreamProxy{path, proxy, nil, nil})
@@ -201,7 +201,7 @@ func NewOAuthProxy(opts *Options, validator func(string) bool) *OAuthProxy {
203 for _, u := range opts.CompiledRegex {
203 for _, u := range opts.CompiledRegex {
204− log.Printf("compiled skip-auth-regex => %q", u)
204+ logger.Printf("compiled skip-auth-regex => %q", u)
207 redirectURL := opts.redirectURL
207 redirectURL := opts.redirectURL
@@ -209,20 +209,20 @@ func NewOAuthProxy(opts *Options, validator func(string) bool) *OAuthProxy {
209 redirectURL.Path = fmt.Sprintf("%s/callback", opts.ProxyPrefix)
209 redirectURL.Path = fmt.Sprintf("%s/callback", opts.ProxyPrefix)
212− log.Printf("OAuthProxy configured for %s Client ID: %s", opts.provider.Data().ProviderName, opts.ClientID)
212+ logger.Printf("OAuthProxy configured for %s Client ID: %s", opts.provider.Data().ProviderName, opts.ClientID)
213 refresh := "disabled"
213 refresh := "disabled"
214 if opts.CookieRefresh != time.Duration(0) {
214 if opts.CookieRefresh != time.Duration(0) {
215 refresh = fmt.Sprintf("after %s", opts.CookieRefresh)
215 refresh = fmt.Sprintf("after %s", opts.CookieRefresh)
218− log.Printf("Cookie settings: name:%s secure(https):%v httponly:%v expiry:%s domain:%s path:%s refresh:%s", opts.CookieName, opts.CookieSecure, opts.CookieHTTPOnly, opts.CookieExpire, opts.CookieDomain, opts.CookiePath, refresh)
218+ logger.Printf("Cookie settings: name:%s secure(https):%v httponly:%v expiry:%s domain:%s path:%s refresh:%s", opts.CookieName, opts.CookieSecure, opts.CookieHTTPOnly, opts.CookieExpire, opts.CookieDomain, opts.CookiePath, refresh)
220 var cipher *cookie.Cipher
220 var cipher *cookie.Cipher
221 if opts.PassAccessToken || opts.SetAuthorization || opts.PassAuthorization || (opts.CookieRefresh != time.Duration(0)) {
221 if opts.PassAccessToken || opts.SetAuthorization || opts.PassAuthorization || (opts.CookieRefresh != time.Duration(0)) {
222 var err error
222 var err error
223 cipher, err = cookie.NewCipher(secretBytes(opts.CookieSecret))
223 cipher, err = cookie.NewCipher(secretBytes(opts.CookieSecret))
224 if err != nil {
224 if err != nil {
225− log.Fatal("cookie-secret error: ", err)
225+ logger.Fatal("cookie-secret error: ", err)
@@ -425,7 +425,7 @@ func (p *OAuthProxy) makeCookie(req *http.Request, name string, value string, ex
425 domain = h
425 domain = h
427 if !strings.HasSuffix(domain, p.CookieDomain) {
427 if !strings.HasSuffix(domain, p.CookieDomain) {
428− log.Printf("Warning: request host is %q but using configured cookie domain of %q", domain, p.CookieDomain)
428+ logger.Printf("Warning: request host is %q but using configured cookie domain of %q", domain, p.CookieDomain)
@@ -529,7 +529,6 @@ func (p *OAuthProxy) PingPage(rw http.ResponseWriter) {
530// ErrorPage writes an error response
530// ErrorPage writes an error response
531func (p *OAuthProxy) ErrorPage(rw http.ResponseWriter, code int, title string, message string) {
531func (p *OAuthProxy) ErrorPage(rw http.ResponseWriter, code int, title string, message string) {
532− log.Printf("ErrorPage %d %s %s", code, title, message)
533 rw.WriteHeader(code)
532 rw.WriteHeader(code)
534 t := struct {
533 t := struct {
535 Title string
534 Title string
@@ -588,9 +587,10 @@ func (p *OAuthProxy) ManualSignIn(rw http.ResponseWriter, req *http.Request) (st
589 // check auth
588 // check auth
590 if p.HtpasswdFile.Validate(user, passwd) {
589 if p.HtpasswdFile.Validate(user, passwd) {
591− log.Printf("authenticated %q via HtpasswdFile", user)
590+ logger.PrintAuthf(user, req, logger.AuthSuccess, "Authenticated via HtpasswdFile")
592 return user, true
591 return user, true
593+ logger.PrintAuthf(user, req, logger.AuthFailure, "Invalid authentication via HtpasswdFile")
594 return "", false
594 return "", false
@@ -686,6 +686,7 @@ func (p *OAuthProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
686func (p *OAuthProxy) SignIn(rw http.ResponseWriter, req *http.Request) {
686func (p *OAuthProxy) SignIn(rw http.ResponseWriter, req *http.Request) {
687 redirect, err := p.GetRedirect(req)
687 redirect, err := p.GetRedirect(req)
688 if err != nil {
688 if err != nil {
689+ logger.Printf("Error obtaining redirect: %s", err.Error())
689 p.ErrorPage(rw, 500, "Internal Error", err.Error())
690 p.ErrorPage(rw, 500, "Internal Error", err.Error())
@@ -714,12 +715,14 @@ func (p *OAuthProxy) SignOut(rw http.ResponseWriter, req *http.Request) {
714func (p *OAuthProxy) OAuthStart(rw http.ResponseWriter, req *http.Request) {
715func (p *OAuthProxy) OAuthStart(rw http.ResponseWriter, req *http.Request) {
715 nonce, err := cookie.Nonce()
716 nonce, err := cookie.Nonce()
716 if err != nil {
717 if err != nil {
718+ logger.Printf("Error obtaining nonce: %s", err.Error())
717 p.ErrorPage(rw, 500, "Internal Error", err.Error())
719 p.ErrorPage(rw, 500, "Internal Error", err.Error())
720 p.SetCSRFCookie(rw, req, nonce)
722 p.SetCSRFCookie(rw, req, nonce)
721 redirect, err := p.GetRedirect(req)
723 redirect, err := p.GetRedirect(req)
722 if err != nil {
724 if err != nil {
725+ logger.Printf("Error obtaining redirect: %s", err.Error())
723 p.ErrorPage(rw, 500, "Internal Error", err.Error())
726 p.ErrorPage(rw, 500, "Internal Error", err.Error())
@@ -735,24 +738,27 @@ func (p *OAuthProxy) OAuthCallback(rw http.ResponseWriter, req *http.Request) {
735 // finish the oauth cycle
738 // finish the oauth cycle
736 err := req.ParseForm()
739 err := req.ParseForm()
737 if err != nil {
740 if err != nil {
741+ logger.Printf("Error while parsing OAuth2 callback: %s" + err.Error())
738 p.ErrorPage(rw, 500, "Internal Error", err.Error())
742 p.ErrorPage(rw, 500, "Internal Error", err.Error())
741 errorString := req.Form.Get("error")
745 errorString := req.Form.Get("error")
742 if errorString != "" {
746 if errorString != "" {
747+ logger.Printf("Error while parsing OAuth2 callback: %s ", errorString)
743 p.ErrorPage(rw, 403, "Permission Denied", errorString)
748 p.ErrorPage(rw, 403, "Permission Denied", errorString)
747 session, err := p.redeemCode(req.Host, req.Form.Get("code"))
752 session, err := p.redeemCode(req.Host, req.Form.Get("code"))
748 if err != nil {
753 if err != nil {
749− log.Printf("%s error redeeming code %s", remoteAddr, err)
754+ logger.Printf("Error redeeming code during OAuth2 callback: %s ", errorString)
750 p.ErrorPage(rw, 500, "Internal Error", "Internal Error")
755 p.ErrorPage(rw, 500, "Internal Error", "Internal Error")
754 s := strings.SplitN(req.Form.Get("state"), ":", 2)
759 s := strings.SplitN(req.Form.Get("state"), ":", 2)
755 if len(s) != 2 {
760 if len(s) != 2 {
761+ logger.Printf("Error while parsing OAuth2 state: invalid length")
756 p.ErrorPage(rw, 500, "Internal Error", "Invalid State")
762 p.ErrorPage(rw, 500, "Internal Error", "Invalid State")
@@ -760,12 +766,13 @@ func (p *OAuthProxy) OAuthCallback(rw http.ResponseWriter, req *http.Request) {
760 redirect := s[1]
766 redirect := s[1]
761 c, err := req.Cookie(p.CSRFCookieName)
767 c, err := req.Cookie(p.CSRFCookieName)
762 if err != nil {
768 if err != nil {
769+ logger.PrintAuthf(session.Email, req, logger.AuthFailure, "Invalid authentication via OAuth2: unable too obtain CSRF cookie")
763 p.ErrorPage(rw, 403, "Permission Denied", err.Error())
770 p.ErrorPage(rw, 403, "Permission Denied", err.Error())
766 p.ClearCSRFCookie(rw, req)
773 p.ClearCSRFCookie(rw, req)
767 if c.Value != nonce {
774 if c.Value != nonce {
768− log.Printf("%s csrf token mismatch, potential attack", remoteAddr)
775+ logger.PrintAuthf(session.Email, req, logger.AuthFailure, "Invalid authentication via OAuth2: csrf token mismatch, potential attack")
769 p.ErrorPage(rw, 403, "Permission Denied", "csrf failed")
776 p.ErrorPage(rw, 403, "Permission Denied", "csrf failed")
@@ -776,16 +783,16 @@ func (p *OAuthProxy) OAuthCallback(rw http.ResponseWriter, req *http.Request) {
777 // set cookie, or deny
784 // set cookie, or deny
778 if p.Validator(session.Email) && p.provider.ValidateGroup(session.Email) {
785 if p.Validator(session.Email) && p.provider.ValidateGroup(session.Email) {
779− log.Printf("%s authentication complete %s", remoteAddr, session)
786+ logger.PrintAuthf(session.Email, req, logger.AuthSuccess, "Authenticated via OAuth2: %s", session)
780 err := p.SaveSession(rw, req, session)
787 err := p.SaveSession(rw, req, session)
781 if err != nil {
788 if err != nil {
782− log.Printf("%s %s", remoteAddr, err)
789+ logger.Printf("%s %s", remoteAddr, err)
783 p.ErrorPage(rw, 500, "Internal Error", "Internal Error")
790 p.ErrorPage(rw, 500, "Internal Error", "Internal Error")
786 http.Redirect(rw, req, redirect, 302)
793 http.Redirect(rw, req, redirect, 302)
788− log.Printf("%s Permission Denied: %q is unauthorized", remoteAddr, session.Email)
795+ logger.PrintAuthf(session.Email, req, logger.AuthSuccess, "Invalid authentication via OAuth2: unauthorized")
789 p.ErrorPage(rw, 403, "Permission Denied", "Invalid Account")
796 p.ErrorPage(rw, 403, "Permission Denied", "Invalid Account")
@@ -827,16 +834,16 @@ func (p *OAuthProxy) Authenticate(rw http.ResponseWriter, req *http.Request) int
828 session, sessionAge, err := p.LoadCookiedSession(req)
835 session, sessionAge, err := p.LoadCookiedSession(req)
829 if err != nil {
836 if err != nil {
830− log.Printf("%s %s", remoteAddr, err)
837+ logger.Printf("Error loading cookied session: %s", err)
832 if session != nil && sessionAge > p.CookieRefresh && p.CookieRefresh != time.Duration(0) {
839 if session != nil && sessionAge > p.CookieRefresh && p.CookieRefresh != time.Duration(0) {
833− log.Printf("%s refreshing %s old session cookie for %s (refresh after %s)", remoteAddr, sessionAge, session, p.CookieRefresh)
840+ logger.Printf("Refreshing %s old session cookie for %s (refresh after %s)", sessionAge, session, p.CookieRefresh)
834 saveSession = true
841 saveSession = true
837 var ok bool
844 var ok bool
838 if ok, err = p.provider.RefreshSessionIfNeeded(session); err != nil {
845 if ok, err = p.provider.RefreshSessionIfNeeded(session); err != nil {
839− log.Printf("%s removing session. error refreshing access token %s %s", remoteAddr, err, session)
846+ logger.Printf("%s removing session. error refreshing access token %s %s", remoteAddr, err, session)
840 clearSession = true
847 clearSession = true
841 session = nil
848 session = nil
842 } else if ok {
849 } else if ok {
@@ -845,7 +852,7 @@ func (p *OAuthProxy) Authenticate(rw http.ResponseWriter, req *http.Request) int
847 if session != nil && session.IsExpired() {
854 if session != nil && session.IsExpired() {
848− log.Printf("%s removing session. token expired %s", remoteAddr, session)
855+ logger.Printf("Removing session: token expired %s", session)
849 session = nil
856 session = nil
850 saveSession = false
857 saveSession = false
851 clearSession = true
858 clearSession = true
@@ -853,7 +860,7 @@ func (p *OAuthProxy) Authenticate(rw http.ResponseWriter, req *http.Request) int
854 if saveSession && !revalidated && session != nil && session.AccessToken != "" {
861 if saveSession && !revalidated && session != nil && session.AccessToken != "" {
855 if !p.provider.ValidateSessionState(session) {
862 if !p.provider.ValidateSessionState(session) {
856− log.Printf("%s removing session. error validating %s", remoteAddr, session)
863+ logger.Printf("Removing session: error validating %s", session)
857 saveSession = false
864 saveSession = false
858 session = nil
865 session = nil
859 clearSession = true
866 clearSession = true
@@ -861,7 +868,7 @@ func (p *OAuthProxy) Authenticate(rw http.ResponseWriter, req *http.Request) int
863 if session != nil && session.Email != "" && !p.Validator(session.Email) {
870 if session != nil && session.Email != "" && !p.Validator(session.Email) {
864− log.Printf("%s Permission Denied: removing session %s", remoteAddr, session)
871+ logger.Printf(session.Email, req, logger.AuthFailure, "Invalid authentication via session: removing session %s", session)
865 session = nil
872 session = nil
866 saveSession = false
873 saveSession = false
867 clearSession = true
874 clearSession = true
@@ -870,7 +877,7 @@ func (p *OAuthProxy) Authenticate(rw http.ResponseWriter, req *http.Request) int
870 if saveSession && session != nil {
877 if saveSession && session != nil {
871 err = p.SaveSession(rw, req, session)
878 err = p.SaveSession(rw, req, session)
872 if err != nil {
879 if err != nil {
873− log.Printf("%s %s", remoteAddr, err)
880+ logger.PrintAuthf(session.Email, req, logger.AuthError, "Save session error %s", err)
874 return http.StatusInternalServerError
881 return http.StatusInternalServerError
@@ -882,7 +889,7 @@ func (p *OAuthProxy) Authenticate(rw http.ResponseWriter, req *http.Request) int
882 if session == nil {
889 if session == nil {
883 session, err = p.CheckBasicAuth(req)
890 session, err = p.CheckBasicAuth(req)
884 if err != nil {
891 if err != nil {
885− log.Printf("%s %s", remoteAddr, err)
892+ logger.Printf("Error during basic auth validation: %s", err)
@@ -958,10 +965,11 @@ func (p *OAuthProxy) CheckBasicAuth(req *http.Request) (*providers.SessionState,
958 return nil, fmt.Errorf("invalid format %s", b)
965 return nil, fmt.Errorf("invalid format %s", b)
960 if p.HtpasswdFile.Validate(pair[0], pair[1]) {
967 if p.HtpasswdFile.Validate(pair[0], pair[1]) {
961− log.Printf("authenticated %q via basic auth", pair[0])
968+ logger.PrintAuthf(pair[0], req, logger.AuthSuccess, "Authenticated via basic auth and HTpasswd File")
962 return &providers.SessionState{User: pair[0]}, nil
969 return &providers.SessionState{User: pair[0]}, nil
964− return nil, fmt.Errorf("%s not in HtpasswdFile", pair[0])
971+ logger.PrintAuthf(pair[0], req, logger.AuthFailure, "Invalid authentication via basic auth: not in Htpasswd File")
967// isAjax checks if a request is an ajax request
975// isAjax checks if a request is an ajax request