Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

this fixes #28

goenning81d2f48195fa
diff --git a/app/dbx/dbx.go b/app/dbx/dbx.go
index 0b54c65c5..86638699c 100644
--- a/app/dbx/dbx.go
+++ b/app/dbx/dbx.go
@@ -49,6 +49,17 @@ func (db Database) QueryInt(command string, args ...interface{}) (int, error) {
return data, nil
}
+// QueryString executes given SQL command and return first string of first row
+func (db Database) QueryString(command string, args ...interface{}) (string, error) {
+ var data string
+ row := db.conn.QueryRow(command, args...)
+ err := row.Scan(&data)
+ if err != nil {
+ return "", err
+ }
+ return data, nil
+}
+
// Query the database with given SQL command
func (db Database) Query(command string, args ...interface{}) (*sql.Rows, error) {
return db.conn.Query(command, args...)
diff --git a/app/identity/handler.go b/app/identity/handler.go
index 4828e55d4..7ab6c220a 100644
--- a/app/identity/handler.go
+++ b/app/identity/handler.go
@@ -54,7 +54,6 @@ func (h OAuthHandlers) Callback(provider string) app.HandlerFunc {
}
user, err := h.userService.GetByEmail(tenant.ID, oauthUser.Email)
-
if err != nil {
if err == app.ErrNotFound {
user = &app.User{
@@ -76,6 +75,14 @@ func (h OAuthHandlers) Callback(provider string) app.HandlerFunc {
} else {
return c.Failure(err)
}
+ } else if !user.HasProvider(provider) {
+ err = h.userService.RegisterProvider(user.ID, &app.UserProvider{
+ UID: oauthUser.ID,
+ Name: provider,
+ })
+ if err != nil {
+ return c.Failure(err)
+ }
}
claims := &app.WechyClaims{
diff --git a/app/identity/handler_test.go b/app/identity/handler_test.go
index d85200079..d5e657564 100644
--- a/app/identity/handler_test.go
+++ b/app/identity/handler_test.go
@@ -28,7 +28,15 @@ func (p OAuthService) GetProfile(provider string, code string) (*identity.OAuthU
return &identity.OAuthUserProfile{
ID: "FB1234",
Name: "Jon Snow",
- Email: "[email protected]",
+ Email: "[email protected]",
+ }, nil
+ }
+
+ if provider == "google" && code == "123" {
+ return &identity.OAuthUserProfile{
+ ID: "GO1234",
+ Name: "Jon Snow",
+ Email: "[email protected]",
}, nil
}
@@ -89,11 +97,13 @@ func TestCallbackHandler_ExistingUserAndProvider(t *testing.T) {
RegisterTestingT(t)
server := mock.NewServer()
- server.Context.Request().URL, _ = url.Parse("http://login.test.canherayou.com/oauth/callback?state=http://orange.test.canhearyou.com&code=123")
+ server.Context.Request().URL, _ = url.Parse("http://demo.test.canherayou.com/oauth/callback?state=http://demo.test.canhearyou.com&code=123")
code, response := server.ExecuteRaw(handlers().Callback(identity.OAuthFacebookProvider))
+ Expect(db.Count("SELECT id FROM users WHERE email = '[email protected]'")).To(Equal(1))
+
Expect(code).To(Equal(http.StatusTemporaryRedirect))
- Expect(response.Header.Get("Location")).To(Equal("http://orange.test.canhearyou.com?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyL2lkIjoxLCJ1c2VyL25hbWUiOiJKb24gU25vdyIsInVzZXIvZW1haWwiOiJqb24uc25vQGdvdC5jb20iLCJ0ZW5hbnQvaWQiOjQwMH0.nxu7QHFXTeYh_ObpKYV6e3p0kk1mkGbNGplITZImSs8"))
+ Expect(response.Header.Get("Location")).To(Equal("http://demo.test.canhearyou.com?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyL2lkIjozMDAsInVzZXIvbmFtZSI6IkpvbiBTbm93IiwidXNlci9lbWFpbCI6Impvbi5zbm93QGdvdC5jb20iLCJ0ZW5hbnQvaWQiOjMwMH0.0sbeKMVxJ5zjm2SIS7UotDi1rsFgpEpTEe20Z3t7aNE"))
}
func TestCallbackHandler_NewUser(t *testing.T) {
@@ -109,3 +119,18 @@ func TestCallbackHandler_NewUser(t *testing.T) {
Expect(code).To(Equal(http.StatusTemporaryRedirect))
Expect(response.Header.Get("Location")).To(Equal("http://orange.test.canhearyou.com?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyL2lkIjoxLCJ1c2VyL25hbWUiOiJTb21lIEZhY2Vib29rIEd1eSIsInVzZXIvZW1haWwiOiJzb21lLmd1eUBmYWNlYm9vay5jb20iLCJ0ZW5hbnQvaWQiOjQwMH0.iWkpvh11QrUDJQnTk9PtqW6m48DnaHbsj-lbl6feK-Q"))
}
+
+func TestCallbackHandler_ExistingUser_NewProvider(t *testing.T) {
+ RegisterTestingT(t)
+
+ server := mock.NewServer()
+ server.Context.Request().URL, _ = url.Parse("http://login.test.canherayou.com/oauth/callback?state=http://demo.test.canhearyou.com&code=123")
+ code, response := server.ExecuteRaw(handlers().Callback(identity.OAuthGoogleProvider))
+
+ Expect(db.Count("SELECT id FROM users WHERE email = '[email protected]'")).To(Equal(1))
+ Expect(db.QueryString("SELECT provider_uid FROM user_providers WHERE user_id = 300 and provider = 'facebook'")).To(Equal("FB1234"))
+ Expect(db.QueryString("SELECT provider_uid FROM user_providers WHERE user_id = 300 and provider = 'google'")).To(Equal("GO1234"))
+
+ Expect(code).To(Equal(http.StatusTemporaryRedirect))
+ Expect(response.Header.Get("Location")).To(Equal("http://demo.test.canhearyou.com?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyL2lkIjozMDAsInVzZXIvbmFtZSI6IkpvbiBTbm93IiwidXNlci9lbWFpbCI6Impvbi5zbm93QGdvdC5jb20iLCJ0ZW5hbnQvaWQiOjMwMH0.0sbeKMVxJ5zjm2SIS7UotDi1rsFgpEpTEe20Z3t7aNE"))
+}
diff --git a/app/identity/service.go b/app/identity/service.go
index 39f5d7227..1acba5a96 100644
--- a/app/identity/service.go
+++ b/app/identity/service.go
@@ -6,6 +6,7 @@ import "github.com/WeCanHearYou/wechy/app"
type UserService interface {
GetByEmail(tenantID int, email string) (*app.User, error)
Register(user *app.User) error
+ RegisterProvider(userID int, provider *app.UserProvider) error
}
// TenantService contains read and write operations for tenants
diff --git a/app/model.go b/app/model.go
index 8458bb890..322a5714e 100644
--- a/app/model.go
+++ b/app/model.go
@@ -18,6 +18,16 @@ type User struct {
Providers []*UserProvider `json:"providers"`
}
+//HasProvider returns true if current user has given provider registered
+func (u User) HasProvider(provider string) bool {
+ for _, p := range u.Providers {
+ if p.Name == provider {
+ return true
+ }
+ }
+ return false
+}
+
//UserProvider represents the relashionship between an User and an Authentication provide
type UserProvider struct {
Name string `json:"name"`
diff --git a/app/postgres/identity.go b/app/postgres/identity.go
index cf3492725..0f0d609c7 100644
--- a/app/postgres/identity.go
+++ b/app/postgres/identity.go
@@ -23,6 +23,18 @@ func (svc UserService) GetByEmail(tenantID int, email string) (*app.User, error)
return nil, app.ErrNotFound
}
+ rows, err := svc.DB.Query("SELECT provider_uid, provider FROM user_providers WHERE user_id = $1", user.ID)
+ if err != nil {
+ return nil, err
+ }
+
+ defer rows.Close()
+ for rows.Next() {
+ p := &app.UserProvider{}
+ rows.Scan(&p.UID, &p.Name)
+ user.Providers = append(user.Providers, p)
+ }
+
return user, nil
}
@@ -50,6 +62,12 @@ func (svc UserService) Register(user *app.User) error {
return tx.Commit()
}
+// RegisterProvider adds given provider to userID
+func (svc UserService) RegisterProvider(userID int, provider *app.UserProvider) error {
+ now := time.Now()
+ return svc.DB.Execute("INSERT INTO user_providers (user_id, provider, provider_uid, created_on) VALUES ($1, $2, $3, $4)", userID, provider.Name, provider.UID, now)
+}
+
// TenantService contains read and write operations for tenants
type TenantService struct {
DB *dbx.Database
diff --git a/app/postgres/identity_test.go b/app/postgres/identity_test.go
index 51cd54de5..2458825af 100644
--- a/app/postgres/identity_test.go
+++ b/app/postgres/identity_test.go
@@ -34,6 +34,9 @@ func TestUserService_GetByEmail(t *testing.T) {
Expect(user.ID).To(Equal(int(300)))
Expect(user.Name).To(Equal("Jon Snow"))
Expect(user.Email).To(Equal("[email protected]"))
+ Expect(len(user.Providers)).To(Equal(1))
+ Expect(user.Providers[0].UID).To(Equal("FB1234"))
+ Expect(user.Providers[0].Name).To(Equal("facebook"))
}
func TestUserService_GetByEmail_WrongTenant(t *testing.T) {