Open platform to collect and prioritize feedback
bootstraping facebook oauth
goenning692fcad3d808
diff --git a/handler/oauth.go b/handler/oauth.goindex c5f1cf486..f161c0503 100644--- a/handler/oauth.go+++ b/handler/oauth.go@@ -1,26 +1,84 @@package handlerimport (+ "io/ioutil"+ "log""net/http"- "time"+ "net/url"+ "os"+ "strings"++ "golang.org/x/oauth2"+ "golang.org/x/oauth2/facebook""github.com/WeCanHearYou/wchy/context""github.com/labstack/echo")-type oauthHandler struct {+var (+ oauthState = os.Getenv("OAUTH_STATE")+)++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/callback?redirect=" + url.QueryEscape(redirect),+ Scopes: []string{"public_profile", "email"},+ Endpoint: facebook.Endpoint,+ }+}++// OAuthHandlers contains multiple oauth HTTP handlers+type OAuthHandlers struct {ctx *context.WchyContext}-// OAuth creates a new OAuth HTTP handler-func OAuth(ctx *context.WchyContext) echo.HandlerFunc {- return oauthHandler{ctx: ctx}.get()+// OAuth creates a new OAuthHandlers+func OAuth(ctx *context.WchyContext) OAuthHandlers {+ return OAuthHandlers{ctx: ctx}+}++// Callback handles OAuth callbacks+func (h OAuthHandlers) Callback() echo.HandlerFunc {+ return func(c echo.Context) error {+ state := c.QueryParam("state")+ code := c.QueryParam("code")+ redirect := c.QueryParam("redirect")++ if state != oauthState {+ c.Logger().Infof("Invalid OAuth state '%s'", state)+ return c.Redirect(http.StatusTemporaryRedirect, redirect) //TODO: redirect to some error page+ }++ config := newFacebookOAuthConfig(redirect)+ token, err := config.Exchange(oauth2.NoContext, code)+ if err != nil {+ c.Logger().Errorf("facebookOAuthConfig.Exchange() failed with %s", err)+ return c.Redirect(http.StatusTemporaryRedirect, redirect) //TODO: redirect to some error page+ }++ resp, err := http.Get("https://graph.facebook.com/me?fields=name,email&access_token=" + url.QueryEscape(token.AccessToken))+ defer resp.Body.Close()+ response, err := ioutil.ReadAll(resp.Body)+ log.Printf("parseResponseBody: %s\n", string(response))++ return c.Redirect(http.StatusTemporaryRedirect, redirect)+ }}-func (h oauthHandler) get() echo.HandlerFunc {+// Login handlers OAuth logins+func (h OAuthHandlers) Login() echo.HandlerFunc {return func(c echo.Context) error {- return c.JSON(http.StatusOK, echo.Map{- "now": time.Now().Format("2006.01.02.150405"),- })+ config := newFacebookOAuthConfig(c.QueryParam("redirect"))+ authURL, _ := url.Parse(config.Endpoint.AuthURL)+ parameters := url.Values{}+ parameters.Add("client_id", config.ClientID)+ parameters.Add("scope", strings.Join(config.Scopes, " "))+ parameters.Add("redirect_uri", config.RedirectURL)+ parameters.Add("response_type", "code")+ parameters.Add("state", oauthState)+ authURL.RawQuery = parameters.Encode()+ return c.Redirect(http.StatusTemporaryRedirect, authURL.String())}}diff --git a/main.go b/main.goindex 1b8a37325..ea48ee765 100644--- a/main.go+++ b/main.go@@ -13,9 +13,11 @@ import ("github.com/WeCanHearYou/wchy/service")-var ctx *context.WchyContext-var db *sql.DB-var buildtime string+var (+ ctx *context.WchyContext+ db *sql.DB+ buildtime string+)func init() {fmt.Printf("Application is starting...\n")diff --git a/router/router.go b/router/router.goindex c97a595f7..c51942cc8 100644--- a/router/router.go+++ b/router/router.go@@ -47,7 +47,8 @@ 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", handler.OAuth(ctx))+ router.GET("/oauth/facebook/", handler.OAuth(ctx).Login())+ router.GET("/oauth/facebook/callback", handler.OAuth(ctx).Callback())app := router.Group("", MultiTenant(ctx)){