Skip to content

Commit c5cce73

Browse files
committed
Add cert FP database schema
1 parent 0afb064 commit c5cce73

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

src/datastore/postgres/PgDataStore.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ interface RoomRecord {
5757
export class PgDataStore implements DataStore, ProvisioningStore {
5858
private serverMappings: {[domain: string]: IrcServer} = {};
5959

60-
public static readonly LATEST_SCHEMA = 9;
60+
public static readonly LATEST_SCHEMA = 10;
6161
private pgPool: Pool;
6262
private hasEnded = false;
6363
private cryptoStore?: StringCrypto;
@@ -505,7 +505,7 @@ export class PgDataStore implements DataStore, ProvisioningStore {
505505

506506
public async getIrcClientConfig(userId: string, domain: string): Promise<IrcClientConfig | null> {
507507
const res = await this.pgPool.query(
508-
"SELECT config, password FROM client_config WHERE user_id = $1 and domain = $2",
508+
"SELECT config, password, cert, key FROM client_config WHERE user_id = $1 and domain = $2",
509509
[
510510
userId,
511511
domain
@@ -524,6 +524,23 @@ export class PgDataStore implements DataStore, ProvisioningStore {
524524
log.warn(`Failed to decrypt password for ${userId} ${domain}`, ex);
525525
}
526526
}
527+
config.certificate = {
528+
cert: row.cert,
529+
key: row.key,
530+
};
531+
// TODO: Testing
532+
if (row.cert && row.key && this.cryptoStore) {
533+
// NOT fatal, but really worrying.
534+
try {
535+
config.certificate = {
536+
cert: this.cryptoStore.decrypt(row.cert),
537+
key: this.cryptoStore.decrypt(row.key)
538+
};
539+
}
540+
catch (ex) {
541+
log.warn(`Failed to decrypt certificate for ${userId} ${domain}`, ex);
542+
}
543+
}
527544
return new IrcClientConfig(userId, domain, config);
528545
}
529546

@@ -536,14 +553,23 @@ export class PgDataStore implements DataStore, ProvisioningStore {
536553
// We need to make sure we have a matrix user in the store.
537554
await this.pgPool.query("INSERT INTO matrix_users VALUES ($1, NULL) ON CONFLICT DO NOTHING", [userId]);
538555
let password = config.getPassword();
556+
const cert: {cert?: string, key?: string} = { };
557+
558+
// This implies without a cryptostore these will be stored plain.
539559
if (password && this.cryptoStore) {
540560
password = this.cryptoStore.encrypt(password);
541561
}
562+
if (config.certificate && this.cryptoStore) {
563+
cert.cert = this.cryptoStore.encrypt(config.certificate.cert);
564+
cert.key = this.cryptoStore.encrypt(config.certificate.key);
565+
}
542566
const parameters = {
543567
user_id: userId,
544568
domain: config.getDomain(),
545569
// either use the decrypted password, or whatever is stored already.
546570
password,
571+
cert: cert?.cert,
572+
key: cert?.key,
547573
config: JSON.stringify(config.serialize(true)),
548574
};
549575
const statement = PgDataStore.BuildUpsertStatement(
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import {PoolClient} from "pg";
2+
3+
export async function runSchema(connection: PoolClient) {
4+
await connection.query(`
5+
ALTER TABLE client_config
6+
ADD COLUMN cert TEXT,
7+
ADD COLUMN key TEXT;`
8+
);
9+
}

src/models/IrcClientConfig.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ import { MatrixUser } from "matrix-appservice-bridge";
1919
export interface IrcClientConfigSeralized {
2020
username?: string;
2121
password?: string;
22+
certificate?: {
23+
key: string;
24+
cert: string;
25+
};
2226
nick?: string;
2327
ipv6?: string;
2428
}
@@ -66,6 +70,14 @@ export class IrcClientConfig {
6670
return this.config.password;
6771
}
6872

73+
public setCertificate(certificate: {cert: string, key: string}) {
74+
this.config.certificate = certificate;
75+
}
76+
77+
public get certificate(): {cert: string, key: string}|undefined {
78+
return this.config.certificate;
79+
}
80+
6981
public setDesiredNick(nick: string) {
7082
this.config.nick = nick;
7183
}

0 commit comments

Comments
 (0)