Java Code Examples for org.whispersystems.signalservice.api.util.UuidUtil#parseOrThrow()

The following examples show how to use org.whispersystems.signalservice.api.util.UuidUtil#parseOrThrow() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RecipientStore.java    From signal-cli with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Set<SignalServiceAddress> deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
    JsonNode node = jsonParser.getCodec().readTree(jsonParser);

    Set<SignalServiceAddress> addresses = new HashSet<>();

    if (node.isArray()) {
        for (JsonNode recipient : node) {
            String recipientName = recipient.get("name").asText();
            UUID uuid = UuidUtil.parseOrThrow(recipient.get("uuid").asText());
            final SignalServiceAddress serviceAddress = new SignalServiceAddress(uuid, recipientName);
            addresses.add(serviceAddress);
        }
    }

    return addresses;
}
 
Example 2
Source File: Recipient.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns a fully-populated {@link Recipient} based off of a string identifier, creating one in
 * the database if necessary. The identifier may be a uuid, phone number, email,
 * or serialized groupId.
 *
 * If the identifier is a UUID of a Signal user, prefer using
 * {@link #externalPush(Context, UUID, String)} or its overload, as this will let us associate
 * the phone number with the recipient.
 */
@WorkerThread
public static @NonNull Recipient external(@NonNull Context context, @NonNull String identifier) {
  Preconditions.checkNotNull(identifier, "Identifier cannot be null!");

  RecipientDatabase db = DatabaseFactory.getRecipientDatabase(context);
  RecipientId       id = null;

  if (UuidUtil.isUuid(identifier)) {
    UUID uuid = UuidUtil.parseOrThrow(identifier);

    if (FeatureFlags.uuids()) {
      id = db.getOrInsertFromUuid(uuid);
    } else {
      Optional<RecipientId> possibleId = db.getByUuid(uuid);

      if (possibleId.isPresent()) {
        id = possibleId.get();
      } else {
        if (!FeatureFlags.uuids() && FeatureFlags.groupsV2()) {
          throw new RuntimeException(new UuidRecipientError());
        } else {
          throw new UuidRecipientError();
        }
      }
    }
  } else if (GroupId.isEncodedGroup(identifier)) {
    id = db.getOrInsertFromGroupId(GroupId.parseOrThrow(identifier));
  } else if (NumberUtil.isValidEmail(identifier)) {
    id = db.getOrInsertFromEmail(identifier);
  } else {
    String e164 = PhoneNumberFormatter.get(context).format(identifier);
    id = db.getOrInsertFromE164(e164);
  }

  return Recipient.resolved(id);
}
 
Example 3
Source File: CodeVerificationRequest.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
private static void verifyAccount(@NonNull Context context,
                                  @NonNull Credentials credentials,
                                  @NonNull String code,
                                  @Nullable String pin,
                                  @Nullable TokenResponse kbsTokenResponse,
                                  @Nullable String kbsStorageCredentials,
                                  @Nullable String fcmToken)
  throws IOException, KeyBackupSystemWrongPinException, KeyBackupSystemNoDataException
{
  boolean    isV2RegistrationLock        = kbsTokenResponse != null;
  int        registrationId              = KeyHelper.generateRegistrationId(false);
  boolean    universalUnidentifiedAccess = TextSecurePreferences.isUniversalUnidentifiedAccess(context);
  ProfileKey profileKey                  = findExistingProfileKey(context, credentials.getE164number());

  if (profileKey == null) {
    profileKey = ProfileKeyUtil.createNew();
    Log.i(TAG, "No profile key found, created a new one");
  }

  byte[] unidentifiedAccessKey = UnidentifiedAccess.deriveAccessKeyFrom(profileKey);

  TextSecurePreferences.setLocalRegistrationId(context, registrationId);
  SessionUtil.archiveAllSessions(context);

  SignalServiceAccountManager accountManager     = AccountManagerFactory.createUnauthenticated(context, credentials.getE164number(), credentials.getPassword());
  KbsPinData                  kbsData            = isV2RegistrationLock ? PinState.restoreMasterKey(pin, kbsStorageCredentials, kbsTokenResponse) : null;
  String                      registrationLockV2 = kbsData != null ? kbsData.getMasterKey().deriveRegistrationLock() : null;
  String                      registrationLockV1 = isV2RegistrationLock ? null : pin;
  boolean                     hasFcm             = fcmToken != null;

  Log.i(TAG, "Calling verifyAccountWithCode(): reglockV1? " + !TextUtils.isEmpty(registrationLockV1) + ", reglockV2? " + !TextUtils.isEmpty(registrationLockV2));

  VerifyAccountResponse response = accountManager.verifyAccountWithCode(code,
                                                                        null,
                                                                        registrationId,
                                                                        !hasFcm,
                                                                        registrationLockV1,
                                                                        registrationLockV2,
                                                                        unidentifiedAccessKey,
                                                                        universalUnidentifiedAccess,
                                                                        AppCapabilities.getCapabilities(true));

  UUID    uuid   = UuidUtil.parseOrThrow(response.getUuid());
  boolean hasPin = response.isStorageCapable();

  IdentityKeyPair    identityKey  = IdentityKeyUtil.getIdentityKeyPair(context);
  List<PreKeyRecord> records      = PreKeyUtil.generatePreKeys(context);
  SignedPreKeyRecord signedPreKey = PreKeyUtil.generateSignedPreKey(context, identityKey, true);

  accountManager = AccountManagerFactory.createAuthenticated(context, uuid, credentials.getE164number(), credentials.getPassword());
  accountManager.setPreKeys(identityKey.getPublicKey(), signedPreKey, records);

  if (hasFcm) {
    accountManager.setGcmId(Optional.fromNullable(fcmToken));
  }

  RecipientDatabase recipientDatabase = DatabaseFactory.getRecipientDatabase(context);
  RecipientId       selfId            = recipientDatabase.getOrInsertFromE164(credentials.getE164number());

  recipientDatabase.setProfileSharing(selfId, true);
  recipientDatabase.markRegistered(selfId, uuid);

  TextSecurePreferences.setLocalNumber(context, credentials.getE164number());
  TextSecurePreferences.setLocalUuid(context, uuid);
  recipientDatabase.setProfileKey(selfId, profileKey);
  ApplicationDependencies.getRecipientCache().clearSelf();

  TextSecurePreferences.setFcmToken(context, fcmToken);
  TextSecurePreferences.setFcmDisabled(context, !hasFcm);
  TextSecurePreferences.setWebsocketRegistered(context, true);

  DatabaseFactory.getIdentityDatabase(context)
                 .saveIdentity(selfId,
                               identityKey.getPublicKey(), IdentityDatabase.VerifiedStatus.VERIFIED,
                               true, System.currentTimeMillis(), true);

  TextSecurePreferences.setVerifying(context, false);
  TextSecurePreferences.setPushRegistered(context, true);
  TextSecurePreferences.setPushServerPassword(context, credentials.getPassword());
  TextSecurePreferences.setSignedPreKeyRegistered(context, true);
  TextSecurePreferences.setPromptedPushRegistration(context, true);
  TextSecurePreferences.setUnauthorizedReceived(context, false);

  PinState.onRegistration(context, kbsData, pin, hasPin);
}