Debian Codegraph @

online-accounts-plugins/lib/module/qml6/OAuth.qml

raw
1 /*
2 * Copyright (C) 2013-2016 Canonical Ltd.
3 *
4 * Contact: Alberto Mardegan <[email protected]>
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 3, as published
8 * by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranties of
12 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
13 * PURPOSE. See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 import QtQuick 2.9
20 import Lomiri.Components
21 import Lomiri.Components.ListItems as ListItem
22 import Lomiri.Components.Popups
23 import SSO.OnlineAccounts 0.1
24 import Lomiri.OnlineAccounts.Plugin 1.0
25
26 Item {
27 id: root
28
29 /* To override the parameters coming from the .provider file: */
30 property var authenticationParameters: {}
31 /* To override the default access control list: */
32 property var accessControlList: ["unconfined"]
33 /* To override the request handler (useful for using an custom Web view or
34 * invoking the native browser): */
35 property var requestHandler: defaultRequestHandler
36
37 property var authReply
38 property bool isNewAccount: false
39 property var __account: account
40 property bool __isAuthenticating: false
41 property alias globalAccountService: globalAccountSettings
42 property bool loading: loader.status == Loader.Null || loader.status == Loader.Loading
43 property string userAgent
44
45 signal authenticated(var reply)
46 signal authenticationError(var error)
47 signal finished
48
49 anchors.fill: parent
50
51 Component.onCompleted: {
52 isNewAccount = (account.accountId === 0)
53 enableAccount()
54 if (requestHandler == defaultRequestHandler) {
55 authenticate()
56 }
57 }
58
59 RequestHandler {
60 id: defaultRequestHandler
61 onRequestChanged: {
62 if (request) {
63 console.log("RequestHandler captured request!")
64 loader.setSource(Qt.resolvedUrl("WebView.qml"), {
65 "signonRequest": request,
66 "userAgent": userAgent
67 })
68 } else {
69 console.log("Request destroyed!")
70 /* Destroying the WebView causes WebEngine to crash (2019-12-16)
71 * https://github.com/ubports/morph-browser/issues/271
72 */
73 //loader.source = ""
74 loader.item.visible = false
75 }
76 }
77 }
78
79 Credentials {
80 id: creds
81 caption: account.provider.id
82 acl: accessControlList
83 onCredentialsIdChanged: root.credentialsStored()
84 }
85
86 AccountService {
87 id: globalAccountSettings
88 objectHandle: account.accountServiceHandle
89 credentials: creds
90 autoSync: false
91
92 onAuthenticated: {
93 __isAuthenticating = false
94 authReply = reply
95 root.authenticated(reply)
96 }
97 onAuthenticationError: {
98 __isAuthenticating = false
99 root.authenticationError(error)
100 }
101 }
102
103 AccountServiceModel {
104 id: accountServices
105 includeDisabled: true
106 account: __account.objectHandle
107 }
108
109 ListItem.Base {
110 visible: loading && !errorItem.visible
111 height: units.gu(7)
112 showDivider: false
113 anchors.top: parent.top
114
115 Item {
116 height: units.gu(5)
117 width: units.gu(30)
118 anchors.horizontalCenter: parent.horizontalCenter
119 anchors.top: parent.top
120 anchors.margins: units.gu(1)
121
122 ActivityIndicator {
123 id: loadingIndicator
124 anchors.verticalCenter: parent.verticalCenter
125 anchors.left: parent.left
126 anchors.leftMargin: units.gu(5)
127 running: loading
128 z: 1
129 }
130 Label {
131 text: i18n.dtr("lomiri-online-accounts", "Loading…")
132 anchors.verticalCenter: parent.verticalCenter
133 anchors.left: loadingIndicator.right
134 anchors.leftMargin: units.gu(3)
135 }
136 }
137 }
138
139 Loader {
140 id: loader
141 anchors {
142 top: parent.top
143 left: parent.left
144 right: parent.right
145 bottom: Qt.inputMethod.visible ? osk.top : cancelButton.top
146 }
147 focus: true
148 visible: !loading
149 }
150
151 ErrorItem {
152 id: errorItem
153 anchors { fill: parent; margins: units.gu(4) }
154 visible: false
155 onRetryRequested: {
156 root.credentialsStored()
157 visible = false
158 }
159 }
160
161 KeyboardRectangle {
162 id: osk
163 }
164
165 ListItem.SingleControl {
166 id: cancelButton
167 anchors.bottom: parent.bottom
168 showDivider: false
169 control: Button {
170 text: i18n.dtr("lomiri-online-accounts", "Cancel")
171 width: parent.width - units.gu(4)
172 onClicked: root.cancel()
173 }
174 }
175
176 AccountServiceModel {
177 id: possiblyDuplicateAccounts
178 service: "global"
179 provider: __account.provider.id
180 }
181
182 function authenticate() {
183 console.log("Authenticating...")
184 creds.sync()
185 }
186
187 function credentialsStored() {
188 console.log("Credentials stored, id: " + creds.credentialsId)
189 if (creds.credentialsId == 0) return
190 var parameters = {}
191 parameters[requestHandler.matchKey] = requestHandler.matchId
192 parameters["providerId"] = account.provider.id
193 for (var p in authenticationParameters) {
194 parameters[p] = authenticationParameters[p]
195 }
196 __isAuthenticating = true
197 globalAccountSettings.authenticate(parameters)
198 }
199
200 function cancel() {
201 if (__isAuthenticating) {
202 /* This will cause the authentication to fail, and this method will
203 * be invoked again to delete the credentials. */
204 globalAccountSettings.cancelAuthentication()
205 return
206 }
207 if (isNewAccount && creds.credentialsId != 0) {
208 console.log("Removing credentials...")
209 creds.remove()
210 creds.removed.connect(finished)
211 } else {
212 finished()
213 }
214 }
215
216 function enableAccount() {
217 globalAccountSettings.updateServiceEnabled(true)
218 }
219
220 function getUserName(reply, callback) {
221 /* This should work for OAuth 1.0a; for OAuth 2.0 this function needs
222 * to be reimplemented */
223 if ('ScreenName' in reply) return reply.ScreenName
224 else if ('UserId' in reply) return reply.UserId
225 return ''
226 }
227
228 function accountIsDuplicate(userName) {
229 var model = possiblyDuplicateAccounts
230 for (var i = 0; i < model.count; i++) {
231 if (model.get(i, "displayName") == userName)
232 return true
233 }
234 return false
235 }
236
237 function __gotUserName(userName, reply) {
238 console.log("UserName: " + userName)
239 if (userName != '') {
240 if (accountIsDuplicate(userName)) {
241 var dialog = PopupUtils.open(Qt.resolvedUrl("DuplicateAccount.qml"))
242 dialog.closed.connect(cancel)
243 return
244 }
245 account.updateDisplayName(userName)
246 }
247 beforeSaving(reply)
248 }
249
250 function saveAccount() {
251 account.synced.connect(finished)
252 account.sync()
253 }
254
255 /* reimplement this function in plugins in order to perform some actions
256 * before quitting the plugin */
257 function beforeSaving(reply) {
258 saveAccount()
259 }
260
261 function __getUserNameAndSave(reply) {
262 /* If the completeCreation function is defined, run it */
263 if (typeof(completeCreation) == "function") {
264 console.warn("The completeCreation method is deprecated; use getUserName() or beforeSaving() instead")
265 completeCreation(reply)
266 return
267 }
268
269 var userName = getUserName(reply, function(name, error) {
270 if (error) {
271 console.warn("Error while getting username: " + error)
272 cancel()
273 } else {
274 __gotUserName(name, reply)
275 }
276 })
277 if (typeof(userName) == "string") {
278 __gotUserName(userName, reply)
279 } else if (userName === false) {
280 cancel()
281 return
282 }
283 // otherwise (userName === true), wait for the callback to be invoked
284 }
285
286 onAuthenticated: __getUserNameAndSave(reply)
287
288 onAuthenticationError: {
289 console.log("Authentication error, code " + error.code)
290 if (error.code == AccountService.NetworkError) {
291 console.log("Network error")
292 errorItem.visible = true
293 return
294 }
295 root.cancel()
296 }
297
298 onFinished: loading = false
299 }