open method

Map<String, Object?> open(
  1. String state, {
  2. required String? principal,
  3. required String method,
  4. Object? request,
  5. DateTime? now,
})

The data sealed in state, or a RequestStateException when the state was altered, has expired, or belongs to another principal or request.

Implementation

Map<String, Object?> open(
  String state, {
  required String? principal,
  required String method,
  Object? request,
  DateTime? now,
}) {
  final dot = state.indexOf('.');
  if (dot < 0) throw const RequestStateException('malformed requestState');
  final List<int> payload;
  final List<int> mac;
  try {
    payload = base64Url.decode(state.substring(0, dot));
    mac = base64Url.decode(state.substring(dot + 1));
  } on FormatException {
    throw const RequestStateException('malformed requestState');
  }
  if (!_equal(_hmac.convert(payload).bytes, mac)) {
    throw const RequestStateException('requestState failed verification');
  }
  final body = jsonDecode(utf8.decode(payload)) as Map<String, Object?>;
  if ((now ?? DateTime.now()).millisecondsSinceEpoch > (body['e'] as int)) {
    throw const RequestStateException('requestState expired');
  }
  if (body['p'] != principal) {
    throw const RequestStateException('requestState belongs to another principal');
  }
  if (body['m'] != method || body['r'] != _digest(request)) {
    throw const RequestStateException('requestState belongs to another request');
  }
  return Map<String, Object?>.from(body['d'] as Map);
}