2
# Miro - an RSS based video player application
3
# Copyright (C) 2005-2010 Participatory Culture Foundation
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
# In addition, as a special exception, the copyright holders give
20
# permission to link the code of portions of this program with the OpenSSL
23
# You must obey the GNU General Public License in all respects for all of
24
# the code used other than OpenSSL. If you modify file(s) with this
25
# exception, you may extend this exception to your version of the file(s),
26
# but you are not obligated to do so. If you do not wish to do so, delete
27
# this exception statement from your version. If you delete this exception
28
# statement from all source files in the program, then also delete it here.
31
#include <Security/Security.h>
34
#define RETURN_NONE_IF(e) if ((e)) return Py_None
36
static PyObject* keychain_getAuthInfo(PyObject* self, PyObject* args)
38
char* serviceName = NULL;
39
int serviceNameLength = 0;
40
int ok = PyArg_ParseTuple(args, "s#", &serviceName, &serviceNameLength);
45
SecKeychainSearchRef search = NULL;
46
SecKeychainAttribute searchAttr[] = { { kSecServerItemAttr, serviceNameLength, serviceName } };
47
SecKeychainAttributeList searchAttrList = { 1, searchAttr };
49
err = SecKeychainSearchCreateFromAttributes( NULL, kSecInternetPasswordItemClass, &searchAttrList, &search);
50
RETURN_NONE_IF(err != noErr);
52
SecKeychainItemRef item;
53
err = SecKeychainSearchCopyNext(search, &item);
54
RETURN_NONE_IF(err != noErr);
56
SecKeychainAttributeList* attrList = NULL;
57
void* passwordData = NULL;
58
UInt32 passwordLength = 0U;
59
UInt32 tags[] = { kSecAccountItemAttr };
60
UInt32 formats[] = { CSSM_DB_ATTRIBUTE_FORMAT_STRING };
61
SecKeychainAttributeInfo info = { 1, tags, formats };
63
err = SecKeychainItemCopyAttributesAndData( item, &info, NULL, &attrList, &passwordLength, &passwordData);
64
RETURN_NONE_IF(err != noErr);
66
PyObject* username = PyString_FromStringAndSize(attrList->attr[0].data, attrList->attr[0].length);
67
PyObject* password = PyString_FromStringAndSize(passwordData, passwordLength);
68
PyObject* authInfo = PyDict_New();
70
PyDict_SetItemString(authInfo, "username", username);
71
PyDict_SetItemString(authInfo, "password", password);
73
SecKeychainItemFreeAttributesAndData(attrList, passwordData);
81
static PyMethodDef KeychainMethods[] =
83
{ "getAuthInfo", keychain_getAuthInfo, METH_VARARGS, "Returns authentication info for the passed domain." },
84
{ NULL, NULL, 0, NULL }
87
PyMODINIT_FUNC initkeychain(void)
89
Py_InitModule("keychain", KeychainMethods);