Skip to content

Commit 78bb5c0

Browse files
committed
Testing non encrypted / encrypted
1 parent 8d1a3c5 commit 78bb5c0

File tree

4 files changed

+100
-26
lines changed

4 files changed

+100
-26
lines changed

electron/rollup.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export default {
1616
'@capacitor/core',
1717
'electron',
1818
'@journeyapps/sqlcipher',
19+
'sqlite3',
1920
'path',
2021
'fs',
2122
'os',

electron/src/electron-utils/utilsFile.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export class UtilsFile {
1414
sep = '/';
1515
appPath: string;
1616
capConfig: any;
17+
isEncryption = false;
1718

1819
constructor() {
1920
this.Path = require('path');
@@ -49,25 +50,41 @@ export class UtilsFile {
4950
).toString(),
5051
);
5152
}
53+
this.isEncryption = this.capConfig.plugins.CapacitorSQLite.electronIsEncryption
54+
? this.capConfig.plugins.CapacitorSQLite.electronIsEncryption
55+
: false;
5256
this.osType = this.Os.type();
5357
switch (this.osType) {
5458
case 'Darwin':
5559
this.pathDB =
56-
this.capConfig.plugins.CapacitorSQLite.electronMacLocation;
60+
this.capConfig.plugins.CapacitorSQLite.electronMacLocation
61+
? this.capConfig.plugins.CapacitorSQLite.electronMacLocation
62+
: 'Databases';
5763
break;
5864
case 'Linux':
5965
this.pathDB =
60-
this.capConfig.plugins.CapacitorSQLite.electronLinuxLocation;
66+
this.capConfig.plugins.CapacitorSQLite.electronLinuxLocation
67+
? this.capConfig.plugins.CapacitorSQLite.electronLinuxLocation
68+
: 'Databases';
6169
break;
6270
case 'Windows_NT':
6371
this.pathDB =
64-
this.capConfig.plugins.CapacitorSQLite.electronWindowsLocation;
72+
this.capConfig.plugins.CapacitorSQLite.electronWindowsLocation
73+
? this.capConfig.plugins.CapacitorSQLite.electronWindowsLocation
74+
: 'Databases';
6575
break;
6676
default:
6777
console.log('other operating system');
6878
}
6979
console.log(`&&& Databases path: ${this.pathDB}`);
7080
}
81+
/**
82+
* Get isEncryption from config
83+
* @returns
84+
*/
85+
public getIsEncryption(): boolean {
86+
return this.isEncryption;
87+
}
7188
/**
7289
* GetExtName
7390
* @param filePath

electron/src/electron-utils/utilsSQLite.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1+
import { UtilsFile } from './utilsFile';
2+
13
const SQLITE_OPEN_READONLY = 1;
24

35
export class UtilsSQLite {
4-
public JSQlite: any;
5-
// public SQLite3: any;
6+
public mSQLite: any;
7+
// public JSQlite: any;
8+
// public SQLite3: any;
9+
private fileUtil: UtilsFile = new UtilsFile();
10+
private isEncryption: boolean = this.fileUtil.getIsEncryption();
611

712
constructor() {
8-
// eslint-disable-next-line @typescript-eslint/no-var-requires
9-
this.JSQlite = require('@journeyapps/sqlcipher').verbose();
10-
// this.SQLite3 = require('sqlite3');
13+
if(this.isEncryption) {
14+
// eslint-disable-next-line @typescript-eslint/no-var-requires
15+
this.mSQLite = require('@journeyapps/sqlcipher').verbose();
16+
} else {
17+
this.mSQLite = require('sqlite3');
18+
}
1119
}
1220
/**
1321
* OpenOrCreateDatabase
@@ -23,11 +31,11 @@ export class UtilsSQLite {
2331
// open sqlite3 database
2432
let mDB: any;
2533
if (!readonly) {
26-
mDB = new this.JSQlite.Database(pathDB, {
34+
mDB = new this.mSQLite.Database(pathDB, {
2735
verbose: console.log,
2836
});
2937
} else {
30-
mDB = new this.JSQlite.Database(pathDB, SQLITE_OPEN_READONLY, {
38+
mDB = new this.mSQLite.Database(pathDB, SQLITE_OPEN_READONLY, {
3139
verbose: console.log,
3240
});
3341
}

electron/src/index.ts

Lines changed: 64 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import { GlobalSQLite } from './GlobalSQLite';
3939
import { Database } from './electron-utils/Database';
4040
import { UtilsJson } from './electron-utils/ImportExportJson/utilsJson';
4141
import { UtilsFile } from './electron-utils/utilsFile';
42+
import { UtilsSQLite } from './electron-utils/utilsSQLite';
4243

4344
export class CapacitorSQLite implements CapacitorSQLitePlugin {
4445
private versionUpgrades: Record<
@@ -48,7 +49,9 @@ export class CapacitorSQLite implements CapacitorSQLitePlugin {
4849
private databases: { [databasename: string]: Database } = {};
4950
private fileUtil: UtilsFile = new UtilsFile();
5051
private jsonUtil: UtilsJson = new UtilsJson();
52+
private sqliteUtil: UtilsSQLite = new UtilsSQLite();
5153
private globalUtil: GlobalSQLite = new GlobalSQLite();
54+
private isEncryption: boolean = this.fileUtil.getIsEncryption();
5255

5356
async createConnection(options: capConnectionOptions): Promise<void> {
5457
const optionKeys = Object.keys(options);
@@ -59,16 +62,21 @@ export class CapacitorSQLite implements CapacitorSQLitePlugin {
5962

6063
const dbName: string = options.database;
6164
const version: number = options.version ? options.version : 1;
62-
// const encrypted = false;
63-
// const inMode = "no-encryption";
6465

65-
const encrypted: boolean = options.encrypted ? options.encrypted : false;
66-
const inMode: string =
67-
options.mode === 'secret'
66+
let encrypted = options.encrypted ? options.encrypted : false;
67+
if(!this.isEncryption && encrypted) {
68+
throw new Error('Must set electronIsEncryption = true in capacitor.config.ts');
69+
}
70+
let inMode: string =
71+
encrypted && options.mode === 'secret'
6872
? 'secret'
69-
: options.mode === 'encryption'
73+
: encrypted && options.mode === 'encryption'
7074
? 'encryption'
7175
: 'no-encryption';
76+
if(!this.isEncryption) {
77+
encrypted = false;
78+
inMode = 'no-encryption';
79+
}
7280
const readonly: boolean = options.readonly ? options.readonly : false;
7381

7482
let upgrades: Record<number, capSQLiteVersionUpgrade> = {};
@@ -460,6 +468,9 @@ export class CapacitorSQLite implements CapacitorSQLitePlugin {
460468
const overwrite: boolean = vJsonObj.overwrite ?? false;
461469
const encrypted: boolean = vJsonObj.encrypted ?? false;
462470
const mode: string = vJsonObj.mode ?? 'no-encryption';
471+
if(!this.isEncryption && encrypted) {
472+
throw new Error('Must set electronIsEncryption = true in capacitor.config.ts');
473+
}
463474

464475
// Create the database
465476
const database: Database = new Database(
@@ -865,6 +876,10 @@ export class CapacitorSQLite implements CapacitorSQLitePlugin {
865876
}
866877

867878
async isSecretStored(): Promise<capSQLiteResult> {
879+
if(!this.isEncryption) {
880+
return Promise.reject(`isSecretStored: Not available electronIsEncryption = false in capacitor.config.ts`);
881+
}
882+
868883
if (this.globalUtil != null) {
869884
let capSQLiteResult = { result: false };
870885
if (
@@ -880,6 +895,9 @@ export class CapacitorSQLite implements CapacitorSQLitePlugin {
880895
}
881896

882897
async setEncryptionSecret(options: capSetSecretOptions): Promise<void> {
898+
if(!this.isEncryption) {
899+
return Promise.reject(`setEncryptionSecret: Not available electronIsEncryption = false in capacitor.config.ts`);
900+
}
883901
if (this.globalUtil != null) {
884902
this.globalUtil.secret = options.passphrase;
885903
Promise.resolve();
@@ -889,6 +907,9 @@ export class CapacitorSQLite implements CapacitorSQLitePlugin {
889907
}
890908

891909
async changeEncryptionSecret(options: capChangeSecretOptions): Promise<void> {
910+
if(!this.isEncryption) {
911+
return Promise.reject(`changeEncryptionSecret: Not available electronIsEncryption = false in capacitor.config.ts`);
912+
}
892913
if (this.globalUtil != null) {
893914
this.globalUtil.secret = options.oldpassphrase;
894915
this.globalUtil.newsecret = options.passphrase;
@@ -909,6 +930,9 @@ export class CapacitorSQLite implements CapacitorSQLitePlugin {
909930
}
910931

911932
async clearEncryptionSecret(): Promise<void> {
933+
if(!this.isEncryption) {
934+
return Promise.reject(`clearEncryptionSecret: Not available electronIsEncryption = false in capacitor.config.ts`);
935+
}
912936
if (this.globalUtil != null) {
913937
this.globalUtil.secret = '';
914938
Promise.resolve();
@@ -917,6 +941,40 @@ export class CapacitorSQLite implements CapacitorSQLitePlugin {
917941
}
918942
}
919943

944+
async isInConfigEncryption(): Promise<capSQLiteResult> {
945+
console.log(`in electron plugin this.isEncryption: ${this.isEncryption}`);
946+
return Promise.resolve({result: this.isEncryption});
947+
}
948+
949+
async isDatabaseEncrypted(
950+
options: capSQLiteOptions,
951+
): Promise<capSQLiteResult> {
952+
const dbName: string = this.getOptionValue(options, 'database');
953+
const isExists: boolean = this.fileUtil.isFileExists(dbName + 'SQLite.db');
954+
console.log(`@@@ isDatabaseEncrypted dbName: ${dbName} isExists: ${isExists}`)
955+
if(isExists) {
956+
const filePath = this.fileUtil.getFilePath(dbName + 'SQLite.db');
957+
try {
958+
const mDB = await this.sqliteUtil.openOrCreateDatabase(
959+
filePath,
960+
"",
961+
true,
962+
);
963+
console.log(`@@@@ dbName: ${dbName} , mDB ${mDB}`)
964+
if(mDB !== null) {
965+
mDB.close();
966+
return Promise.resolve({result: false});
967+
} else {
968+
return Promise.resolve({result: true});
969+
}
970+
} catch (error) {
971+
return Promise.resolve({result: true});
972+
}
973+
} else {
974+
return Promise.reject(`isDatabaseEncryptedt: Database ${dbName} does not exist`);
975+
}
976+
}
977+
920978
////////////////////////////////
921979
//// UNIMPLEMENTED METHODS
922980
////////////////////////////////
@@ -997,16 +1055,6 @@ export class CapacitorSQLite implements CapacitorSQLitePlugin {
9971055
throw new Error('Method not implemented.');
9981056
}
9991057

1000-
async isDatabaseEncrypted(
1001-
options: capSQLiteOptions,
1002-
): Promise<capSQLiteResult> {
1003-
console.log('isDatabaseEncrypted', options);
1004-
throw new Error('Not implemented on web.');
1005-
}
1006-
1007-
async isInConfigEncryption(): Promise<capSQLiteResult> {
1008-
throw new Error('Not implemented on web.');
1009-
}
10101058

10111059
async isInConfigBiometricAuth(): Promise<capSQLiteResult> {
10121060
throw new Error('Not implemented on web.');

0 commit comments

Comments
 (0)