-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathFxAContentViewController.swift
271 lines (226 loc) · 10.3 KB
/
FxAContentViewController.swift
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
268
269
270
271
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import Shared
import SnapKit
import UIKit
import WebKit
import SwiftyJSON
protocol FxAContentViewControllerDelegate: class {
func contentViewControllerDidSignIn(_ viewController: FxAContentViewController, withFlags: FxALoginFlags)
func contentViewControllerDidCancel(_ viewController: FxAContentViewController)
}
/**
* A controller that manages a single web view connected to the Firefox
* Accounts (Desktop) Sync postMessage interface.
*
* The postMessage interface is not really documented, but it is simple
* enough. I reverse engineered it from the Desktop Firefox code and the
* fxa-content-server git repository.
*/
class FxAContentViewController: SettingsContentViewController, WKScriptMessageHandler {
fileprivate enum RemoteCommand: String {
case canLinkAccount = "can_link_account"
case loaded = "loaded"
case login = "login"
case sessionStatus = "session_status"
case signOut = "sign_out"
}
weak var delegate: FxAContentViewControllerDelegate?
let profile: Profile
init(profile: Profile, fxaOptions: FxALaunchParams? = nil) {
self.profile = profile
super.init(backgroundColor: UIColor(red: 242 / 255.0, green: 242 / 255.0, blue: 242 / 255.0, alpha: 1.0), title: NSAttributedString(string: "Firefox Accounts"))
if AppConstants.MOZ_FXA_DEEP_LINK_FORM_FILL {
self.url = self.createFxAURLWith(fxaOptions, profile: profile)
} else {
self.url = profile.accountConfiguration.signInURL
}
NotificationCenter.default.addObserver(self, selector: #selector(userDidVerify), name: .FirefoxAccountVerified, object: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
if AppConstants.MOZ_SHOW_FXA_AVATAR {
profile.getAccount()?.updateProfile()
}
// If the FxAContentViewController was launched from a FxA deferred link
// onboarding might not have been shown. Check to see if it needs to be
// displayed and don't animate.
if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
appDelegate.browserViewController.presentIntroViewController(false, animated: false)
}
}
override func makeWebView() -> WKWebView {
// Handle messages from the content server (via our user script).
let contentController = WKUserContentController()
contentController.add(LeakAvoider(delegate: self), name: "accountsCommandHandler")
// Inject our user script after the page loads.
if let path = Bundle.main.path(forResource: "FxASignIn", ofType: "js") {
if let source = try? String(contentsOfFile: path, encoding: .utf8) {
let userScript = WKUserScript(source: source, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
contentController.addUserScript(userScript)
}
}
let config = WKWebViewConfiguration()
config.userContentController = contentController
let webView = WKWebView(
frame: CGRect(width: 1, height: 1),
configuration: config
)
webView.allowsLinkPreview = false
webView.navigationDelegate = self
webView.accessibilityLabel = NSLocalizedString("Web content", comment: "Accessibility label for the main web content view")
// Don't allow overscrolling.
webView.scrollView.bounces = false
return webView
}
// Send a message to the content server.
func injectData(_ type: String, content: [String: Any]) {
let data = [
"type": type,
"content": content,
] as [String: Any]
let json = JSON(data).stringValue() ?? ""
let script = "window.postMessage(\(json), '\(self.url.absoluteString)');"
webView.evaluateJavaScript(script, completionHandler: nil)
}
fileprivate func onCanLinkAccount(_ data: JSON) {
// // We need to confirm a relink - see shouldAllowRelink for more
// let ok = shouldAllowRelink(accountData.email);
let ok = true
injectData("message", content: ["status": "can_link_account", "data": ["ok": ok]])
}
// We're not signed in to a Firefox Account at this time, which we signal by returning an error.
fileprivate func onSessionStatus(_ data: JSON) {
injectData("message", content: ["status": "error"])
}
// We're not signed in to a Firefox Account at this time. We should never get a sign out message!
fileprivate func onSignOut(_ data: JSON) {
injectData("message", content: ["status": "error"])
}
// The user has signed in to a Firefox Account. We're done!
fileprivate func onLogin(_ data: JSON) {
injectData("message", content: ["status": "login"])
let app = UIApplication.shared
let helper = FxALoginHelper.sharedInstance
helper.delegate = self
helper.application(app, didReceiveAccountJSON: data)
if profile.hasAccount() {
LeanPlumClient.shared.set(attributes: [LPAttributeKey.signedInSync: true])
}
LeanPlumClient.shared.track(event: .signsInFxa)
}
@objc fileprivate func userDidVerify(_ notification: Notification) {
guard let account = profile.getAccount() else {
return
}
// We can't verify against the actionNeeded of the account,
// because of potential race conditions.
// However, we restrict visibility of this method, and make sure
// we only Notify via the FxALoginStateMachine.
let flags = FxALoginFlags(pushEnabled: account.pushRegistration != nil,
verified: true)
LeanPlumClient.shared.set(attributes: [LPAttributeKey.signedInSync: true])
DispatchQueue.main.async {
self.delegate?.contentViewControllerDidSignIn(self, withFlags: flags)
}
}
// The content server page is ready to be shown.
fileprivate func onLoaded() {
self.timer?.invalidate()
self.timer = nil
self.isLoaded = true
}
// Handle a message coming from the content server.
func handleRemoteCommand(_ rawValue: String, data: JSON) {
if let command = RemoteCommand(rawValue: rawValue) {
if !isLoaded && command != .loaded {
// Work around https://github.com/mozilla/fxa-content-server/issues/2137
onLoaded()
}
switch command {
case .loaded:
onLoaded()
case .login:
onLogin(data)
case .canLinkAccount:
onCanLinkAccount(data)
case .sessionStatus:
onSessionStatus(data)
case .signOut:
onSignOut(data)
}
}
}
// Dispatch webkit messages originating from our child webview.
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
// Make sure we're communicating with a trusted page. That is, ensure the origin of the
// message is the same as the origin of the URL we initially loaded in this web view.
// Note that this exploit wouldn't be possible if we were using WebChannels; see
// https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/WebChannel.jsm
let origin = message.frameInfo.securityOrigin
guard origin.`protocol` == url.scheme && origin.host == url.host && origin.port == (url.port ?? 0) else {
print("Ignoring message - \(origin) does not match expected origin: \(url.origin ?? "nil")")
return
}
if message.name == "accountsCommandHandler" {
let body = JSON(message.body)
let detail = body["detail"]
handleRemoteCommand(detail["command"].stringValue, data: detail["data"])
}
}
// Configure the FxA signin url based on any passed options.
public func createFxAURLWith(_ fxaOptions: FxALaunchParams?, profile: Profile) -> URL {
let profileUrl = profile.accountConfiguration.signInURL
guard let launchParams = fxaOptions else {
return profileUrl
}
// Only append `signin`, `entrypoint` and `utm_*` parameters. Note that you can't
// override the service and context params.
var params = launchParams.query
params.removeValue(forKey: "service")
params.removeValue(forKey: "context")
let queryURL = params.filter { $0.key == "signin" || $0.key == "entrypoint" || $0.key.range(of: "utm_") != nil }.map({
return "\($0.key)=\($0.value)"
}).joined(separator: "&")
return URL(string: "\(profileUrl)&\(queryURL)") ?? profileUrl
}
override func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
// Ignore for now.
}
override func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
// Ignore for now.
}
}
extension FxAContentViewController: FxAPushLoginDelegate {
func accountLoginDidSucceed(withFlags flags: FxALoginFlags) {
DispatchQueue.main.async {
self.delegate?.contentViewControllerDidSignIn(self, withFlags: flags)
}
}
func accountLoginDidFail() {
DispatchQueue.main.async {
self.delegate?.contentViewControllerDidCancel(self)
}
}
}
/*
LeakAvoider prevents leaks with WKUserContentController
http://stackoverflow.com/questions/26383031/wkwebview-causes-my-view-controller-to-leak
*/
class LeakAvoider: NSObject, WKScriptMessageHandler {
weak var delegate: WKScriptMessageHandler?
init(delegate: WKScriptMessageHandler) {
self.delegate = delegate
super.init()
}
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
self.delegate?.userContentController(userContentController, didReceive: message)
}
}