File: signon.h

package info (click to toggle)
accounts-qml-module 0.7%2Bgit20231028.05e79eb-6
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 580 kB
  • sloc: cpp: 3,817; makefile: 35; sh: 5
file content (267 lines) | stat: -rw-r--r-- 9,452 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
 * Copyright (C) 2013 Canonical Ltd.
 *
 * Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; version 2.1.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef MOCK_SIGNON_H
#define MOCK_SIGNON_H

#include <QObject>
#include <QPointer>
#include <QTimer>
#include <QVariantMap>

namespace SignOn {

class AuthSession;
typedef QPointer<AuthSession> AuthSessionP;

class Error
{
public:
    enum ErrorType {
        Unknown = 1,               /**< Catch-all for errors not distinguished
                                        by another code. */
        InternalServer = 2,        /**< Signon Daemon internal error. */
        InternalCommunication = 3, /**< Communication with Signon Daemon
                                     error. */
        PermissionDenied = 4,      /**< The operation cannot be performed due to
                                        insufficient client permissions. */
        EncryptionFailure,         /**< Failure during data
                                     encryption/decryption. */
        AuthServiceErr = 100,           /* Placeholder to rearrange enumeration
                                         - AuthService specific */
        MethodNotKnown,            /**< The method with this name is not
                                     found. */
        ServiceNotAvailable,       /**< The service is temporarily
                                     unavailable. */
        InvalidQuery,              /**< Parameters for the query are invalid. */
        IdentityErr = 200,              /* Placeholder to rearrange enumeration
                                         - Identity specific */
        MethodNotAvailable,        /**< The requested method is not available. */
        IdentityNotFound,          /**< The identity matching this Identity
                                     object was not found on the service. */
        StoreFailed,               /**< Storing credentials failed. */
        RemoveFailed,              /**< Removing credentials failed. */
        SignOutFailed,             /**< SignOut failed. */
        IdentityOperationCanceled, /**< Identity operation was canceled by
                                     user. */
        CredentialsNotAvailable,   /**< Query failed. */
        ReferenceNotFound,         /**< Trying to remove nonexistent
                                     reference. */
        AuthSessionErr = 300,      /* Placeholder to rearrange enumeration
                                     - AuthSession/AuthPluginInterface
                                     specific */
        MechanismNotAvailable,     /**< The requested mechanism is not
                                     available. */
        MissingData,               /**< The SessionData object does not contain
                                        necessary information. */
        InvalidCredentials,        /**< The supplied credentials are invalid for
                                        the mechanism implementation. */
        NotAuthorized,             /**< Authorization failed. */
        WrongState,                /**< An operation method has been called in
                                        a wrong state. */
        OperationNotSupported,     /**< The operation is not supported by the
                                        mechanism implementation. */
        NoConnection,              /**< No Network connetion. */
        Network,                   /**< Network connetion failed. */
        Ssl,                       /**< Ssl connection failed. */
        Runtime,                   /**< Casting SessionData into subclass
                                     failed */
        SessionCanceled,           /**< Challenge was cancelled. */
        TimedOut,                  /**< Challenge was timed out. */
        UserInteraction,           /**< User interaction dialog failed */
        OperationFailed,           /**< Temporary failure in authentication. */
        EncryptionFailed,          /**< @deprecated Failure during data
                                     encryption/decryption. */
        TOSNotAccepted,            /**< User declined Terms of Service. */
        ForgotPassword,            /**< User requested reset password
                                     sequence. */
        MethodOrMechanismNotAllowed, /**< Method or mechanism not allowed for
                                       this identity. */
        IncorrectDate,             /**< Date time incorrect on device. */
        UserErr = 400                   /* Placeholder to rearrange enumeration
                                         - User space specific */
    };

    Error() : m_type((int)Unknown), m_message(QString()) {}
    Error(const Error &src) :
        m_type(src.type()), m_message(src.message()) {}
    Error(int type, const QString &message = QString()):
        m_type(type), m_message(message) {}
    Error &operator=(const Error &src)
        { m_type = src.type(); m_message = src.message(); return *this; }

    virtual ~Error() {}

    void setType(int type) { m_type = type; }
    void setMessage(const QString &message) { m_message = message; }
    int type() const { return m_type; }
    QString message() const { return m_message; }

private:
    int m_type;
    QString m_message;
};

class SessionData
{
public:
    SessionData(const QVariantMap &data = QVariantMap()) { m_data = data; }
    SessionData(const SessionData &other) { m_data = other.m_data; }
    SessionData &operator=(const SessionData &other) {
        m_data = other.m_data;
        return *this;
    }

    QVariantMap toMap() const { return m_data; }

protected:
    QVariantMap m_data;
};

typedef QString MethodName;
typedef QStringList MechanismsList;
class IdentityInfoImpl;
class IdentityInfo
{
public:
    enum CredentialsType {
        Other = 0,
        Application = 1 << 0,
        Web = 1 << 1,
        Network = 1 << 2
    };
public:
    IdentityInfo();
    IdentityInfo(const IdentityInfo &other);
    IdentityInfo &operator=(const IdentityInfo &other);
    ~IdentityInfo();

    void setId(const quint32 id);
    quint32 id() const;

    void setSecret(const QString &secret, const bool storeSecret = true);
    QString secret() const;

    bool isStoringSecret() const;
    void setStoreSecret(const bool storeSecret);

    void setUserName(const QString &userName);
    const QString userName() const;

    void setCaption(const QString &caption);
    const QString caption() const;

    void setRealms(const QStringList &realms);
    QStringList realms() const;

    void setOwner(const QString &ownerToken);
    QString owner() const;

    void setAccessControlList(const QStringList &accessControlList);
    QStringList accessControlList() const;

    void setMethod(const MethodName &method,
                   const MechanismsList &mechanismsList);
    void removeMethod(const MethodName &method);

    void setType(CredentialsType type);
    CredentialsType type() const;

    QList<MethodName> methods() const;
    MechanismsList mechanisms(const MethodName &method) const;

private:
    class IdentityInfoImpl *impl;
};

class Identity: public QObject
{
    Q_OBJECT
    Q_DISABLE_COPY(Identity)

protected:
    Identity(const quint32 id = 0, QObject *parent = 0);

public:
    static Identity *newIdentity(const IdentityInfo &info = IdentityInfo(),
                                 QObject *parent = 0);
    static Identity *existingIdentity(const quint32 id, QObject *parent = 0);
    virtual ~Identity();

    AuthSessionP createSession(const QString &methodName);
    void storeCredentials(const IdentityInfo &info = IdentityInfo());
    void remove();
    void queryInfo();

Q_SIGNALS:
    void error(const SignOn::Error &err);
    void credentialsStored(const quint32 id);
    void info(const SignOn::IdentityInfo &info);
    void removed();

private Q_SLOTS:
    void emitCredentialsStored();
    void emitInfo();
    void emitRemoved();

private:
    static quint32 lastId;
    quint32 m_id;
    IdentityInfo m_info;
    QTimer m_storeTimer;
    QTimer m_infoTimer;
    QTimer m_removeTimer;
};

class AuthSession: public QObject
{
    Q_OBJECT

    friend class Identity;
protected:
    AuthSession(quint32 id, const QString &methodName, QObject *parent = 0);
    ~AuthSession();

public:
    const QString name() const;
    void process(const SessionData &sessionData,
                 const QString &mechanism = QString());
    void cancel();

Q_SIGNALS:
    void error(const SignOn::Error &err);
    void response(const SignOn::SessionData &sessionData);

private Q_SLOTS:
    void respond();

private:
    quint32 m_id;
    QString m_method;
    QString m_mechanism;
    QVariantMap m_sessionData;
    QTimer responseTimer;
};

}; // namespace

Q_DECLARE_METATYPE(SignOn::Error)
Q_DECLARE_METATYPE(SignOn::IdentityInfo)
Q_DECLARE_METATYPE(SignOn::SessionData)

#endif // MOCK_SIGNON_H