Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,28 @@ extension RestClient {
return self.records(forRequest: request, withDecoder: decoder)
}
}

// MARK: Requests
extension RestClient {

/// Creates a RestRequest object that revokes the access token for a user
/// - Parameters:
/// - user: User to revoken the token for, current user will be used if none provided
/// - Returns: A `RestRequest` object to revoke the access token if access token is present, nil otherwise
@objc
public func requestForRevokeAccessToken(user: UserAccount? = UserAccountManager.shared.currentUserAccount) -> RestRequest? {
guard let user = user ?? UserAccountManager.shared.currentUserAccount,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The currentUser default is here and in the signature so Swift can call it without providing an argument like: 
RestClient.shared.requestForRevokeAccessToken()

and obj-c can call with nil:
[[SFRestAPI sharedInstance] requestForRevokeAccessTokenWithUser:nil]

let accessToken = user.credentials.accessToken,
let encodedToken = accessToken.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
return nil
}

let request = RestRequest(method: .POST, path: "/services/oauth2/revoke", queryParams: nil)
request.endpoint = ""

// Set the request body with URL-encoded token
let bodyString = "token=\(encodedToken)"
request.setCustomRequestBodyString(bodyString, contentType: "application/x-www-form-urlencoded")
return request
}
}
22 changes: 2 additions & 20 deletions libs/SalesforceSDKCore/SalesforceSDKCoreTests/RestClientTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
let request = RestClient.shared.request(forQuery: "select name from CONTACT", apiVersion: nil)

var erroredResult: RestClientError?
RestClient.shared.fetchRecords(ofModelType: TestContact.self, forRequest: request) { result in

Check warning on line 56 in libs/SalesforceSDKCore/SalesforceSDKCoreTests/RestClientTest.swift

View workflow job for this annotation

GitHub Actions / ios-pr (SalesforceSDKCore, ^18) / test-ios

'fetchRecords(ofModelType:forRequest:withDecoder:_:)' is deprecated: Deprecated in Salesforce Mobile SDK 13.0 and will be removed in Salesforce Mobile SDK 14.0. Use the async/await version of `fetchRecords(ofModelType:forRequest:withDecoder:)` instead.
switch (result) {
case .failure(let error):
erroredResult = error
Expand Down Expand Up @@ -324,7 +324,7 @@
var response: CompositeResponse?
var restClientError: Error?

RestClient.shared.send(compositeRequest: compositeRequest) { result in

Check warning on line 327 in libs/SalesforceSDKCore/SalesforceSDKCoreTests/RestClientTest.swift

View workflow job for this annotation

GitHub Actions / ios-pr (SalesforceSDKCore, ^18) / test-ios

'send(compositeRequest:_:)' is deprecated: Deprecated in Salesforce Mobile SDK 13.0 and will be removed in Salesforce Mobile SDK 14.0. Use the async/await version of `send(compositeRequest:)` instead.
defer { expectation.fulfill() }
switch (result) {
case .success(let resp):
Expand Down Expand Up @@ -383,7 +383,7 @@
var response: CompositeResponse?
var restClientError: Error?

RestClient.shared.send(compositeRequest: compositeRequest) { result in

Check warning on line 386 in libs/SalesforceSDKCore/SalesforceSDKCoreTests/RestClientTest.swift

View workflow job for this annotation

GitHub Actions / ios-pr (SalesforceSDKCore, ^18) / test-ios

'send(compositeRequest:_:)' is deprecated: Deprecated in Salesforce Mobile SDK 13.0 and will be removed in Salesforce Mobile SDK 14.0. Use the async/await version of `send(compositeRequest:)` instead.
defer { expectation.fulfill() }
switch (result) {
case .success(let resp):
Expand Down Expand Up @@ -420,7 +420,7 @@
var response: CompositeResponse?
var restClientError: Error?

RestClient.shared.send(compositeRequest: compositeRequest) { result in

Check warning on line 423 in libs/SalesforceSDKCore/SalesforceSDKCoreTests/RestClientTest.swift

View workflow job for this annotation

GitHub Actions / ios-pr (SalesforceSDKCore, ^18) / test-ios

'send(compositeRequest:_:)' is deprecated: Deprecated in Salesforce Mobile SDK 13.0 and will be removed in Salesforce Mobile SDK 14.0. Use the async/await version of `send(compositeRequest:)` instead.
defer { expectation.fulfill() }
switch (result) {
case .success(let resp):
Expand Down Expand Up @@ -461,7 +461,7 @@
var response: BatchResponse?
var restClientError: Error?

RestClient.shared.send(batchRequest: batchRequest) { result in

Check warning on line 464 in libs/SalesforceSDKCore/SalesforceSDKCoreTests/RestClientTest.swift

View workflow job for this annotation

GitHub Actions / ios-pr (SalesforceSDKCore, ^18) / test-ios

'send(batchRequest:_:)' is deprecated: Deprecated in Salesforce Mobile SDK 13.0 and will be removed in Salesforce Mobile SDK 14.0. Use the async/await version of `send(batchRequest:)` instead.
defer { expectation.fulfill() }
switch (result) {
case .success(let resp):
Expand Down Expand Up @@ -536,7 +536,7 @@
var response: BatchResponse?
var restClientError: Error?

RestClient.shared.send(batchRequest: batchRequest) { result in

Check warning on line 539 in libs/SalesforceSDKCore/SalesforceSDKCoreTests/RestClientTest.swift

View workflow job for this annotation

GitHub Actions / ios-pr (SalesforceSDKCore, ^18) / test-ios

'send(batchRequest:_:)' is deprecated: Deprecated in Salesforce Mobile SDK 13.0 and will be removed in Salesforce Mobile SDK 14.0. Use the async/await version of `send(batchRequest:)` instead.
defer { expectation.fulfill() }
switch (result) {
case .success(let resp):
Expand Down Expand Up @@ -592,7 +592,7 @@
var response: BatchResponse?
var restClientError: Error?

RestClient.shared.send(batchRequest: batchRequest) { result in

Check warning on line 595 in libs/SalesforceSDKCore/SalesforceSDKCoreTests/RestClientTest.swift

View workflow job for this annotation

GitHub Actions / ios-pr (SalesforceSDKCore, ^18) / test-ios

'send(batchRequest:_:)' is deprecated: Deprecated in Salesforce Mobile SDK 13.0 and will be removed in Salesforce Mobile SDK 14.0. Use the async/await version of `send(batchRequest:)` instead.
defer { expectation.fulfill() }
switch (result) {
case .success(let resp):
Expand Down Expand Up @@ -773,7 +773,8 @@

func testRefreshWithSuccesfulRequests() async throws {
// Revoke access token
try await revokeAccessToken()
let request = try XCTUnwrap(RestClient.shared.requestForRevokeAccessToken())
_ = try await RestClient.shared.send(request: request)

// Send multiple requests for replay and verify completion block is only called once per request
let resourcesRequest = RestClient.shared.request(forResources: nil)
Expand Down Expand Up @@ -806,25 +807,6 @@
await fulfillment(of: [resourcesExpectation, describeExpectation, contactExpectation])
}

private func revokeAccessToken() async throws {
guard let currentUser = UserAccountManager.shared.currentUserAccount,
let accessToken = currentUser.credentials.accessToken,
let encodedToken = accessToken.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
XCTFail("No current user access token")
return
}

let request = RestRequest(method: .POST, path: "/services/oauth2/revoke", queryParams: nil)
request.endpoint = ""

// Set the request body with URL-encoded token
let bodyString = "token=\(encodedToken)"
request.setCustomRequestBodyString(bodyString, contentType: "application/x-www-form-urlencoded")

// Send the request
_ = try await RestClient.shared.send(request: request)
}

private func generateRecordName() -> String {
let timecode = Date.timeIntervalSinceReferenceDate
return "SwiftTestsiOS\(timecode)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,31 +96,17 @@ struct RevokeView: View {

@MainActor
private func revokeAccessToken() async {
guard let credentials = UserAccountManager.shared.currentUserAccount?.credentials else {
alertType = .error("No credentials found")
return
}

guard let accessToken = credentials.accessToken,
let encodedToken = accessToken.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
alertType = .error("Invalid access token")
guard let request = RestClient.shared.requestForRevokeAccessToken() else {
alertType = .error("Token revoke request couldn't be generated")
return
}

isRevoking = true

do {
// Create POST request to revoke endpoint
let request = RestRequest(method: .POST, path: "/services/oauth2/revoke", queryParams: nil)
request.endpoint = ""

// Set the request body with URL-encoded token
let bodyString = "token=\(encodedToken)"
request.setCustomRequestBodyString(bodyString, contentType: "application/x-www-form-urlencoded")

// Send the request
_ = try await RestClient.shared.send(request: request)

alertType = .success

// Notify parent to refresh fields
Expand Down
Loading