Add management peer logout

This commit is contained in:
bcmmbaga 2023-12-07 20:03:53 +03:00
parent b03343bc4d
commit 97350ff7b7
No known key found for this signature in database
GPG Key ID: 7249A19D20613553
3 changed files with 35 additions and 0 deletions

View File

@ -112,6 +112,7 @@ type AccountManager interface {
GetPeer(accountID, peerID, userID string) (*nbpeer.Peer, error) GetPeer(accountID, peerID, userID string) (*nbpeer.Peer, error)
UpdateAccountSettings(accountID, userID string, newSettings *Settings) (*Account, error) UpdateAccountSettings(accountID, userID string, newSettings *Settings) (*Account, error)
LoginPeer(login PeerLogin) (*nbpeer.Peer, *NetworkMap, error) // used by peer gRPC API LoginPeer(login PeerLogin) (*nbpeer.Peer, *NetworkMap, error) // used by peer gRPC API
LogoutPeer(peerPubKey string) error // used by peer gRPC API
SyncPeer(sync PeerSync) (*nbpeer.Peer, *NetworkMap, error) // used by peer gRPC API SyncPeer(sync PeerSync) (*nbpeer.Peer, *NetworkMap, error) // used by peer gRPC API
GetAllConnectedPeers() (map[string]struct{}, error) GetAllConnectedPeers() (map[string]struct{}, error)
HasConnectedChannel(peerID string) bool HasConnectedChannel(peerID string) bool

View File

@ -78,6 +78,7 @@ type MockAccountManager struct {
GetPeerFunc func(accountID, peerID, userID string) (*nbpeer.Peer, error) GetPeerFunc func(accountID, peerID, userID string) (*nbpeer.Peer, error)
UpdateAccountSettingsFunc func(accountID, userID string, newSettings *server.Settings) (*server.Account, error) UpdateAccountSettingsFunc func(accountID, userID string, newSettings *server.Settings) (*server.Account, error)
LoginPeerFunc func(login server.PeerLogin) (*nbpeer.Peer, *server.NetworkMap, error) LoginPeerFunc func(login server.PeerLogin) (*nbpeer.Peer, *server.NetworkMap, error)
LogoutPeerFunc func(peerPubKey string) error
SyncPeerFunc func(sync server.PeerSync) (*nbpeer.Peer, *server.NetworkMap, error) SyncPeerFunc func(sync server.PeerSync) (*nbpeer.Peer, *server.NetworkMap, error)
InviteUserFunc func(accountID string, initiatorUserID string, targetUserEmail string) error InviteUserFunc func(accountID string, initiatorUserID string, targetUserEmail string) error
GetAllConnectedPeersFunc func() (map[string]struct{}, error) GetAllConnectedPeersFunc func() (map[string]struct{}, error)
@ -607,6 +608,14 @@ func (am *MockAccountManager) LoginPeer(login server.PeerLogin) (*nbpeer.Peer, *
return nil, nil, status.Errorf(codes.Unimplemented, "method LoginPeer is not implemented") return nil, nil, status.Errorf(codes.Unimplemented, "method LoginPeer is not implemented")
} }
// LogoutPeer mocks LogoutPeer of the AccountManager interface
func (am *MockAccountManager) LogoutPeer(peerPubKey string) error {
if am.LogoutPeerFunc != nil {
return am.LogoutPeerFunc(peerPubKey)
}
return status.Errorf(codes.Unimplemented, "method LogoutPeer is not implemented")
}
// SyncPeer mocks SyncPeer of the AccountManager interface // SyncPeer mocks SyncPeer of the AccountManager interface
func (am *MockAccountManager) SyncPeer(sync server.PeerSync) (*nbpeer.Peer, *server.NetworkMap, error) { func (am *MockAccountManager) SyncPeer(sync server.PeerSync) (*nbpeer.Peer, *server.NetworkMap, error) {
if am.SyncPeerFunc != nil { if am.SyncPeerFunc != nil {

View File

@ -576,6 +576,31 @@ func (am *DefaultAccountManager) LoginPeer(login PeerLogin) (*nbpeer.Peer, *Netw
return peer, account.GetPeerNetworkMap(peer.ID, am.dnsDomain), nil return peer, account.GetPeerNetworkMap(peer.ID, am.dnsDomain), nil
} }
// LogoutPeer logs out registered peer by delete it and send updates to the remote peers.
func (am *DefaultAccountManager) LogoutPeer(peerPubKey string) error {
account, err := am.Store.GetAccountByPeerPubKey(peerPubKey)
if err != nil {
if errStatus, ok := status.FromError(err); ok && errStatus.Type() == status.NotFound {
return status.Errorf(status.Unauthenticated, "peer is not registered")
}
log.Errorf("failed while logging out peer %s: %v", peerPubKey, err)
return status.Errorf(status.Internal, "failed while logging out peer")
}
peer, err := account.FindPeerByPubKey(peerPubKey)
if err != nil {
return status.Errorf(status.Unauthenticated, "peer is not registered")
}
// prevent logging out peer added by setup key
if peer.UserID == "" {
return status.Errorf(status.PermissionDenied, "peer was added by setup key")
}
return am.DeletePeer(account.Id, peer.ID, peer.UserID)
}
func checkIfPeerOwnerIsBlocked(peer *nbpeer.Peer, account *Account) error { func checkIfPeerOwnerIsBlocked(peer *nbpeer.Peer, account *Account) error {
if peer.AddedWithSSOLogin() { if peer.AddedWithSSOLogin() {
user, err := account.FindUser(peer.UserID) user, err := account.FindUser(peer.UserID)