mirror of
https://github.com/bitwarden/browser
synced 2025-12-16 00:03:56 +00: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:
@@ -25,12 +25,20 @@ export class SafariApp {
|
|||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
const messageId = now.getTime().toString() + '_' + Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
|
const messageId = now.getTime().toString() + '_' + Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
|
||||||
(window as any).webkit.messageHandlers.bitwardenApp.postMessage(JSON.stringify({
|
if (typeof safari === typeof undefined) {
|
||||||
id: messageId,
|
(window as any).webkit.messageHandlers.bitwardenApp.postMessage(JSON.stringify({
|
||||||
command: command,
|
id: messageId,
|
||||||
data: data,
|
command: command,
|
||||||
responseData: null,
|
data: data,
|
||||||
}));
|
responseData: null,
|
||||||
|
}));
|
||||||
|
} else {
|
||||||
|
safari.extension.dispatchMessage('bitwarden', {
|
||||||
|
command: command,
|
||||||
|
data: data,
|
||||||
|
responseData: null,
|
||||||
|
});
|
||||||
|
}
|
||||||
if (resolveNow) {
|
if (resolveNow) {
|
||||||
resolve();
|
resolve();
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -3,6 +3,15 @@ window.addEventListener('message', (event) => {
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
if (event.data.command && (event.data.command === 'authResult')) {
|
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({
|
chrome.runtime.sendMessage({
|
||||||
command: event.data.command,
|
command: event.data.command,
|
||||||
code: event.data.code,
|
code: event.data.code,
|
||||||
|
|||||||
@@ -41,11 +41,11 @@ export class SsoComponent extends BaseSsoComponent {
|
|||||||
this.redirectUri = url + '/sso-connector.html';
|
this.redirectUri = url + '/sso-connector.html';
|
||||||
this.clientId = 'browser';
|
this.clientId = 'browser';
|
||||||
|
|
||||||
super.onSuccessfulLogin = () => {
|
super.onSuccessfulLogin = async () => {
|
||||||
|
await syncService.fullSync(true);
|
||||||
BrowserApi.reloadOpenWindows();
|
BrowserApi.reloadOpenWindows();
|
||||||
const thisWindow = window.open('', '_self');
|
const thisWindow = window.open('', '_self');
|
||||||
thisWindow.close();
|
thisWindow.close();
|
||||||
return syncService.fullSync(true);
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,14 +16,10 @@ class SafariExtensionViewController: SFSafariExtensionViewController, WKScriptMe
|
|||||||
if initedWebView {
|
if initedWebView {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String
|
|
||||||
initedWebView = true
|
initedWebView = true
|
||||||
let parentHeight = SafariExtensionViewController.shared.preferredContentSize.height
|
let parentHeight = SafariExtensionViewController.shared.preferredContentSize.height
|
||||||
let parentWidth = SafariExtensionViewController.shared.preferredContentSize.width
|
let parentWidth = SafariExtensionViewController.shared.preferredContentSize.width
|
||||||
let webViewConfig = WKWebViewConfiguration()
|
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: "allowFileAccessFromFileURLs")
|
||||||
webViewConfig.preferences.setValue(true, forKey: "developerExtrasEnabled")
|
webViewConfig.preferences.setValue(true, forKey: "developerExtrasEnabled")
|
||||||
webViewConfig.userContentController.add(self, name: "bitwardenApp")
|
webViewConfig.userContentController.add(self, name: "bitwardenApp")
|
||||||
@@ -31,12 +27,26 @@ class SafariExtensionViewController: SFSafariExtensionViewController, WKScriptMe
|
|||||||
configuration: webViewConfig)
|
configuration: webViewConfig)
|
||||||
webView.navigationDelegate = self
|
webView.navigationDelegate = self
|
||||||
webView.allowsLinkPreview = false
|
webView.allowsLinkPreview = false
|
||||||
webView.loadFileURL(url!, allowingReadAccessTo: bundleURL)
|
navigateWebView("app/popup/index.html")
|
||||||
webView.alphaValue = 0.0
|
webView.alphaValue = 0.0
|
||||||
webView.uiDelegate = self
|
webView.uiDelegate = self
|
||||||
view.addSubview(webView)
|
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!) {
|
func webView(_ webView: WKWebView, didFinish _: WKNavigation!) {
|
||||||
if #available(OSXApplicationExtension 10.12, *) {
|
if #available(OSXApplicationExtension 10.12, *) {
|
||||||
NSAnimationContext.runAnimationGroup({ _ in
|
NSAnimationContext.runAnimationGroup({ _ in
|
||||||
@@ -179,6 +189,14 @@ class SafariExtensionViewController: SFSafariExtensionViewController, WKScriptMe
|
|||||||
replyMessage(message: m)
|
replyMessage(message: m)
|
||||||
} else if command == "createNewTab" {
|
} else if command == "createNewTab" {
|
||||||
if let data = m.data, let url = URL(string: data) {
|
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
|
SFSafariApplication.getActiveWindow { win in
|
||||||
win?.openTab(with: url, makeActiveIfPossible: true, completionHandler: { _ in
|
win?.openTab(with: url, makeActiveIfPossible: true, completionHandler: { _ in
|
||||||
// Tab opened
|
// Tab opened
|
||||||
|
|||||||
Reference in New Issue
Block a user