Merge pull request #26 from loki-project/multi-device-database

[Multi Device] Added Database.
This commit is contained in:
Mikunj Varsani 2019-09-24 14:02:43 +10:00 committed by GitHub
commit ed9c3ae692
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 84 additions and 6 deletions

View File

@ -35,12 +35,7 @@ import org.thoughtcrime.securesms.database.StickerDatabase;
import org.thoughtcrime.securesms.database.ThreadDatabase;
import org.thoughtcrime.securesms.jobs.RefreshPreKeysJob;
import org.thoughtcrime.securesms.logging.Log;
import org.thoughtcrime.securesms.loki.LokiAPIDatabase;
import org.thoughtcrime.securesms.loki.LokiPreKeyRecordDatabase;
import org.thoughtcrime.securesms.loki.LokiMessageDatabase;
import org.thoughtcrime.securesms.loki.LokiPreKeyBundleDatabase;
import org.thoughtcrime.securesms.loki.LokiThreadDatabase;
import org.thoughtcrime.securesms.loki.LokiUserDatabase;
import org.thoughtcrime.securesms.loki.*;
import org.thoughtcrime.securesms.notifications.NotificationChannels;
import org.thoughtcrime.securesms.service.KeyCachingService;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
@ -74,6 +69,7 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper {
private static final int STICKERS = 21;
private static final int lokiV1 = 22;
private static final int lokiV2 = 23;
private static final int lokiV3 = 24;
private static final int DATABASE_VERSION = lokiV2; // Loki - onUpgrade(...) must be updated to use Loki version numbers if Signal makes any database changes
private static final String DATABASE_NAME = "signal.db";
@ -136,6 +132,7 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper {
db.execSQL(LokiThreadDatabase.getCreateSessionResetTableCommand());
db.execSQL(LokiUserDatabase.getCreateDisplayNameTableCommand());
db.execSQL(LokiUserDatabase.getCreateServerDisplayNameTableCommand());
db.execSQL(LokiMultiDeviceDatabase.getCreateTableCommand());
executeStatements(db, SmsDatabase.CREATE_INDEXS);
executeStatements(db, MmsDatabase.CREATE_INDEXS);
@ -498,6 +495,10 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper {
db.execSQL(LokiUserDatabase.getCreateServerDisplayNameTableCommand());
}
if (oldVersion < lokiV3) {
db.execSQL(LokiMultiDeviceDatabase.getCreateTableCommand());
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();

View File

@ -0,0 +1,77 @@
package org.thoughtcrime.securesms.loki
import android.content.ContentValues
import android.content.Context
import net.sqlcipher.Cursor
import org.thoughtcrime.securesms.database.Database
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper
import org.thoughtcrime.securesms.util.Base64
import org.whispersystems.signalservice.loki.api.LokiPairingAuthorisation
import java.util.*
class LokiMultiDeviceDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper) {
companion object {
// Authorisation
private val authorisation_table = "loki_multi_device_authorisation"
private val primaryDevice = "primary_device"
private val secondaryDevice = "secondary_device"
private val requestSignature = "request_signature"
private val grantSignature = "grant_signature"
@JvmStatic
val createTableCommand = "CREATE TABLE $authorisation_table(_id INTEGER PRIMARY KEY AUTOINCREMENT," +
"$primaryDevice TEXT," +
"$secondaryDevice TEXT," +
"$requestSignature TEXT NULLABLE" +
"$grantSignature TEXT NULLABLE" +
");"
}
fun insertOrUpdatePairingAuthorisation(authorisation: LokiPairingAuthorisation) {
val database = databaseHelper.writableDatabase
val values = ContentValues()
values.put(primaryDevice, authorisation.primaryDevicePubKey)
values.put(secondaryDevice, authorisation.secondaryDevicePubKey)
if (authorisation.requestSignature != null) { values.put(requestSignature, Base64.encodeBytes(authorisation.requestSignature)) }
if (authorisation.grantSignature != null) { values.put(grantSignature, Base64.encodeBytes(authorisation.grantSignature)) }
database.insertOrUpdate(authorisation_table, values, "$primaryDevice = ? AND $secondaryDevice = ?", arrayOf(authorisation.primaryDevicePubKey, authorisation.secondaryDevicePubKey))
}
fun getAuthorisationForSecondaryDevice(pubKey: String): LokiPairingAuthorisation? {
val database = databaseHelper.readableDatabase
return database.get(authorisation_table, "$secondaryDevice = ?", arrayOf(pubKey)) { cursor ->
val primaryDevicePubKey = cursor.getString(primaryDevice)
val secondaryDevicePubKey = cursor.getString(secondaryDevice)
val requestSignature: ByteArray? = if (cursor.isNull(cursor.getColumnIndexOrThrow(requestSignature))) null else cursor.getBase64EncodedData(requestSignature)
val grantSignature: ByteArray? = if (cursor.isNull(cursor.getColumnIndexOrThrow(grantSignature))) null else cursor.getBase64EncodedData(grantSignature)
LokiPairingAuthorisation(primaryDevicePubKey, secondaryDevicePubKey, requestSignature, grantSignature)
}
}
fun getSecondaryDevices(primaryDevicePubKey: String): List<String> {
val database = databaseHelper.readableDatabase
var cursor: Cursor? = null
val results = LinkedList<String>()
try {
cursor = database.query(authorisation_table, arrayOf(secondaryDevice), "$primaryDevice = ?", arrayOf(primaryDevicePubKey), null, null, null)
if (cursor != null && cursor.moveToNext()) {
results.add(cursor.getString(secondaryDevice))
}
} catch (e: Exception) {
// Do nothing
} finally {
cursor?.close()
}
return results
}
fun getPrimaryDevice(secondaryDevicePubKey: String): String? {
val database = databaseHelper.readableDatabase
return database.get(authorisation_table, "$secondaryDevice = ?", arrayOf(secondaryDevicePubKey)) { cursor ->
cursor.getString(primaryDevice)
}
}
}