Commit 6d1b121b authored by Leon's avatar Leon Committed by Sietse Ringers

add functionality to remove complete storage

parent 9a13f190
...@@ -331,33 +331,35 @@ func (client *Client) RemoveCredentialByHash(hash string) error { ...@@ -331,33 +331,35 @@ func (client *Client) RemoveCredentialByHash(hash string) error {
return client.RemoveCredential(cred.CredentialType().Identifier(), index) return client.RemoveCredential(cred.CredentialType().Identifier(), index)
} }
// RemoveAllCredentials removes all credentials. // Removes all attributes, signatures, logs and userdata
func (client *Client) RemoveAllCredentials() error { // Includes the user's secret key, keyshare servers and preferences/updates
removed := map[irma.CredentialTypeIdentifier][]irma.TranslatedString{} // A fresh secret key is installed.
for _, attrlistlist := range client.attributes { func (client *Client) RemoveStorage() error {
for _, attrs := range attrlistlist {
if attrs.CredentialType() != nil {
removed[attrs.CredentialType().Identifier()] = attrs.Strings()
}
}
}
client.attributes = map[irma.CredentialTypeIdentifier][]*irma.AttributeList{}
logentry := &LogEntry{ // Remove data from memory
Type: ActionRemoval, client.attributes = make(map[irma.CredentialTypeIdentifier][]*irma.AttributeList)
Time: irma.Timestamp(time.Now()), client.keyshareServers = make(map[irma.SchemeManagerIdentifier]*keyshareServer)
Removed: removed, client.credentialsCache = make(map[irma.CredentialTypeIdentifier]map[int]*credential)
err := client.storage.Transaction(func(tx *transaction) error {
if err := client.storage.TxDeleteAll(tx); err != nil {
return err
} }
return nil
})
return client.storage.Transaction(func(tx *transaction) error { // Client assumes there is always a secret key, so we have to load a new one
if err := client.storage.TxDeleteAllAttributes(tx); err != nil { client.secretkey, err = client.storage.LoadSecretKey()
if err != nil {
return err return err
} }
if err := client.storage.TxDeleteAllSignatures(tx); err != nil {
if client.Preferences, err = client.storage.LoadPreferences(); err != nil {
return err return err
} }
return client.storage.TxAddLogEntry(tx, logentry) client.applyPreferences()
})
return nil
} }
// Attribute and credential getter methods // Attribute and credential getter methods
......
...@@ -65,6 +65,15 @@ func (s *storage) Close() error { ...@@ -65,6 +65,15 @@ func (s *storage) Close() error {
return s.db.Close() return s.db.Close()
} }
func (s *storage) BucketExists(name []byte) bool {
return s.Transaction(func(tx *transaction) error {
if tx.Bucket(name) == nil {
return bbolt.ErrBucketNotFound
}
return nil
}) == nil
}
func (s *storage) txStore(tx *transaction, bucketName string, key string, value interface{}) error { func (s *storage) txStore(tx *transaction, bucketName string, key string, value interface{}) error {
b, err := tx.CreateBucketIfNotExists([]byte(bucketName)) b, err := tx.CreateBucketIfNotExists([]byte(bucketName))
if err != nil { if err != nil {
...@@ -339,3 +348,27 @@ func (s *storage) LoadPreferences() (Preferences, error) { ...@@ -339,3 +348,27 @@ func (s *storage) LoadPreferences() (Preferences, error) {
_, err := s.load(userdataBucket, preferencesKey, &config) _, err := s.load(userdataBucket, preferencesKey, &config)
return config, err return config, err
} }
func (s *storage) TxDeleteUserdata(tx *transaction) error {
return tx.DeleteBucket([]byte(userdataBucket))
}
func (s *storage) TxDeleteLogs(tx *transaction) error {
return tx.DeleteBucket([]byte(logsBucket))
}
func (s *storage) TxDeleteAll(tx *transaction) error {
if err := s.TxDeleteAllAttributes(tx); err != nil && err != bbolt.ErrBucketNotFound {
return err
}
if err := s.TxDeleteAllSignatures(tx); err != nil && err != bbolt.ErrBucketNotFound {
return err
}
if err := s.TxDeleteUserdata(tx); err != nil && err != bbolt.ErrBucketNotFound {
return err
}
if err := s.TxDeleteLogs(tx); err != nil && err != bbolt.ErrBucketNotFound {
return err
}
return nil
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment