-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SIANXSVC-1230: migrate old realm accounts to new authenticator app
- Loading branch information
1 parent
2831038
commit cc1b2d2
Showing
34 changed files
with
3,649 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package com.twofasapp.migration; | ||
|
||
import android.net.Uri; | ||
|
||
import java.util.Date; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.UUID; | ||
|
||
import io.realm.Realm; | ||
import io.realm.RealmObject; | ||
import io.realm.RealmQuery; | ||
import io.realm.RealmResults; | ||
import io.realm.Sort; | ||
import io.realm.annotations.Ignore; | ||
import io.realm.annotations.PrimaryKey; | ||
|
||
/** | ||
* Created by mrauter on 28.02.18. | ||
*/ | ||
|
||
public class Account extends RealmObject { | ||
|
||
@PrimaryKey | ||
private String id = UUID.randomUUID().toString(); | ||
private String secret; | ||
private String issuer; | ||
private String username; | ||
private int digits = 6; | ||
private int period = 30; | ||
private Boolean engine = false; | ||
private Date created = new Date(); | ||
|
||
@Ignore | ||
private Boolean selected = false; | ||
|
||
public Account() { | ||
|
||
} | ||
|
||
public Account(String secret, String issuer, String username, int digits, int period, Boolean engine) { | ||
this.secret = secret; | ||
this.issuer = issuer; | ||
this.username = username; | ||
this.digits = digits; | ||
this.period = period; | ||
this.engine = engine; | ||
} | ||
|
||
public static List<Account> readAllFromRealm() { | ||
Realm realm = Realm.getDefaultInstance(); | ||
RealmQuery<Account> query = realm.where(Account.class); | ||
RealmResults<Account> results = query.findAll().sort("created", Sort.ASCENDING); | ||
if (results != null) { | ||
return realm.copyFromRealm(results); | ||
} | ||
return null; | ||
} | ||
|
||
public static void writeToRealm(Account account) { | ||
if (account != null) { | ||
Realm realm = Realm.getDefaultInstance(); | ||
realm.beginTransaction(); | ||
realm.copyToRealm(account); | ||
realm.commitTransaction(); | ||
} | ||
} | ||
|
||
public static void deleteFromRealm(Account account) { | ||
if (account != null) { | ||
Realm realm = Realm.getDefaultInstance(); | ||
realm.beginTransaction(); | ||
Account result = realm.where(Account.class).equalTo("id", account.getId()).findFirst(); | ||
if(result != null) { | ||
result.deleteFromRealm(); | ||
} | ||
realm.commitTransaction(); | ||
} | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
|
||
public String getSecret() { | ||
return secret; | ||
} | ||
|
||
public void setSecret(String secret) { | ||
this.secret = secret; | ||
} | ||
|
||
public String getIssuer() { | ||
return issuer; | ||
} | ||
|
||
public void setIssuer(String issuer) { | ||
this.issuer = issuer; | ||
} | ||
|
||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
public void setUsername(String username) { | ||
this.username = username; | ||
} | ||
|
||
public Boolean getSelected() { | ||
return selected; | ||
} | ||
|
||
public void setSelected(Boolean selected) { | ||
this.selected = selected; | ||
} | ||
|
||
public int getDigits() { | ||
return digits; | ||
} | ||
|
||
public void setDigits(int digits) { | ||
this.digits = digits; | ||
} | ||
|
||
public int getPeriod() { | ||
return period; | ||
} | ||
|
||
public void setPeriod(int period) { | ||
this.period = period; | ||
} | ||
|
||
public Boolean getEngine() { | ||
return engine; | ||
} | ||
|
||
public void setEngine(Boolean engine) { | ||
this.engine = engine; | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
app/src/main/java/com/twofasapp/migration/MigrateRealmToRoom.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package com.twofasapp.migration | ||
|
||
import android.content.Context | ||
import com.twofasapp.common.domain.BackupSyncStatus | ||
import com.twofasapp.common.domain.Service | ||
import com.twofasapp.common.domain.Service.Algorithm | ||
import com.twofasapp.common.time.TimeProvider | ||
import com.twofasapp.data.services.ServicesRepository | ||
import com.twofasapp.parsers.ServiceIcons | ||
import com.twofasapp.prefs.usecase.MigratedRealmToRoomPreference | ||
import io.realm.Realm | ||
import io.realm.RealmConfiguration | ||
|
||
class MigrateRealmToRoom( | ||
private val context: Context, | ||
private val servicesRepository: ServicesRepository, | ||
private val migratedRealmToRoomPreference: MigratedRealmToRoomPreference, | ||
private val timeProvider: TimeProvider, | ||
) { | ||
|
||
suspend fun invoke() { | ||
try { | ||
if (migratedRealmToRoomPreference.get()) { | ||
return | ||
} | ||
|
||
if (context.filesDir?.list()?.contains("default.realm") == false) { | ||
return | ||
} | ||
|
||
val oldAccounts = getDataFromRealmDb() | ||
val now = timeProvider.systemCurrentTime() | ||
|
||
servicesRepository.addServices( | ||
oldAccounts.map { | ||
Service( | ||
id = 0, | ||
name = it.issuer, | ||
secret = it.secret, | ||
authType = com.twofasapp.common.domain.Service.AuthType.TOTP, | ||
backupSyncStatus = BackupSyncStatus.NOT_SYNCED, | ||
updatedAt = now, | ||
assignedDomains = emptyList(), | ||
serviceTypeId = null, | ||
iconCollectionId = ServiceIcons.defaultCollectionId, | ||
imageType = Service.ImageType.Label, | ||
labelText = it.issuer.take(2).uppercase(), | ||
source = com.twofasapp.common.domain.Service.Source.Manual, | ||
info = it.username, | ||
link = null, | ||
issuer = it.issuer, | ||
period = it.period, | ||
digits = it.digits, | ||
algorithm = Algorithm.SHA1, | ||
iconLight = "", | ||
iconDark = "", | ||
badgeColor = null, | ||
tags = listOf(), | ||
isDeleted = false, | ||
) | ||
} | ||
) | ||
|
||
val roomServices = servicesRepository.getServices() | ||
val isSuccess = roomServices.map { it.secret.lowercase().trim() }.distinct() | ||
.containsAll(oldAccounts.map { it.secret.lowercase().trim() }.distinct()) | ||
migratedRealmToRoomPreference.put(isSuccess) | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
} | ||
} | ||
|
||
private fun getDataFromRealmDb(): List<Account> { | ||
Realm.init(context) | ||
val config = RealmConfiguration.Builder().schemaVersion(1).build() | ||
Realm.setDefaultConfiguration(config) | ||
|
||
val accounts = Account.readAllFromRealm() | ||
accounts.forEach { account -> | ||
if (account.issuer == null) { | ||
account.issuer = account.username | ||
account.username = "" | ||
} | ||
} | ||
return accounts | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="24dp" | ||
android:height="24dp" | ||
android:viewportWidth="960" | ||
android:viewportHeight="960"> | ||
android:viewportWidth="24" | ||
android:viewportHeight="24"> | ||
<path | ||
android:fillColor="#FF000000" | ||
android:pathData="M477.74,819.22q-141.35,0 -240.28,-98.93Q138.52,621.35 138.52,480q0,-141.35 98.93,-240.28 98.93,-98.93 240.28,-98.93 74.09,0 140.76,30.19 66.67,30.2 113.93,87.15v-72.83q0,-18.92 12.8,-31.72 12.8,-12.8 31.72,-12.8 18.92,0 31.72,12.8t12.8,31.72v210.17q0,22.09 -15.46,37.54 -15.46,15.46 -37.54,15.46L557.17,448.48q-18.68,0 -31.32,-12.8 -12.64,-12.8 -12.64,-31.72 0,-18.68 12.8,-31.32Q538.82,360 557.74,360h120.09q-32,-52.61 -85,-82.91 -53.01,-30.3 -115.08,-30.3 -97.17,0 -165.2,68.02Q244.52,382.83 244.52,480q0,97.17 68.02,165.2 68.02,68.02 165.2,68.02 61.78,0 114.04,-30.26 52.26,-30.26 83.83,-80.91 11.18,-17.48 31.44,-24.35 20.26,-6.87 39.63,0.39 20.67,7.26 29.93,26.65 9.26,19.39 -1,36.78 -44.39,80.57 -123.5,129.13 -79.11,48.56 -174.37,48.56Z"/> | ||
android:pathData="M16.028,20.505C15.95,20.505 15.882,20.495 15.823,20.476C15.667,20.397 15.589,20.261 15.589,20.065V17.487H10.96C10.843,17.487 10.735,17.448 10.638,17.37C10.56,17.292 10.521,17.194 10.521,17.077C10.521,16.94 10.56,16.833 10.638,16.755C10.735,16.657 10.843,16.608 10.96,16.608H15.999C16.136,16.608 16.243,16.657 16.321,16.755C16.399,16.833 16.448,16.931 16.468,17.048V19.187L22.532,14.528L16.468,9.87V12.009C16.448,12.126 16.399,12.233 16.321,12.331C16.243,12.409 16.146,12.448 16.028,12.448H8.44V15.026C8.44,15.202 8.362,15.329 8.206,15.407C8.05,15.485 7.903,15.476 7.767,15.378L0.501,9.841C0.403,9.743 0.345,9.626 0.325,9.489C0.325,9.333 0.384,9.216 0.501,9.138L7.767,3.601C7.903,3.483 8.05,3.464 8.206,3.542C8.362,3.62 8.44,3.757 8.44,3.952V6.501H13.069C13.186,6.521 13.284,6.569 13.362,6.647C13.46,6.726 13.509,6.833 13.509,6.97C13.509,7.087 13.46,7.185 13.362,7.263C13.284,7.341 13.186,7.39 13.069,7.409H8.001C7.884,7.39 7.776,7.341 7.679,7.263C7.601,7.185 7.562,7.077 7.562,6.94V4.831L1.468,9.489L7.562,14.148V12.009C7.562,11.892 7.601,11.794 7.679,11.716C7.776,11.618 7.884,11.569 8.001,11.569H15.56V8.991C15.579,8.815 15.667,8.688 15.823,8.61C15.979,8.532 16.126,8.542 16.263,8.64L23.499,14.177C23.616,14.274 23.675,14.401 23.675,14.558C23.675,14.694 23.616,14.802 23.499,14.88L16.263,20.417C16.204,20.476 16.126,20.505 16.028,20.505Z" | ||
android:fillColor="#77BC1F"/> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.