Move accounts into rove-server.internal

This commit is contained in:
Marc Di Luzio 2020-07-10 18:57:57 +01:00
parent 9ccb7ac019
commit 46f81abbd7
5 changed files with 6 additions and 7 deletions

View file

@ -0,0 +1,28 @@
package internal
// Accountant decribes something that stores accounts and account values
type Accountant interface {
// RegisterAccount will register a new account and return it's info
RegisterAccount(name string) (acc Account, err error)
// AssignData stores a custom account key value pair
AssignData(account string, key string, value string) error
// GetValue returns custom account data for a specific key
GetValue(account string, key string) (string, error)
// VerifySecret will verify whether the account secret matches with the
VerifySecret(account string, secret string) (bool, error)
// GetSecret gets the secret associated with an account
GetSecret(account string) (string, error)
}
// Account represents a registered user
type Account struct {
// Name simply describes the account and must be unique
Name string `json:"name"`
// Data represents internal account data
Data map[string]string `json:"data"`
}

View file

@ -0,0 +1,63 @@
package internal
import (
"testing"
"github.com/google/uuid"
)
func TestNewAccountant(t *testing.T) {
// Very basic verify here for now
accountant := NewSimpleAccountant()
if accountant == nil {
t.Error("Failed to create accountant")
}
}
func TestAccountant_RegisterAccount(t *testing.T) {
accountant := NewSimpleAccountant()
// Start by making two accounts
namea := uuid.New().String()
acca, err := accountant.RegisterAccount(namea)
if err != nil {
t.Error(err)
} else if acca.Name != namea {
t.Errorf("Missmatched account name after register, expected: %s, actual: %s", namea, acca.Name)
}
nameb := uuid.New().String()
accb, err := accountant.RegisterAccount(nameb)
if err != nil {
t.Error(err)
} else if accb.Name != nameb {
t.Errorf("Missmatched account name after register, expected: %s, actual: %s", nameb, acca.Name)
}
// Verify another request gets rejected
_, err = accountant.RegisterAccount(namea)
if err == nil {
t.Error("Duplicate account name did not produce error")
}
}
func TestAccountant_AssignGetData(t *testing.T) {
accountant := NewSimpleAccountant()
name := uuid.New().String()
a, err := accountant.RegisterAccount(name)
if err != nil {
t.Error(err)
}
err = accountant.AssignData(a.Name, "key", "value")
if err != nil {
t.Error("Failed to set data for created account")
} else if id, err := accountant.GetValue(a.Name, "key"); err != nil {
t.Error("Failed to get data for account")
} else if id != "value" {
t.Error("Fetched data is incorrect for account")
}
}

View file

@ -6,7 +6,6 @@ import (
"net"
"sync"
"github.com/mdiluz/rove/pkg/accounts"
"github.com/mdiluz/rove/pkg/persistence"
"github.com/mdiluz/rove/pkg/rove"
"github.com/mdiluz/rove/pkg/roveapi"
@ -29,7 +28,7 @@ type Server struct {
world *rove.World
// Accountant
accountant accounts.Accountant
accountant Accountant
// gRPC server
netListener net.Listener
@ -81,7 +80,7 @@ func NewServer(opts ...ServerOption) *Server {
persistence: EphemeralData,
schedule: cron.New(),
world: rove.NewWorld(32),
accountant: accounts.NewSimpleAccountant(),
accountant: NewSimpleAccountant(),
}
// Apply all options

View file

@ -0,0 +1,90 @@
package internal
import (
"fmt"
"time"
"github.com/google/uuid"
)
// SimpleAccountant manages a set of accounts
type SimpleAccountant struct {
Accounts map[string]Account `json:"accounts"`
}
// NewSimpleAccountant creates a new accountant
func NewSimpleAccountant() Accountant {
return &SimpleAccountant{
Accounts: make(map[string]Account),
}
}
// RegisterAccount adds an account to the set of internal accounts
func (a *SimpleAccountant) RegisterAccount(name string) (acc Account, err error) {
// Set up the account info
acc.Name = name
acc.Data = make(map[string]string)
// Verify this acount isn't already registered
for _, a := range a.Accounts {
if a.Name == acc.Name {
return Account{}, fmt.Errorf("account name already registered: %s", a.Name)
}
}
// Set the creation time
acc.Data["created"] = time.Now().String()
// Create a secret
acc.Data["secret"] = uuid.New().String()
// Simply add the account to the map
a.Accounts[acc.Name] = acc
return
}
// VerifySecret verifies if an account secret is correct
func (a *SimpleAccountant) VerifySecret(account string, secret string) (bool, error) {
// Find the account matching the ID
if this, ok := a.Accounts[account]; ok {
return this.Data["secret"] == secret, nil
}
return false, fmt.Errorf("no account found for id: %s", account)
}
// GetSecret gets the internal secret
func (a *SimpleAccountant) GetSecret(account string) (string, error) {
// Find the account matching the ID
if this, ok := a.Accounts[account]; ok {
return this.Data["secret"], nil
}
return "", fmt.Errorf("no account found for id: %s", account)
}
// AssignData assigns data to an account
func (a *SimpleAccountant) AssignData(account string, key string, value string) error {
// Find the account matching the ID
if this, ok := a.Accounts[account]; ok {
this.Data[key] = value
a.Accounts[account] = this
} else {
return fmt.Errorf("no account found for id: %s", account)
}
return nil
}
// GetValue gets the rover rover for the account
func (a *SimpleAccountant) GetValue(account string, key string) (string, error) {
// Find the account matching the ID
this, ok := a.Accounts[account]
if !ok {
return "", fmt.Errorf("no account found for id: %s", account)
}
return this.Data[key], nil
}