Skip to content

Commit ea1564a

Browse files
committed
Adding logout functionality
1 parent 7caae77 commit ea1564a

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

Diff for: Sources/OAuthClient/OAuthClient.swift

+4
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ public class OAuthClient {
104104
}
105105
}
106106

107+
public func logout() {
108+
let _ = keychainHelper.remove(withKey: OAuthGrantType.password("", "").storageKey)
109+
}
110+
107111
private func buildParamsForRequest(grant: OAuthGrantType) -> [String: String] {
108112
var params = grant.params
109113
params["client_id"] = serverConnection.clientID

Diff for: Tests/OAuthClientTests/OAuthClientTests.swift

+45
Original file line numberDiff line numberDiff line change
@@ -368,4 +368,49 @@ class OAuthClientTests: XCTestCase {
368368

369369
waitForExpectations(timeout: 5, handler: nil)
370370
}
371+
372+
func testLogoutWorksCorrectly() throws {
373+
let expect = expectation(description: "Token is deleted")
374+
375+
let jsonData = TestStrings.oAuthTokenResponse.data(using: .utf8)
376+
377+
let tokenToStore = try JSONDecoder().decode(OAuthAccessToken.self, from: jsonData!)
378+
379+
try keychain.add(tokenToStore, withKey: "password")
380+
381+
// Verify the token is infact stored
382+
client.fetchStoredToken(type: .password("", "")) { [weak self] result in
383+
guard let self = self else {
384+
XCTFail()
385+
return
386+
}
387+
388+
switch result {
389+
case .success(let returnedToken):
390+
XCTAssertEqual(returnedToken.accessToken, tokenToStore.accessToken)
391+
// Perform the logout
392+
self.client.logout()
393+
394+
// Verify we can no longer fetch a stored password token
395+
self.client.fetchStoredToken(type: .password("", "")) { secondResult in
396+
switch secondResult {
397+
case .success(_):
398+
XCTFail("Token was still retreived")
399+
case .failure(let returnedError):
400+
let decodedError = returnedError as! OAuthClientError
401+
switch decodedError {
402+
case .errorReadingTokenFromStorage(_):
403+
expect.fulfill()
404+
default:
405+
XCTFail("Wrong error type")
406+
}
407+
}
408+
}
409+
case .failure(_):
410+
XCTFail("Token not stored")
411+
}
412+
}
413+
414+
waitForExpectations(timeout: 5, handler: nil)
415+
}
371416
}

0 commit comments

Comments
 (0)