signon  8.40
signonidentityinfo.cpp
Go to the documentation of this file.
00001 /*
00002  * This file is part of signon
00003  *
00004  * Copyright (C) 2009-2010 Nokia Corporation.
00005  *
00006  * Contact: Aurel Popirtac <ext-aurel.popirtac@nokia.com>
00007  * Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
00008  *
00009  * This library is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Lesser General Public License
00011  * version 2.1 as published by the Free Software Foundation.
00012  *
00013  * This library is distributed in the hope that it will be useful, but
00014  * WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with this library; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00021  * 02110-1301 USA
00022  */
00023 #include "signonidentityinfo.h"
00024 
00025 #include <QBuffer>
00026 #include <QDataStream>
00027 #include <QDebug>
00028 
00029 namespace SignonDaemonNS {
00030 
00031 SignonIdentityInfo::SignonIdentityInfo():
00032     m_id(0),
00033     m_userName(QString()),
00034     m_password(QString()),
00035     m_storePassword(false),
00036     m_caption(QString()),
00037     m_methods(QMap<QString, QStringList>()),
00038     m_realms(QStringList()),
00039     m_accessControlList(QStringList()),
00040     m_ownerList(QStringList()),
00041     m_type(0),
00042     m_refCount(0),
00043     m_validated(false),
00044     m_isUserNameSecret(false)
00045 {
00046 }
00047 
00048 SignonIdentityInfo::SignonIdentityInfo(const QVariantMap &info):
00049     m_id(0),
00050     m_userName(QString()),
00051     m_password(QString()),
00052     m_storePassword(false),
00053     m_caption(QString()),
00054     m_methods(QMap<QString, QStringList>()),
00055     m_realms(QStringList()),
00056     m_accessControlList(QStringList()),
00057     m_ownerList(QStringList()),
00058     m_type(0),
00059     m_refCount(0),
00060     m_validated(false),
00061     m_isUserNameSecret(false)
00062 {
00063     m_id = info.value(SIGNOND_IDENTITY_INFO_ID).toInt();
00064     m_userName = info.value(SIGNOND_IDENTITY_INFO_USERNAME).toString();
00065     m_password = info.value(SIGNOND_IDENTITY_INFO_SECRET).toString();
00066     m_storePassword = info.value(SIGNOND_IDENTITY_INFO_STORESECRET).toBool();
00067     m_caption = info.value(SIGNOND_IDENTITY_INFO_CAPTION).toString();
00068     m_methods =
00069         info.value(SIGNOND_IDENTITY_INFO_AUTHMETHODS).value<MethodMap>();
00070 
00071     m_realms = info.value(SIGNOND_IDENTITY_INFO_REALMS).toStringList();
00072     m_accessControlList = info.value(SIGNOND_IDENTITY_INFO_ACL).toStringList();
00073     m_ownerList = info.value(SIGNOND_IDENTITY_INFO_OWNER).toStringList();
00074     m_type = info.value(SIGNOND_IDENTITY_INFO_TYPE).toInt();
00075     m_refCount = info.value(SIGNOND_IDENTITY_INFO_REFCOUNT).toInt();
00076     m_validated = info.value(SIGNOND_IDENTITY_INFO_VALIDATED).toBool();
00077 }
00078 
00079 SignonIdentityInfo::SignonIdentityInfo(const quint32 id,
00080                                        const QString &userName,
00081                                        const QString &password,
00082                                        const bool storePassword,
00083                                        const QString &caption,
00084                                        const MethodMap &methods,
00085                                        const QStringList &realms,
00086                                        const QStringList &accessControlList,
00087                                        const QStringList &ownerList,
00088                                        int type,
00089                                        int refCount,
00090                                        bool validated):
00091     m_id(id),
00092     m_userName(userName),
00093     m_password(password),
00094     m_storePassword(storePassword),
00095     m_caption(caption),
00096     m_methods(methods),
00097     m_realms(realms),
00098     m_accessControlList(accessControlList),
00099     m_ownerList(ownerList),
00100     m_type(type),
00101     m_refCount(refCount),
00102     m_validated(validated),
00103     m_isUserNameSecret(false)
00104 {
00105 }
00106 
00107 const QList<QVariant> SignonIdentityInfo::toVariantList()
00108 {
00109     QList<QVariant> list;
00110     list << m_id
00111          << m_userName
00112          << m_password
00113          << m_caption
00114          << m_realms
00115          << QVariant::fromValue(m_methods)
00116          << m_accessControlList
00117          << m_type
00118          << m_refCount
00119          << m_validated
00120          << m_isUserNameSecret;
00121 
00122     return list;
00123 }
00124 
00125 const QVariantMap SignonIdentityInfo::toMap() const
00126 {
00127     QVariantMap values;
00128     values.insert(SIGNOND_IDENTITY_INFO_ID, m_id);
00129     values.insert(SIGNOND_IDENTITY_INFO_USERNAME, m_userName);
00130     values.insert(SIGNOND_IDENTITY_INFO_SECRET, m_password);
00131     values.insert(SIGNOND_IDENTITY_INFO_CAPTION, m_caption);
00132     values.insert(SIGNOND_IDENTITY_INFO_REALMS, m_realms);
00133     values.insert(SIGNOND_IDENTITY_INFO_AUTHMETHODS,
00134                   QVariant::fromValue(m_methods));
00135     values.insert(SIGNOND_IDENTITY_INFO_ACL, m_accessControlList);
00136     values.insert(SIGNOND_IDENTITY_INFO_TYPE, m_type);
00137     values.insert(SIGNOND_IDENTITY_INFO_REFCOUNT, m_refCount);
00138     values.insert(SIGNOND_IDENTITY_INFO_VALIDATED, m_validated);
00139     values.insert(SIGNOND_IDENTITY_INFO_USERNAME_IS_SECRET,
00140                   m_isUserNameSecret);
00141     return values;
00142 }
00143 
00144 bool SignonIdentityInfo::operator==(const SignonIdentityInfo &other) const
00145 {
00146     //do not care about list element order
00147     SignonIdentityInfo me = *this;
00148     SignonIdentityInfo you = other;
00149     me.m_realms.sort();
00150     you.m_realms.sort();
00151     me.m_accessControlList.sort();
00152     you.m_accessControlList.sort();
00153     QMapIterator<QString, QStringList> it(me.m_methods);
00154     while (it.hasNext()) {
00155         it.next();
00156         QStringList list1 = it.value();
00157         QStringList list2 = you.m_methods.value(it.key());
00158         list1.sort();
00159         list2.sort();
00160         if (list1 != list2) return false;
00161     }
00162 
00163     return (m_id == other.m_id)
00164             && (m_userName == other.m_userName)
00165             && (m_password == other.m_password)
00166             && (m_caption == other.m_caption)
00167             && (me.m_realms ==you.m_realms)
00168             && (me.m_accessControlList == you.m_accessControlList)
00169             && (m_type == other.m_type)
00170             && (m_validated == other.m_validated);
00171 }
00172 
00173 bool SignonIdentityInfo::checkMethodAndMechanism(const QString &method,
00174                                                  const QString &mechanism,
00175                                                  QString &allowedMechanism)
00176 {
00177     // If no methods have been specified for an identity assume anything goes
00178     if (m_methods.isEmpty())
00179         return true;
00180 
00181     if (!m_methods.contains(method))
00182         return false;
00183 
00184     MechanismsList mechs = m_methods[method];
00185     // If no mechanisms have been specified for a method, assume anything goes
00186     if (mechs.isEmpty())
00187         return true;
00188 
00189     if (mechs.contains(mechanism)) {
00190         allowedMechanism = mechanism;
00191         return true;
00192     }
00193 
00194     /* in the case of SASL authentication (and possibly others),
00195      * mechanism can be a list of strings, separated by a space;
00196      * therefore, let's split the list first, and see if any of the
00197      * mechanisms is allowed.
00198      */
00199     QStringList mechanisms =
00200         mechanism.split(QLatin1Char(' '), QString::SkipEmptyParts);
00201 
00202     /* if the list is empty of it has only one element, then we already know
00203      * that it didn't pass the previous checks */
00204     if (mechanisms.size() <= 1)
00205         return false;
00206 
00207     QStringList allowedMechanisms;
00208     foreach (const QString &mech, mechanisms) {
00209         if (mechs.contains(mech))
00210             allowedMechanisms.append(mech);
00211     }
00212     if (allowedMechanisms.isEmpty())
00213         return false;
00214 
00215     allowedMechanism = allowedMechanisms.join(QLatin1String(" "));
00216     return true;
00217 }
00218 
00219 SignonIdentityInfo &
00220 SignonIdentityInfo::operator=(const SignonIdentityInfo &other)
00221 {
00222 
00223     m_id = other.m_id;
00224     m_userName = other.m_userName;
00225     m_password = other.m_password ;
00226     m_storePassword = other.m_storePassword;
00227     m_caption = other.m_caption;
00228     m_realms = other.m_realms;
00229     m_accessControlList = other.m_accessControlList;
00230     m_ownerList = other.m_ownerList;
00231     m_type = other.m_type;
00232     m_refCount = other.m_refCount;
00233     m_validated = other.m_validated;
00234     m_methods = other.m_methods;
00235     return *this;
00236 }
00237 
00238 } //namespace SignonDaemonNS