Skip to content
Snippets Groups Projects
Select Git revision
  • 7b302f7812bc8378c76f775e1c0a0ad0151c55fe
  • release default protected
  • 11-22-implement-kv-interface-defined-in-collectiveversionedkvgo
  • hotfix/TestHostPool_UpdateNdf_AddFilter
  • XX-4719/announcementChannels
  • xx-4717/logLevel
  • jonah/noob-channel
  • master protected
  • XX-4707/tagDiskJson
  • xx-4698/notification-retry
  • hotfix/notifylockup
  • syncNodes
  • hotfix/localCB
  • XX-4677/NewChanManagerMobile
  • XX-4689/DmSync
  • duplicatePrefix
  • XX-4601/HavenInvites
  • finalizedUICallbacks
  • XX-4673/AdminKeySync
  • debugNotifID
  • anne/test
  • v4.7.5
  • v4.7.4
  • v4.7.3
  • v4.7.2
  • v4.7.1
  • v4.6.3
  • v4.6.1
  • v4.5.0
  • v4.4.4
  • v4.3.11
  • v4.3.8
  • v4.3.7
  • v4.3.6
  • v4.3.5
  • v4.2.0
  • v4.3.0
  • v4.3.4
  • v4.3.3
  • v4.3.2
  • v4.3.1
41 results

manager_test.go

Blame
  • remove.go 3.82 KiB
    ////////////////////////////////////////////////////////////////////////////////
    // Copyright © 2022 xx foundation                                             //
    //                                                                            //
    // Use of this source code is governed by a license that can be found in the  //
    // LICENSE file.                                                              //
    ////////////////////////////////////////////////////////////////////////////////
    
    package ud
    
    import (
    	"fmt"
    	"github.com/pkg/errors"
    	jww "github.com/spf13/jwalterweatherman"
    	"gitlab.com/elixxir/comms/mixmessages"
    	"gitlab.com/elixxir/crypto/factID"
    	"gitlab.com/elixxir/crypto/hash"
    	"gitlab.com/elixxir/primitives/fact"
    	"gitlab.com/xx_network/comms/connect"
    	"gitlab.com/xx_network/crypto/signature/rsa"
    	"gitlab.com/xx_network/primitives/id"
    )
    
    // RemoveFact removes a previously confirmed fact. This will fail
    // if the fact passed in is not UD service does not associate this
    // fact with this user.
    func (m *Manager) RemoveFact(f fact.Fact) error {
    	jww.INFO.Printf("ud.RemoveFact(%s)", f.Stringify())
    	m.factMux.Lock()
    	defer m.factMux.Unlock()
    	return m.removeFact(f, m.comms)
    }
    
    // removeFact is a helper function which contacts the UD service
    // to remove the association of a fact with a user.
    func (m *Manager) removeFact(f fact.Fact,
    	rFC removeFactComms) error {
    
    	// Construct the message to send
    	// Convert our Fact to a mixmessages Fact for sending
    	mmFact := mixmessages.Fact{
    		Fact:     f.Fact,
    		FactType: uint32(f.T),
    	}
    
    	// Create a hash of our fact
    	fHash := factID.Fingerprint(f)
    
    	// Sign our inFact for putting into the request
    	identity := m.user.GetReceptionIdentity()
    	privKey, err := identity.GetRSAPrivateKey()
    	if err != nil {
    		return err
    	}
    	stream := m.getRng().GetStream()
    	defer stream.Close()
    	fSig, err := rsa.Sign(stream, privKey, hash.CMixHash, fHash, nil)
    	if err != nil {
    		return err
    	}
    
    	// Create our Fact Removal Request message data
    	remFactMsg := mixmessages.FactRemovalRequest{
    		UID:         identity.ID.Marshal(),
    		RemovalData: &mmFact,
    		FactSig:     fSig,
    	}
    
    	// Send the message
    	_, err = rFC.SendRemoveFact(m.ud.host, &remFactMsg)
    	if err != nil {
    		return err
    	}
    
    	// Remove from storage
    	return m.store.DeleteFact(f)
    }
    
    // PermanentDeleteAccount removes the username associated with this user
    // from the UD service. This will only take a username type fact,
    // and the fact must be associated with this user.
    func (m *Manager) PermanentDeleteAccount(f fact.Fact) error {
    	jww.INFO.Printf("ud.PermanentDeleteAccount(%s)", f.Stringify())
    	if f.T != fact.Username {
    		return errors.New(fmt.Sprintf("PermanentDeleteAccount must only remove "+
    			"a username. Cannot remove fact %q", f.Fact))
    	}
    	identity := m.user.GetReceptionIdentity()
    	privKey, err := identity.GetRSAPrivateKey()
    	if err != nil {
    		return err
    	}
    
    	return m.permanentDeleteAccount(f, identity.ID, privKey, m.comms, m.ud.host)
    }
    
    // permanentDeleteAccount is a helper function for PermanentDeleteAccount.
    func (m *Manager) permanentDeleteAccount(f fact.Fact, myId *id.ID, privateKey *rsa.PrivateKey,
    	rFC removeUserComms, udHost *connect.Host) error {
    
    	// Construct the message to send
    	// Convert our Fact to a mixmessages Fact for sending
    	mmFact := mixmessages.Fact{
    		Fact:     f.Fact,
    		FactType: uint32(f.T),
    	}
    
    	// Create a hash of our fact
    	fHash := factID.Fingerprint(f)
    
    	// Sign our inFact for putting into the request
    	stream := m.getRng().GetStream()
    	defer stream.Close()
    	fsig, err := rsa.Sign(stream, privateKey, hash.CMixHash, fHash, nil)
    	if err != nil {
    		return err
    	}
    
    	// Create our Fact Removal Request message data
    	remFactMsg := mixmessages.FactRemovalRequest{
    		UID:         myId.Marshal(),
    		RemovalData: &mmFact,
    		FactSig:     fsig,
    	}
    
    	// Send the message
    	_, err = rFC.SendRemoveUser(udHost, &remFactMsg)
    
    	// Return the error
    	return err
    }