Refactor previous COOPGO Identity service - Initial commit
This commit is contained in:
93
storage/storage.go
Normal file
93
storage/storage.go
Normal file
@@ -0,0 +1,93 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// Storage interface
|
||||
type Storage struct {
|
||||
DB DBStorage
|
||||
KV KVStore
|
||||
}
|
||||
|
||||
func NewStorage(cfg *viper.Viper) (Storage, error) {
|
||||
dbstorage, err := NewDBStorage(cfg)
|
||||
if err != nil {
|
||||
return Storage{}, err
|
||||
}
|
||||
kvstore, err := NewKVStore(cfg)
|
||||
if err != nil {
|
||||
return Storage{}, err
|
||||
}
|
||||
return Storage{
|
||||
DB: dbstorage,
|
||||
KV: kvstore,
|
||||
}, nil
|
||||
}
|
||||
|
||||
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)
|
||||
CreateAccount(account Account) error
|
||||
UpdateAccount(account Account) error
|
||||
}
|
||||
|
||||
func NewDBStorage(cfg *viper.Viper) (DBStorage, error) {
|
||||
var (
|
||||
storage_type = cfg.GetString("storage.db.type")
|
||||
)
|
||||
|
||||
switch storage_type {
|
||||
case "mongodb":
|
||||
s, err := NewMongoDBStorage(cfg)
|
||||
return s, err
|
||||
default:
|
||||
return nil, fmt.Errorf("storage type %v is not supported", storage_type)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
type KVStore interface {
|
||||
Put(string, any) error
|
||||
PutWithTTL(string, any, time.Duration) error
|
||||
Get(string) (any, error)
|
||||
Delete(string) error
|
||||
}
|
||||
|
||||
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