verifyToken method

  1. @override
Future<bool> verifyToken(
  1. Token token
)
override

Verifies the given token is valid.

Implementation

@override
Future<bool> verifyToken(Token token) async {
  log('Verifying token for ${token.webservice.name}');

  try {
    final respone = await _apiService.callFunction(
      function: 'tokenverify',
      token: token.token,
      body: {},
    );

    log('API call succeeded, this should not happen', respone);

    return true; // this will never be reached
  } on ApiServiceException catch (e, stack) {
    final body = e.data;

    if (body == null) {
      log('Token verification failed', e, stack);

      return false;
    }

    final errorCode = body['errorcode'] as String?;

    if (errorCode == 'accessexception') {
      log('Token expired', body);

      return false;
    }

    if (errorCode == 'invalidtoken') {
      log('Token is invalid', body);

      return false;
    }

    if (errorCode == 'invalidrecord') {
      // The 'invalidrecord' error code indicates that the token is valid, but
      // the specific record being accessed does not exist because we passed
      // an empty function name. This means the token itself has passed the
      // verification step and thus is valid.
      log('Token verified');

      return true;
    }

    log('Unknown errorcode. Assuming token is invalid', body);

    return false;
  } catch (e, stack) {
    log('Token verification failed', e, stack);

    return false;
  }
}