Add support for openid with github and facebook
This commit is contained in:
@@ -2,6 +2,7 @@ package storage
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
@@ -19,10 +20,13 @@ type DB struct {
|
||||
Instances map[string]*types.Instance `json:"instances"`
|
||||
Clients map[string]*types.Client `json:"clients"`
|
||||
WindowsInstances map[string]*types.WindowsInstance `json:"windows_instances"`
|
||||
LoginRequests map[string]*types.LoginRequest `json:"login_requests"`
|
||||
Users map[string]*types.User `json:"user"`
|
||||
|
||||
WindowsInstancesBySessionId map[string][]string `json:"windows_instances_by_session_id"`
|
||||
InstancesBySessionId map[string][]string `json:"instances_by_session_id"`
|
||||
ClientsBySessionId map[string][]string `json:"clients_by_session_id"`
|
||||
UsersByProvider map[string]string `json:"users_by_providers"`
|
||||
}
|
||||
|
||||
func (store *storage) SessionGet(id string) (*types.Session, error) {
|
||||
@@ -300,6 +304,56 @@ func (store *storage) ClientFindBySessionId(sessionId string) ([]*types.Client,
|
||||
return clients, nil
|
||||
}
|
||||
|
||||
func (store *storage) LoginRequestPut(loginRequest *types.LoginRequest) error {
|
||||
store.rw.Lock()
|
||||
defer store.rw.Unlock()
|
||||
|
||||
store.db.LoginRequests[loginRequest.Id] = loginRequest
|
||||
return nil
|
||||
}
|
||||
func (store *storage) LoginRequestGet(id string) (*types.LoginRequest, error) {
|
||||
store.rw.Lock()
|
||||
defer store.rw.Unlock()
|
||||
|
||||
if lr, found := store.db.LoginRequests[id]; !found {
|
||||
return nil, NotFoundError
|
||||
} else {
|
||||
return lr, nil
|
||||
}
|
||||
}
|
||||
func (store *storage) LoginRequestDelete(id string) error {
|
||||
store.rw.Lock()
|
||||
defer store.rw.Unlock()
|
||||
|
||||
delete(store.db.LoginRequests, id)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (store *storage) UserFindByProvider(providerName, providerUserId string) (*types.User, error) {
|
||||
store.rw.Lock()
|
||||
defer store.rw.Unlock()
|
||||
|
||||
if userId, found := store.db.UsersByProvider[fmt.Sprintf("%s_%s", providerName, providerUserId)]; !found {
|
||||
return nil, NotFoundError
|
||||
} else {
|
||||
if user, found := store.db.Users[userId]; !found {
|
||||
return nil, NotFoundError
|
||||
} else {
|
||||
return user, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (store *storage) UserPut(user *types.User) error {
|
||||
store.rw.Lock()
|
||||
defer store.rw.Unlock()
|
||||
|
||||
store.db.UsersByProvider[fmt.Sprintf("%s_%s", user.Provider, user.ProviderUserId)] = user.Id
|
||||
store.db.Users[user.Id] = user
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (store *storage) load() error {
|
||||
file, err := os.Open(store.path)
|
||||
|
||||
@@ -312,13 +366,16 @@ func (store *storage) load() error {
|
||||
}
|
||||
} else {
|
||||
store.db = &DB{
|
||||
Sessions: map[string]*types.Session{},
|
||||
Instances: map[string]*types.Instance{},
|
||||
Clients: map[string]*types.Client{},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{},
|
||||
Sessions: map[string]*types.Session{},
|
||||
Instances: map[string]*types.Instance{},
|
||||
Clients: map[string]*types.Client{},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{},
|
||||
LoginRequests: map[string]*types.LoginRequest{},
|
||||
Users: map[string]*types.User{},
|
||||
WindowsInstancesBySessionId: map[string][]string{},
|
||||
InstancesBySessionId: map[string][]string{},
|
||||
ClientsBySessionId: map[string][]string{},
|
||||
UsersByProvider: map[string]string{},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -30,13 +30,16 @@ func TestSessionPut(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
|
||||
expectedDB := &DB{
|
||||
Sessions: map[string]*types.Session{s.Id: s},
|
||||
Instances: map[string]*types.Instance{},
|
||||
Clients: map[string]*types.Client{},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{},
|
||||
Sessions: map[string]*types.Session{s.Id: s},
|
||||
Instances: map[string]*types.Instance{},
|
||||
Clients: map[string]*types.Client{},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{},
|
||||
LoginRequests: map[string]*types.LoginRequest{},
|
||||
Users: map[string]*types.User{},
|
||||
WindowsInstancesBySessionId: map[string][]string{},
|
||||
InstancesBySessionId: map[string][]string{},
|
||||
ClientsBySessionId: map[string][]string{},
|
||||
UsersByProvider: map[string]string{},
|
||||
}
|
||||
var loadedDB *DB
|
||||
|
||||
@@ -56,13 +59,16 @@ func TestSessionPut(t *testing.T) {
|
||||
func TestSessionGet(t *testing.T) {
|
||||
expectedSession := &types.Session{Id: "aaabbbccc"}
|
||||
expectedDB := &DB{
|
||||
Sessions: map[string]*types.Session{expectedSession.Id: expectedSession},
|
||||
Instances: map[string]*types.Instance{},
|
||||
Clients: map[string]*types.Client{},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{},
|
||||
Sessions: map[string]*types.Session{expectedSession.Id: expectedSession},
|
||||
Instances: map[string]*types.Instance{},
|
||||
Clients: map[string]*types.Client{},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{},
|
||||
LoginRequests: map[string]*types.LoginRequest{},
|
||||
Users: map[string]*types.User{},
|
||||
WindowsInstancesBySessionId: map[string][]string{},
|
||||
InstancesBySessionId: map[string][]string{},
|
||||
ClientsBySessionId: map[string][]string{},
|
||||
UsersByProvider: map[string]string{},
|
||||
}
|
||||
|
||||
tmpfile, err := ioutil.TempFile("", "pwd")
|
||||
@@ -92,13 +98,16 @@ func TestSessionGetAll(t *testing.T) {
|
||||
s1 := &types.Session{Id: "aaabbbccc"}
|
||||
s2 := &types.Session{Id: "dddeeefff"}
|
||||
expectedDB := &DB{
|
||||
Sessions: map[string]*types.Session{s1.Id: s1, s2.Id: s2},
|
||||
Instances: map[string]*types.Instance{},
|
||||
Clients: map[string]*types.Client{},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{},
|
||||
Sessions: map[string]*types.Session{s1.Id: s1, s2.Id: s2},
|
||||
Instances: map[string]*types.Instance{},
|
||||
Clients: map[string]*types.Client{},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{},
|
||||
LoginRequests: map[string]*types.LoginRequest{},
|
||||
Users: map[string]*types.User{},
|
||||
WindowsInstancesBySessionId: map[string][]string{},
|
||||
InstancesBySessionId: map[string][]string{},
|
||||
ClientsBySessionId: map[string][]string{},
|
||||
UsersByProvider: map[string]string{},
|
||||
}
|
||||
|
||||
tmpfile, err := ioutil.TempFile("", "pwd")
|
||||
@@ -154,13 +163,16 @@ func TestSessionDelete(t *testing.T) {
|
||||
func TestInstanceGet(t *testing.T) {
|
||||
expectedInstance := &types.Instance{SessionId: "aaabbbccc", Name: "i1", IP: "10.0.0.1"}
|
||||
expectedDB := &DB{
|
||||
Sessions: map[string]*types.Session{},
|
||||
Instances: map[string]*types.Instance{expectedInstance.Name: expectedInstance},
|
||||
Clients: map[string]*types.Client{},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{},
|
||||
Sessions: map[string]*types.Session{},
|
||||
Instances: map[string]*types.Instance{expectedInstance.Name: expectedInstance},
|
||||
Clients: map[string]*types.Client{},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{},
|
||||
LoginRequests: map[string]*types.LoginRequest{},
|
||||
Users: map[string]*types.User{},
|
||||
WindowsInstancesBySessionId: map[string][]string{},
|
||||
InstancesBySessionId: map[string][]string{expectedInstance.SessionId: []string{expectedInstance.Name}},
|
||||
ClientsBySessionId: map[string][]string{},
|
||||
UsersByProvider: map[string]string{},
|
||||
}
|
||||
|
||||
tmpfile, err := ioutil.TempFile("", "pwd")
|
||||
@@ -205,13 +217,16 @@ func TestInstancePut(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
|
||||
expectedDB := &DB{
|
||||
Sessions: map[string]*types.Session{s.Id: s},
|
||||
Instances: map[string]*types.Instance{i.Name: i},
|
||||
Clients: map[string]*types.Client{},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{},
|
||||
Sessions: map[string]*types.Session{s.Id: s},
|
||||
Instances: map[string]*types.Instance{i.Name: i},
|
||||
Clients: map[string]*types.Client{},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{},
|
||||
LoginRequests: map[string]*types.LoginRequest{},
|
||||
Users: map[string]*types.User{},
|
||||
WindowsInstancesBySessionId: map[string][]string{},
|
||||
InstancesBySessionId: map[string][]string{i.SessionId: []string{i.Name}},
|
||||
ClientsBySessionId: map[string][]string{},
|
||||
UsersByProvider: map[string]string{},
|
||||
}
|
||||
var loadedDB *DB
|
||||
|
||||
@@ -265,13 +280,16 @@ func TestInstanceFindBySessionId(t *testing.T) {
|
||||
i1 := &types.Instance{SessionId: "aaabbbccc", Name: "c1"}
|
||||
i2 := &types.Instance{SessionId: "aaabbbccc", Name: "c2"}
|
||||
expectedDB := &DB{
|
||||
Sessions: map[string]*types.Session{},
|
||||
Instances: map[string]*types.Instance{i1.Name: i1, i2.Name: i2},
|
||||
Clients: map[string]*types.Client{},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{},
|
||||
Sessions: map[string]*types.Session{},
|
||||
Instances: map[string]*types.Instance{i1.Name: i1, i2.Name: i2},
|
||||
Clients: map[string]*types.Client{},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{},
|
||||
LoginRequests: map[string]*types.LoginRequest{},
|
||||
Users: map[string]*types.User{},
|
||||
WindowsInstancesBySessionId: map[string][]string{},
|
||||
InstancesBySessionId: map[string][]string{i1.SessionId: []string{i1.Name, i2.Name}},
|
||||
ClientsBySessionId: map[string][]string{},
|
||||
UsersByProvider: map[string]string{},
|
||||
}
|
||||
|
||||
tmpfile, err := ioutil.TempFile("", "pwd")
|
||||
@@ -298,13 +316,16 @@ func TestWindowsInstanceGetAll(t *testing.T) {
|
||||
i1 := &types.WindowsInstance{SessionId: "aaabbbccc", Id: "i1"}
|
||||
i2 := &types.WindowsInstance{SessionId: "aaabbbccc", Id: "i2"}
|
||||
expectedDB := &DB{
|
||||
Sessions: map[string]*types.Session{},
|
||||
Instances: map[string]*types.Instance{},
|
||||
Clients: map[string]*types.Client{},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{i1.Id: i1, i2.Id: i2},
|
||||
Sessions: map[string]*types.Session{},
|
||||
Instances: map[string]*types.Instance{},
|
||||
Clients: map[string]*types.Client{},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{i1.Id: i1, i2.Id: i2},
|
||||
LoginRequests: map[string]*types.LoginRequest{},
|
||||
Users: map[string]*types.User{},
|
||||
WindowsInstancesBySessionId: map[string][]string{i1.SessionId: []string{i1.Id, i2.Id}},
|
||||
InstancesBySessionId: map[string][]string{},
|
||||
ClientsBySessionId: map[string][]string{},
|
||||
UsersByProvider: map[string]string{},
|
||||
}
|
||||
|
||||
tmpfile, err := ioutil.TempFile("", "pwd")
|
||||
@@ -350,13 +371,16 @@ func TestWindowsInstancePut(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
|
||||
expectedDB := &DB{
|
||||
Sessions: map[string]*types.Session{s.Id: s},
|
||||
Instances: map[string]*types.Instance{},
|
||||
Clients: map[string]*types.Client{},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{i.Id: i},
|
||||
Sessions: map[string]*types.Session{s.Id: s},
|
||||
Instances: map[string]*types.Instance{},
|
||||
Clients: map[string]*types.Client{},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{i.Id: i},
|
||||
LoginRequests: map[string]*types.LoginRequest{},
|
||||
Users: map[string]*types.User{},
|
||||
WindowsInstancesBySessionId: map[string][]string{i.SessionId: []string{i.Id}},
|
||||
InstancesBySessionId: map[string][]string{},
|
||||
ClientsBySessionId: map[string][]string{},
|
||||
UsersByProvider: map[string]string{},
|
||||
}
|
||||
var loadedDB *DB
|
||||
|
||||
@@ -409,13 +433,16 @@ func TestWindowsInstanceDelete(t *testing.T) {
|
||||
func TestClientGet(t *testing.T) {
|
||||
c := &types.Client{SessionId: "aaabbbccc", Id: "c1"}
|
||||
expectedDB := &DB{
|
||||
Sessions: map[string]*types.Session{},
|
||||
Instances: map[string]*types.Instance{},
|
||||
Clients: map[string]*types.Client{c.Id: c},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{},
|
||||
Sessions: map[string]*types.Session{},
|
||||
Instances: map[string]*types.Instance{},
|
||||
Clients: map[string]*types.Client{c.Id: c},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{},
|
||||
LoginRequests: map[string]*types.LoginRequest{},
|
||||
Users: map[string]*types.User{},
|
||||
WindowsInstancesBySessionId: map[string][]string{},
|
||||
InstancesBySessionId: map[string][]string{},
|
||||
ClientsBySessionId: map[string][]string{c.SessionId: []string{c.Id}},
|
||||
UsersByProvider: map[string]string{},
|
||||
}
|
||||
|
||||
tmpfile, err := ioutil.TempFile("", "pwd")
|
||||
@@ -460,13 +487,16 @@ func TestClientPut(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
|
||||
expectedDB := &DB{
|
||||
Sessions: map[string]*types.Session{s.Id: s},
|
||||
Instances: map[string]*types.Instance{},
|
||||
Clients: map[string]*types.Client{c.Id: c},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{},
|
||||
Sessions: map[string]*types.Session{s.Id: s},
|
||||
Instances: map[string]*types.Instance{},
|
||||
Clients: map[string]*types.Client{c.Id: c},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{},
|
||||
LoginRequests: map[string]*types.LoginRequest{},
|
||||
Users: map[string]*types.User{},
|
||||
WindowsInstancesBySessionId: map[string][]string{},
|
||||
InstancesBySessionId: map[string][]string{},
|
||||
ClientsBySessionId: map[string][]string{c.SessionId: []string{c.Id}},
|
||||
UsersByProvider: map[string]string{},
|
||||
}
|
||||
var loadedDB *DB
|
||||
|
||||
@@ -520,13 +550,16 @@ func TestClientFindBySessionId(t *testing.T) {
|
||||
c1 := &types.Client{SessionId: "aaabbbccc", Id: "c1"}
|
||||
c2 := &types.Client{SessionId: "aaabbbccc", Id: "c2"}
|
||||
expectedDB := &DB{
|
||||
Sessions: map[string]*types.Session{},
|
||||
Instances: map[string]*types.Instance{},
|
||||
Clients: map[string]*types.Client{c1.Id: c1, c2.Id: c2},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{},
|
||||
Sessions: map[string]*types.Session{},
|
||||
Instances: map[string]*types.Instance{},
|
||||
Clients: map[string]*types.Client{c1.Id: c1, c2.Id: c2},
|
||||
WindowsInstances: map[string]*types.WindowsInstance{},
|
||||
LoginRequests: map[string]*types.LoginRequest{},
|
||||
Users: map[string]*types.User{},
|
||||
WindowsInstancesBySessionId: map[string][]string{},
|
||||
InstancesBySessionId: map[string][]string{},
|
||||
ClientsBySessionId: map[string][]string{c1.SessionId: []string{c1.Id, c2.Id}},
|
||||
UsersByProvider: map[string]string{},
|
||||
}
|
||||
|
||||
tmpfile, err := ioutil.TempFile("", "pwd")
|
||||
|
||||
@@ -82,3 +82,23 @@ func (m *Mock) ClientFindBySessionId(sessionId string) ([]*types.Client, error)
|
||||
args := m.Called(sessionId)
|
||||
return args.Get(0).([]*types.Client), args.Error(1)
|
||||
}
|
||||
func (m *Mock) LoginRequestPut(loginRequest *types.LoginRequest) error {
|
||||
args := m.Called(loginRequest)
|
||||
return args.Error(0)
|
||||
}
|
||||
func (m *Mock) LoginRequestGet(id string) (*types.LoginRequest, error) {
|
||||
args := m.Called(id)
|
||||
return args.Get(0).(*types.LoginRequest), args.Error(1)
|
||||
}
|
||||
func (m *Mock) LoginRequestDelete(id string) error {
|
||||
args := m.Called(id)
|
||||
return args.Error(0)
|
||||
}
|
||||
func (m *Mock) UserFindByProvider(providerName, providerUserId string) (*types.User, error) {
|
||||
args := m.Called(providerName, providerUserId)
|
||||
return args.Get(0).(*types.User), args.Error(1)
|
||||
}
|
||||
func (m *Mock) UserPut(user *types.User) error {
|
||||
args := m.Called(user)
|
||||
return args.Error(0)
|
||||
}
|
||||
|
||||
@@ -34,4 +34,11 @@ type StorageApi interface {
|
||||
ClientDelete(id string) error
|
||||
ClientCount() (int, error)
|
||||
ClientFindBySessionId(sessionId string) ([]*types.Client, error)
|
||||
|
||||
LoginRequestPut(loginRequest *types.LoginRequest) error
|
||||
LoginRequestGet(id string) (*types.LoginRequest, error)
|
||||
LoginRequestDelete(id string) error
|
||||
|
||||
UserFindByProvider(providerName, providerUserId string) (*types.User, error)
|
||||
UserPut(user *types.User) error
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user