Add missing gRPC functions
This commit is contained in:
28
storage/accounts.go
Normal file
28
storage/accounts.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package storage
|
||||
|
||||
type Account struct {
|
||||
ID string `json:"id" bson:"_id"`
|
||||
Namespace string `json:"namespace"`
|
||||
Authentication AccountAuth `json:"-" bson:"authentication"`
|
||||
Data map[string]any `json:"data"`
|
||||
Metadata map[string]any `json:"metadata"`
|
||||
}
|
||||
|
||||
type AccountAuth struct {
|
||||
Local LocalAuth
|
||||
//TODO handle SSO
|
||||
}
|
||||
|
||||
type LocalAuth struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
Email string `json:"email"`
|
||||
EmailValidation Validation `json:"email_validation" bson:"email_validation"`
|
||||
PhoneNumber string `json:"phone_number" bson:"phone_number"`
|
||||
PhoneNumberValidation Validation `json:"phone_number_validation" bson:"phone_number_validation"`
|
||||
}
|
||||
|
||||
type Validation struct {
|
||||
Validated bool
|
||||
ValidationCode string `json:"validation_code" bson:"validation_code"`
|
||||
}
|
||||
@@ -40,17 +40,10 @@ func NewEtcdKVStore(cfg *viper.Viper) (EtcdKVStore, error) {
|
||||
}
|
||||
|
||||
func (s EtcdKVStore) Put(k string, v any) error {
|
||||
// var data bytes.Buffer // Stand-in for a network connection
|
||||
// enc := gob.NewEncoder(&data)
|
||||
// err := enc.Encode(v)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
data, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// _, err = s.Client.KV.Put(context.TODO(), k, data.String())
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
_, err = s.Client.KV.Put(ctx, k, string(data))
|
||||
cancel()
|
||||
@@ -66,17 +59,10 @@ func (s EtcdKVStore) PutWithTTL(k string, v any, duration time.Duration) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// var data bytes.Buffer // Stand-in for a network connection
|
||||
// enc := gob.NewEncoder(&data)
|
||||
// err = enc.Encode(v)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
data, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// _, err = s.Client.KV.Put(context.TODO(), k, data.String(), clientv3.WithLease(lease.ID))
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
_, err = s.Client.KV.Put(ctx, k, string(data), clientv3.WithLease(lease.ID))
|
||||
cancel()
|
||||
@@ -95,10 +81,6 @@ func (s EtcdKVStore) Get(k string) (any, error) {
|
||||
}
|
||||
for _, v := range resp.Kvs {
|
||||
var data any
|
||||
// var reader bytes.Buffer
|
||||
// reader.Write(v.Value)
|
||||
// enc := gob.NewDecoder(&reader)
|
||||
// err := enc.Decode(&data)
|
||||
err := json.Unmarshal([]byte(v.Value), &data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -3,6 +3,7 @@ package storage
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
@@ -46,17 +47,6 @@ func NewMongoDBStorage(cfg *viper.Viper) (MongoDBStorage, error) {
|
||||
return storage, err
|
||||
}
|
||||
|
||||
func (s MongoDBStorage) GetAccount(id string) (*Account, error) {
|
||||
collection := s.Client.Database(s.DbName).Collection(s.Collections["users"])
|
||||
|
||||
account := &Account{}
|
||||
if err := collection.FindOne(context.TODO(), bson.M{"_id": id}).Decode(account); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return account, nil
|
||||
}
|
||||
|
||||
// LocalAuthentication returns an Account matching with one of username, email or password.
|
||||
// If username, is provided (not an empty string), it will search by username only
|
||||
// If username is an empty string and email is provided, it will search by email
|
||||
@@ -85,6 +75,17 @@ func (s MongoDBStorage) LocalAuthentication(namespace string, username string, e
|
||||
return account, nil
|
||||
}
|
||||
|
||||
func (s MongoDBStorage) GetAccount(id string) (*Account, error) {
|
||||
collection := s.Client.Database(s.DbName).Collection(s.Collections["users"])
|
||||
|
||||
account := &Account{}
|
||||
if err := collection.FindOne(context.TODO(), bson.M{"_id": id}).Decode(account); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return account, nil
|
||||
}
|
||||
|
||||
func (s MongoDBStorage) GetAccounts(namespaces []string) (accounts []Account, err error) {
|
||||
collection := s.Client.Database(s.DbName).Collection(s.Collections["users"])
|
||||
|
||||
@@ -123,6 +124,41 @@ func (s MongoDBStorage) GetAccounts(namespaces []string) (accounts []Account, er
|
||||
return
|
||||
}
|
||||
|
||||
func (s MongoDBStorage) GetAccountsByIds(accountids []string) (accounts []Account, err error) {
|
||||
collection := s.Client.Database(s.DbName).Collection(s.Collections["users"])
|
||||
|
||||
var cur *mongo.Cursor
|
||||
|
||||
findOptions := options.Find()
|
||||
|
||||
if len(accountids) == 0 {
|
||||
return accounts, errors.New("missing account ids")
|
||||
} else {
|
||||
cur, err = collection.Find(context.TODO(), bson.M{"_id": bson.M{"$in": accountids}}, findOptions)
|
||||
if err != nil {
|
||||
return accounts, err
|
||||
}
|
||||
}
|
||||
|
||||
for cur.Next(context.TODO()) {
|
||||
var account Account
|
||||
var elem bson.M
|
||||
|
||||
err := cur.Decode(&elem)
|
||||
if err != nil {
|
||||
return accounts, err
|
||||
}
|
||||
|
||||
bsonBytes, _ := bson.Marshal(elem)
|
||||
bson.Unmarshal(bsonBytes, &account)
|
||||
|
||||
accounts = append(accounts, account)
|
||||
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (s MongoDBStorage) CreateAccount(account Account) error {
|
||||
collection := s.Client.Database(s.DbName).Collection(s.Collections["users"])
|
||||
if _, err := collection.InsertOne(context.TODO(), account); err != nil {
|
||||
@@ -135,6 +171,7 @@ func (s MongoDBStorage) CreateAccount(account Account) error {
|
||||
func (s MongoDBStorage) UpdateAccount(account Account) error {
|
||||
collection := s.Client.Database(s.DbName).Collection(s.Collections["users"])
|
||||
if _, err := collection.ReplaceOne(context.TODO(), bson.M{"_id": account.ID}, account); err != nil {
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@@ -32,6 +32,7 @@ type DBStorage interface {
|
||||
GetAccount(id string) (*Account, error)
|
||||
LocalAuthentication(namespace string, username string, email string, phone_number string) (*Account, error)
|
||||
GetAccounts(namespaces []string) ([]Account, error)
|
||||
GetAccountsByIds(accountids []string) ([]Account, error)
|
||||
CreateAccount(account Account) error
|
||||
UpdateAccount(account Account) error
|
||||
}
|
||||
@@ -62,32 +63,3 @@ func NewKVStore(cfg *viper.Viper) (KVStore, error) {
|
||||
kv, err := NewEtcdKVStore(cfg)
|
||||
return kv, err
|
||||
}
|
||||
|
||||
// Data models
|
||||
|
||||
type Account struct {
|
||||
ID string `json:"id" bson:"_id"`
|
||||
Namespace string `json:"namespace"`
|
||||
Authentication AccountAuth `json:"authentication" bson:"authentication"`
|
||||
Data map[string]any `json:"data"`
|
||||
Metadata map[string]any `json:"metadata"`
|
||||
}
|
||||
|
||||
type AccountAuth struct {
|
||||
Local LocalAuth
|
||||
//TODO handle SSO
|
||||
}
|
||||
|
||||
type LocalAuth struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
Email string `json:"email"`
|
||||
EmailValidation Validation `json:"email_validation" bson:"email_validation"`
|
||||
PhoneNumber string `json:"phone_number" bson:"phone_number"`
|
||||
PhoneNumberValidation Validation `json:"phone_number_validation" bson:"phone_number_validation"`
|
||||
}
|
||||
|
||||
type Validation struct {
|
||||
Validated bool
|
||||
ValidationCode string `json:"validation_code" bson:"validation_code"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user