1 /* 2 * Copyright (C) 2013 Canonical Ltd. 3 * 4 * Contact: Alberto Mardegan <alberto.mardegan@canonical.com> 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU Lesser General Public License as published by 8 * the Free Software Foundation; version 2.1. 9 * 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 Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 */ 18 19 #ifndef MOCK_SIGNON_H 20 #define MOCK_SIGNON_H 21 22 #include <QObject> 23 #include <QPointer> 24 #include <QTimer> 25 #include <QVariantMap> 26 27 namespace SignOn { 28 29 class AuthSession; 30 typedef QPointer<AuthSession> AuthSessionP; 31 32 class Error 33 { 34 public: 35 enum ErrorType { 36 Unknown = 1, /**< Catch-all for errors not distinguished 37 by another code. */ 38 InternalServer = 2, /**< Signon Daemon internal error. */ 39 InternalCommunication = 3, /**< Communication with Signon Daemon 40 error. */ 41 PermissionDenied = 4, /**< The operation cannot be performed due to 42 insufficient client permissions. */ 43 EncryptionFailure, /**< Failure during data 44 encryption/decryption. */ 45 AuthServiceErr = 100, /* Placeholder to rearrange enumeration 46 - AuthService specific */ 47 MethodNotKnown, /**< The method with this name is not 48 found. */ 49 ServiceNotAvailable, /**< The service is temporarily 50 unavailable. */ 51 InvalidQuery, /**< Parameters for the query are invalid. */ 52 IdentityErr = 200, /* Placeholder to rearrange enumeration 53 - Identity specific */ 54 MethodNotAvailable, /**< The requested method is not available. */ 55 IdentityNotFound, /**< The identity matching this Identity 56 object was not found on the service. */ 57 StoreFailed, /**< Storing credentials failed. */ 58 RemoveFailed, /**< Removing credentials failed. */ 59 SignOutFailed, /**< SignOut failed. */ 60 IdentityOperationCanceled, /**< Identity operation was canceled by 61 user. */ 62 CredentialsNotAvailable, /**< Query failed. */ 63 ReferenceNotFound, /**< Trying to remove nonexistent 64 reference. */ 65 AuthSessionErr = 300, /* Placeholder to rearrange enumeration 66 - AuthSession/AuthPluginInterface 67 specific */ 68 MechanismNotAvailable, /**< The requested mechanism is not 69 available. */ 70 MissingData, /**< The SessionData object does not contain 71 necessary information. */ 72 InvalidCredentials, /**< The supplied credentials are invalid for 73 the mechanism implementation. */ 74 NotAuthorized, /**< Authorization failed. */ 75 WrongState, /**< An operation method has been called in 76 a wrong state. */ 77 OperationNotSupported, /**< The operation is not supported by the 78 mechanism implementation. */ 79 NoConnection, /**< No Network connetion. */ 80 Network, /**< Network connetion failed. */ 81 Ssl, /**< Ssl connection failed. */ 82 Runtime, /**< Casting SessionData into subclass 83 failed */ 84 SessionCanceled, /**< Challenge was cancelled. */ 85 TimedOut, /**< Challenge was timed out. */ 86 UserInteraction, /**< User interaction dialog failed */ 87 OperationFailed, /**< Temporary failure in authentication. */ 88 EncryptionFailed, /**< @deprecated Failure during data 89 encryption/decryption. */ 90 TOSNotAccepted, /**< User declined Terms of Service. */ 91 ForgotPassword, /**< User requested reset password 92 sequence. */ 93 MethodOrMechanismNotAllowed, /**< Method or mechanism not allowed for 94 this identity. */ 95 IncorrectDate, /**< Date time incorrect on device. */ 96 UserErr = 400 /* Placeholder to rearrange enumeration 97 - User space specific */ 98 }; 99 Error()100 Error() : m_type((int)Unknown), m_message(QString()) {} Error(const Error & src)101 Error(const Error &src) : 102 m_type(src.type()), m_message(src.message()) {} 103 Error(int type, const QString &message = QString()): m_type(type)104 m_type(type), m_message(message) {} 105 Error &operator=(const Error &src) 106 { m_type = src.type(); m_message = src.message(); return *this; } 107 ~Error()108 virtual ~Error() {} 109 setType(int type)110 void setType(int type) { m_type = type; } setMessage(const QString & message)111 void setMessage(const QString &message) { m_message = message; } type()112 int type() const { return m_type; } message()113 QString message() const { return m_message; } 114 115 private: 116 int m_type; 117 QString m_message; 118 }; 119 120 class SessionData 121 { 122 public: 123 SessionData(const QVariantMap &data = QVariantMap()) { m_data = data; } SessionData(const SessionData & other)124 SessionData(const SessionData &other) { m_data = other.m_data; } 125 SessionData &operator=(const SessionData &other) { 126 m_data = other.m_data; 127 return *this; 128 } 129 toMap()130 QVariantMap toMap() const { return m_data; } 131 132 protected: 133 QVariantMap m_data; 134 }; 135 136 typedef QString MethodName; 137 typedef QStringList MechanismsList; 138 class IdentityInfoImpl; 139 class IdentityInfo 140 { 141 public: 142 enum CredentialsType { 143 Other = 0, 144 Application = 1 << 0, 145 Web = 1 << 1, 146 Network = 1 << 2 147 }; 148 public: 149 IdentityInfo(); 150 IdentityInfo(const IdentityInfo &other); 151 IdentityInfo &operator=(const IdentityInfo &other); 152 ~IdentityInfo(); 153 154 void setId(const quint32 id); 155 quint32 id() const; 156 157 void setSecret(const QString &secret, const bool storeSecret = true); 158 QString secret() const; 159 160 bool isStoringSecret() const; 161 void setStoreSecret(const bool storeSecret); 162 163 void setUserName(const QString &userName); 164 const QString userName() const; 165 166 void setCaption(const QString &caption); 167 const QString caption() const; 168 169 void setRealms(const QStringList &realms); 170 QStringList realms() const; 171 172 void setOwner(const QString &ownerToken); 173 QString owner() const; 174 175 void setAccessControlList(const QStringList &accessControlList); 176 QStringList accessControlList() const; 177 178 void setMethod(const MethodName &method, 179 const MechanismsList &mechanismsList); 180 void removeMethod(const MethodName &method); 181 182 void setType(CredentialsType type); 183 CredentialsType type() const; 184 185 QList<MethodName> methods() const; 186 MechanismsList mechanisms(const MethodName &method) const; 187 188 private: 189 class IdentityInfoImpl *impl; 190 }; 191 192 class Identity: public QObject 193 { 194 Q_OBJECT 195 Q_DISABLE_COPY(Identity) 196 197 protected: 198 Identity(const quint32 id = 0, QObject *parent = 0); 199 200 public: 201 static Identity *newIdentity(const IdentityInfo &info = IdentityInfo(), 202 QObject *parent = 0); 203 static Identity *existingIdentity(const quint32 id, QObject *parent = 0); 204 virtual ~Identity(); 205 206 AuthSessionP createSession(const QString &methodName); 207 void storeCredentials(const IdentityInfo &info = IdentityInfo()); 208 void remove(); 209 void queryInfo(); 210 211 Q_SIGNALS: 212 void error(const SignOn::Error &err); 213 void credentialsStored(const quint32 id); 214 void info(const SignOn::IdentityInfo &info); 215 void removed(); 216 217 private Q_SLOTS: 218 void emitCredentialsStored(); 219 void emitInfo(); 220 void emitRemoved(); 221 222 private: 223 static quint32 lastId; 224 quint32 m_id; 225 IdentityInfo m_info; 226 QTimer m_storeTimer; 227 QTimer m_infoTimer; 228 QTimer m_removeTimer; 229 }; 230 231 class AuthSession: public QObject 232 { 233 Q_OBJECT 234 235 friend class Identity; 236 protected: 237 AuthSession(quint32 id, const QString &methodName, QObject *parent = 0); 238 ~AuthSession(); 239 240 public: 241 const QString name() const; 242 void process(const SessionData &sessionData, 243 const QString &mechanism = QString()); 244 void cancel(); 245 246 Q_SIGNALS: 247 void error(const SignOn::Error &err); 248 void response(const SignOn::SessionData &sessionData); 249 250 private Q_SLOTS: 251 void respond(); 252 253 private: 254 quint32 m_id; 255 QString m_method; 256 QString m_mechanism; 257 QVariantMap m_sessionData; 258 QTimer responseTimer; 259 }; 260 261 }; // namespace 262 263 Q_DECLARE_METATYPE(SignOn::Error) 264 Q_DECLARE_METATYPE(SignOn::IdentityInfo) 265 Q_DECLARE_METATYPE(SignOn::SessionData) 266 267 #endif // MOCK_SIGNON_H 268