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

Commit

Permalink
Store nonce in AppDelegate
Browse files Browse the repository at this point in the history
  • Loading branch information
JaCzekanski committed Sep 29, 2019
1 parent a263a2d commit 534980d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
6 changes: 3 additions & 3 deletions Noti/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion Noti/IntroViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 = [
Expand Down

0 comments on commit 534980d

Please sign in to comment.