signon  8.42
signonidentityinfo.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of signon
3  *
4  * Copyright (C) 2009-2010 Nokia Corporation.
5  *
6  * Contact: Aurel Popirtac <ext-aurel.popirtac@nokia.com>
7  * Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * version 2.1 as published by the Free Software Foundation.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  */
23 #include "signonidentityinfo.h"
24 
25 #include <QBuffer>
26 #include <QDataStream>
27 #include <QDebug>
28 
29 namespace SignonDaemonNS {
30 
32  m_id(0),
33  m_userName(QString()),
34  m_password(QString()),
35  m_storePassword(false),
36  m_caption(QString()),
37  m_methods(QMap<QString, QStringList>()),
38  m_realms(QStringList()),
39  m_accessControlList(QStringList()),
40  m_ownerList(QStringList()),
41  m_type(0),
42  m_refCount(0),
43  m_validated(false),
44  m_isUserNameSecret(false)
45 {
46 }
47 
48 SignonIdentityInfo::SignonIdentityInfo(const QVariantMap &info):
49  m_id(0),
50  m_userName(QString()),
51  m_password(QString()),
52  m_storePassword(false),
53  m_caption(QString()),
54  m_methods(QMap<QString, QStringList>()),
55  m_realms(QStringList()),
56  m_accessControlList(QStringList()),
57  m_ownerList(QStringList()),
58  m_type(0),
59  m_refCount(0),
60  m_validated(false),
61  m_isUserNameSecret(false)
62 {
63  m_id = info.value(SIGNOND_IDENTITY_INFO_ID).toInt();
64  m_userName = info.value(SIGNOND_IDENTITY_INFO_USERNAME).toString();
65  m_password = info.value(SIGNOND_IDENTITY_INFO_SECRET).toString();
66  m_storePassword = info.value(SIGNOND_IDENTITY_INFO_STORESECRET).toBool();
67  m_caption = info.value(SIGNOND_IDENTITY_INFO_CAPTION).toString();
68  m_methods =
69  info.value(SIGNOND_IDENTITY_INFO_AUTHMETHODS).value<MethodMap>();
70 
71  m_realms = info.value(SIGNOND_IDENTITY_INFO_REALMS).toStringList();
72  m_accessControlList = info.value(SIGNOND_IDENTITY_INFO_ACL).toStringList();
73  m_ownerList = info.value(SIGNOND_IDENTITY_INFO_OWNER).toStringList();
74  m_type = info.value(SIGNOND_IDENTITY_INFO_TYPE).toInt();
75  m_refCount = info.value(SIGNOND_IDENTITY_INFO_REFCOUNT).toInt();
76  m_validated = info.value(SIGNOND_IDENTITY_INFO_VALIDATED).toBool();
77 }
78 
80  const QString &userName,
81  const QString &password,
82  const bool storePassword,
83  const QString &caption,
84  const MethodMap &methods,
85  const QStringList &realms,
86  const QStringList &accessControlList,
87  const QStringList &ownerList,
88  int type,
89  int refCount,
90  bool validated):
91  m_id(id),
92  m_userName(userName),
93  m_password(password),
94  m_storePassword(storePassword),
95  m_caption(caption),
96  m_methods(methods),
97  m_realms(realms),
98  m_accessControlList(accessControlList),
99  m_ownerList(ownerList),
100  m_type(type),
101  m_refCount(refCount),
102  m_validated(validated),
103  m_isUserNameSecret(false)
104 {
105 }
106 
107 const QList<QVariant> SignonIdentityInfo::toVariantList()
108 {
109  QList<QVariant> list;
110  list << m_id
111  << m_userName
112  << m_password
113  << m_caption
114  << m_realms
115  << QVariant::fromValue(m_methods)
116  << m_accessControlList
117  << m_type
118  << m_refCount
119  << m_validated
120  << m_isUserNameSecret;
121 
122  return list;
123 }
124 
125 const QVariantMap SignonIdentityInfo::toMap() const
126 {
127  QVariantMap values;
128  values.insert(SIGNOND_IDENTITY_INFO_ID, m_id);
129  values.insert(SIGNOND_IDENTITY_INFO_USERNAME, m_userName);
130  values.insert(SIGNOND_IDENTITY_INFO_SECRET, m_password);
131  values.insert(SIGNOND_IDENTITY_INFO_CAPTION, m_caption);
132  values.insert(SIGNOND_IDENTITY_INFO_REALMS, m_realms);
133  values.insert(SIGNOND_IDENTITY_INFO_AUTHMETHODS,
134  QVariant::fromValue(m_methods));
135  values.insert(SIGNOND_IDENTITY_INFO_ACL, m_accessControlList);
136  values.insert(SIGNOND_IDENTITY_INFO_TYPE, m_type);
137  values.insert(SIGNOND_IDENTITY_INFO_REFCOUNT, m_refCount);
138  values.insert(SIGNOND_IDENTITY_INFO_VALIDATED, m_validated);
139  values.insert(SIGNOND_IDENTITY_INFO_USERNAME_IS_SECRET,
140  m_isUserNameSecret);
141  return values;
142 }
143 
145 {
146  //do not care about list element order
147  SignonIdentityInfo me = *this;
148  SignonIdentityInfo you = other;
149  me.m_realms.sort();
150  you.m_realms.sort();
151  me.m_accessControlList.sort();
152  you.m_accessControlList.sort();
153  QMapIterator<QString, QStringList> it(me.m_methods);
154  while (it.hasNext()) {
155  it.next();
156  QStringList list1 = it.value();
157  QStringList list2 = you.m_methods.value(it.key());
158  list1.sort();
159  list2.sort();
160  if (list1 != list2) return false;
161  }
162 
163  return (m_id == other.m_id)
164  && (m_userName == other.m_userName)
165  && (m_password == other.m_password)
166  && (m_caption == other.m_caption)
167  && (me.m_realms ==you.m_realms)
168  && (me.m_accessControlList == you.m_accessControlList)
169  && (m_type == other.m_type)
170  && (m_validated == other.m_validated);
171 }
172 
174  const QString &mechanism,
175  QString &allowedMechanism)
176 {
177  // If no methods have been specified for an identity assume anything goes
178  if (m_methods.isEmpty())
179  return true;
180 
181  if (!m_methods.contains(method))
182  return false;
183 
184  MechanismsList mechs = m_methods[method];
185  // If no mechanisms have been specified for a method, assume anything goes
186  if (mechs.isEmpty())
187  return true;
188 
189  if (mechs.contains(mechanism)) {
190  allowedMechanism = mechanism;
191  return true;
192  }
193 
194  /* in the case of SASL authentication (and possibly others),
195  * mechanism can be a list of strings, separated by a space;
196  * therefore, let's split the list first, and see if any of the
197  * mechanisms is allowed.
198  */
199  QStringList mechanisms =
200  mechanism.split(QLatin1Char(' '), QString::SkipEmptyParts);
201 
202  /* if the list is empty of it has only one element, then we already know
203  * that it didn't pass the previous checks */
204  if (mechanisms.size() <= 1)
205  return false;
206 
207  QStringList allowedMechanisms;
208  foreach (const QString &mech, mechanisms) {
209  if (mechs.contains(mech))
210  allowedMechanisms.append(mech);
211  }
212  if (allowedMechanisms.isEmpty())
213  return false;
214 
215  allowedMechanism = allowedMechanisms.join(QLatin1String(" "));
216  return true;
217 }
218 
221 {
222 
223  m_id = other.m_id;
224  m_userName = other.m_userName;
225  m_password = other.m_password ;
226  m_storePassword = other.m_storePassword;
227  m_caption = other.m_caption;
228  m_realms = other.m_realms;
229  m_accessControlList = other.m_accessControlList;
230  m_ownerList = other.m_ownerList;
231  m_type = other.m_type;
232  m_refCount = other.m_refCount;
233  m_validated = other.m_validated;
234  m_methods = other.m_methods;
235  return *this;
236 }
237 
238 } //namespace SignonDaemonNS