2020-06-30 23:34:49 +01:00
|
|
|
package accounts
|
2020-05-31 19:15:57 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-06-11 18:38:18 +01:00
|
|
|
"time"
|
2020-07-07 22:20:23 +01:00
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2020-05-31 19:15:57 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Account represents a registered user
|
|
|
|
type Account struct {
|
|
|
|
// Name simply describes the account and must be unique
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
2020-06-10 22:48:45 +01:00
|
|
|
// Data represents internal account data
|
|
|
|
Data map[string]string `json:"data"`
|
2020-05-31 19:15:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Accountant manages a set of accounts
|
|
|
|
type Accountant struct {
|
2020-06-10 18:56:24 +01:00
|
|
|
Accounts map[string]Account `json:"accounts"`
|
2020-05-31 19:15:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewAccountant creates a new accountant
|
2020-06-02 19:16:02 +01:00
|
|
|
func NewAccountant() *Accountant {
|
2020-06-02 16:10:45 +01:00
|
|
|
return &Accountant{
|
2020-06-10 18:56:24 +01:00
|
|
|
Accounts: make(map[string]Account),
|
2020-06-02 16:10:45 +01:00
|
|
|
}
|
2020-05-31 19:15:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterAccount adds an account to the set of internal accounts
|
2020-06-02 17:59:33 +01:00
|
|
|
func (a *Accountant) RegisterAccount(name string) (acc Account, err error) {
|
2020-05-31 19:15:57 +01:00
|
|
|
|
2020-07-07 22:20:23 +01:00
|
|
|
// Set up the account info
|
2020-06-02 18:06:34 +01:00
|
|
|
acc.Name = name
|
2020-06-10 23:23:09 +01:00
|
|
|
acc.Data = make(map[string]string)
|
2020-05-31 19:15:57 +01:00
|
|
|
|
|
|
|
// Verify this acount isn't already registered
|
2020-06-02 17:51:54 +01:00
|
|
|
for _, a := range a.Accounts {
|
2020-05-31 19:15:57 +01:00
|
|
|
if a.Name == acc.Name {
|
2020-06-12 18:58:38 +01:00
|
|
|
return Account{}, fmt.Errorf("account name already registered: %s", a.Name)
|
2020-05-31 19:15:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 18:38:18 +01:00
|
|
|
// Set the creation time
|
|
|
|
acc.Data["created"] = time.Now().String()
|
|
|
|
|
2020-07-07 22:20:23 +01:00
|
|
|
// Create a secret
|
|
|
|
acc.Data["secret"] = uuid.New().String()
|
|
|
|
|
2020-06-02 17:44:39 +01:00
|
|
|
// Simply add the account to the map
|
2020-06-10 18:56:24 +01:00
|
|
|
a.Accounts[acc.Name] = acc
|
2020-05-31 19:15:57 +01:00
|
|
|
|
2020-06-02 17:59:33 +01:00
|
|
|
return
|
2020-05-31 19:15:57 +01:00
|
|
|
}
|
|
|
|
|
2020-07-07 22:20:23 +01:00
|
|
|
// VerifySecret verifies if an account secret is correct
|
|
|
|
func (a *Accountant) 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)
|
|
|
|
}
|
|
|
|
|
2020-06-30 23:59:58 +01:00
|
|
|
// AssignData assigns data to an account
|
2020-06-10 22:48:45 +01:00
|
|
|
func (a *Accountant) AssignData(account string, key string, value string) error {
|
2020-06-02 17:44:39 +01:00
|
|
|
|
|
|
|
// Find the account matching the ID
|
2020-06-02 17:51:54 +01:00
|
|
|
if this, ok := a.Accounts[account]; ok {
|
2020-06-10 22:48:45 +01:00
|
|
|
this.Data[key] = value
|
2020-06-02 17:51:54 +01:00
|
|
|
a.Accounts[account] = this
|
2020-06-02 17:44:39 +01:00
|
|
|
} else {
|
|
|
|
return fmt.Errorf("no account found for id: %s", account)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-06-03 18:40:19 +01:00
|
|
|
|
2020-06-30 23:59:58 +01:00
|
|
|
// GetValue gets the rover rover for the account
|
2020-06-10 23:23:09 +01:00
|
|
|
func (a *Accountant) GetValue(account string, key string) (string, error) {
|
2020-06-03 18:40:19 +01:00
|
|
|
// Find the account matching the ID
|
2020-06-30 23:59:58 +01:00
|
|
|
this, ok := a.Accounts[account]
|
|
|
|
if !ok {
|
2020-06-10 22:48:45 +01:00
|
|
|
return "", fmt.Errorf("no account found for id: %s", account)
|
2020-06-03 18:40:19 +01:00
|
|
|
}
|
2020-06-30 23:59:58 +01:00
|
|
|
return this.Data[key], nil
|
2020-06-03 18:40:19 +01:00
|
|
|
}
|