sessionKeys static method

Future<List<String>?> sessionKeys()

Implementation

static Future<List<String>?> sessionKeys() async {
  int delayedCounter = 0;
  while ((await storage.isCupertinoProtectedDataAvailable()) == false) {
    if (delayedCounter > 10) {
      _log.severe('Secure Store: not available after 10 seconds');
      throw 'Secure Store: not available';
    }
    delayedCounter += 1;
    _log.info('Secure Store: not available yet. Delaying');
    await Future.delayed(const Duration(milliseconds: 50));
  }

  _log.info('Secure Store: available. Attempting to read.');
  if (Platform.isAndroid) {
    // fake read for https://github.com/mogol/flutter_secure_storage/issues/566
    _log.info('Secure Store: fake read for android');
    await storage.read(key: _sessionKey);
  }
  _log.info('Secure Store: attempting to check if $_sessionKey exists');
  String? sessionsStr;
  try {
    sessionsStr = await storage.read(key: _sessionKey);
  } on PlatformException catch (error, stack) {
    if (error.code == '-25300') {
      _log.severe('Ignoring read failure for missing key $_sessionKey');
    } else {
      _log.severe(
        'Ignoring read failure of session key $_sessionKey',
        error,
        stack,
      );
    }
  } catch (error, stack) {
    _log.severe(
      'Ignoring read failure of session key $_sessionKey',
      error,
      stack,
    );
  }

  if (sessionsStr == null) {
    _log.info('Secure Store: session key not found, checking for migration');
    return null;
  }

  _log.info('Secure Store: decoding sessions');
  try {
    final List<dynamic> sessionKeys = json.decode(sessionsStr);
    _log.info('Secure Store: decoding sessions: ${sessionKeys.length} found');
    return sessionKeys.map((e) => e as String).toList();
  } catch (error, stack) {
    _log.severe(
      "Parsing sessions keys '$sessionKeys' failed.",
      error,
      stack,
    );
    return [];
  }
}