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 {
return client.RemoveCredential(cred.CredentialType().Identifier(), index)
}
// RemoveAllCredentials removes all credentials.
func (client *Client) RemoveAllCredentials() error {
removed := map[irma.CredentialTypeIdentifier][]irma.TranslatedString{}
for _, attrlistlist := range client.attributes {
for _, attrs := range attrlistlist {
if attrs.CredentialType() != nil {
removed[attrs.CredentialType().Identifier()] = attrs.Strings()
}
}
}
client.attributes = map[irma.CredentialTypeIdentifier][]*irma.AttributeList{}
// Removes all attributes, signatures, logs and userdata
// Includes the user's secret key, keyshare servers and preferences/updates
// A fresh secret key is installed.
func (client *Client) RemoveStorage() error {
logentry := &LogEntry{
Type: ActionRemoval,
Time: irma.Timestamp(time.Now()),
Removed: removed,
}
// Remove data from memory
client.attributes = make(map[irma.CredentialTypeIdentifier][]*irma.AttributeList)
client.keyshareServers = make(map[irma.SchemeManagerIdentifier]*keyshareServer)
client.credentialsCache = make(map[irma.CredentialTypeIdentifier]map[int]*credential)
return client.storage.Transaction(func(tx *transaction) error {
if err := client.storage.TxDeleteAllAttributes(tx); err != nil {
return err
}
if err := client.storage.TxDeleteAllSignatures(tx); err != nil {
err := client.storage.Transaction(func(tx *transaction) error {
if err := client.storage.TxDeleteAll(tx); err != nil {
return err
}
return client.storage.TxAddLogEntry(tx, logentry)
return nil
})
// Client assumes there is always a secret key, so we have to load a new one
client.secretkey, err = client.storage.LoadSecretKey()
if err != nil {
return err
}
if client.Preferences, err = client.storage.LoadPreferences(); err != nil {
return err
}
client.applyPreferences()
return nil
}
// Attribute and credential getter methods
......
......@@ -65,6 +65,15 @@ func (s *storage) Close() error {
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 {
b, err := tx.CreateBucketIfNotExists([]byte(bucketName))
if err != nil {
......@@ -339,3 +348,27 @@ func (s *storage) LoadPreferences() (Preferences, error) {
_, err := s.load(userdataBucket, preferencesKey, &config)
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