Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.

Commit bb9001f

Browse files
committed
Call invalid token handler when receiving 'reauthentication_required' errors
1 parent 8be531d commit bb9001f

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

Sources/CoreAPI/WordPressComRestApi.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public typealias WordPressComRestApiError = WordPressComRestApiErrorCode
3030
case preconditionFailure
3131
case malformedURL
3232
case invalidQuery
33+
case reauthorizationRequired
3334
}
3435

3536
public struct WordPressComRestApiEndpointError: Error {
@@ -536,11 +537,12 @@ extension WordPressComRestApi {
536537
"authorization_required": .authorizationRequired,
537538
"upload_error": .uploadFailed,
538539
"unauthorized": .authorizationRequired,
539-
"invalid_query": .invalidQuery
540+
"invalid_query": .invalidQuery,
541+
"reauthorization_required": .reauthorizationRequired,
540542
]
541543

542544
let mappedError = errorsMap[errorCode] ?? .unknown
543-
if mappedError == .invalidToken {
545+
if mappedError == .invalidToken || mappedError == .reauthorizationRequired {
544546
// Call `invalidTokenHandler in the main thread since it's typically used by the apps to present an authentication UI.
545547
DispatchQueue.main.async {
546548
self.invalidTokenHandler?()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"message": "A fresh access token must be used to query information about the current user.",
3+
"error": "reauthorization_required"
4+
}

Tests/CoreAPITests/WordPressComRestApiTests.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,31 @@ class WordPressComRestApiTests: XCTestCase {
189189
self.waitForExpectations(timeout: 2, handler: nil)
190190
}
191191

192+
func testInvalidTokenFailedCallWithReauthenticationRequiredError() throws {
193+
let stubPath = try XCTUnwrap(
194+
OHPathForFileInBundle("WordPressComRestApiFailReauthenticationRequired.json", Bundle.coreAPITestsBundle)
195+
)
196+
stub(condition: isRestAPIRequest()) { _ in
197+
return fixture(filePath: stubPath, status: 401, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
198+
}
199+
200+
let expect = self.expectation(description: "One callback should be invoked")
201+
let handlerCalled = self.expectation(description: "Handler should be called")
202+
let api = WordPressComRestApi(oAuthToken: "fakeToken")
203+
api.setInvalidTokenHandler {
204+
handlerCalled.fulfill()
205+
}
206+
api.GET(wordPressMediaRoutePath, parameters: nil, success: { (_: AnyObject, _: HTTPURLResponse?) in
207+
expect.fulfill()
208+
XCTFail("This call should fail")
209+
}, failure: { (error, _) in
210+
expect.fulfill()
211+
XCTAssert(error.domain == "WordPressKit.WordPressComRestApiError", "The error should a WordPressComRestApiError")
212+
XCTAssert(error.code == Int(WordPressComRestApiErrorCode.reauthorizationRequired.rawValue), "The error code should be invalid token")
213+
})
214+
self.wait(for: [expect, handlerCalled], timeout: 2)
215+
}
216+
192217
func testInvalidJSONReceivedFailedCall() throws {
193218
let stubPath = try XCTUnwrap(
194219
OHPathForFileInBundle("WordPressComRestApiFailInvalidJSON.json", Bundle.coreAPITestsBundle)

WordPressKit.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@
224224
4A3239642B73132B00EFD2A8 /* SelfHostedPluginManagementClientTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A3239632B73132B00EFD2A8 /* SelfHostedPluginManagementClientTests.swift */; };
225225
4A3239662B7314E200EFD2A8 /* self-hosted-plugins-get.json in Resources */ = {isa = PBXBuildFile; fileRef = 4A3239652B7314E200EFD2A8 /* self-hosted-plugins-get.json */; };
226226
4A3239682B74319400EFD2A8 /* self-hosted-plugins-install.json in Resources */ = {isa = PBXBuildFile; fileRef = 4A3239672B74319400EFD2A8 /* self-hosted-plugins-install.json */; };
227+
4A399CD52E0B7A9D0014E6AE /* WordPressComRestApiFailReauthenticationRequired.json in Resources */ = {isa = PBXBuildFile; fileRef = 4A399CD42E0B7A9D0014E6AE /* WordPressComRestApiFailReauthenticationRequired.json */; };
227228
4A40F6552B2A5A1A0015DA77 /* WordPressAPIErrorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A40F6542B2A5A1A0015DA77 /* WordPressAPIErrorTests.swift */; };
228229
4A57A6812B549144008D0660 /* WordPressComRestApiTests+Error.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A57A6802B549144008D0660 /* WordPressComRestApiTests+Error.swift */; };
229230
4A68E3CD29404181004AC3DC /* RemoteBlog.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A68E3CC29404181004AC3DC /* RemoteBlog.swift */; };
@@ -1005,6 +1006,7 @@
10051006
4A3239632B73132B00EFD2A8 /* SelfHostedPluginManagementClientTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SelfHostedPluginManagementClientTests.swift; sourceTree = "<group>"; };
10061007
4A3239652B7314E200EFD2A8 /* self-hosted-plugins-get.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "self-hosted-plugins-get.json"; sourceTree = "<group>"; };
10071008
4A3239672B74319400EFD2A8 /* self-hosted-plugins-install.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "self-hosted-plugins-install.json"; sourceTree = "<group>"; };
1009+
4A399CD42E0B7A9D0014E6AE /* WordPressComRestApiFailReauthenticationRequired.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = WordPressComRestApiFailReauthenticationRequired.json; sourceTree = "<group>"; };
10081010
4A40F6542B2A5A1A0015DA77 /* WordPressAPIErrorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WordPressAPIErrorTests.swift; sourceTree = "<group>"; };
10091011
4A57A6802B549144008D0660 /* WordPressComRestApiTests+Error.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "WordPressComRestApiTests+Error.swift"; sourceTree = "<group>"; };
10101012
4A68E3CC29404181004AC3DC /* RemoteBlog.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RemoteBlog.swift; sourceTree = "<group>"; };
@@ -1743,6 +1745,7 @@
17431745
74B335DF1F06F6290053A184 /* WordPressComRestApiFailInvalidInput.json */,
17441746
74B335DD1F06F5A50053A184 /* WordPressComRestApiFailInvalidJSON.json */,
17451747
74B335E11F06F6730053A184 /* WordPressComRestApiFailRequestInvalidToken.json */,
1748+
4A399CD42E0B7A9D0014E6AE /* WordPressComRestApiFailReauthenticationRequired.json */,
17461749
93F50A3B1F226C0100B5BEBA /* WordPressComRestApiFailThrottled.json */,
17471750
74B335E71F06F7200053A184 /* WordPressComRestApiFailUnauthorized.json */,
17481751
74B335E51F06F6E90053A184 /* WordPressComRestApiMedia.json */,
@@ -3139,6 +3142,7 @@
31393142
FFE247AF20C891E6002DF3A2 /* WordPressComOAuthWrongPasswordFail.json in Resources */,
31403143
0CE311C52DCBB970003AADB3 /* site-subscriber-stats-response.json in Resources */,
31413144
F194E1252417EE7E00874408 /* atomic-get-auth-cookie-success.json in Resources */,
3145+
4A399CD52E0B7A9D0014E6AE /* WordPressComRestApiFailReauthenticationRequired.json in Resources */,
31423146
731BA83A21DED358000FDFCD /* site-creation-success.json in Resources */,
31433147
FEFFD99726C158F400F34231 /* share-app-content-success.json in Resources */,
31443148
465F88B7263B455300F4C950 /* get_wp_v2_themes_twentytwentyone-no-colors.json in Resources */,

0 commit comments

Comments
 (0)