Debian Codegraph @

online-accounts-daemon/lib/LomiriOnlineAccountsDaemon/authenticator.cpp

raw symbols SCIP
1 /*
2 * This file is part of OnlineAccountsDaemon
3 *
4 * Copyright (C) 2015 Canonical Ltd.
5 *
6 * Contact: Alberto Mardegan <[email protected]>
7 *
8 * This program is free software: you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License version 3, as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranties of
14 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "authenticator.h"
22
23 #include <Accounts/AuthData>
24 #include <QDebug>
25 #include <SignOn/AuthSession>
26 #include <SignOn/Identity>
27 #include <SignOn/SessionData>
28 #include "dbus_constants.h"
29
30 using namespace OnlineAccountsDaemon;
31
32 namespace {
33
34 QVariantMap mergeMaps(const QVariantMap &map1,
35 const QVariantMap &map2)
36 {
37 if (map1.isEmpty()) return map2;
38 if (map2.isEmpty()) return map1;
39
40 QVariantMap map = map1;
41 //map2 values will overwrite map1 values for the same keys.
42 QMapIterator<QString, QVariant> it(map2);
43 while (it.hasNext()) {
44 it.next();
45 map.insert(it.key(), it.value());
46 }
47 return map;
48 }
49
50 } // namespace
51
52 namespace OnlineAccountsDaemon {
53
54 class AuthenticatorPrivate: public QObject
55 {
56 Q_OBJECT
57 Q_DECLARE_PUBLIC(Authenticator)
58
59 public:
60 AuthenticatorPrivate(Authenticator *q);
61
62 void authenticate(const Accounts::AuthData &authData,
63 const QVariantMap &parameters);
64 static QString signonErrorName(int type);
65
66 private Q_SLOTS:
67 void onAuthSessionResponse(const SignOn::SessionData &sessionData);
68 void onAuthSessionError(const SignOn::Error &error);
69
70 private:
71 SignOn::AuthSession *m_authSession;
72 SignOn::Identity *m_identity;
73 QVariantMap m_parameters;
74 int m_authMethod;
75 QVariantMap m_reply;
76 QVariantMap m_extraReplyData;
77 QString m_errorName;
78 QString m_errorMessage;
79 bool m_invalidateCache;
80 Authenticator *q_ptr;
81 };
82
83 } // namespace
84
85 AuthenticatorPrivate::AuthenticatorPrivate(Authenticator *q):
86 QObject(q),
87 m_authSession(0),
88 m_identity(0),
89 m_authMethod(ONLINE_ACCOUNTS_AUTH_METHOD_UNKNOWN),
90 m_invalidateCache(false),
91 q_ptr(q)
92 {
93 }
94
95 void AuthenticatorPrivate::authenticate(const Accounts::AuthData &authData,
96 const QVariantMap &parameters)
97 {
98 if (!m_identity) {
99 m_identity =
100 SignOn::Identity::existingIdentity(authData.credentialsId(), this);
101 }
102 if (!m_authSession) {
103 m_authSession = m_identity->createSession(authData.method());
104 QObject::connect(m_authSession,
105 SIGNAL(response(const SignOn::SessionData&)),
106 this,
107 SLOT(onAuthSessionResponse(const SignOn::SessionData&)));
108 QObject::connect(m_authSession, SIGNAL(error(const SignOn::Error&)),
109 this, SLOT(onAuthSessionError(const SignOn::Error&)));
110 }
111
112 QVariantMap allSessionData =
113 mergeMaps(authData.parameters(), mergeMaps(parameters, m_parameters));
114 QString mechanism = authData.mechanism();
115
116 m_authMethod = Authenticator::authMethod(authData);
117
118 if (m_invalidateCache) {
119 /* This works for OAuth 1.0 and 2.0; other authentication plugins should
120 * implement a similar flag. */
121 allSessionData["ForceTokenRefresh"] = true;
122 if (m_authMethod == ONLINE_ACCOUNTS_AUTH_METHOD_PASSWORD ||
123 m_authMethod == ONLINE_ACCOUNTS_AUTH_METHOD_SASL) {
124 uint uiPolicy = allSessionData.value("UiPolicy").toUInt();
125 if (uiPolicy != SignOn::NoUserInteractionPolicy) {
126 allSessionData["UiPolicy"] = SignOn::RequestPasswordPolicy;
127 }
128 }
129 }
130
131 m_extraReplyData.clear();
132 if (m_authMethod == ONLINE_ACCOUNTS_AUTH_METHOD_OAUTH1) {
133 /* For OAuth 1.0, let's return also the Consumer key and secret along
134 * with the reply. */
135 m_extraReplyData[ONLINE_ACCOUNTS_AUTH_KEY_CONSUMER_KEY] =
136 allSessionData.value("ConsumerKey");
137 m_extraReplyData[ONLINE_ACCOUNTS_AUTH_KEY_CONSUMER_SECRET] =
138 allSessionData.value("ConsumerSecret");
139 }
140
141 m_authSession->process(allSessionData, mechanism);
142 }
143
144 void AuthenticatorPrivate::onAuthSessionResponse(const SignOn::SessionData &sessionData)
145 {
146 Q_Q(Authenticator);
147 QVariantMap signonReply;
148
149 /* Perform some method-specific translation of reply keys */
150 if (m_authMethod == ONLINE_ACCOUNTS_AUTH_METHOD_PASSWORD) {
151 signonReply[ONLINE_ACCOUNTS_AUTH_KEY_USERNAME] = sessionData.UserName();
152 signonReply[ONLINE_ACCOUNTS_AUTH_KEY_PASSWORD] = sessionData.Secret();
153 } else {
154 signonReply = sessionData.toMap();
155 }
156
157 m_reply = mergeMaps(m_extraReplyData, signonReply);
158 Q_EMIT q->finished();
159 }
160
161 QString AuthenticatorPrivate::signonErrorName(int type)
162 {
163 #define HANDLE_CASE(name) \
164 case SignOn::Error::name: return ONLINE_ACCOUNTS_ERROR_PREFIX #name
165
166 switch (type) {
167 HANDLE_CASE(MechanismNotAvailable);
168 HANDLE_CASE(MissingData);
169 HANDLE_CASE(InvalidCredentials);
170 HANDLE_CASE(NotAuthorized);
171 HANDLE_CASE(WrongState);
172 HANDLE_CASE(OperationNotSupported);
173 HANDLE_CASE(NoConnection);
174 HANDLE_CASE(Network);
175 HANDLE_CASE(Ssl);
176 HANDLE_CASE(Runtime);
177 HANDLE_CASE(SessionCanceled);
178 HANDLE_CASE(TimedOut);
179 HANDLE_CASE(OperationFailed);
180 HANDLE_CASE(TOSNotAccepted);
181 HANDLE_CASE(ForgotPassword);
182 HANDLE_CASE(MethodOrMechanismNotAllowed);
183 HANDLE_CASE(IncorrectDate);
184 case SignOn::Error::UserInteraction:
185 return ONLINE_ACCOUNTS_ERROR_INTERACTION_REQUIRED;
186 default:
187 qWarning() << "Unhandled signond error code:" << type;
188 return ONLINE_ACCOUNTS_ERROR_PREFIX "UnknownError";
189 };
190 }
191
192 void AuthenticatorPrivate::onAuthSessionError(const SignOn::Error &error)
193 {
194 Q_Q(Authenticator);
195 m_errorName = signonErrorName(error.type());
196 m_errorMessage = error.message();
197 Q_EMIT q->finished();
198 }
199
200 Authenticator::Authenticator(QObject *parent):
201 QObject(parent),
202 d_ptr(new AuthenticatorPrivate(this))
203 {
204 }
205
206 Authenticator::~Authenticator()
207 {
208 delete d_ptr;
209 }
210
211 void Authenticator::setInteractive(bool interactive)
212 {
213 Q_D(Authenticator);
214 d->m_parameters["UiPolicy"] =
215 interactive ? SignOn::DefaultPolicy : SignOn::NoUserInteractionPolicy;
216 }
217
218 void Authenticator::invalidateCache()
219 {
220 Q_D(Authenticator);
221 d->m_invalidateCache = true;
222 }
223
224 void Authenticator::authenticate(const Accounts::AuthData &authData,
225 const QVariantMap &parameters)
226 {
227 Q_D(Authenticator);
228 d->authenticate(authData, parameters);
229 }
230
231 QVariantMap Authenticator::reply() const
232 {
233 Q_D(const Authenticator);
234 return d->m_reply;
235 }
236
237 QString Authenticator::errorName() const
238 {
239 Q_D(const Authenticator);
240 return d->m_errorName;
241 }
242
243 QString Authenticator::errorMessage() const
244 {
245 Q_D(const Authenticator);
246 return d->m_errorMessage;
247 }
248
249 int Authenticator::authMethod(const Accounts::AuthData &authData)
250 {
251 QString method = authData.method();
252 QString mechanism = authData.mechanism();
253 if (method == "oauth2") {
254 if (mechanism == "web_server" || mechanism == "user_agent") {
255 return ONLINE_ACCOUNTS_AUTH_METHOD_OAUTH2;
256 } else if (mechanism == "HMAC-SHA1" || mechanism == "PLAINTEXT") {
257 return ONLINE_ACCOUNTS_AUTH_METHOD_OAUTH1;
258 }
259 } else if (method == "sasl") {
260 return ONLINE_ACCOUNTS_AUTH_METHOD_SASL;
261 } else if (method == "password") {
262 return ONLINE_ACCOUNTS_AUTH_METHOD_PASSWORD;
263 }
264
265 return ONLINE_ACCOUNTS_AUTH_METHOD_UNKNOWN;
266 }
267
268 #include "authenticator.moc"