Add support for gitlab personal token storage

This commit is contained in:
Will Hunt 2020-07-04 22:27:01 +01:00
parent c5d5fac43d
commit fb57b013b1
3 changed files with 15 additions and 9 deletions

View File

@ -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;

View File

@ -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<NotificationsEnableEvent>({
@ -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;
}

View File

@ -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<void> {
await this.intent.underlyingClient.setAccountData(`${ACCOUNT_DATA_TYPE}${userId}`, {
public async storeUserToken(type: "github"|"gitlab", userId: string, token: string): Promise<void> {
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<string|null> {
public async getUserToken(type: "github"|"gitlab", userId: string): Promise<string|null> {
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");