1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-24 12:06:15 +01:00

Fix safari sso (#1508)

* Fix extension tab creation

TODO: still getting errors thrown by safariApp at
`(window as any).webkit.messageHandlers` upon loading the extension
window

* Support message sending from app extension context

* Load sso login in popover

* Handle nil urlComponents and nil queryItems
This commit is contained in:
Matt Gibson 2020-12-29 09:18:14 -06:00 committed by GitHub
parent 5941a4387d
commit a0a032957e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 13 deletions

View File

@ -25,12 +25,20 @@ export class SafariApp {
return new Promise((resolve) => {
const now = new Date();
const messageId = now.getTime().toString() + '_' + Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
(window as any).webkit.messageHandlers.bitwardenApp.postMessage(JSON.stringify({
id: messageId,
command: command,
data: data,
responseData: null,
}));
if (typeof safari === typeof undefined) {
(window as any).webkit.messageHandlers.bitwardenApp.postMessage(JSON.stringify({
id: messageId,
command: command,
data: data,
responseData: null,
}));
} else {
safari.extension.dispatchMessage('bitwarden', {
command: command,
data: data,
responseData: null,
});
}
if (resolveNow) {
resolve();
} else {

View File

@ -3,6 +3,15 @@ window.addEventListener('message', (event) => {
return;
if (event.data.command && (event.data.command === 'authResult')) {
if (typeof chrome === typeof undefined) {
safari.extension.dispatchMessage('bitwarden', {
command: event.data.command,
code: event.data.code,
state: event.data.state,
referrer: event.source.location.hostname,
});
return;
}
chrome.runtime.sendMessage({
command: event.data.command,
code: event.data.code,

View File

@ -41,11 +41,11 @@ export class SsoComponent extends BaseSsoComponent {
this.redirectUri = url + '/sso-connector.html';
this.clientId = 'browser';
super.onSuccessfulLogin = () => {
super.onSuccessfulLogin = async () => {
await syncService.fullSync(true);
BrowserApi.reloadOpenWindows();
const thisWindow = window.open('', '_self');
thisWindow.close();
return syncService.fullSync(true);
};
}
}

View File

@ -16,14 +16,10 @@ class SafariExtensionViewController: SFSafariExtensionViewController, WKScriptMe
if initedWebView {
return
}
let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String
initedWebView = true
let parentHeight = SafariExtensionViewController.shared.preferredContentSize.height
let parentWidth = SafariExtensionViewController.shared.preferredContentSize.width
let webViewConfig = WKWebViewConfiguration()
let bundleURL = Bundle.main.resourceURL!.absoluteURL
let html = bundleURL.appendingPathComponent("app/popup/index.html")
let url = URL(string: "\(html.absoluteString)?appVersion=\(version!)")
webViewConfig.preferences.setValue(true, forKey: "allowFileAccessFromFileURLs")
webViewConfig.preferences.setValue(true, forKey: "developerExtrasEnabled")
webViewConfig.userContentController.add(self, name: "bitwardenApp")
@ -31,12 +27,26 @@ class SafariExtensionViewController: SFSafariExtensionViewController, WKScriptMe
configuration: webViewConfig)
webView.navigationDelegate = self
webView.allowsLinkPreview = false
webView.loadFileURL(url!, allowingReadAccessTo: bundleURL)
navigateWebView("app/popup/index.html")
webView.alphaValue = 0.0
webView.uiDelegate = self
view.addSubview(webView)
}
func navigateWebView(_ relativeUrl: String){
let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String
let bundleUrl = Bundle.main.resourceURL!.absoluteURL
if var urlComponents = URLComponents(string: bundleUrl.absoluteString + relativeUrl) {
if (urlComponents.queryItems?.first(where: { $0.name == "appVersion" })?.value == nil) {
urlComponents.queryItems = urlComponents.queryItems ?? []
urlComponents.queryItems!.append(URLQueryItem(name: "appVersion", value: version))
}
webView.loadFileURL(urlComponents.url!, allowingReadAccessTo: bundleUrl)
}
}
func webView(_ webView: WKWebView, didFinish _: WKNavigation!) {
if #available(OSXApplicationExtension 10.12, *) {
NSAnimationContext.runAnimationGroup({ _ in
@ -179,6 +189,14 @@ class SafariExtensionViewController: SFSafariExtensionViewController, WKScriptMe
replyMessage(message: m)
} else if command == "createNewTab" {
if let data = m.data, let url = URL(string: data) {
if !data.starts(with: "https://") && !data.starts(with: "http://") {
SFSafariApplication.getActiveWindow { win in
win?.getToolbarItem(completionHandler: { item in
item?.showPopover()
self.navigateWebView("app/" + url.absoluteString)
})
}
}
SFSafariApplication.getActiveWindow { win in
win?.openTab(with: url, makeActiveIfPossible: true, completionHandler: { _ in
// Tab opened