Open platform to collect and prioritize feedback
facebook auth working
goenning2df13ef91096
diff --git a/Makefile b/Makefileindex 17c18a3e4..b3f781c0e 100644--- a/Makefile+++ b/Makefile@@ -10,7 +10,9 @@ define tag_dockerendeftest:- GO_ENV=test go test $$(go list ./... | grep -v /vendor/) -cover+ AUTH_ENDPOINT=http://login.test.canhearyou.com:3000 \+ GO_ENV=test \+ go test $$(go list ./... | grep -v /vendor/) -coversetup-ci:go get github.com/kardianos/govendordiff --git a/handler/oauth.go b/handler/oauth.goindex 6d60f740c..0ab1ed9d0 100644--- a/handler/oauth.go+++ b/handler/oauth.go@@ -23,14 +23,17 @@ type facebookUser struct {}var (- oauthState = os.Getenv("OAUTH_STATE")+ oauthState = os.Getenv("OAUTH_STATE")+ fbClientID = os.Getenv("OAUTH_FACEBOOK_APPID")+ fbClientSecret = os.Getenv("OAUTH_FACEBOOK_SECRET")+ authEndpoint = os.Getenv("AUTH_ENDPOINT"))func newFacebookOAuthConfig(redirect string) *oauth2.Config {return &oauth2.Config{- ClientID: os.Getenv("OAUTH_FACEBOOK_APPID"),- ClientSecret: os.Getenv("OAUTH_FACEBOOK_SECRET"),- RedirectURL: os.Getenv("AUTH_ENDPOINT") + "/oauth/facebook/callback?redirect=" + url.QueryEscape(redirect),+ ClientID: oauthState,+ ClientSecret: fbClientSecret,+ RedirectURL: authEndpoint + "/oauth/facebook/callback?redirect=" + url.QueryEscape(redirect),Scopes: []string{"public_profile", "email"},Endpoint: facebook.Endpoint,}@@ -46,6 +49,25 @@ func OAuth(ctx *context.WchyContext) OAuthHandlers {return OAuthHandlers{ctx: ctx}}+func doGet(url string, v interface{}) error {+ r, err := http.Get(url)+ defer r.Body.Close()+ if err != nil {+ return err+ }++ bytes, err := ioutil.ReadAll(r.Body)+ if err != nil {+ return err+ }++ return json.Unmarshal(bytes, v)+}++func facebookMe(token *oauth2.Token) string {+ return "https://graph.facebook.com/me?fields=name,email&access_token=" + url.QueryEscape(token.AccessToken)+}+// Callback handles OAuth callbacksfunc (h OAuthHandlers) Callback() echo.HandlerFunc {return func(c echo.Context) error {@@ -54,7 +76,7 @@ func (h OAuthHandlers) Callback() echo.HandlerFunc {redirect := c.QueryParam("redirect")if state != oauthState {- c.Logger().Infof("Invalid OAuth state '%s'", state)+ c.Logger().Errorf("Invalid OAuth state '%s'", state)return c.Redirect(http.StatusTemporaryRedirect, redirect) //TODO: redirect to some error page}@@ -65,12 +87,11 @@ func (h OAuthHandlers) Callback() echo.HandlerFunc {return c.Redirect(http.StatusTemporaryRedirect, redirect) //TODO: redirect to some error page}- r, err := http.Get("https://graph.facebook.com/me?fields=name,email&access_token=" + url.QueryEscape(token.AccessToken))- defer r.Body.Close()-fbUser := &facebookUser{}- bytes, err := ioutil.ReadAll(r.Body)- err = json.Unmarshal(bytes, fbUser)+ if err = doGet(facebookMe(token), fbUser); err != nil {+ c.Logger().Errorf("HTTP Facebook/Me failed with %s", err)+ return c.Redirect(http.StatusTemporaryRedirect, redirect) //TODO: redirect to some error page+ }user, err := h.ctx.Auth.GetByEmail(fbUser.Email)if err != nil {@@ -89,6 +110,8 @@ func (h OAuthHandlers) Callback() echo.HandlerFunc {if err != nil {c.Logger().Error(err)}+ } else {+ c.Logger().Error(err)}}diff --git a/mock/server.go b/mock/server.goindex fe1335774..d6aee6dfb 100644--- a/mock/server.go+++ b/mock/server.go@@ -23,7 +23,7 @@ type Server struct {// NewServer creates a new test serverfunc NewServer() *Server {engine := echo.New()- engine.Renderer = router.NewHTMLRenderer()+ engine.Renderer = router.NewHTMLRenderer(engine.Logger)request, _ := http.NewRequest(echo.GET, "/", nil)recorder := httptest.NewRecorder()diff --git a/router/hostchecker.go b/router/hostchecker.gonew file mode 100644index 000000000..d3835a933--- /dev/null+++ b/router/hostchecker.go@@ -0,0 +1,28 @@+package router++import (+ "net/http"+ "strings"++ "github.com/labstack/echo"+)++// HostChecker checks for a specific host+func HostChecker(baseURL string) echo.MiddlewareFunc {+ return func(next echo.HandlerFunc) echo.HandlerFunc {+ return func(c echo.Context) error {++ scheme := "http"+ if c.Request().TLS != nil {+ scheme = "https"+ }+ url := scheme + "://" + c.Request().Host++ if strings.Index(url, baseURL) < 0 {+ c.Logger().Errorf("%s is not valid for this operation", url)+ return c.NoContent(http.StatusBadRequest)+ }+ return next(c)+ }+ }+}diff --git a/router/router.go b/router/router.goindex 257099997..238665d58 100644--- a/router/router.go+++ b/router/router.go@@ -47,8 +47,11 @@ func GetMainEngine(ctx *context.WchyContext) *echo.Echo {router.Static("/public", "public")router.Static("/assets", "node_modules") //TODO: Don't expose node_modules- router.GET("/oauth/facebook", handler.OAuth(ctx).Login())- router.GET("/oauth/facebook/callback", handler.OAuth(ctx).Callback())+ auth := router.Group("", HostChecker(env.MustGet("AUTH_ENDPOINT")))+ {+ auth.GET("/oauth/facebook", handler.OAuth(ctx).Login())+ auth.GET("/oauth/facebook/callback", handler.OAuth(ctx).Callback())+ }app := router.Group("", MultiTenant(ctx)){