Add support for openid with github and facebook
This commit is contained in:
@@ -43,7 +43,7 @@ func TestClientNew(t *testing.T) {
|
||||
p := NewPWD(_f, _e, _s, sp, ipf)
|
||||
p.generator = _g
|
||||
|
||||
session, err := p.SessionNew(time.Hour, "", "", "")
|
||||
session, err := p.SessionNew("", time.Hour, "", "", "")
|
||||
assert.Nil(t, err)
|
||||
|
||||
client := p.ClientNew("foobar", session)
|
||||
@@ -82,7 +82,7 @@ func TestClientCount(t *testing.T) {
|
||||
p := NewPWD(_f, _e, _s, sp, ipf)
|
||||
p.generator = _g
|
||||
|
||||
session, err := p.SessionNew(time.Hour, "", "", "")
|
||||
session, err := p.SessionNew("", time.Hour, "", "", "")
|
||||
assert.Nil(t, err)
|
||||
|
||||
p.ClientNew("foobar", session)
|
||||
@@ -123,7 +123,7 @@ func TestClientResizeViewPort(t *testing.T) {
|
||||
p := NewPWD(_f, _e, _s, sp, ipf)
|
||||
p.generator = _g
|
||||
|
||||
session, err := p.SessionNew(time.Hour, "", "", "")
|
||||
session, err := p.SessionNew("", time.Hour, "", "", "")
|
||||
assert.Nil(t, err)
|
||||
client := p.ClientNew("foobar", session)
|
||||
_s.On("ClientFindBySessionId", "aaaabbbbcccc").Return([]*types.Client{client}, nil)
|
||||
|
||||
@@ -70,7 +70,7 @@ func TestInstanceNew(t *testing.T) {
|
||||
p := NewPWD(_f, _e, _s, sp, ipf)
|
||||
p.generator = _g
|
||||
|
||||
session, err := p.SessionNew(time.Hour, "", "", "")
|
||||
session, err := p.SessionNew("", time.Hour, "", "", "")
|
||||
assert.Nil(t, err)
|
||||
|
||||
expectedInstance := types.Instance{
|
||||
@@ -138,7 +138,7 @@ func TestInstanceNew_WithNotAllowedImage(t *testing.T) {
|
||||
p := NewPWD(_f, _e, _s, sp, ipf)
|
||||
p.generator = _g
|
||||
|
||||
session, err := p.SessionNew(time.Hour, "", "", "")
|
||||
session, err := p.SessionNew("", time.Hour, "", "", "")
|
||||
|
||||
assert.Nil(t, err)
|
||||
|
||||
@@ -207,7 +207,7 @@ func TestInstanceNew_WithCustomHostname(t *testing.T) {
|
||||
p := NewPWD(_f, _e, _s, sp, ipf)
|
||||
p.generator = _g
|
||||
|
||||
session, err := p.SessionNew(time.Hour, "", "", "")
|
||||
session, err := p.SessionNew("", time.Hour, "", "", "")
|
||||
assert.Nil(t, err)
|
||||
|
||||
expectedInstance := types.Instance{
|
||||
|
||||
17
pwd/mock.go
17
pwd/mock.go
@@ -13,7 +13,7 @@ type Mock struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
func (m *Mock) SessionNew(duration time.Duration, stack string, stackName, imageName string) (*types.Session, error) {
|
||||
func (m *Mock) SessionNew(userId string, duration time.Duration, stack string, stackName, imageName string) (*types.Session, error) {
|
||||
args := m.Called(duration, stack, stackName, imageName)
|
||||
return args.Get(0).(*types.Session), args.Error(1)
|
||||
}
|
||||
@@ -104,3 +104,18 @@ func (m *Mock) ClientCount() int {
|
||||
args := m.Called()
|
||||
return args.Int(0)
|
||||
}
|
||||
|
||||
func (m *Mock) UserNewLoginRequest(providerName string) (*types.LoginRequest, error) {
|
||||
args := m.Called(providerName)
|
||||
return args.Get(0).(*types.LoginRequest), args.Error(1)
|
||||
}
|
||||
|
||||
func (m *Mock) UserGetLoginRequest(id string) (*types.LoginRequest, error) {
|
||||
args := m.Called(id)
|
||||
return args.Get(0).(*types.LoginRequest), args.Error(1)
|
||||
}
|
||||
|
||||
func (m *Mock) UserLogin(loginRequest *types.LoginRequest, user *types.User) (*types.User, error) {
|
||||
args := m.Called(loginRequest, user)
|
||||
return args.Get(0).(*types.User), args.Error(1)
|
||||
}
|
||||
|
||||
@@ -72,7 +72,7 @@ func SessionNotEmpty(e error) bool {
|
||||
}
|
||||
|
||||
type PWDApi interface {
|
||||
SessionNew(duration time.Duration, stack string, stackName, imageName string) (*types.Session, error)
|
||||
SessionNew(userId string, duration time.Duration, stack string, stackName, imageName string) (*types.Session, error)
|
||||
SessionClose(session *types.Session) error
|
||||
SessionGetSmallestViewPort(sessionId string) types.ViewPort
|
||||
SessionDeployStack(session *types.Session) error
|
||||
@@ -93,6 +93,10 @@ type PWDApi interface {
|
||||
ClientResizeViewPort(client *types.Client, cols, rows uint)
|
||||
ClientClose(client *types.Client)
|
||||
ClientCount() int
|
||||
|
||||
UserNewLoginRequest(providerName string) (*types.LoginRequest, error)
|
||||
UserGetLoginRequest(id string) (*types.LoginRequest, error)
|
||||
UserLogin(loginRequest *types.LoginRequest, user *types.User) (*types.User, error)
|
||||
}
|
||||
|
||||
func NewPWD(f docker.FactoryApi, e event.EventApi, s storage.StorageApi, sp provisioner.SessionProvisionerApi, ipf provisioner.InstanceProvisionerFactoryApi) *pwd {
|
||||
|
||||
@@ -44,7 +44,7 @@ type SessionSetupInstanceConf struct {
|
||||
Tls bool `json:"tls"`
|
||||
}
|
||||
|
||||
func (p *pwd) SessionNew(duration time.Duration, stack, stackName, imageName string) (*types.Session, error) {
|
||||
func (p *pwd) SessionNew(userId string, duration time.Duration, stack, stackName, imageName string) (*types.Session, error) {
|
||||
defer observeAction("SessionNew", time.Now())
|
||||
|
||||
s := &types.Session{}
|
||||
@@ -53,6 +53,7 @@ func (p *pwd) SessionNew(duration time.Duration, stack, stackName, imageName str
|
||||
s.ExpiresAt = s.CreatedAt.Add(duration)
|
||||
s.Ready = true
|
||||
s.Stack = stack
|
||||
s.UserId = userId
|
||||
|
||||
if s.Stack != "" {
|
||||
s.Ready = false
|
||||
|
||||
@@ -45,7 +45,7 @@ func TestSessionNew(t *testing.T) {
|
||||
|
||||
before := time.Now()
|
||||
|
||||
s, e := p.SessionNew(time.Hour, "", "", "")
|
||||
s, e := p.SessionNew("", time.Hour, "", "", "")
|
||||
assert.Nil(t, e)
|
||||
assert.NotNil(t, s)
|
||||
|
||||
@@ -56,7 +56,7 @@ func TestSessionNew(t *testing.T) {
|
||||
assert.WithinDuration(t, s.ExpiresAt, before.Add(time.Hour), time.Second)
|
||||
assert.True(t, s.Ready)
|
||||
|
||||
s, _ = p.SessionNew(time.Hour, "stackPath", "stackName", "imageName")
|
||||
s, _ = p.SessionNew("", time.Hour, "stackPath", "stackName", "imageName")
|
||||
|
||||
assert.Equal(t, "stackPath", s.Stack)
|
||||
assert.Equal(t, "stackName", s.StackName)
|
||||
|
||||
@@ -15,6 +15,7 @@ type Session struct {
|
||||
StackName string `json:"stack_name" bson:"stack_name"`
|
||||
ImageName string `json:"image_name" bson:"image_name"`
|
||||
Host string `json:"host" bson:"host"`
|
||||
UserId string `json:"user_id" bson:"user_id"`
|
||||
rw sync.Mutex `json:"-"`
|
||||
}
|
||||
|
||||
|
||||
15
pwd/types/user.go
Normal file
15
pwd/types/user.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package types
|
||||
|
||||
type User struct {
|
||||
Id string `json:"id" bson:"id"`
|
||||
Name string `json:"name" bson:"name"`
|
||||
ProviderUserId string `json:"provider_user_id" bson:"provider_user_id"`
|
||||
Avatar string `json:"avatar" bson:"avatar"`
|
||||
Provider string `json:"provider" bson:"provider"`
|
||||
Email string `json:"email" bson:"email"`
|
||||
}
|
||||
|
||||
type LoginRequest struct {
|
||||
Id string `json:"id" bson:"id"`
|
||||
Provider string `json:"provider" bson:"provider"`
|
||||
}
|
||||
43
pwd/user.go
Normal file
43
pwd/user.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package pwd
|
||||
|
||||
import (
|
||||
"github.com/play-with-docker/play-with-docker/pwd/types"
|
||||
"github.com/play-with-docker/play-with-docker/storage"
|
||||
)
|
||||
|
||||
func (p *pwd) UserNewLoginRequest(providerName string) (*types.LoginRequest, error) {
|
||||
req := &types.LoginRequest{Id: p.generator.NewId(), Provider: providerName}
|
||||
if err := p.storage.LoginRequestPut(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func (p *pwd) UserGetLoginRequest(id string) (*types.LoginRequest, error) {
|
||||
if req, err := p.storage.LoginRequestGet(id); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return req, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (p *pwd) UserLogin(loginRequest *types.LoginRequest, user *types.User) (*types.User, error) {
|
||||
if err := p.storage.LoginRequestDelete(loginRequest.Id); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
u, err := p.storage.UserFindByProvider(user.Provider, user.ProviderUserId)
|
||||
if err != nil {
|
||||
if storage.NotFound(err) {
|
||||
user.Id = p.generator.NewId()
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
user.Id = u.Id
|
||||
}
|
||||
if err := p.storage.UserPut(user); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
Reference in New Issue
Block a user