Skip to content

Commit

Permalink
fix: crypto doesn't work the way I want (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mara-Li authored Jun 10, 2023
1 parent c27b467 commit 8ccfbdc
Show file tree
Hide file tree
Showing 11 changed files with 498 additions and 598 deletions.
876 changes: 437 additions & 439 deletions src/i18n/locales/en.json

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions src/i18n/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,10 @@
},
"ghToken": {
"button": {
"notice": "Le token chiffré a été regénéré !",
"title": "Regénérer le token chiffré",
"tooltip": "Régénérera la clé publique et privée et enregistrera le nouveau jeton crypté dans vos paramètres."
},
"desc": "Un token GitHub avec autorisation de dépôt. Vous pouvez le générer ",
"encrypted": "Le jeton sera enregistré crypté dans vos paramètres.",
"encrypted": "Le token sera enregistré dans le fichier \"env\".",
"error": "Le token ne doit pas être vide !",
"title": "Token Github"
},
"repoName": {
Expand Down
20 changes: 18 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
uploadAllNotesCallback, uploadAllEditedNotesCallback, shareEditedOnlyCallback,
uploadNewNotesCallback, checkRepositoryValidityCallback
} from "./commands/callback";
import { decrypt } from "./settings/crypto";

/**
* Main class of the plugin
Expand Down Expand Up @@ -110,14 +109,31 @@ export default class GithubPublisher extends Plugin {
}
}
}

/**
* Read the env file to get the token of the plugin
* Form of the file:
* ```
* GITHUB_TOKEN=token
* ```
* @returns {Promise<string>} - The token of the plugin
*/

async loadToken(): Promise<string> {
const tokenFile = await this.app.vault.adapter.read(`${this.app.vault.configDir}/plugins/${this.manifest.id}/env`);
if (tokenFile) {
return tokenFile.split("=")[1];
}
return "";
}

/**
* Create a new instance of Octokit to load a new instance of GithubBranch
*/
async reloadOctokit() {
let octokit: Octokit;
const apiSettings = this.settings.github.api;
const token = await decrypt(this.settings.github.token, this);
const token = await this.loadToken();
if (apiSettings.tiersForApi === GithubTiersVersion.entreprise && apiSettings.hostname.length > 0) {
octokit = new Octokit(
{
Expand Down
2 changes: 1 addition & 1 deletion src/publish/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ export default class Publisher {
this.plugin
);
const frontmatter = this.metadataCache.getFileCache(file).frontmatter;
const isNotEmpty = checkEmptyConfiguration(getRepoFrontmatter(this.settings, shortRepo, frontmatter), this.settings);
const isNotEmpty = checkEmptyConfiguration(getRepoFrontmatter(this.settings, shortRepo, frontmatter), this.plugin);
if (
!isShared(frontmatter, this.settings, file, shortRepo) ||
fileHistory.includes(file) ||
Expand Down
27 changes: 11 additions & 16 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { ExportModal, ImportModal } from "./settings/modals/import_export";
import i18next from "i18next";
import { enumbSettingsTabId } from "./settings/interface";
import {ModalAddingNewRepository} from "./settings/modals/manage_repo";
import { encrypt, decrypt, isEncrypted, regenerateTokenKeyPair } from "./settings/crypto";
import { migrateToken } from "./settings/migrate";


export class GithubPublisherSettingsTab extends PluginSettingTab {
Expand Down Expand Up @@ -212,30 +212,25 @@ export class GithubPublisherSettingsTab extends PluginSettingTab {
});
span.createEl("div", null, (p) => p.innerText = i18next.t("settings.github.ghToken.encrypted"));
});
new Setting(this.settingsPage)
const tokenSettings = new Setting(this.settingsPage)
.setName(i18next.t("settings.github.ghToken.title"))
.setDesc(desc_ghToken)
.addText(async (text) => {
const decryptedToken = isEncrypted(this.plugin) ? await decrypt(githubSettings.token, this.plugin) : githubSettings.token;
const decryptedToken:string = await this.plugin.loadToken();
text
.setPlaceholder("ghp_15457498545647987987112184")
.setValue(decryptedToken)
.onChange(async (value) => {
githubSettings.token = await encrypt(value.trim(), this.plugin);
if (value.trim().length === 0 ) {
tokenSettings.controlEl.querySelector("input").style.border = "1px solid red";
new Notice(i18next.t("settings.github.ghToken.error"));
} else {
tokenSettings.controlEl.querySelector("input").style.border = "";
await migrateToken(this.plugin, value.trim());
}
await this.plugin.saveSettings();
});
})
.addButton((button) => {
button
.setButtonText(i18next.t("settings.github.ghToken.button.title"))
.setTooltip(i18next.t("settings.github.ghToken.button.tooltip"))
.onClick(async () => {
await regenerateTokenKeyPair(this.plugin);
new Notice(i18next.t("settings.github.ghToken.button.notice"));
this.renderGithubConfiguration();
});
});

new Setting(this.settingsPage)
.setName(i18next.t("settings.github.branch.title"))
.setDesc(i18next.t("settings.github.branch.desc"))
Expand Down Expand Up @@ -268,7 +263,7 @@ export class GithubPublisherSettingsTab extends PluginSettingTab {
.setClass("github-publisher-connect-button")
.onClick(async () => {
const octokit = await this.plugin.reloadOctokit();
await checkRepositoryValidity(this.branchName, octokit, this.plugin.settings, null,null, this.app.metadataCache);
await checkRepositoryValidity(octokit, this.plugin.settings, null,null, this.app.metadataCache);
})
)
.addButton((button) =>
Expand Down
113 changes: 0 additions & 113 deletions src/settings/crypto.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/settings/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export interface GitHubPublisherSettings {
user: string;
repo: string;
branch: string;
token: string;
automaticallyMergePR: boolean;
api: {
tiersForApi: GithubTiersVersion;
Expand Down Expand Up @@ -146,7 +145,6 @@ export const DEFAULT_SETTINGS: GitHubPublisherSettings = {
user: "",
repo: "",
branch: "main",
token: "",
automaticallyMergePR: true,
api: {
tiersForApi: GithubTiersVersion.free,
Expand Down
30 changes: 20 additions & 10 deletions src/settings/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {FolderSettings, GithubTiersVersion, TextCleaner, TypeOfEditRegex} from "
import GithubPublisher from "../main";
import {noticeLog} from "../src/utils";
import i18next from "i18next";
import { encrypt, isEncrypted } from "./crypto";

export interface OldSettings {
githubRepo: string;
Expand Down Expand Up @@ -58,7 +57,7 @@ export async function migrateSettings(old: OldSettings, plugin: GithubPublisher)
await migrateSubFolder(plugin);
await migrateCensor(plugin);
await migrateWorFlow(plugin);
await migrateEncryptToken(plugin);
await migrateToken(plugin);
await migrateOtherRepository(plugin);
}

Expand Down Expand Up @@ -121,16 +120,25 @@ async function migrateWorFlow(plugin: GithubPublisher) {
}
}

async function migrateEncryptToken(plugin: GithubPublisher) {
const encrypted = await isEncrypted(plugin);
if (!encrypted) {
noticeLog("Encrypting token", plugin.settings);
const encryptedToken = await encrypt(plugin.settings.github.token, plugin);
plugin.settings.github.token = encryptedToken;
export async function migrateToken(plugin: GithubPublisher, token?: string) {
//@ts-ignore
if (plugin.settings.github.token && !token) {
noticeLog("migrating token in settings", plugin.settings);
//@ts-ignore
token = plugin.settings.github.token;
//@ts-ignore
delete plugin.settings.github.token;
await plugin.saveSettings();
}
await plugin.saveSettings();
noticeLog("migrating token in another file", plugin.settings);
if (token === undefined) {
token = "";
}
const envToken = `GITHUB_TOKEN=${token}`;
await plugin.app.vault.adapter.write(`${plugin.app.vault.configDir}/plugins/${plugin.manifest.id}/env`, envToken);
}


async function migrateOtherRepository(plugin: GithubPublisher) {
noticeLog("Configuring other repositories", plugin.settings);
const otherRepo = plugin.settings.github?.otherRepo ?? [];
Expand Down Expand Up @@ -177,7 +185,6 @@ async function migrateOldSettings(plugin: GithubPublisher, old: OldSettings) {
{
user: old.githubName ? old.githubName : plugin.settings.github.user ? plugin.settings.github.user : "",
repo: old.githubRepo ? old.githubRepo : plugin.settings.github.repo ? plugin.settings.github.repo : "",
token: old.GhToken ? old.GhToken : plugin.settings.github.token ? plugin.settings.github.token : "",
branch: old.githubBranch,
automaticallyMergePR: old.automaticallyMergePR,
api: {
Expand Down Expand Up @@ -258,6 +265,9 @@ async function migrateOldSettings(plugin: GithubPublisher, old: OldSettings) {
displayModalRepoEditing: false
}
};
//@ts-ignore
const token = old.GhToken ? old.GhToken : plugin.settings.github.token ? plugin.settings.github.token : "";
await migrateToken(plugin, token);
await plugin.saveSettings();
}
}
4 changes: 1 addition & 3 deletions src/settings/modals/import_export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export class ImportModal extends Modal {
async censoreRepositoryData(actualSettings: GitHubPublisherSettings) {
this.plugin.settings.plugin = actualSettings.plugin;
this.plugin.settings.github.repo = actualSettings.github.repo;
this.plugin.settings.github.token = actualSettings.github.token;
this.plugin.settings.github.user = actualSettings.github.user;
for (const repo of actualSettings.github.otherRepo) {
//search the same repo in this.settings.github.otherRepo
Expand Down Expand Up @@ -186,7 +185,6 @@ export class ExportModal extends Modal {

censoreGithubSettingsData(censuredSettings: GitHubPublisherSettings) {
delete censuredSettings.github.repo;
delete censuredSettings.github.token;
delete censuredSettings.github.user;
delete censuredSettings.plugin;
for (const repo of censuredSettings.github.otherRepo) {
Expand Down Expand Up @@ -254,7 +252,7 @@ export class ExportModal extends Modal {
this.app.vault.adapter.write(`${app.vault.configDir}/plugins/obsidian-mkdocs-publisher/._tempSettings.json`, output);
//open the file with default application
//eslint-disable-next-line
(this.app as any).openWithDefaultApp(`${app.vault.configDir}/plugins/obsidian-mkdocs-publisher/._tempSettings.json`);
(this.app as any).openWithDefaultApp(`${app.vault.configDir}/plugins/obsidian-mkdocs-publisher/._tempSettings.json`);
}));
}
});
Expand Down
1 change: 0 additions & 1 deletion src/settings/modals/manage_repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,6 @@ class ModalEditingRepository extends Modal {
.setClass("github-publisher-connect-button")
.onClick(async () => {
await checkRepositoryValidity(
this.branchName,
await this.plugin.reloadOctokit(),
this.plugin.settings,
this.repository,
Expand Down
Loading

0 comments on commit 8ccfbdc

Please sign in to comment.