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 (!isHex(accessToken)) {
    throw ArgumentError.value(
      accessToken,
      'accessToken',
      'Invalid hexadecimal string',
    );
  }
  final strList = accessToken.split('');
  List<String> value = List<String>.from([]);

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

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

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

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

  return DelegationChain.fromJSON(chainJson);
}