@@ -67,6 +67,16 @@ void free_g_error(void *f, gError *err)
6767 fn(err);
6868}
6969
70+ // clear (delete) a secret. f must be a pointer to secret_password_clear_sync
71+ // https://gnome.pages.gitlab.gnome.org/libsecret/func.password_clear_sync.html
72+ int clear(void *f, schema *sch, void* cancellable, gError **err, char *key1, char *value1, char *key2, char *value2)
73+ {
74+ int (*fn)(schema *sch, void* cancellable, gError **err, char *key1, char *value1, char *key2, char *value2, ...);
75+ fn = (int (*)(schema *sch, void* cancellable, gError **err, char *key1, char *value1, char *key2, char *value2, ...))f;
76+ int r = fn(sch, cancellable, err, key1, value1, key2, value2, NULL);
77+ return r;
78+ }
79+
7080// lookup a password. f must be a pointer to secret_password_lookup_sync
7181// https://gnome.pages.gitlab.gnome.org/libsecret/func.password_lookup_sync.html
7282char *lookup(void *f, schema *sch, void* cancellable, gError **err, char *key1, char *value1, char *key2, char *value2)
@@ -137,8 +147,8 @@ type Storage struct {
137147 handle unsafe.Pointer
138148 // label of the secret schema
139149 label string
140- // freeError, lookup and store are the addresses of libsecret functions
141- freeError , lookup , store unsafe.Pointer
150+ // clear, freeError, lookup and store are the addresses of libsecret functions
151+ clear , freeError , lookup , store unsafe.Pointer
142152 // schema identifies the cached data in the secret service
143153 schema * C.schema
144154}
@@ -179,6 +189,10 @@ func New(name string, opts ...option) (*Storage, error) {
179189 }
180190 })
181191
192+ clear , err := s .symbol ("secret_password_clear_sync" )
193+ if err != nil {
194+ return nil , err
195+ }
182196 freeError , err := s .symbol ("g_error_free" )
183197 if err != nil {
184198 return nil , err
@@ -191,6 +205,7 @@ func New(name string, opts ...option) (*Storage, error) {
191205 if err != nil {
192206 return nil , err
193207 }
208+ s .clear = clear
194209 s .freeError = freeError
195210 s .lookup = lookup
196211 s .store = store
@@ -205,6 +220,27 @@ func New(name string, opts ...option) (*Storage, error) {
205220 return & s , nil
206221}
207222
223+ // Delete deletes the stored data, if any exists.
224+ func (s * Storage ) Delete (context.Context ) error {
225+ // the first nil terminates the list and libsecret ignores any extras
226+ attrs := []* C.char {nil , nil , nil , nil }
227+ for i , attr := range s .attributes {
228+ name := C .CString (attr .name )
229+ defer C .free (unsafe .Pointer (name ))
230+ value := C .CString (attr .value )
231+ defer C .free (unsafe .Pointer (value ))
232+ attrs [i * 2 ] = name
233+ attrs [(i * 2 )+ 1 ] = value
234+ }
235+ var e * C.gError
236+ _ = C .clear (s .clear , s .schema , nil , & e , attrs [0 ], attrs [1 ], attrs [2 ], attrs [3 ])
237+ if e != nil {
238+ defer C .free_g_error (s .freeError , e )
239+ return fmt .Errorf ("couldn't delete cache data: %q" , C .GoString (e .message ))
240+ }
241+ return nil
242+ }
243+
208244// Read returns data stored according to the secret schema or, if no such data exists, a nil slice and nil error.
209245func (s * Storage ) Read (context.Context ) ([]byte , error ) {
210246 // the first nil terminates the list and libsecret ignores any extras
@@ -274,3 +310,5 @@ func (s *Storage) symbol(name string) (unsafe.Pointer, error) {
274310 }
275311 return fp , nil
276312}
313+
314+ var _ Accessor = (* Storage )(nil )
0 commit comments