|
| 1 | +/* |
| 2 | + * Copyright (c) 2023 Dennis Sädtler <[email protected]> |
| 3 | + * |
| 4 | + * Permission to use, copy, modify, and distribute this software for any |
| 5 | + * purpose with or without fee is hereby granted, provided that the above |
| 6 | + * copyright notice and this permission notice appear in all copies. |
| 7 | + * |
| 8 | + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 | + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 | + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 | + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 | + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 | + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 | + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 | + */ |
| 16 | + |
| 17 | +#include <gio/gio.h> |
| 18 | +#include <libsecret/secret.h> |
| 19 | + |
| 20 | +#include "platform.h" |
| 21 | +#include "bmem.h" |
| 22 | + |
| 23 | +static const SecretSchema obs_schema = { |
| 24 | + "com.obsproject.libobs.Secret", |
| 25 | + SECRET_SCHEMA_NONE, |
| 26 | + { |
| 27 | + {"key", SECRET_SCHEMA_ATTRIBUTE_STRING}, |
| 28 | + {"NULL", 0}, |
| 29 | + }}; |
| 30 | + |
| 31 | +bool os_keychain_available(void) |
| 32 | +{ |
| 33 | + return true; |
| 34 | +} |
| 35 | + |
| 36 | +bool os_keychain_save(const char *label, const char *key, const char *data) |
| 37 | +{ |
| 38 | + if (!label || !key || !data) |
| 39 | + return false; |
| 40 | + |
| 41 | + GError *error = NULL; |
| 42 | + secret_password_store_sync(&obs_schema, SECRET_COLLECTION_DEFAULT, |
| 43 | + label, data, NULL, &error, "key", key, NULL); |
| 44 | + if (error != NULL) { |
| 45 | + blog(LOG_ERROR, |
| 46 | + "Keychain item \"%s::%s\" could not be saved: %s", label, |
| 47 | + key, error->message); |
| 48 | + g_error_free(error); |
| 49 | + return false; |
| 50 | + } |
| 51 | + |
| 52 | + return true; |
| 53 | +} |
| 54 | + |
| 55 | +bool os_keychain_load(const char *label, const char *key, char **data) |
| 56 | +{ |
| 57 | + if (!label || !key || !data) |
| 58 | + return false; |
| 59 | + |
| 60 | + GError *error = NULL; |
| 61 | + gchar *password = secret_password_lookup_sync(&obs_schema, NULL, &error, |
| 62 | + "key", key, NULL); |
| 63 | + |
| 64 | + if (error != NULL) { |
| 65 | + blog(LOG_ERROR, |
| 66 | + "Keychain item \"%s::%s\" could not be read: %s", label, |
| 67 | + key, error->message); |
| 68 | + g_error_free(error); |
| 69 | + return false; |
| 70 | + } else if (password == NULL) { |
| 71 | + return false; |
| 72 | + } |
| 73 | + |
| 74 | + *data = bstrdup(password); |
| 75 | + secret_password_free(password); |
| 76 | + return true; |
| 77 | +} |
| 78 | + |
| 79 | +bool os_keychain_delete(const char *label, const char *key) |
| 80 | +{ |
| 81 | + if (!label || !key) |
| 82 | + return false; |
| 83 | + |
| 84 | + GError *error = NULL; |
| 85 | + gboolean removed = secret_password_clear_sync(&obs_schema, NULL, &error, |
| 86 | + "key", key, NULL); |
| 87 | + |
| 88 | + if (error != NULL) { |
| 89 | + if (error->code == SECRET_ERROR_NO_SUCH_OBJECT) { |
| 90 | + removed = true; |
| 91 | + } else { |
| 92 | + blog(LOG_ERROR, |
| 93 | + "Keychain item \"%s::%s\" could not be deleted: %s", |
| 94 | + label, key, error->message); |
| 95 | + } |
| 96 | + |
| 97 | + g_error_free(error); |
| 98 | + } |
| 99 | + |
| 100 | + return removed; |
| 101 | +} |
0 commit comments