parseApiKey function

ApiKey parseApiKey(
  1. String key
)

Implementation

ApiKey parseApiKey(String key) {
  if (!key.startsWith('ma-')) {
    throw ArgumentError('invalid api key');
  }

  final rest = key.substring(3);
  final firstSeparator = rest.indexOf('-');

  if (firstSeparator == -1) {
    throw ArgumentError('invalid api key');
  }

  final secondSeparator = rest.indexOf('-', firstSeparator + 1);

  if (secondSeparator == -1) {
    throw ArgumentError('invalid api key');
  }

  final idPart = rest.substring(0, firstSeparator);
  final projectPart = rest.substring(firstSeparator + 1, secondSeparator);
  final secret = rest.substring(secondSeparator + 1);

  return ApiKey(id: base64DecompressUuid(idPart), projectId: base64DecompressUuid(projectPart), secret: secret);
}