~apachelogger/ubuntu-sso-client/gsoc

270 by Harald Sitter
No more linking against kwallet && gnomekeyring support && as a result ::clear() is broken && less kde in the upper levels (still we need quite some KDE foo for the build)
1
/*
2
  Copyright © 2010 Harald Sitter <apachelogger@ubuntu.com>
3
4
  This program is free software; you can redistribute it and/or
5
  modify it under the terms of the GNU General Public License as
6
  published by the Free Software Foundation; either version 2 of
7
  the License or (at your option) version 3 or any later version
8
  accepted by the membership of KDE e.V. (or its successor approved
9
  by the membership of KDE e.V.), which shall act as a proxy
10
  defined in Section 14 of version 3 of the license.
11
12
  This program is distributed in the hope that it will be useful,
13
  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
  GNU General Public License for more details.
16
17
  You should have received a copy of the GNU General Public License
18
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
*/
20
21
#include "GnomeKeyringSecretsInterface.h"
22
23
#include <QtCore/QtPlugin>
24
#include <QtCore/QByteArray>
25
26
#include <glib-2.0/glib.h>
27
#include <gnome-keyring-1/gnome-keyring.h>
28
29
QString GnomeKeyringSecretsInterface::getTokensFor(const QString &realm, const QString &consumer)
30
{
31
    GList *found_list = keyringItems(realm, consumer);
32
    if (found_list == NULL) {
33
        return QString();
34
    }
35
36
    GnomeKeyringFound *match = reinterpret_cast<GnomeKeyringFound*>(found_list->data);
37
    QString token(match->secret);
38
39
    gnome_keyring_found_list_free(found_list);
40
41
    return token;
42
}
43
44
bool GnomeKeyringSecretsInterface::storeToken(const QString &realm, const QString &consumer, const QString &token)
45
{
46
    char *crealm = qStringToChar(realm);
47
    char *cconsumer = qStringToChar(consumer);
48
    QString name = QString("UbuntuOne token for %1").arg(realm);
49
    char *cname = qStringToChar(name);
50
    char *ctoken = qStringToChar(token);
51
52
    GnomeKeyringAttributeList *attributes = gnome_keyring_attribute_list_new();
53
    gnome_keyring_attribute_list_append_string(attributes, "ubuntuone-realm", crealm);
54
    gnome_keyring_attribute_list_append_string(attributes, "oauth-consumer-key", cconsumer);
55
    guint32 itemId;
56
57
    delete []crealm;
58
    delete []cconsumer;
59
60
    bool success = true;
61
    GnomeKeyringResult result = gnome_keyring_item_create_sync(NULL, GNOME_KEYRING_ITEM_GENERIC_SECRET,
62
                                   cname, attributes, ctoken, TRUE, &itemId);
63
    if (result != GNOME_KEYRING_RESULT_OK) {
64
        success = false;
65
    }
66
67
    delete []cname;
68
    delete []ctoken;
69
    gnome_keyring_attribute_list_free(attributes);
70
71
    return success;
72
}
73
74
void GnomeKeyringSecretsInterface::clearToken(const QString &realm, const QString &consumer)
75
{
76
    GList *found_list = keyringItems(realm, consumer);
77
78
    GList *iter = NULL;
79
    GList *last = g_list_last(found_list);
80
    while (iter != last) {
81
        GnomeKeyringFound *item = reinterpret_cast<GnomeKeyringFound*>(found_list->data);
82
        gnome_keyring_item_delete_sync(NULL, item->item_id);
83
    }
84
85
    gnome_keyring_found_list_free(found_list);
86
}
87
88
char *GnomeKeyringSecretsInterface::qStringToChar(const QString &str)
89
{
90
    QByteArray b = str.toLatin1();
91
    char *c = new char[b.size()+1];
92
    strcpy(c, b.data());
93
    return c;
94
}
95
96
GList *GnomeKeyringSecretsInterface::keyringItems(const QString &realm, const QString &consumerKey)
97
{
98
    char *crealm = qStringToChar(realm);
99
    char *cconsumer = qStringToChar(consumerKey);
100
101
    GnomeKeyringAttributeList *attributes = gnome_keyring_attribute_list_new();
102
    gnome_keyring_attribute_list_append_string(attributes, "ubuntuone-realm", crealm);
103
    gnome_keyring_attribute_list_append_string(attributes, "oauth-consumer-key", cconsumer);
104
    GList *found = NULL;
105
106
    delete []crealm;
107
    delete []cconsumer;
108
109
    GnomeKeyringResult result = gnome_keyring_find_items_sync(GNOME_KEYRING_ITEM_GENERIC_SECRET, attributes, &found);
110
    if (result != GNOME_KEYRING_RESULT_OK) {
111
        gnome_keyring_attribute_list_free(attributes);
112
        return NULL;
113
    }
114
    gnome_keyring_attribute_list_free(attributes);
115
    return found;
116
}
117
118
Q_EXPORT_PLUGIN2(gnomekeyringplugin, GnomeKeyringSecretsInterface)