Authorization

The SDK requires an authorization for opening an Interaction with Talkdesk. This is done using an access token, which is a time limited Talkdesk ID token provided by your backend on the user’s behalf. Your server is in charge of granting or denying token requests according to your business logic requirements. This can be an additional layer of security to protect your account from abuse.

Access tokens currently expire after 10 minutes, but this can change at any time with no prior notice, so your application or backend logic should not depend on this. For more information, check the Talkdesk ID documentation.

Authorizing

Authorization Delegate

The SDK will request an authorization when:

  • An Interaction is started and no authorization exists
  • The current authorization expired

By setting TalkdeskSDK.authorizationDelegate to a class that conforms to the MediaSessionAuthorizationDelegate protocol, TalkdeskSDK will inform the delegate that a new Authorization is required by calling the shouldAuthorize method. The app then should authorize with TalkdeskSDK.authorize(with:).

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    func applicationDidFinishLaunching(_ application: UIApplication) {
        // Set our authorization delegate
        TalkdeskSDK.authorizationDelegate = AuthorizationController()
    }
}

And our sample AuthorizationController:

class AuthorizationController: MediaSessionAuthorizationDelegate {
  /// Method called by the SDK when an authorization is required
    func shouldAuthorize() {
        fetchAuthorization()
    }

  /// Fetch the access token from the backend
    func fetchAuthorization() {
        if let url = URL(string: "https://example.com/access") {
            let session = URLSession.shared
            let request = URLRequest(url: url)

            let dataTask = session.dataTask(with: request) { (data, response, _) in
                guard response.statusCode == 200 else {
                    TalkdeskSDK.authorize(with:.notAuthorized(reason: "Not authorized"))
                    return
                }
            if let data = data {
                TalkdeskSDK.authorize(with: .accessToken(String(data: data, encoding: .utf8)))
            }
        }
        dataTask.resume()
    }
}

If the authorizationDelegate doesn’t call TalkdeskSDK.authorize(with:) within 30 seconds, the Interaction will fail due to timeout. You can change this value by affecting TalkdeskSDK.authorizationTimeoutSeconds.

A Sample Server

The Mobile SDK Ruby Server Sample is a reference implementation and quickstart for a simple server that can either be deployed to Heroku with the Deploy to Heroku button, or run locally.

It contains a bare-bones implementation of the authorization workflow your backend should perform in order to provide an access token to your application.