Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

working, but needs tests

goenning7527050ef62a
diff --git a/app/identity/handler.go b/app/identity/handler.go
index 7432addfc..5ca0d714b 100644
--- a/app/identity/handler.go
+++ b/app/identity/handler.go
@@ -3,6 +3,7 @@ package identity
import (
"net/http"
"net/url"
+ "strings"
"time"
"github.com/WeCanHearYou/wechy/app"
@@ -17,29 +18,34 @@ type oauthUserProfile struct {
// OAuthHandlers contains multiple oauth HTTP handlers
type OAuthHandlers struct {
- oauthService OAuthService
- userService UserService
+ tenantService TenantService
+ oauthService OAuthService
+ userService UserService
}
// OAuth creates a new OAuthHandlers
-func OAuth(oauthService OAuthService, userService UserService) OAuthHandlers {
- return OAuthHandlers{oauthService, userService}
+func OAuth(tenantService TenantService, oauthService OAuthService, userService UserService) OAuthHandlers {
+ return OAuthHandlers{tenantService, oauthService, userService}
}
// Callback handles OAuth callbacks
func (h OAuthHandlers) Callback(provider string) app.HandlerFunc {
return func(c app.Context) error {
- var redirectURL *url.URL
- var err error
redirect := c.QueryParam("state")
code := c.QueryParam("code")
+ redirectURL, err := url.Parse(redirect)
- if redirectURL, err = url.Parse(redirect); err != nil {
+ if err != nil {
c.Logger().Errorf("Could not parse url %s", redirect)
return c.Render(http.StatusInternalServerError, "500.html", echo.Map{})
}
+ tenant, err := h.tenantService.GetByDomain(stripPort(redirectURL.Host))
+ if err != nil {
+ return err
+ }
+
//TODO: Check if code is empty (or other querystring parameter)
//Because the user can deny access to it
oauthUser, err := h.oauthService.GetProfile(provider, code)
@@ -52,8 +58,9 @@ func (h OAuthHandlers) Callback(provider string) app.HandlerFunc {
if err != nil {
if err == app.ErrNotFound {
user = &app.User{
- Name: oauthUser.Name,
- Email: oauthUser.Email,
+ Name: oauthUser.Name,
+ Tenant: tenant,
+ Email: oauthUser.Email,
Providers: []*app.UserProvider{
&app.UserProvider{
UID: oauthUser.ID,
@@ -64,10 +71,10 @@ func (h OAuthHandlers) Callback(provider string) app.HandlerFunc {
err = h.userService.Register(user)
if err != nil {
- c.Logger().Error(err)
+ return err
}
} else {
- c.Logger().Error(err)
+ return err
}
}
@@ -75,6 +82,7 @@ func (h OAuthHandlers) Callback(provider string) app.HandlerFunc {
UserID: user.ID,
UserName: user.Name,
UserEmail: user.Email,
+ TenantID: tenant.ID,
}
var token string
@@ -109,3 +117,11 @@ func (h OAuthHandlers) Logout() app.HandlerFunc {
return c.Redirect(http.StatusTemporaryRedirect, c.QueryParam("redirect"))
}
}
+
+func stripPort(hostport string) string {
+ colon := strings.IndexByte(hostport, ':')
+ if colon == -1 {
+ return hostport
+ }
+ return hostport[:colon]
+}
diff --git a/app/identity/handler_test.go b/app/identity/handler_test.go
index e7b08896f..1c7131fa3 100644
--- a/app/identity/handler_test.go
+++ b/app/identity/handler_test.go
@@ -29,7 +29,7 @@ func TestLoginHandlers(t *testing.T) {
oauth := &mockOAuthService{}
server := mock.NewServer()
- code, response := server.ExecuteRaw(identity.OAuth(oauth, nil).Login(identity.OAuthFacebookProvider))
+ code, response := server.ExecuteRaw(identity.OAuth(nil, oauth, nil).Login(identity.OAuthFacebookProvider))
Expect(code).To(Equal(http.StatusTemporaryRedirect))
Expect(response.Header.Get("Location")).To(Equal("http://myapp/oauth/token?provider=facebook&redirect="))
diff --git a/app/identity/middleware.go b/app/identity/middleware.go
index 1a7077082..c34c626c0 100644
--- a/app/identity/middleware.go
+++ b/app/identity/middleware.go
@@ -3,7 +3,6 @@ package identity
import (
"net/http"
"net/url"
- "strings"
"github.com/WeCanHearYou/wechy/app"
)
@@ -98,11 +97,3 @@ func HostChecker(baseURL string) app.MiddlewareFunc {
}
}
}
-
-func stripPort(hostport string) string {
- colon := strings.IndexByte(hostport, ':')
- if colon == -1 {
- return hostport
- }
- return hostport[:colon]
-}
diff --git a/app/model.go b/app/model.go
index e88ae7b0a..8458bb890 100644
--- a/app/model.go
+++ b/app/model.go
@@ -14,6 +14,7 @@ type User struct {
ID int `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
+ Tenant *Tenant `json:"tenant"`
Providers []*UserProvider `json:"providers"`
}
diff --git a/app/postgres/identity.go b/app/postgres/identity.go
index b0ced8344..07c555970 100644
--- a/app/postgres/identity.go
+++ b/app/postgres/identity.go
@@ -35,7 +35,7 @@ func (svc UserService) Register(user *app.User) error {
now := time.Now()
- if err = tx.QueryRow("INSERT INTO users (name, email, created_on) VALUES ($1, $2, $3) RETURNING id", user.Name, user.Email, now).Scan(&user.ID); err != nil {
+ if err = tx.QueryRow("INSERT INTO users (name, email, created_on, tenant_id) VALUES ($1, $2, $3, $4) RETURNING id", user.Name, user.Email, now, user.Tenant.ID).Scan(&user.ID); err != nil {
tx.Rollback()
return err
}
diff --git a/app/postgres/identity_test.go b/app/postgres/identity_test.go
index 0872ce918..ee8b13e90 100644
--- a/app/postgres/identity_test.go
+++ b/app/postgres/identity_test.go
@@ -42,10 +42,15 @@ func TestUserService_Register(t *testing.T) {
db := setup()
defer teardown(db)
+ execute(db, "INSERT INTO tenants (name, subdomain, created_on) VALUES ('My Domain Inc.','mydomain', now())")
+
svc := &postgres.UserService{DB: db}
user := &app.User{
Name: "Jon Snow",
+ Tenant: &app.Tenant{
+ ID: 1,
+ },
Providers: []*app.UserProvider{
{
UID: "123123123",
@@ -66,10 +71,15 @@ func TestUserService_Register_MultipleProviders(t *testing.T) {
db := setup()
defer teardown(db)
+ execute(db, "INSERT INTO tenants (name, subdomain, created_on) VALUES ('My Domain Inc.','mydomain', now())")
+
svc := &postgres.UserService{DB: db}
user := &app.User{
Name: "Jon Snow",
+ Tenant: &app.Tenant{
+ ID: 1,
+ },
Providers: []*app.UserProvider{
{
UID: "123123123",
diff --git a/router.go b/router.go
index ab5bc8dc2..494157a1d 100644
--- a/router.go
+++ b/router.go
@@ -101,10 +101,10 @@ func GetMainEngine(ctx *WechyServices) *echo.Echo {
authGroup := group(router, "/oauth", identity.HostChecker(env.MustGet("AUTH_ENDPOINT")))
{
- get(authGroup, "/facebook", identity.OAuth(ctx.OAuth, ctx.User).Login(identity.OAuthFacebookProvider))
- get(authGroup, "/facebook/callback", identity.OAuth(ctx.OAuth, ctx.User).Callback(identity.OAuthFacebookProvider))
- get(authGroup, "/google", identity.OAuth(ctx.OAuth, ctx.User).Login(identity.OAuthGoogleProvider))
- get(authGroup, "/google/callback", identity.OAuth(ctx.OAuth, ctx.User).Callback(identity.OAuthGoogleProvider))
+ get(authGroup, "/facebook", identity.OAuth(ctx.Tenant, ctx.OAuth, ctx.User).Login(identity.OAuthFacebookProvider))
+ get(authGroup, "/facebook/callback", identity.OAuth(ctx.Tenant, ctx.OAuth, ctx.User).Callback(identity.OAuthFacebookProvider))
+ get(authGroup, "/google", identity.OAuth(ctx.Tenant, ctx.OAuth, ctx.User).Login(identity.OAuthGoogleProvider))
+ get(authGroup, "/google/callback", identity.OAuth(ctx.Tenant, ctx.OAuth, ctx.User).Callback(identity.OAuthGoogleProvider))
}
appGroup := group(router, "")
@@ -115,7 +115,7 @@ func GetMainEngine(ctx *WechyServices) *echo.Echo {
get(appGroup, "/", feedback.Handlers(ctx.Idea).List())
get(appGroup, "/ideas/:id", feedback.Handlers(ctx.Idea).Details())
- get(appGroup, "/logout", identity.OAuth(ctx.OAuth, ctx.User).Logout())
+ get(appGroup, "/logout", identity.OAuth(ctx.Tenant, ctx.OAuth, ctx.User).Logout())
get(appGroup, "/api/status", infra.Status(ctx.Health, ctx.Settings))
post(appGroup, "/api/ideas", feedback.Handlers(ctx.Idea).PostIdea())