Skip to content
This repository has been archived by the owner on Jul 6, 2022. It is now read-only.

Commit

Permalink
Retry with timer when PushManager.userInfo call fails (closes #18).
Browse files Browse the repository at this point in the history
Fix potential security problems (don't redirect to a real website anymore)
  • Loading branch information
jariz committed Jul 27, 2016
1 parent 2eeb6d3 commit f73f8fb
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 14 deletions.
6 changes: 3 additions & 3 deletions Noti/AuthViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class AuthViewController: NSViewController, WebFrameLoadDelegate {
override func viewDidLoad() {
super.viewDidLoad()

let req = NSURLRequest(URL:NSURL(string:"https://www.pushbullet.com/authorize?client_id=K5fHgHU2IipmPaMov3CkOHkJb0DKC6Oc&redirect_uri=https%3A%2F%2Fjari.io%2Fnoti%2Fredirect&response_type=token&scope=everything")!)
let req = NSURLRequest(URL:NSURL(string:"https://www.pushbullet.com/authorize?client_id=QTVK7zATuEcu4sME8TrwLBMuoW7vC7Wr&redirect_uri=about:blank&response_type=token&scope=everything")!)
webView.frameLoadDelegate = self
webView.mainFrame.loadRequest(req)
}
Expand All @@ -35,8 +35,8 @@ class AuthViewController: NSViewController, WebFrameLoadDelegate {

if let ds = frame.dataSource {
if let url = ds.response.URL {
if url.absoluteString.hasPrefix("https://jari.io/noti/redirect") {
let token = (url.absoluteString as NSString).substringFromIndex(43)
if url.absoluteString.hasPrefix("about:blank") {
let token = (url.absoluteString as NSString).substringFromIndex(27)
let appDelegate = NSApplication.sharedApplication().delegate as! AppDelegate

print("Got token!", token, "saving and restarting PushManager")
Expand Down
2 changes: 1 addition & 1 deletion Noti/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@
<customObject id="Ady-hI-5gd" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
<menu id="P5i-8m-dBU">
<items>
<menuItem title="Logging in..." id="wTQ-e6-phS">
<menuItem title="Disconnected. Please log in." id="wTQ-e6-phS">
<modifierMask key="keyEquivalentModifierMask"/>
</menuItem>
<menuItem isSeparatorItem="YES" id="JDX-TC-DAQ"/>
Expand Down
6 changes: 3 additions & 3 deletions Noti/IntroViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class IntroViewController: NSViewController {
self.view.window!.styleMask -= NSClosableWindowMask

NSAnimationContext.runAnimationGroup({ (context) -> Void in
context.duration = 0.25
context.duration = 0.50
self.authTxt.animator().alphaValue = 0
self.authBtn.animator().alphaValue = 0

Expand All @@ -44,13 +44,13 @@ class IntroViewController: NSViewController {
self.authImg.alphaValue = 0

NSAnimationContext.runAnimationGroup({ (context) -> Void in
context.duration = 0.25
context.duration = 0.50
self.authImg.animator().alphaValue = 1
}, completionHandler: nil)

})

NSTimer.scheduledTimerWithTimeInterval(1.5, target: NSBlockOperation(block: self.view.window!.close), selector: #selector(NSOperation.main), userInfo: nil, repeats: false)
NSTimer.scheduledTimerWithTimeInterval(3, target: NSBlockOperation(block: self.view.window!.close), selector: #selector(NSOperation.main), userInfo: nil, repeats: false)

}

Expand Down
43 changes: 37 additions & 6 deletions Noti/PushManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class PushManager: NSObject, WebSocketDelegate, NSUserNotificationCenterDelegate
self.initCrypt()

print("Getting user info...")
getUserInfo { () in
getUserInfo {
self.connect()
}
}
Expand Down Expand Up @@ -80,7 +80,11 @@ class PushManager: NSObject, WebSocketDelegate, NSUserNotificationCenterDelegate
NSNotificationCenter.defaultCenter().postNotificationName("StateChange", object: object)
}

var _callback:(() -> Void)? = nil
func getUserInfo(callback: (() -> Void)?) {
//todo: this is kinda dirty ...
self._callback = callback

let headers = [
"Access-Token": token
];
Expand All @@ -90,16 +94,36 @@ class PushManager: NSObject, WebSocketDelegate, NSUserNotificationCenterDelegate
if let info = response.result.value {
debugPrint(info)
self.userInfo = JSON.parse(info)
if callback != nil {
callback!()

if self.userInfo!["error"].isExists() {
self.killed = true
self.disconnect()
self.setState("Disconnected: " + self.userInfo!["error"]["message"].string!, disabled: true)
} else {
if callback != nil {
callback!()
}
}

} else if response.result.error != nil {
self.setState("I'm RIP") //todo
if callback == nil {
self.killed = true
self.disconnect()
self.setState("Failed to log in.")
} else {
self.setState("Failed to log in, retrying in 2 seconds...")
NSTimer.scheduledTimerWithTimeInterval(2, target: NSBlockOperation(block: self.retryUserInfo), selector: #selector(NSOperation.main), userInfo: nil, repeats: false)
}

}
}

}

func retryUserInfo() {
getUserInfo(self._callback)
}


func userNotificationCenter(center: NSUserNotificationCenter, didActivateNotification notification: NSUserNotification) {
switch notification.activationType {
case .ActionButtonClicked:
Expand Down Expand Up @@ -232,10 +256,17 @@ class PushManager: NSObject, WebSocketDelegate, NSUserNotificationCenterDelegate

if(!self.killed) {
print("Reconnecting in 5 sec");
setState("Disconnected, retrying in 5 seconds.", disabled: true)
if error != nil {
setState("Disconnected: \(error!.localizedDescription), retrying...", disabled: true)
}
else {
setState("Disconnected, retrying...", disabled: true)
}

NSTimer.scheduledTimerWithTimeInterval(5, target: NSBlockOperation(block: self.connect), selector: #selector(NSOperation.main), userInfo: nil, repeats: false)
} else {
print("Not going to reconnect: I'm killed")
setState("Disconnected. Please log in.", disabled: true)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Noti/StatusMenuController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class StatusMenuController: NSObject, NSUserNotificationCenterDelegate {
button.image = NSImage(named: "StatusBarButtonImage")
statusItem.menu = menu;
}
statusItem.button?.appearsDisabled = true
menuItem.enabled = true

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(StatusMenuController.stateChange(_:)), name:"StateChange", object: nil)
Expand All @@ -46,7 +47,6 @@ class StatusMenuController: NSObject, NSUserNotificationCenterDelegate {
newImage.size = destSize
let finalImage = NSImage(data: newImage.TIFFRepresentation!)!
menuItem.image = finalImage

} else {
menuItem.image = nil
}
Expand Down

0 comments on commit f73f8fb

Please sign in to comment.