createDelegationChainFromAccessToken function

DelegationChain createDelegationChainFromAccessToken(
  1. String accessToken
)

Create a DelegationChain from an AccessToken extracted from a redirect URL. @param accessToken The access token extracted from a redirect URL.

Implementation

DelegationChain createDelegationChainFromAccessToken(String accessToken) {
  // Transform the HEXADECIMAL string into the JSON it represents.
  if (!isHexadecimal(accessToken) || (accessToken.length % 2) != 0) {
    throw 'Invalid hexadecimal string for accessToken.';
  }

  var strList = accessToken.split("");

  var value = List<String>.from([], growable: true);

  List<String> combineFunc(List<String> acc, String curr, int i) {
    var index = (i ~/ 2) | 0;
    var resultAcc = List<String>.from(acc);

    if (index < resultAcc.length) {
      resultAcc[index] = resultAcc[index] + curr;
    } else {
      resultAcc.add('' + curr);
    }
    return resultAcc;
  }

  for (var i = 0; i < strList.length; i += 1) {
    value = combineFunc(value, strList[i], i);
  }

  var chainJson = value
      .map((e) => int.parse(e, radix: 16))
      .toList()
      .map((e) => String.fromCharCode(e))
      .join("");

  return DelegationChain.fromJSON(chainJson);
}