authenticate method

Future<AtOnboardingResponseStatus> authenticate(
  1. String? atsign, {
  2. String? cramSecret,
  3. String? jsonData,
  4. String? decryptKey,
  5. OnboardingStatus? status,
})

Returns false if fails in authenticating atsign with cramSecret/privateKey. Throws Excpetion if atsign is null.

Implementation

Future<AtOnboardingResponseStatus> authenticate(String? atsign,
    {String? cramSecret,
    String? jsonData,
    String? decryptKey,
    OnboardingStatus? status}) async {
  _isPkam = false;
  atsign = formatAtSign(atsign);
  if (atsign == null) {
    throw 'atSign cannot be null';
  }
  Completer<AtOnboardingResponseStatus> c =
      Completer<AtOnboardingResponseStatus>();
  try {
    serverStatus = await _checkAtSignServerStatus(atsign);
    if (serverStatus != ServerStatus.teapot &&
        serverStatus != ServerStatus.activated) {
      c.complete(AtOnboardingResponseStatus.serverNotReached);
      if (cramSecret == null) {
        _isPkam = true;
      }
      return c.future;
    }
    AtClientService atClientService = _getClientServiceForAtsign(atsign)!;
    _atClientPreference.cramSecret = cramSecret;
    if (cramSecret != null) {
      _atClientPreference.privateKey = null;
    }
    bool isAuthenticated = await atClientService.authenticate(
        atsign, _atClientPreference,
        jsonData: jsonData, decryptKey: decryptKey, status: status);
    if (isAuthenticated) {
      _atsign = atsign;
      atClientServiceMap.putIfAbsent(_atsign, () => atClientService);
      c.complete(AtOnboardingResponseStatus.authSuccess);
      await _sync(_atsign);
    }
  } catch (e) {
    _logger.severe('error in authenticating =>  ${e.toString()}');
    if (e == AtOnboardingResponseStatus.timeOut) {
      c.completeError(e);
    } else {
      c.completeError(e.runtimeType == OnboardingStatus
          ? e
          : AtOnboardingResponseStatus.authFailed);
    }
  }
  return c.future;
}