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 Exception 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;
    }
    AtAuthService atAuthService =
        AtClientMobile.authService(atsign, atClientPreference);
    _atClientPreference.cramSecret = cramSecret;
    if (cramSecret != null) {
      _atClientPreference.privateKey = null;
    }

    AtAuthRequest atAuthRequest = AtAuthRequest(atsign)
      ..rootDomain = _atClientPreference.rootDomain
      ..rootPort = _atClientPreference.rootPort;

    if (jsonData != null) {
      atAuthRequest.encryptedKeysMap = jsonDecode(jsonData);
    }

    AtAuthResponse atAuthResponse =
        await atAuthService.authenticate(atAuthRequest);
    if (atAuthResponse.isSuccessful) {
      _atsign = atsign;
      // Initializing "AtClientService" and adding to it map for backward compatibility.
      // Invoke init() method to initialize AtClient and AtLookup instances in atClientService.
      // ignore: deprecated_member_use, deprecated_export_use
      AtClientService atClientService = AtClientService();
      // ignore: deprecated_export_use
      // ignore: deprecated_member_use_from_same_package
      atClientServiceMap.putIfAbsent(_atsign, () => atClientService);
      c.complete(AtOnboardingResponseStatus.authSuccess);
    }
  } 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;
}