authenticateIntoTheSdk

Overview

    +-------------+           +----------+           +------------+
    |  Customer   |----(2)--->| Customer |----(3)--->|  PayMitto  |
    | Application |<---(5)----|   API    |<---(4)----|    API     |
    +-------------+           +----------+           +------------+
           |
           |                  +------------+
            <----------(1)----|  PayMitto  |
            -----------(6)--->|    SDK     |
                              +------------+
  1. SDK calls back the authenticateIntoTheSdk to request a new authentication token.
  2. Customer application makes a network request to the Customer API to initiate the creation
    of an access token for the PayMitto SDK.
  3. Customer API makes a network request to the PayMitto API to create an access token for
    the PayMitto SDK.
  4. PayMitto API returns the access token details or an error.
  5. Customer API returns the access token details or an error.
  6. Customer application returns the access token details and makes authenticateIntoTheSdk callback return and AuthenticationResult object (Success or Error).

Expected objects to be sent by the app in the callback response to the sdk

TypeDescription
AccessTokenDetailsA credential representing the authorization granted by the resource owner (user) to the PayMitto SDK.
PayMittoErrorA generic representation of an error for the PayMitto SDK.

Sample Implementation


private val authenticateIntoTheSdk = suspend {
    withContext(Dispatchers.IO) {
        val authRequest = AuthRequest(
            clientId = "client_id",
            clientSecret = "client_secret",
            audience = "audience",
            grantType = "grant_type",
            senderId = "sender_id"
        )
        val response = oAuthApi.requestAuth(authURL, authRequest)
        val accessTokenResponse = response.body()
        if (response.isSuccessful && accessTokenResponse != null) {
            tokenValue = accessTokenResponse.accessToken
            AuthenticationResult.Success(
                AccessTokenDetails(
                    accessToken = accessTokenResponse.accessToken,
                    expiresIn = accessTokenResponse.expiresInSeconds,
                    scope = accessTokenResponse.scope,
                    tokenType = accessTokenResponse.tokenType
                )
            )
        } else {
            AuthenticationResult.Error(
                PayMittoError(
                    code = response.code().toString(),
                    message = response.message()
                )
            )
        }
    }
}