Skip to content

Commit 01c5e4f

Browse files
committed
Use ephemeral URLSession to send .org REST API requests
1 parent 916af77 commit 01c5e4f

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

WordPress/Classes/Networking/WordPressClient.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,19 @@ actor WordPressClient {
4545
self.rootUrl = rootUrl.url()
4646
}
4747

48-
static func `for`(site: WordPressSite, in session: URLSession) throws -> WordPressClient {
48+
static func `for`(site: WordPressSite) throws -> WordPressClient {
4949
let parsedUrl = try ParsedUrl.parse(input: site.baseUrl)
5050

51+
// At the moment, the app supports account password and application password.
52+
// When a site is initially signed in with account password, WordPress login cookies are stored
53+
// in `URLSession.shared`. After switching the site to application password authentication,
54+
// the store cookies may interfere with application-password authentication, which results in 401
55+
// errors from REST API.
56+
//
57+
// To avoid the above issue, we'll use ephemeral URLSession for now (which stores cookies in memory
58+
// rather than using the shared one on disk).
59+
let session = URLSession(configuration: .ephemeral)
60+
5161
switch site.type {
5262
case let .dotCom(authToken):
5363
let api = WordPressAPI(urlSession: session, baseUrl: parsedUrl, authenticationStategy: .authorizationHeader(token: authToken))

WordPress/Classes/ViewRelated/Blog/Blog Dashboard/ViewModel/BlogDashboardViewModel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ final class BlogDashboardViewModel {
108108
var _error: Error?
109109

110110
do {
111-
self.wordpressClient = try WordPressClient.for(site: .from(blog: self.blog), in: .shared)
111+
self.wordpressClient = try WordPressClient.for(site: .from(blog: self.blog))
112112
} catch {
113113
_error = error
114114
self.wordpressClient = nil

WordPress/Classes/ViewRelated/Blog/Blog Details/BlogDetailsViewController+SectionHelpers.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ extension BlogDetailsViewController {
130130

131131
do {
132132
let site = try WordPressSite.from(blog: self.blog)
133-
let client = try WordPressClient.for(site: site, in: .shared)
133+
let client = try WordPressClient.for(site: site)
134134
return ApplicationPasswordService(api: client, currentUserId: userId)
135135
} catch {
136136
DDLogError("Failed to create WordPressClient: \(error)")

0 commit comments

Comments
 (0)