Skip to content

Commit 0dfc8f0

Browse files
authored
Merge pull request #1400 from Criptext/multiple-accounts
Multiple Accounts
2 parents 5801acd + 2726d86 commit 0dfc8f0

File tree

183 files changed

+5895
-3100
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

183 files changed

+5895
-3100
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ valgrind-out.txt
1616
CriptextEncrypt.db-shm
1717
CriptextEncrypt.db-wal
1818
recovery.key
19+
umzug.json
1920

2021
# production
2122
*_interface/build

db_interface/src/axolotl/Account.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@ CriptextDB::Account CriptextDB::getAccount(database db, char *recipientId) {
88
string myPrivKey;
99
string myPubKey;
1010
int regId = 0;
11-
db << "select privKey, pubKey, registrationId from account where recipientId == ?;"
11+
int accountId = 0;
12+
db << "select id, privKey, pubKey, registrationId from account where recipientId == ?;"
1213
<< recipientId
13-
>> [&] (string privKey, string pubKey, int registrationId) {
14+
>> [&] (int id, string privKey, string pubKey, int registrationId) {
15+
accountId = id;
1416
myPrivKey = privKey;
1517
myPubKey = pubKey;
1618
regId = registrationId;
1719
};
1820

1921
connection_type con = db.connection();
2022
Account account = {
23+
.id = accountId,
2124
.privKey = myPrivKey,
2225
.pubKey = myPubKey,
2326
.registrationId = regId,
@@ -37,23 +40,25 @@ int CriptextDB::createAccount(database db, char* recipientId, char* name, int de
3740
hasRow = true;
3841
};
3942
if (hasRow) {
40-
db << "update account set name = ?, deviceId = ?, privKey = ?, pubKey = ?, registrationId = ? where recipientId == ?;"
43+
db << "update account set name = ?, deviceId = ?, privKey = ?, pubKey = ?, registrationId = ?, isLoggedIn = false, isActive = false where recipientId == ?;"
4144
<< name
4245
<< deviceId
4346
<< privKey
4447
<< pubKey
4548
<< registrationId
4649
<< recipientId;
4750
} else {
48-
db << "insert into account (recipientId, name, deviceId, jwt, refreshToken, privKey, pubKey, registrationId) values (?,?,?,?,?,?,?,?);"
51+
db << "insert into account (recipientId, name, deviceId, jwt, refreshToken, privKey, pubKey, registrationId, isLoggedIn, isActive) values (?,?,?,?,?,?,?,?,?,?);"
4952
<< recipientId
5053
<< name
5154
<< deviceId
5255
<< ""
5356
<< ""
5457
<< privKey
5558
<< pubKey
56-
<< registrationId;
59+
<< registrationId
60+
<< false
61+
<< false;
5762
}
5863
db << "commit;";
5964
} catch (exception& e) {

db_interface/src/axolotl/Account.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ using namespace std;
1414

1515
namespace CriptextDB {
1616
struct Account {
17+
int id;
1718
string privKey;
1819
string pubKey;
1920
int registrationId;

db_interface/src/axolotl/IdentityKey.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
using namespace sqlite;
66
using namespace std;
77

8-
CriptextDB::IdentityKey CriptextDB::getIdentityKey(database db, string recipientId, long int deviceId) {
8+
CriptextDB::IdentityKey CriptextDB::getIdentityKey(database db, int accountId, string recipientId, long int deviceId) {
99
IdentityKey identityKey;
10-
db << "Select * from identitykeyrecord where recipientId == ? and deviceId == ?;"
10+
db << "Select * from identitykeyrecord where recipientId == ? and deviceId == ? and accountId == ?;"
1111
<< recipientId
1212
<< deviceId
13-
>> [&] (string recipientId, int deviceId, string identity) {
13+
<< accountId
14+
>> [&] (int id, string recipientId, int deviceId, string identity) {
1415
identityKey = {
1516
.recipientId = recipientId,
1617
.deviceId = deviceId,
@@ -24,26 +25,29 @@ CriptextDB::IdentityKey CriptextDB::getIdentityKey(database db, string recipient
2425
return identityKey;
2526
}
2627

27-
bool CriptextDB::createIdentityKey(database db, string recipientId, int deviceId, char *identityKey) {
28+
bool CriptextDB::createIdentityKey(database db, int accountId, string recipientId, int deviceId, char *identityKey) {
2829
try {
2930
bool hasRow = false;
3031
db << "begin;";
31-
db << "Select * from identitykeyrecord where recipientId == ? and deviceId == ?;"
32+
db << "Select * from identitykeyrecord where recipientId == ? and deviceId == ? and accountId == ?;"
3233
<< recipientId
3334
<< deviceId
35+
<< accountId
3436
>> [&] (string recipientId, int deviceId, string identity) {
3537
hasRow = true;
3638
};
3739
if (hasRow) {
38-
db << "update identitykeyrecord set identityKey = ? where recipientId == ? and deviceId == ?;"
40+
db << "update identitykeyrecord set identityKey = ? where recipientId == ? and deviceId == ? and accountId == ?;"
3941
<< identityKey
4042
<< recipientId
41-
<< deviceId;
43+
<< deviceId
44+
<< accountId;
4245
} else {
43-
db << "insert into identitykeyrecord (recipientId, deviceId, identityKey) values (?,?,?);"
46+
db << "insert into identitykeyrecord (recipientId, deviceId, identityKey, accountId) values (?,?,?,?);"
4447
<< recipientId
4548
<< deviceId
46-
<< identityKey;
49+
<< identityKey
50+
<< accountId;
4751
}
4852
db << "commit;";
4953
} catch (exception& e) {

db_interface/src/axolotl/IdentityKey.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ namespace CriptextDB {
1616
string identityKey;
1717
};
1818

19-
IdentityKey getIdentityKey(database db, string recipientId, long int deviceId);
20-
bool createIdentityKey(database db, string recipientId, int deviceId, char *identityKey);
19+
IdentityKey getIdentityKey(database db, int accountId, string recipientId, long int deviceId);
20+
bool createIdentityKey(database db, int accountId, string recipientId, int deviceId, char *identityKey);
2121
}
2222

2323
#endif

db_interface/src/axolotl/PreKey.cpp

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
using namespace std;
44
using namespace sqlite;
55

6-
CriptextDB::PreKey CriptextDB::getPreKey(database db, short int id) {
6+
CriptextDB::PreKey CriptextDB::getPreKey(database db, int accountId, short int id) {
77
string myPreKey;
88
size_t myLen = 0;
9-
db << "Select * from prekeyrecord where preKeyId == ?;"
9+
db << "Select * from prekeyrecord where preKeyId == ? and accountId == ?;"
1010
<< id
11-
>> [&] (int preKeyId, string record, int recordLength) {
11+
<< accountId
12+
>> [&] (int id, int preKeyId, string record, int recordLength) {
1213
myPreKey = record;
1314
myLen = (size_t)recordLength;
1415
};
@@ -24,23 +25,42 @@ CriptextDB::PreKey CriptextDB::getPreKey(database db, short int id) {
2425
return preKey;
2526
}
2627

27-
bool CriptextDB::createPreKey(database db, short int id, char *keyRecord, size_t len) {
28+
bool CriptextDB::createPreKey(database db, int accountId, short int id, char *keyRecord, size_t len) {
2829
try {
29-
db << "insert into prekeyrecord (preKeyId, record, recordLength) values (?,?,?);"
30+
bool hasRow = false;
31+
db << "begin;";
32+
db << "Select preKeyId from prekeyrecord where preKeyId == ? and accountId == ?;"
3033
<< id
31-
<< keyRecord
32-
<< static_cast<int>(len);
34+
<< accountId
35+
>> [&] (string preKeyId) {
36+
hasRow = true;
37+
};
38+
if (hasRow) {
39+
db << "update prekeyrecord set record = ?, recordLength = ? where preKeyId == ? and accountId == ?;"
40+
<< keyRecord
41+
<< static_cast<int>(len)
42+
<< id
43+
<< accountId;
44+
} else {
45+
db << "insert into prekeyrecord (preKeyId, record, recordLength, accountId) values (?,?,?,?);"
46+
<< id
47+
<< keyRecord
48+
<< static_cast<int>(len)
49+
<< accountId;
50+
}
51+
db << "commit;";
3352
return true;
3453
} catch (exception& e) {
3554
std::cout << e.what() << std::endl;
3655
return false;
3756
}
3857
}
3958

40-
bool CriptextDB::deletePreKey(database db, short int id) {
59+
bool CriptextDB::deletePreKey(database db, int accountId, short int id) {
4160
try {
42-
db << "delete from prekeyrecord where preKeyId == ?;"
43-
<< id;
61+
db << "delete from prekeyrecord where preKeyId == ? and accountId == ?;"
62+
<< id
63+
<< accountId;
4464
return true;
4565
} catch (exception& e) {
4666
std::cout << e.what() << std::endl;

db_interface/src/axolotl/PreKey.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ namespace CriptextDB {
1717
size_t len;
1818
};
1919

20-
PreKey getPreKey(database db, short int id);
21-
bool createPreKey(database db, short int id, char *keyRecord, size_t len);
22-
bool deletePreKey(database db, short int id);
20+
PreKey getPreKey(database db, int accountId, short int id);
21+
bool createPreKey(database db, int accountId, short int id, char *keyRecord, size_t len);
22+
bool deletePreKey(database db, int accountId, short int id);
2323

2424
}
2525

db_interface/src/axolotl/SessionRecord.cpp

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
using namespace sqlite;
44
using namespace std;
55

6-
CriptextDB::SessionRecord CriptextDB::getSessionRecord(database db, string recipientId, long int deviceId) {
6+
CriptextDB::SessionRecord CriptextDB::getSessionRecord(database db, int accountId, string recipientId, long int deviceId) {
77
string myRecord;
88
int myLen = 0;
9-
db << "Select * from sessionrecord where recipientId == ? and deviceId == ?;"
9+
db << "Select * from sessionrecord where recipientId == ? and deviceId == ? and accountId == ?;"
1010
<< recipientId
1111
<< deviceId
12-
>> [&] (string recipientId, int deviceId, string record, int recordLength) {
12+
<< accountId
13+
>> [&] (int id, string recipientId, int deviceId, string record, int recordLength) {
1314
myLen = recordLength;
1415
myRecord = record;
1516
};
@@ -26,13 +27,14 @@ CriptextDB::SessionRecord CriptextDB::getSessionRecord(database db, string recip
2627
return sessionRecord;
2728
}
2829

29-
vector<CriptextDB::SessionRecord> CriptextDB::getSessionRecords(database db, string recipientId) {
30+
vector<CriptextDB::SessionRecord> CriptextDB::getSessionRecords(database db, int accountId, string recipientId) {
3031
vector<CriptextDB::SessionRecord> sessionRecords;
3132

3233
try {
33-
db << "Select * from sessionrecord where recipientId == ?;"
34+
db << "Select * from sessionrecord where recipientId == ? and accountId == ?;"
3435
<< recipientId
35-
>> [&] (string recipientId, int deviceId, string record, int recordLength) {
36+
<< accountId
37+
>> [&] (int id, string recipientId, int deviceId, string record, int recordLength) {
3638
SessionRecord mySessionRecord = {
3739
.recipientId = recipientId,
3840
.deviceId = deviceId,
@@ -49,28 +51,31 @@ vector<CriptextDB::SessionRecord> CriptextDB::getSessionRecords(database db, str
4951
return sessionRecords;
5052
}
5153

52-
bool CriptextDB::createSessionRecord(database db, string recipientId, long int deviceId, char* record, size_t len) {
54+
bool CriptextDB::createSessionRecord(database db, int accountId, string recipientId, long int deviceId, char* record, size_t len) {
5355
try {
5456
bool hasRow = false;
5557
db << "begin;";
56-
db << "Select * from sessionrecord where recipientId == ? and deviceId == ?;"
58+
db << "Select * from sessionrecord where recipientId == ? and deviceId == ? and accountId == ?;"
5759
<< recipientId
5860
<< deviceId
61+
<< accountId
5962
>> [&] (string recipientId, int deviceId, string record, int recordLength) {
6063
hasRow = true;
6164
};
6265
if (hasRow) {
63-
db << "update sessionrecord set record = ?, recordLength = ? where recipientId == ? and deviceId == ?;"
66+
db << "update sessionrecord set record = ?, recordLength = ? where recipientId == ? and deviceId == ? and accountId == ?;"
6467
<< record
6568
<< static_cast<int>(len)
6669
<< recipientId
67-
<< deviceId;
70+
<< deviceId
71+
<< accountId;
6872
} else {
69-
db << "insert into sessionrecord (recipientId, deviceId, record, recordLength) values (?,?,?,?);"
73+
db << "insert into sessionrecord (recipientId, deviceId, record, recordLength, accountId) values (?,?,?,?,?);"
7074
<< recipientId
7175
<< deviceId
7276
<< record
73-
<< static_cast<int>(len);
77+
<< static_cast<int>(len)
78+
<< accountId;
7479
}
7580
db << "commit;";
7681
} catch (exception& e) {
@@ -80,22 +85,24 @@ bool CriptextDB::createSessionRecord(database db, string recipientId, long int d
8085
return true;
8186
}
8287

83-
bool CriptextDB::deleteSessionRecord(database db, string recipientId, long int deviceId) {
88+
bool CriptextDB::deleteSessionRecord(database db, int accountId, string recipientId, long int deviceId) {
8489
try {
85-
db << "delete from sessionrecord where recipientId == ? and deviceId == ?;"
90+
db << "delete from sessionrecord where recipientId == ? and deviceId == ? and accountId;"
8691
<< recipientId
87-
<< deviceId;
92+
<< deviceId
93+
<< accountId;
8894
} catch (exception& e) {
8995
std::cout << "Error deleting session : " << e.what() << std::endl;
9096
return false;
9197
}
9298
return true;
9399
}
94100

95-
bool CriptextDB::deleteSessionRecords(database db, string recipientId) {
101+
bool CriptextDB::deleteSessionRecords(database db, int accountId, string recipientId) {
96102
try {
97-
db << "delete from sessionrecord where recipientId == ?;"
98-
<< recipientId;
103+
db << "delete from sessionrecord where recipientId == ? and accountId == ?;"
104+
<< recipientId
105+
<< accountId;
99106
} catch (exception& e) {
100107
std::cout << "Error deleting sessions : " << e.what() << std::endl;
101108
return false;

db_interface/src/axolotl/SessionRecord.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ namespace CriptextDB {
2020
size_t len;
2121
};
2222

23-
SessionRecord getSessionRecord(database db, string recipientId, long int deviceId);
24-
vector<SessionRecord> getSessionRecords(database db, string recipientId);
25-
bool createSessionRecord(database db, string recipientId, long int deviceId, char* record, size_t len);
26-
bool deleteSessionRecord(database db, string recipientId, long int deviceId);
27-
bool deleteSessionRecords(database db, string recipientId);
23+
SessionRecord getSessionRecord(database db, int accountId, string recipientId, long int deviceId);
24+
vector<SessionRecord> getSessionRecords(database db, int accountId, string recipientId);
25+
bool createSessionRecord(database db, int accountId, string recipientId, long int deviceId, char* record, size_t len);
26+
bool deleteSessionRecord(database db, int accountId, string recipientId, long int deviceId);
27+
bool deleteSessionRecords(database db, int accountId, string recipientId);
2828
}
2929

3030
#endif

0 commit comments

Comments
 (0)