'Obtaining Refresh Token from MSAL during ADAL to MSAL migration

We are migrating from ADAL4J to MSAL4J. In ADAL, we store the refresh token in database and shared it between different micro services for token exchange. With MSAL, the refresh token is not easily accessible. We have built the code in a way that it's very difficult to get rid of the usage of the "refresh token". So we are seeking a solution to obtain the refresh token in MSAL.

Currently we have a few proposals (code examples are in scala):

Option 1. Convert package-scoped class msal.AuthenticationResult to a customized Result class

the helper class is defined under com.microsoft.aad.msal4j

package com.microsoft.aad.msal4j

trait AADTokenExchangeHelper(...) {
    def acquireToken(...) = {
      val result = app.acquireToken(requestParams).get.asInstanceOf[AuthenticationResult]
      // store result.getRefreshToken
    }
}

Option 2. Parse TokenCache and get refresh token

val tokenCache: String = app.tokenCache().serialize()
val tokensMap = ObjectMapper.fromJson[String, Map[String, Any]](tokenCache)
val refreshTokensMeta = tokensMap("RefreshToken")
 .asInstanceOf[Map[String, Map[String, String]]]
 .values
 .toSeq
// may filter by client_id
val refreshTokens = refreshTokensMeta.map(m => m("secret"))

Option 3. Customize TokenCache

I don't have full details here. It seems that we need to parse the cache string as in Option 2

Are these options acceptable? Any suggestions are appreciated!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source