rove/pkg/accounts/accounts.go
Marc Di Luzio 92222127a6 Add basic account security
This adds a secret token associated with each account

	The token must then be sent with follow-up requests to ensure they get accepted

	This is _very_ basic security, and without TLS is completely vulnerable to MITM attacks, as well as brute force guessing (though it'd take a while to guess the a correct UUID)
2020-07-07 22:20:23 +01:00

89 lines
2.1 KiB
Go

package accounts
import (
"fmt"
"time"
"github.com/google/uuid"
)
// 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"`
}
// Accountant manages a set of accounts
type Accountant struct {
Accounts map[string]Account `json:"accounts"`
}
// NewAccountant creates a new accountant
func NewAccountant() *Accountant {
return &Accountant{
Accounts: make(map[string]Account),
}
}
// RegisterAccount adds an account to the set of internal accounts
func (a *Accountant) 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 *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)
}
// AssignData assigns data to an account
func (a *Accountant) 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 *Accountant) 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
}