diff --git a/Noti/AppDelegate.swift b/Noti/AppDelegate.swift index bd47790..7438267 100644 --- a/Noti/AppDelegate.swift +++ b/Noti/AppDelegate.swift @@ -18,6 +18,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { let userDefaults: UserDefaults = UserDefaults.standard var iwc:NSWindowController?; var pwc:NSWindowController?; + var nonce: String? = nil; func setPassword(password: String) { pushManager?.setPassword(password: password) @@ -84,15 +85,14 @@ class AppDelegate: NSObject, NSApplicationDelegate { guard let url = URLComponents(url: urls.first!, resolvingAgainstBaseURL: true) else { return } guard let token = url.fragment?.split(separator: "=")[1] else { return } guard let receivedNonce = url.queryItems?.first(where: { $0.name == "nonce" })?.value else { return } - guard let storedNonce = userDefaults.string(forKey: "nonce") else { return } // Verify nonce - if receivedNonce != storedNonce { + if receivedNonce != nonce { return } // Dispose to prevent replays - userDefaults.removeObject(forKey: "nonce") + nonce = nil userDefaults.setValue(token, forKeyPath: "token") loadPushManager() diff --git a/Noti/IntroViewController.swift b/Noti/IntroViewController.swift index 19f039d..afc98dd 100644 --- a/Noti/IntroViewController.swift +++ b/Noti/IntroViewController.swift @@ -9,6 +9,7 @@ import Cocoa class IntroViewController: NSViewController { + let appDelegate = NSApp.delegate as! AppDelegate let authUrl = "https://www.pushbullet.com/authorize" let clientId = "lIdYYNaWmj7ZJaCaycRXevhQz9yhdeJS" let redirectUri = "noti://redirect" @@ -76,7 +77,7 @@ class IntroViewController: NSViewController { @IBAction func startAuth(_ sender: AnyObject) { guard let nonce = generateNonce() else { return } - UserDefaults.standard.set(nonce, forKey: "nonce") + appDelegate.nonce = nonce var url = URLComponents(string: authUrl)! url.queryItems = [