Skip to content

Commit 2f6a5fb

Browse files
fix(http): update endpoint authentication
1 parent a85eaa8 commit 2f6a5fb

File tree

5 files changed

+52
-84
lines changed

5 files changed

+52
-84
lines changed

GitHub Notifications/AppDelegate.swift

Lines changed: 42 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -66,60 +66,55 @@ class AppDelegate: NSObject, NSApplicationDelegate {
6666
}
6767

6868
@objc func update() {
69-
let username = Defaults[.username]
7069
let password = Defaults[.password]
71-
72-
let basicAuth = "\(username):\(password)".data(using: .utf8)
73-
74-
if let base64Encoded = basicAuth?.base64EncodedString(options: Data.Base64EncodingOptions(rawValue: 0)) {
75-
let headers = ["authorization": "Basic \(base64Encoded)"]
70+
71+
let headers = ["authorization": "token \(password)"]
7672

77-
let request = NSMutableURLRequest(
78-
url: NSURL(string: "https://api.github.com/notifications")! as URL,
79-
cachePolicy: .reloadIgnoringLocalCacheData,
80-
timeoutInterval: 10.0
81-
)
82-
83-
request.httpMethod = "GET"
84-
request.allHTTPHeaderFields = headers
73+
let request = NSMutableURLRequest(
74+
url: NSURL(string: "https://api.github.com/notifications")! as URL,
75+
cachePolicy: .reloadIgnoringLocalCacheData,
76+
timeoutInterval: 10.0
77+
)
78+
79+
request.httpMethod = "GET"
80+
request.allHTTPHeaderFields = headers
8581

86-
let session = URLSession.shared
87-
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
88-
if error != nil {
89-
print(error!)
90-
} else {
91-
guard let responseData = data else {
92-
print("Error: did not receive data")
93-
return
94-
}
82+
let session = URLSession.shared
83+
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
84+
if error != nil {
85+
print(error!)
86+
} else {
87+
guard let responseData = data else {
88+
print("Error: did not receive data")
89+
return
90+
}
9591

96-
do {
97-
guard let notifications = try JSONSerialization.jsonObject(with: responseData, options: [])
98-
as? [Any] else {
99-
print("error trying to convert data to JSON 1")
100-
return
101-
}
102-
103-
self.firstMenuItem?.title = "\(notifications.count) notification\(notifications.count > 1 ? "s" : "")"
92+
do {
93+
guard let notifications = try JSONSerialization.jsonObject(with: responseData, options: [])
94+
as? [Any] else {
95+
print("error trying to convert data to JSON 1")
96+
return
97+
}
98+
99+
self.firstMenuItem?.title = "\(notifications.count) notification\(notifications.count > 1 ? "s" : "")"
100+
101+
let doesNotificationExist = notifications.count >= 1
104102

105-
let doesNotificationExist = notifications.count >= 1
106-
107-
DispatchQueue.main.async {
108-
self.setStatusItemImage(
109-
named: doesNotificationExist
110-
? "StatusItemImageNotification"
111-
: "StatusItemImage"
112-
)
113-
}
114-
} catch {
115-
print("error trying to convert data to JSON")
116-
return
117-
}
103+
DispatchQueue.main.async {
104+
self.setStatusItemImage(
105+
named: doesNotificationExist
106+
? "StatusItemImageNotification"
107+
: "StatusItemImage"
108+
)
109+
}
110+
} catch {
111+
print("error trying to convert data to JSON")
112+
return
118113
}
119-
})
114+
}
115+
})
120116

121-
dataTask.resume()
122-
}
117+
dataTask.resume()
123118
}
124119

125120
func setStatusItemImage(named: String = "StatusItemImage") {

GitHub Notifications/Constants.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,5 @@ import Cocoa
1010
import Defaults
1111

1212
extension Defaults.Keys {
13-
static let username = Key<String>("username", default: "")
1413
static let password = Key<String>("password", default: "")
1514
}

GitHub Notifications/GeneralPreferenceViewController.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,13 @@ final class GeneralPreferenceViewController: NSViewController, PreferencePane {
1616
let preferencePaneTitle = "General"
1717

1818
override var nibName: NSNib.Name? { "GeneralPreferenceViewController" }
19-
20-
@IBOutlet private var usernameTextField: NSTextField!
19+
2120
@IBOutlet private var passwordTextField: NSTextField!
2221
@IBOutlet private var saveButton: NSButton!
2322

2423
override func viewDidLoad() {
2524
super.viewDidLoad()
2625

27-
usernameTextField?.placeholderString = "username"
28-
usernameTextField?.stringValue = Defaults[.username]
2926
passwordTextField?.placeholderString = "********"
3027
passwordTextField?.stringValue = Defaults[.password]
3128

@@ -34,7 +31,6 @@ final class GeneralPreferenceViewController: NSViewController, PreferencePane {
3431

3532
@objc func save() {
3633
// Save input values in User Defaults
37-
Defaults[.username] = usernameTextField!.stringValue
3834
Defaults[.password] = passwordTextField!.stringValue
3935

4036
// Close the window when done

GitHub Notifications/GeneralPreferenceViewController.xib

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,54 +9,32 @@
99
<connections>
1010
<outlet property="passwordTextField" destination="0nJ-7C-dkw" id="mZW-Cn-tij"/>
1111
<outlet property="saveButton" destination="djG-W7-VfA" id="aWI-3q-aho"/>
12-
<outlet property="usernameTextField" destination="vxI-a4-2Tu" id="F1o-1f-f8D"/>
1312
<outlet property="view" destination="c22-O7-iKe" id="ZQe-UH-LCX"/>
1413
</connections>
1514
</customObject>
1615
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
1716
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
18-
<customView id="c22-O7-iKe">
19-
<rect key="frame" x="0.0" y="0.0" width="510" height="194"/>
17+
<customView misplaced="YES" id="c22-O7-iKe">
18+
<rect key="frame" x="0.0" y="0.0" width="480" height="136"/>
2019
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
2120
<subviews>
2221
<gridView xPlacement="center" yPlacement="center" rowAlignment="none" rowSpacing="8" columnSpacing="10" translatesAutoresizingMaskIntoConstraints="NO" id="lkV-Y2-eOl">
23-
<rect key="frame" x="30" y="20" width="450" height="154"/>
22+
<rect key="frame" x="30" y="20" width="450" height="96"/>
2423
<constraints>
2524
<constraint firstAttribute="width" constant="450" id="5HG-su-XZq"/>
2625
</constraints>
2726
<rows>
28-
<gridRow height="47" id="ukr-az-gqc"/>
2927
<gridRow yPlacement="top" id="gSl-iE-cwq"/>
3028
<gridRow id="zkx-zp-WqT"/>
3129
</rows>
3230
<columns>
33-
<gridColumn xPlacement="trailing" width="153" id="GGj-JC-w4E"/>
31+
<gridColumn xPlacement="trailing" width="151" id="GGj-JC-w4E"/>
3432
<gridColumn xPlacement="leading" id="cs9-fm-l2F"/>
3533
</columns>
3634
<gridCells>
37-
<gridCell row="ukr-az-gqc" column="GGj-JC-w4E" id="sRb-hw-f4U">
38-
<textField key="contentView" horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="Vt6-jC-8Jo">
39-
<rect key="frame" x="85" y="123" width="70" height="16"/>
40-
<textFieldCell key="cell" lineBreakMode="clipping" title="Username:" id="uKy-ok-dDi">
41-
<font key="font" usesAppearanceFont="YES"/>
42-
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
43-
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
44-
</textFieldCell>
45-
</textField>
46-
</gridCell>
47-
<gridCell row="ukr-az-gqc" column="cs9-fm-l2F" id="jPa-TM-cRP">
48-
<textField key="contentView" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="vxI-a4-2Tu">
49-
<rect key="frame" x="163" y="120" width="100" height="21"/>
50-
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" borderStyle="bezel" drawsBackground="YES" id="Dfa-xZ-Y74">
51-
<font key="font" usesAppearanceFont="YES"/>
52-
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
53-
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
54-
</textFieldCell>
55-
</textField>
56-
</gridCell>
5735
<gridCell row="gSl-iE-cwq" column="GGj-JC-w4E" id="IXl-mV-waK">
5836
<textField key="contentView" horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="D0h-m5-qzI" userLabel="Personal Access Token:">
59-
<rect key="frame" x="8" y="83" width="147" height="16"/>
37+
<rect key="frame" x="6" y="80" width="147" height="16"/>
6038
<textFieldCell key="cell" lineBreakMode="clipping" title="Personal Access Token:" id="Z3e-UB-ZDm" userLabel="Personal Access Token:">
6139
<font key="font" usesAppearanceFont="YES"/>
6240
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
@@ -66,7 +44,7 @@
6644
</gridCell>
6745
<gridCell row="gSl-iE-cwq" column="cs9-fm-l2F" id="yFx-k0-cPt">
6846
<secureTextField key="contentView" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="0nJ-7C-dkw">
69-
<rect key="frame" x="163" y="78" width="100" height="21"/>
47+
<rect key="frame" x="161" y="75" width="100" height="21"/>
7048
<secureTextFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" borderStyle="bezel" drawsBackground="YES" usesSingleLineMode="YES" id="nAh-Vn-Nyc">
7149
<font key="font" metaFont="system"/>
7250
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
@@ -80,7 +58,7 @@
8058
<gridCell row="zkx-zp-WqT" column="GGj-JC-w4E" id="1B5-ah-fUN"/>
8159
<gridCell row="zkx-zp-WqT" column="cs9-fm-l2F" id="xrS-Ra-IX6">
8260
<button key="contentView" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="djG-W7-VfA">
83-
<rect key="frame" x="157" y="18" width="70" height="32"/>
61+
<rect key="frame" x="155" y="16" width="70" height="32"/>
8462
<buttonCell key="cell" type="push" title="Save" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="TLl-wY-a6p">
8563
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
8664
<font key="font" metaFont="system"/>
@@ -97,7 +75,7 @@
9775
<constraint firstAttribute="bottom" secondItem="lkV-Y2-eOl" secondAttribute="bottom" priority="750" constant="20" id="bsa-Ju-YOT"/>
9876
<constraint firstItem="lkV-Y2-eOl" firstAttribute="centerX" secondItem="c22-O7-iKe" secondAttribute="centerX" id="rdL-1S-HXy"/>
9977
</constraints>
100-
<point key="canvasLocation" x="83" y="211"/>
78+
<point key="canvasLocation" x="70" y="141"/>
10179
</customView>
10280
</objects>
10381
</document>

GitHub Notifications/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<key>CFBundlePackageType</key>
1818
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
1919
<key>CFBundleShortVersionString</key>
20-
<string>1.0.0</string>
20+
<string>1.0.1</string>
2121
<key>CFBundleVersion</key>
2222
<string>1</string>
2323
<key>LSApplicationCategoryType</key>

0 commit comments

Comments
 (0)