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.SERVER_NOT_REACHED);
      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.AUTH_SUCCESS);
      await _sync(_atsign);
    }
  } catch (e) {
    _logger.severe('error in authenticating =>  ${e.toString()}');
    if (e == ResponseStatus.TIME_OUT) {
      c.completeError(e);
    } else {
      c.completeError(
          e.runtimeType == OnboardingStatus ? e : ResponseStatus.AUTH_FAILED);
    }
  }
  return c.future;
}