2021-07-30 17:46:38 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2021-12-27 13:17:15 +01:00
|
|
|
"github.com/rs/xid"
|
2021-08-01 19:06:01 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-01-24 11:21:30 +01:00
|
|
|
"github.com/wiretrustee/wiretrustee/management/server/idp"
|
2022-03-01 15:22:18 +01:00
|
|
|
"github.com/wiretrustee/wiretrustee/management/server/jwtclaims"
|
2021-10-31 12:06:44 +01:00
|
|
|
"github.com/wiretrustee/wiretrustee/util"
|
2021-07-30 17:46:38 +02:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2022-03-01 15:22:18 +01:00
|
|
|
"strings"
|
2021-07-30 17:46:38 +02:00
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2022-03-01 15:22:18 +01:00
|
|
|
const (
|
|
|
|
PublicCategory = "public"
|
|
|
|
PrivateCategory = "private"
|
|
|
|
UnknownCategory = "unknown"
|
|
|
|
)
|
|
|
|
|
2022-02-22 11:28:19 +01:00
|
|
|
type AccountManager interface {
|
|
|
|
GetOrCreateAccountByUser(userId, domain string) (*Account, error)
|
|
|
|
GetAccountByUser(userId string) (*Account, error)
|
|
|
|
AddSetupKey(accountId string, keyName string, keyType SetupKeyType, expiresIn *util.Duration) (*SetupKey, error)
|
|
|
|
RevokeSetupKey(accountId string, keyId string) (*SetupKey, error)
|
|
|
|
RenameSetupKey(accountId string, keyId string, newName string) (*SetupKey, error)
|
|
|
|
GetAccountById(accountId string) (*Account, error)
|
|
|
|
GetAccountByUserOrAccountId(userId, accountId, domain string) (*Account, error)
|
2022-03-01 15:22:18 +01:00
|
|
|
GetAccountWithAuthorizationClaims(claims jwtclaims.AuthorizationClaims) (*Account, error)
|
2022-02-22 11:28:19 +01:00
|
|
|
AccountExists(accountId string) (*bool, error)
|
|
|
|
AddAccount(accountId, userId, domain string) (*Account, error)
|
|
|
|
GetPeer(peerKey string) (*Peer, error)
|
|
|
|
MarkPeerConnected(peerKey string, connected bool) error
|
|
|
|
RenamePeer(accountId string, peerKey string, newName string) (*Peer, error)
|
|
|
|
DeletePeer(accountId string, peerKey string) (*Peer, error)
|
|
|
|
GetPeerByIP(accountId string, peerIP string) (*Peer, error)
|
|
|
|
GetNetworkMap(peerKey string) (*NetworkMap, error)
|
|
|
|
AddPeer(setupKey string, peer *Peer) (*Peer, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type DefaultAccountManager struct {
|
2021-07-30 17:46:38 +02:00
|
|
|
Store Store
|
|
|
|
// mutex to synchronise account operations (e.g. generating Peer IP address inside the Network)
|
2021-09-07 18:36:46 +02:00
|
|
|
mux sync.Mutex
|
|
|
|
peersUpdateManager *PeersUpdateManager
|
2022-01-24 11:21:30 +01:00
|
|
|
idpManager idp.Manager
|
2021-07-30 17:46:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Account represents a unique account of the system
|
|
|
|
type Account struct {
|
2021-12-27 13:17:15 +01:00
|
|
|
Id string
|
|
|
|
// User.Id it was created by
|
2022-03-01 15:22:18 +01:00
|
|
|
CreatedBy string
|
|
|
|
Domain string
|
|
|
|
DomainCategory string
|
|
|
|
IsDomainPrimaryAccount bool
|
|
|
|
SetupKeys map[string]*SetupKey
|
|
|
|
Network *Network
|
|
|
|
Peers map[string]*Peer
|
|
|
|
Users map[string]*User
|
2021-12-27 13:17:15 +01:00
|
|
|
}
|
|
|
|
|
2022-01-14 14:34:27 +01:00
|
|
|
// NewAccount creates a new Account with a generated ID and generated default setup keys
|
2022-02-11 17:18:18 +01:00
|
|
|
func NewAccount(userId, domain string) *Account {
|
2022-01-14 14:34:27 +01:00
|
|
|
accountId := xid.New().String()
|
2022-02-11 17:18:18 +01:00
|
|
|
return newAccountWithId(accountId, userId, domain)
|
2022-01-14 14:34:27 +01:00
|
|
|
}
|
|
|
|
|
2021-12-27 13:17:15 +01:00
|
|
|
func (a *Account) Copy() *Account {
|
|
|
|
peers := map[string]*Peer{}
|
|
|
|
for id, peer := range a.Peers {
|
|
|
|
peers[id] = peer.Copy()
|
|
|
|
}
|
|
|
|
|
|
|
|
users := map[string]*User{}
|
|
|
|
for id, user := range a.Users {
|
|
|
|
users[id] = user.Copy()
|
|
|
|
}
|
|
|
|
|
|
|
|
setupKeys := map[string]*SetupKey{}
|
|
|
|
for id, key := range a.SetupKeys {
|
|
|
|
setupKeys[id] = key.Copy()
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Account{
|
|
|
|
Id: a.Id,
|
|
|
|
CreatedBy: a.CreatedBy,
|
|
|
|
SetupKeys: setupKeys,
|
|
|
|
Network: a.Network.Copy(),
|
|
|
|
Peers: peers,
|
|
|
|
Users: users,
|
|
|
|
}
|
2021-07-30 17:46:38 +02:00
|
|
|
}
|
|
|
|
|
2022-02-22 11:28:19 +01:00
|
|
|
// NewManager creates a new DefaultAccountManager with a provided Store
|
|
|
|
func NewManager(store Store, peersUpdateManager *PeersUpdateManager, idpManager idp.Manager) *DefaultAccountManager {
|
|
|
|
return &DefaultAccountManager{
|
2021-09-07 18:36:46 +02:00
|
|
|
Store: store,
|
|
|
|
mux: sync.Mutex{},
|
|
|
|
peersUpdateManager: peersUpdateManager,
|
2022-01-24 11:21:30 +01:00
|
|
|
idpManager: idpManager,
|
2021-07-30 17:46:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-20 22:33:43 +02:00
|
|
|
//AddSetupKey generates a new setup key with a given name and type, and adds it to the specified account
|
2022-02-22 11:28:19 +01:00
|
|
|
func (am *DefaultAccountManager) AddSetupKey(accountId string, keyName string, keyType SetupKeyType, expiresIn *util.Duration) (*SetupKey, error) {
|
2021-09-07 18:36:46 +02:00
|
|
|
am.mux.Lock()
|
|
|
|
defer am.mux.Unlock()
|
2021-08-20 22:33:43 +02:00
|
|
|
|
2021-10-31 12:06:44 +01:00
|
|
|
keyDuration := DefaultSetupKeyDuration
|
|
|
|
if expiresIn != nil {
|
|
|
|
keyDuration = expiresIn.Duration
|
|
|
|
}
|
|
|
|
|
2021-09-07 18:36:46 +02:00
|
|
|
account, err := am.Store.GetAccount(accountId)
|
2021-08-20 22:33:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "account not found")
|
|
|
|
}
|
|
|
|
|
2021-10-31 12:06:44 +01:00
|
|
|
setupKey := GenerateSetupKey(keyName, keyType, keyDuration)
|
2021-08-20 22:33:43 +02:00
|
|
|
account.SetupKeys[setupKey.Key] = setupKey
|
|
|
|
|
2021-09-07 18:36:46 +02:00
|
|
|
err = am.Store.SaveAccount(account)
|
2021-08-20 22:33:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed adding account key")
|
|
|
|
}
|
|
|
|
|
|
|
|
return setupKey, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//RevokeSetupKey marks SetupKey as revoked - becomes not valid anymore
|
2022-02-22 11:28:19 +01:00
|
|
|
func (am *DefaultAccountManager) RevokeSetupKey(accountId string, keyId string) (*SetupKey, error) {
|
2021-09-07 18:36:46 +02:00
|
|
|
am.mux.Lock()
|
|
|
|
defer am.mux.Unlock()
|
2021-08-20 22:33:43 +02:00
|
|
|
|
2021-09-07 18:36:46 +02:00
|
|
|
account, err := am.Store.GetAccount(accountId)
|
2021-08-20 22:33:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "account not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
setupKey := getAccountSetupKeyById(account, keyId)
|
|
|
|
if setupKey == nil {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "unknown setupKey %s", keyId)
|
|
|
|
}
|
|
|
|
|
|
|
|
keyCopy := setupKey.Copy()
|
|
|
|
keyCopy.Revoked = true
|
|
|
|
account.SetupKeys[keyCopy.Key] = keyCopy
|
2021-09-07 18:36:46 +02:00
|
|
|
err = am.Store.SaveAccount(account)
|
2021-08-20 22:33:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed adding account key")
|
|
|
|
}
|
|
|
|
|
|
|
|
return keyCopy, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//RenameSetupKey renames existing setup key of the specified account.
|
2022-02-22 11:28:19 +01:00
|
|
|
func (am *DefaultAccountManager) RenameSetupKey(accountId string, keyId string, newName string) (*SetupKey, error) {
|
2021-09-07 18:36:46 +02:00
|
|
|
am.mux.Lock()
|
|
|
|
defer am.mux.Unlock()
|
2021-08-20 22:33:43 +02:00
|
|
|
|
2021-09-07 18:36:46 +02:00
|
|
|
account, err := am.Store.GetAccount(accountId)
|
2021-08-20 22:33:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "account not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
setupKey := getAccountSetupKeyById(account, keyId)
|
|
|
|
if setupKey == nil {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "unknown setupKey %s", keyId)
|
|
|
|
}
|
|
|
|
|
|
|
|
keyCopy := setupKey.Copy()
|
|
|
|
keyCopy.Name = newName
|
|
|
|
account.SetupKeys[keyCopy.Key] = keyCopy
|
2021-09-07 18:36:46 +02:00
|
|
|
err = am.Store.SaveAccount(account)
|
2021-08-20 22:33:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed adding account key")
|
|
|
|
}
|
|
|
|
|
|
|
|
return keyCopy, nil
|
|
|
|
}
|
|
|
|
|
2022-02-11 17:18:18 +01:00
|
|
|
//GetAccountById returns an existing account using its ID or error (NotFound) if doesn't exist
|
2022-02-22 11:28:19 +01:00
|
|
|
func (am *DefaultAccountManager) GetAccountById(accountId string) (*Account, error) {
|
2021-09-07 18:36:46 +02:00
|
|
|
am.mux.Lock()
|
|
|
|
defer am.mux.Unlock()
|
2021-08-12 12:49:10 +02:00
|
|
|
|
2021-09-07 18:36:46 +02:00
|
|
|
account, err := am.Store.GetAccount(accountId)
|
2021-08-12 12:49:10 +02:00
|
|
|
if err != nil {
|
2021-08-20 15:44:18 +02:00
|
|
|
return nil, status.Errorf(codes.NotFound, "account not found")
|
2021-08-12 12:49:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return account, nil
|
|
|
|
}
|
|
|
|
|
2022-01-24 11:21:30 +01:00
|
|
|
//GetAccountByUserOrAccountId look for an account by user or account Id, if no account is provided and
|
|
|
|
// user id doesn't have an account associated with it, one account is created
|
2022-02-22 11:28:19 +01:00
|
|
|
func (am *DefaultAccountManager) GetAccountByUserOrAccountId(userId, accountId, domain string) (*Account, error) {
|
2022-01-24 11:21:30 +01:00
|
|
|
|
|
|
|
if accountId != "" {
|
2022-02-11 17:18:18 +01:00
|
|
|
return am.GetAccountById(accountId)
|
2022-01-24 11:21:30 +01:00
|
|
|
} else if userId != "" {
|
2022-02-11 17:18:18 +01:00
|
|
|
account, err := am.GetOrCreateAccountByUser(userId, domain)
|
2022-01-24 11:21:30 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "account not found using user id: %s", userId)
|
|
|
|
}
|
2022-03-01 15:22:18 +01:00
|
|
|
err = am.updateIDPMetadata(userId, account.Id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-01-24 11:21:30 +01:00
|
|
|
}
|
|
|
|
return account, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, status.Errorf(codes.NotFound, "no valid user or account Id provided")
|
|
|
|
}
|
|
|
|
|
2022-03-01 15:22:18 +01:00
|
|
|
// updateIDPMetadata update user's app metadata in idp manager
|
|
|
|
func (am *DefaultAccountManager) updateIDPMetadata(userId, accountID string) error {
|
|
|
|
if am.idpManager != nil {
|
|
|
|
err := am.idpManager.UpdateUserAppMetadata(userId, idp.AppMetadata{WTAccountId: accountID})
|
|
|
|
if err != nil {
|
|
|
|
return status.Errorf(codes.Internal, "updating user's app metadata failed with: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// updateAccountDomainAttributes updates the account domain attributes and then, saves the account
|
|
|
|
func (am *DefaultAccountManager) updateAccountDomainAttributes(account *Account, claims jwtclaims.AuthorizationClaims, primaryDomain bool) error {
|
|
|
|
account.IsDomainPrimaryAccount = primaryDomain
|
|
|
|
account.Domain = strings.ToLower(claims.Domain)
|
|
|
|
account.DomainCategory = claims.DomainCategory
|
|
|
|
err := am.Store.SaveAccount(account)
|
|
|
|
if err != nil {
|
|
|
|
return status.Errorf(codes.Internal, "failed saving updated account")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleExistingUserAccount handles existing User accounts and update its domain attributes.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// If there is no primary domain account yet, we set the account as primary for the domain. Otherwise,
|
|
|
|
// we compare the account's ID with the domain account ID, and if they don't match, we set the account as
|
|
|
|
// non-primary account for the domain. We don't merge accounts at this stage, because of cases when a domain
|
|
|
|
// was previously unclassified or classified as public so N users that logged int that time, has they own account
|
|
|
|
// and peers that shouldn't be lost.
|
|
|
|
func (am *DefaultAccountManager) handleExistingUserAccount(existingAcc *Account, domainAcc *Account, claims jwtclaims.AuthorizationClaims) error {
|
|
|
|
var err error
|
|
|
|
|
2022-03-10 13:47:36 +01:00
|
|
|
if domainAcc != nil && existingAcc.Id != domainAcc.Id {
|
2022-03-01 15:22:18 +01:00
|
|
|
err = am.updateAccountDomainAttributes(existingAcc, claims, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-10 13:47:36 +01:00
|
|
|
} else {
|
|
|
|
err = am.updateAccountDomainAttributes(existingAcc, claims, true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-01 15:22:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// we should register the account ID to this user's metadata in our IDP manager
|
|
|
|
err = am.updateIDPMetadata(claims.UserId, existingAcc.Id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleNewUserAccount validates if there is an existing primary account for the domain, if so it adds the new user to that account,
|
|
|
|
// otherwise it will create a new account and make it primary account for the domain.
|
|
|
|
func (am *DefaultAccountManager) handleNewUserAccount(domainAcc *Account, claims jwtclaims.AuthorizationClaims) (*Account, error) {
|
|
|
|
var (
|
2022-03-10 13:47:36 +01:00
|
|
|
account *Account
|
|
|
|
err error
|
2022-03-01 15:22:18 +01:00
|
|
|
)
|
|
|
|
lowerDomain := strings.ToLower(claims.Domain)
|
|
|
|
// if domain already has a primary account, add regular user
|
|
|
|
if domainAcc != nil {
|
|
|
|
account = domainAcc
|
|
|
|
account.Users[claims.UserId] = NewRegularUser(claims.UserId)
|
|
|
|
} else {
|
|
|
|
account = NewAccount(claims.UserId, lowerDomain)
|
|
|
|
account.Users[claims.UserId] = NewAdminUser(claims.UserId)
|
2022-03-10 13:47:36 +01:00
|
|
|
err = am.updateAccountDomainAttributes(account, claims, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-03-01 15:22:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
err = am.updateIDPMetadata(claims.UserId, account.Id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return account, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAccountWithAuthorizationClaims retrievs an account using JWT Claims.
|
|
|
|
// if domain is of the PrivateCategory category, it will evaluate
|
|
|
|
// if account is new, existing or if there is another account with the same domain
|
|
|
|
//
|
|
|
|
// Use cases:
|
|
|
|
//
|
|
|
|
// New user + New account + New domain -> create account, user role = admin (if private domain, index domain)
|
|
|
|
//
|
|
|
|
// New user + New account + Existing Private Domain -> add user to the existing account, user role = regular (not admin)
|
|
|
|
//
|
|
|
|
// New user + New account + Existing Public Domain -> create account, user role = admin
|
|
|
|
//
|
|
|
|
// Existing user + Existing account + Existing Domain -> Nothing changes (if private, index domain)
|
|
|
|
//
|
|
|
|
// Existing user + Existing account + Existing Indexed Domain -> Nothing changes
|
|
|
|
//
|
|
|
|
// Existing user + Existing account + Existing domain reclassified Domain as private -> Nothing changes (index domain)
|
|
|
|
func (am *DefaultAccountManager) GetAccountWithAuthorizationClaims(claims jwtclaims.AuthorizationClaims) (*Account, error) {
|
|
|
|
// if Account ID is part of the claims
|
|
|
|
// it means that we've already classified the domain and user has an account
|
2022-03-09 13:31:42 +01:00
|
|
|
if claims.DomainCategory != PrivateCategory {
|
2022-03-01 15:22:18 +01:00
|
|
|
return am.GetAccountByUserOrAccountId(claims.UserId, claims.AccountId, claims.Domain)
|
2022-03-09 13:31:42 +01:00
|
|
|
} else if claims.AccountId != "" {
|
|
|
|
accountFromID, err := am.GetAccountByUserOrAccountId(claims.UserId, claims.AccountId, claims.Domain)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if accountFromID.DomainCategory == PrivateCategory || claims.DomainCategory != PrivateCategory {
|
|
|
|
return accountFromID, nil
|
|
|
|
}
|
2022-03-01 15:22:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
am.mux.Lock()
|
|
|
|
defer am.mux.Unlock()
|
|
|
|
|
|
|
|
// We checked if the domain has a primary account already
|
|
|
|
domainAccount, err := am.Store.GetAccountByPrivateDomain(claims.Domain)
|
|
|
|
accStatus, _ := status.FromError(err)
|
|
|
|
if accStatus.Code() != codes.OK && accStatus.Code() != codes.NotFound {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
account, err := am.Store.GetUserAccount(claims.UserId)
|
|
|
|
if err == nil {
|
|
|
|
err = am.handleExistingUserAccount(account, domainAccount, claims)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return account, nil
|
|
|
|
} else if s, ok := status.FromError(err); ok && s.Code() == codes.NotFound {
|
|
|
|
return am.handleNewUserAccount(domainAccount, claims)
|
|
|
|
} else {
|
|
|
|
// other error
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-12 12:49:10 +02:00
|
|
|
//AccountExists checks whether account exists (returns true) or not (returns false)
|
2022-02-22 11:28:19 +01:00
|
|
|
func (am *DefaultAccountManager) AccountExists(accountId string) (*bool, error) {
|
2021-09-07 18:36:46 +02:00
|
|
|
am.mux.Lock()
|
|
|
|
defer am.mux.Unlock()
|
2021-08-12 12:49:10 +02:00
|
|
|
|
|
|
|
var res bool
|
2021-09-07 18:36:46 +02:00
|
|
|
_, err := am.Store.GetAccount(accountId)
|
2021-08-12 12:49:10 +02:00
|
|
|
if err != nil {
|
|
|
|
if s, ok := status.FromError(err); ok && s.Code() == codes.NotFound {
|
|
|
|
res = false
|
|
|
|
return &res, nil
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res = true
|
|
|
|
return &res, nil
|
|
|
|
}
|
|
|
|
|
2021-12-27 13:17:15 +01:00
|
|
|
// AddAccount generates a new Account with a provided accountId and userId, saves to the Store
|
2022-02-22 11:28:19 +01:00
|
|
|
func (am *DefaultAccountManager) AddAccount(accountId, userId, domain string) (*Account, error) {
|
2021-08-12 12:49:10 +02:00
|
|
|
|
2021-09-07 18:36:46 +02:00
|
|
|
am.mux.Lock()
|
|
|
|
defer am.mux.Unlock()
|
2021-08-12 12:49:10 +02:00
|
|
|
|
2022-02-11 17:18:18 +01:00
|
|
|
return am.createAccount(accountId, userId, domain)
|
2021-08-12 12:49:10 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-02-22 11:28:19 +01:00
|
|
|
func (am *DefaultAccountManager) createAccount(accountId, userId, domain string) (*Account, error) {
|
2022-02-11 17:18:18 +01:00
|
|
|
account := newAccountWithId(accountId, userId, domain)
|
2021-08-12 12:49:10 +02:00
|
|
|
|
2021-09-07 18:36:46 +02:00
|
|
|
err := am.Store.SaveAccount(account)
|
2021-08-12 12:49:10 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed creating account")
|
|
|
|
}
|
|
|
|
|
|
|
|
return account, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// newAccountWithId creates a new Account with a default SetupKey (doesn't store in a Store) and provided id
|
2022-02-11 17:18:18 +01:00
|
|
|
func newAccountWithId(accountId, userId, domain string) *Account {
|
2021-08-01 19:06:01 +02:00
|
|
|
|
|
|
|
log.Debugf("creating new account")
|
|
|
|
|
|
|
|
setupKeys := make(map[string]*SetupKey)
|
2021-09-07 18:36:46 +02:00
|
|
|
defaultKey := GenerateDefaultSetupKey()
|
|
|
|
oneOffKey := GenerateSetupKey("One-off key", SetupKeyOneOff, DefaultSetupKeyDuration)
|
|
|
|
setupKeys[defaultKey.Key] = defaultKey
|
|
|
|
setupKeys[oneOffKey.Key] = oneOffKey
|
2022-01-14 14:34:27 +01:00
|
|
|
network := NewNetwork()
|
2021-08-01 19:06:01 +02:00
|
|
|
peers := make(map[string]*Peer)
|
2021-12-27 13:17:15 +01:00
|
|
|
users := make(map[string]*User)
|
2021-08-01 19:06:01 +02:00
|
|
|
|
2021-09-07 18:36:46 +02:00
|
|
|
log.Debugf("created new account %s with setup key %s", accountId, defaultKey.Key)
|
2021-08-01 19:06:01 +02:00
|
|
|
|
2022-02-11 17:18:18 +01:00
|
|
|
return &Account{
|
|
|
|
Id: accountId,
|
|
|
|
SetupKeys: setupKeys,
|
|
|
|
Network: network,
|
|
|
|
Peers: peers,
|
|
|
|
Users: users,
|
|
|
|
CreatedBy: userId,
|
|
|
|
Domain: domain,
|
|
|
|
}
|
2021-08-12 12:49:10 +02:00
|
|
|
}
|
2021-08-20 22:33:43 +02:00
|
|
|
|
|
|
|
func getAccountSetupKeyById(acc *Account, keyId string) *SetupKey {
|
|
|
|
for _, k := range acc.SetupKeys {
|
|
|
|
if keyId == k.Id {
|
|
|
|
return k
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAccountSetupKeyByKey(acc *Account, key string) *SetupKey {
|
|
|
|
for _, k := range acc.SetupKeys {
|
|
|
|
if key == k.Key {
|
|
|
|
return k
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|