diff --git a/src/AdminRoom.ts b/src/AdminRoom.ts index 71b02da0..0428a7e2 100644 --- a/src/AdminRoom.ts +++ b/src/AdminRoom.ts @@ -100,11 +100,11 @@ export class AdminRoom extends EventEmitter { return; } await this.sendNotice(`Connected as ${me.data.login}. Storing token..`); - await this.tokenStore.storeUserToken(this.userId, accessToken); + await this.tokenStore.storeUserToken("github", this.userId, accessToken); } private async hasPersonalToken() { - const result = await this.tokenStore.getUserToken(this.userId); + const result = await this.tokenStore.getUserToken("github", this.userId); if (result === null) { await this.sendNotice("You do not currently have a token stored"); return; diff --git a/src/GithubBridge.ts b/src/GithubBridge.ts index fece2a3a..b926625d 100644 --- a/src/GithubBridge.ts +++ b/src/GithubBridge.ts @@ -602,7 +602,7 @@ export class GithubBridge { log.info(`Settings changed for ${adminRoom.userId} ${settings}`); if (adminRoom.notificationsEnabled) { log.info(`Notifications enabled for ${adminRoom.userId}`); - const token = await this.tokenStore.getUserToken(adminRoom.userId); + const token = await this.tokenStore.getUserToken("github", adminRoom.userId); if (token) { log.info(`Notifications enabled for ${adminRoom.userId} and token was found`); await this.queue.push({ @@ -631,7 +631,7 @@ export class GithubBridge { } private async getOctokitForUser(userId: string) { - const senderToken = await this.tokenStore.getUserToken(userId); + const senderToken = await this.tokenStore.getUserToken("github", userId); if (!senderToken) { return null; } diff --git a/src/UserTokenStore.ts b/src/UserTokenStore.ts index ed205ff6..4a8feab9 100644 --- a/src/UserTokenStore.ts +++ b/src/UserTokenStore.ts @@ -4,6 +4,7 @@ import { publicEncrypt, privateDecrypt } from "crypto"; import { LogWrapper } from "./LogWrapper"; const ACCOUNT_DATA_TYPE = "uk.half-shot.matrix-github.password-store:"; +const ACCOUNT_DATA_GITLAB_TYPE = "uk.half-shot.matrix-github.gitlab.password-store:"; const log = new LogWrapper("UserTokenStore"); export class UserTokenStore { @@ -17,21 +18,26 @@ export class UserTokenStore { this.key = await fs.readFile(this.keyPath); } - public async storeUserToken(userId: string, token: string): Promise { - await this.intent.underlyingClient.setAccountData(`${ACCOUNT_DATA_TYPE}${userId}`, { + public async storeUserToken(type: "github"|"gitlab", userId: string, token: string): Promise { + const prefix = type === "github" ? ACCOUNT_DATA_TYPE : ACCOUNT_DATA_GITLAB_TYPE; + await this.intent.underlyingClient.setAccountData(`${prefix}${userId}`, { encrypted: publicEncrypt(this.key, Buffer.from(token)).toString("base64"), }); this.userTokens.set(userId, token); - log.info("Stored new token for", userId); + log.info(`Stored new ${type} token for ${userId}`); } - public async getUserToken(userId: string): Promise { + public async getUserToken(type: "github"|"gitlab", userId: string): Promise { if (this.userTokens.has(userId)) { return this.userTokens.get(userId)!; } let obj; try { - obj = await this.intent.underlyingClient.getAccountData(`${ACCOUNT_DATA_TYPE}${userId}`); + if (type === "github") { + obj = await this.intent.underlyingClient.getAccountData(`${ACCOUNT_DATA_TYPE}${userId}`); + } else if (type === "gitlab") { + obj = await this.intent.underlyingClient.getAccountData(`${ACCOUNT_DATA_GITLAB_TYPE}${userId}`); + } const encryptedTextB64 = obj.encrypted; const encryptedText = Buffer.from(encryptedTextB64, "base64"); const token = privateDecrypt(this.key, encryptedText).toString("utf-8");