authenticate method

Future 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<dynamic> authenticate(String? atsign,
    {String? cramSecret,
    String? jsonData,
    String? decryptKey,
    OnboardingStatus? status}) async {
  _isPkam = false;
  atsign = formatAtSign(atsign);
  if (atsign == null) {
    throw '@sign cannot be null';
  }
  Completer<dynamic> c = Completer<dynamic>();
  try {
    serverStatus = await _checkAtSignServerStatus(atsign);
    if (serverStatus != ServerStatus.teapot &&
        serverStatus != ServerStatus.activated) {
      c.complete(ResponseStatus.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(ResponseStatus.authSuccess);
      await _sync(_atsign);
    }
  } catch (e) {
    _logger.severe('error in authenticating =>  ${e.toString()}');
    if (e == ResponseStatus.timeOut) {
      c.completeError(e);
    } else {
      c.completeError(
          e.runtimeType == OnboardingStatus ? e : ResponseStatus.authFailed);
    }
  }
  return c.future;
}