Skip to content

Commit 9dd2324

Browse files
authored
Remove saved credentials when removing self-hosted sites (#23725)
* Delete application password when removing site * Remove self-hosted site cookies when removing site * Fix a unit test compiling issue
1 parent 2f39128 commit 9dd2324

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

WordPress/Classes/Models/Blog+SelfHosted.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ extension Blog {
5555
try keychainImplementation.getPassword(for: self.getUsername(), serviceName: self.getUrlString())
5656
}
5757

58+
/// Delete Application Token
59+
///
60+
func deleteApplicationToken(using keychainImplementation: KeychainAccessible = KeychainUtils()) throws {
61+
try? keychainImplementation.setPassword(for: self.getUsername(), to: nil, serviceName: self.getUrlString())
62+
}
63+
64+
@available(swift, obsoleted: 1.0)
65+
@objc(deleteApplicationToken)
66+
func objc_deleteApplicationToken() {
67+
_ = try? deleteApplicationToken()
68+
}
69+
5870
/// Store Application Tokens
5971
///
6072
func setApplicationToken(

WordPress/Classes/Models/Blog.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,23 @@ - (void)prepareForDeletion
106106
self.password = nil;
107107
}
108108

109+
if (self.account == nil) {
110+
[self deleteApplicationToken];
111+
}
112+
109113
[_xmlrpcApi invalidateAndCancelTasks];
110114
[_selfHostedSiteRestApi invalidateAndCancelTasks];
115+
116+
// Remove the self-hosted site cookies from the shared cookie storage.
117+
if (self.account == nil && self.url != nil) {
118+
NSURL *siteURL = [NSURL URLWithString:self.url];
119+
if (siteURL != nil) {
120+
NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
121+
for (NSHTTPCookie *cookie in [cookieJar cookiesForURL:siteURL]) {
122+
[cookieJar deleteCookie:cookie];
123+
}
124+
}
125+
}
111126
}
112127

113128
- (void)didTurnIntoFault

WordPress/Classes/Utility/KeychainUtils.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,16 @@ extension KeychainUtils: KeychainAccessible {
4141
try self.keychainUtils.getPasswordForUsername(username, andServiceName: serviceName)
4242
}
4343

44-
func setPassword(for username: String, to newValue: String, serviceName: String) throws {
45-
try keychainUtils.storeUsername(username, andPassword: newValue, forServiceName: serviceName, updateExisting: true)
44+
func setPassword(for username: String, to newValue: String?, serviceName: String) throws {
45+
if let newValue {
46+
try keychainUtils.storeUsername(username, andPassword: newValue, forServiceName: serviceName, updateExisting: true)
47+
} else {
48+
try keychainUtils.deleteItem(forUsername: username, andServiceName: serviceName)
49+
}
4650
}
4751
}
4852

4953
protocol KeychainAccessible {
5054
func getPassword(for username: String, serviceName: String) throws -> String
51-
func setPassword(for username: String, to newValue: String, serviceName: String) throws
55+
func setPassword(for username: String, to newValue: String?, serviceName: String) throws
5256
}

WordPress/WordPressTest/TestKeychain.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ class TestKeychain: KeychainAccessible {
2121
return keychainItem.password
2222
}
2323

24-
func setPassword(for username: String, to newValue: String, serviceName: String) throws {
25-
keychain[serviceName] = KeychainItem(username: username, password: newValue)
24+
func setPassword(for username: String, to newValue: String?, serviceName: String) throws {
25+
if let newValue {
26+
keychain[serviceName] = KeychainItem(username: username, password: newValue)
27+
} else {
28+
keychain[serviceName] = nil
29+
}
2630
}
2731
}

0 commit comments

Comments
 (0)