changeset 1815:9360b7767e42

keyring fix.
author Severin Gehwolf <sgehwolf@redhat.com>
date Tue, 29 Sep 2015 10:03:49 +0200
parents 27f294831a12
children e255ee1b2f90
files keyring/Makefile keyring/src/main/native/GnomeKeyringLibraryNative.c keyring/src/test/java/com/redhat/thermostat/utils/keyring/impl/KeyringImplTest.java
diffstat 3 files changed, 61 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/keyring/Makefile	Thu Sep 24 15:23:52 2015 +0200
+++ b/keyring/Makefile	Tue Sep 29 10:03:49 2015 +0200
@@ -14,8 +14,8 @@
 
 EXECUTABLE = libGnomeKeyringWrapper.so
 
-MYCFLAGS   += `pkg-config --cflags gnome-keyring-1`
-MYLDFLAGS  += `pkg-config --libs gnome-keyring-1`
+MYCFLAGS   += `pkg-config --cflags libsecret-1`
+MYLDFLAGS  += `pkg-config --libs libsecret-1`
 
 .PHONY:
 JNI_LIST = com.redhat.thermostat.utils.keyring.impl.KeyringImpl
--- a/keyring/src/main/native/GnomeKeyringLibraryNative.c	Thu Sep 24 15:23:52 2015 +0200
+++ b/keyring/src/main/native/GnomeKeyringLibraryNative.c	Tue Sep 29 10:03:49 2015 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright 2012-2014 Red Hat, Inc.
+ * Copyright 2012-2015 Red Hat, Inc.
  *
  * This file is part of Thermostat.
  *
@@ -38,18 +38,18 @@
 
 #include <jni.h>
 #include <glib.h>
-#include <gnome-keyring.h>
+#include <libsecret/secret.h>
 #include <stdlib.h>
 #include <string.h>
 
-GnomeKeyringPasswordSchema thermostat_schema = {
-    GNOME_KEYRING_ITEM_GENERIC_SECRET,
-    {
-        { "username", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
-        { "url", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
-        { NULL, 0 }
-    }
-};
+SecretSchema thermostat_schema = {
+        "com.redhat.thermostat.password", SECRET_SCHEMA_NONE,
+        {
+            { "username", SECRET_SCHEMA_ATTRIBUTE_STRING },
+            { "url", SECRET_SCHEMA_ATTRIBUTE_STRING },
+            { "NULL", 0 }
+        }
+    };
 
 static void init(void) {
     if (g_get_application_name() == NULL) {
@@ -104,7 +104,7 @@
 
     const char *description = (*env)->GetStringUTFChars(env, jdescription, NULL);
     if (description == NULL) {
-    	(*env)->ReleaseStringUTFChars(env, jurl, url);
+        (*env)->ReleaseStringUTFChars(env, jurl, url);
         (*env)->ReleaseStringUTFChars(env, juserName, userName);
         for (passIndex = 0; passIndex < (int) passwordLength; passIndex++) {
             gpassword[passIndex] = (gchar) '\0';
@@ -114,13 +114,21 @@
     }
 
     init();
-    GnomeKeyringResult res = gnome_keyring_store_password_sync(&thermostat_schema,
-                                                                GNOME_KEYRING_DEFAULT,
-                                                                description,
-                                                                gpassword,
-                                                                "username", userName,
-                                                                "url", url,
-                                                                NULL);
+    GError *error = NULL;
+    jboolean is_success = JNI_TRUE;
+    secret_password_store_sync(&thermostat_schema,
+                               SECRET_COLLECTION_DEFAULT,
+                               description,
+                               gpassword,
+                               NULL,
+                               &error,
+                               "username", userName,
+                               "url", url,
+                               NULL);
+    if (error != NULL) {
+        is_success = JNI_FALSE;
+        g_error_free (error);
+    }
     (*env)->ReleaseStringUTFChars(env, jurl, url);
     (*env)->ReleaseStringUTFChars(env, juserName, userName);
     for (passIndex = 0; passIndex < (int) passwordLength; passIndex++) {
@@ -129,31 +137,31 @@
     free(gpassword);
     (*env)->ReleaseStringUTFChars(env, jdescription, description);
 
-    return (res == GNOME_KEYRING_RESULT_OK) ? JNI_TRUE : JNI_FALSE;
+    return is_success;
 }
 
 JNIEXPORT jbyteArray JNICALL
 Java_com_redhat_thermostat_utils_keyring_impl_KeyringImpl_gnomeKeyringWrapperGetPasswordNative
   (JNIEnv *env, jclass GnomeKeyringLibraryNative, jstring jurl, jstring juserName)
 {
-	const char *url = (*env)->GetStringUTFChars(env, jurl, NULL);
+    const char *url = (*env)->GetStringUTFChars(env, jurl, NULL);
     if (url == NULL) {
-
         return NULL;
     }
 
     const char *userName = (*env)->GetStringUTFChars(env, juserName, NULL);
     if (userName == NULL) {
-    	(*env)->ReleaseStringUTFChars(env, jurl, url);
+        (*env)->ReleaseStringUTFChars(env, jurl, url);
         return NULL;
     }
 
     gchar *password = NULL;
-    GnomeKeyringResult res;
+    GError *error = NULL;
 
     init();
-    res = gnome_keyring_find_password_sync(&thermostat_schema,
-                                           &password,
+    password = secret_password_lookup_sync(&thermostat_schema,
+                                           NULL,
+                                           &error,
                                            "username", userName,
                                            "url", url,
                                            NULL);
@@ -161,15 +169,20 @@
     (*env)->ReleaseStringUTFChars(env, jurl, url);
     (*env)->ReleaseStringUTFChars(env, juserName, userName);
 
-    if (res == GNOME_KEYRING_RESULT_OK) {
+    if (error == NULL) {
+        // Password may be null if not found in secret store
+        if (password == NULL) {
+            return NULL;
+        }
         const jbyte *jbytePassword = (const jbyte *) password;
 
         jsize passwordLength = strlen(password);
         jbyteArray jpassword = (*env)->NewByteArray(env, passwordLength);
         (*env)->SetByteArrayRegion(env, jpassword, 0, passwordLength, jbytePassword);
-        gnome_keyring_free_password(password);
+        secret_password_free(password);
         return jpassword;
     } else {
+        g_error_free(error);
         return NULL;
     }
 }
@@ -189,14 +202,22 @@
     }
 
     init();
-    GnomeKeyringResult res = gnome_keyring_delete_password_sync(&thermostat_schema,
-                                                                "username", userName,
-                                                                "url", url,
-                                                                NULL);
+    GError *error = NULL;
+    jboolean is_success = JNI_TRUE;
+    secret_password_clear_sync(&thermostat_schema,
+                               NULL,
+                               &error,
+                               "username", userName,
+                               "url", url,
+                               NULL);
 
+    if (error != NULL) {
+        is_success = JNI_FALSE;
+        g_error_free (error);
+    }
     (*env)->ReleaseStringUTFChars(env, jurl, url);
     (*env)->ReleaseStringUTFChars(env, juserName, userName);
 
-    return (res == GNOME_KEYRING_RESULT_OK) ? JNI_TRUE : JNI_FALSE;
+    return is_success;
 }
 
--- a/keyring/src/test/java/com/redhat/thermostat/utils/keyring/impl/KeyringImplTest.java	Thu Sep 24 15:23:52 2015 +0200
+++ b/keyring/src/test/java/com/redhat/thermostat/utils/keyring/impl/KeyringImplTest.java	Tue Sep 29 10:03:49 2015 +0200
@@ -43,6 +43,12 @@
 import org.junit.Ignore;
 import org.junit.Test;
 
+/**
+ * This test needs property "com.redhat.thermostat.shared.loader.testNativesHome"
+ * to be set when run from an IDE. The property value should be the location of
+ * the native SO's.
+ *
+ */
 public class KeyringImplTest {
 
     @Ignore // FIXME Hangs waiting for user to unlock keyring.