From da329fc8768a7811bd0b1eb1d978e2a35efd9f52 Mon Sep 17 00:00:00 2001 From: luckylat Date: Tue, 3 Oct 2023 23:09:01 +0900 Subject: [PATCH 1/3] feat: add APIkey settings field --- src/apis/base_api.ts | 2 +- src/apis/google_books_api.ts | 5 ++++- src/settings/settings.ts | 26 ++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/apis/base_api.ts b/src/apis/base_api.ts index ac874a8..2971159 100644 --- a/src/apis/base_api.ts +++ b/src/apis/base_api.ts @@ -11,7 +11,7 @@ export interface BaseBooksApiImpl { export function factoryServiceProvider(settings: BookSearchPluginSettings): BaseBooksApiImpl { if (settings.serviceProvider === ServiceProvider.google) { - return new GoogleBooksApi(settings.localePreference); + return new GoogleBooksApi(settings.localePreference, settings.apiKey); } if (settings.serviceProvider === ServiceProvider.naver) { if (!settings.naverClientId || !settings.naverClientSecret) { diff --git a/src/apis/google_books_api.ts b/src/apis/google_books_api.ts index 2124a32..50bfea1 100644 --- a/src/apis/google_books_api.ts +++ b/src/apis/google_books_api.ts @@ -3,7 +3,7 @@ import { apiGet, BaseBooksApiImpl } from '@apis/base_api'; import { GoogleBooksResponse, VolumeInfo } from './models/google_books_response'; export class GoogleBooksApi implements BaseBooksApiImpl { - constructor(private readonly localePreference: string) {} + constructor(private readonly localePreference: string, private readonly apiKey?: string) {} async getByQuery(query: string) { try { @@ -18,6 +18,9 @@ export class GoogleBooksApi implements BaseBooksApiImpl { } else { params['langRestrict'] = langRestrict; } + if (this.apiKey !== '') { + params['key'] = this.apiKey; + } const searchResults = await apiGet('https://www.googleapis.com/books/v1/volumes', params); if (!searchResults?.totalItems) { return []; diff --git a/src/settings/settings.ts b/src/settings/settings.ts index 376cc5f..f3d61ce 100644 --- a/src/settings/settings.ts +++ b/src/settings/settings.ts @@ -1,6 +1,11 @@ import { App, PluginSettingTab, Setting } from 'obsidian'; import { replaceDateInString } from '@utils/utils'; +//const Electron = require('electron'); +//const { +// remote: { safeStorage }, +//} = Electron; + import BookSearchPlugin from '../main'; import { FileNameFormatSuggest } from './suggesters/FileNameFormatSuggester'; import { FolderSuggest } from './suggesters/FolderSuggester'; @@ -27,6 +32,7 @@ export interface BookSearchPluginSettings { naverClientId: string; naverClientSecret: string; localePreference: string; + apiKey: string; } export const DEFAULT_SETTINGS: BookSearchPluginSettings = { @@ -41,6 +47,7 @@ export const DEFAULT_SETTINGS: BookSearchPluginSettings = { naverClientId: '', naverClientSecret: '', localePreference: 'default', + apiKey: '', }; export class BookSearchSettingTab extends PluginSettingTab { @@ -265,6 +272,25 @@ export class BookSearchSettingTab extends PluginSettingTab { }); }), ); + + // API Settings + // + // TODO: More Secure Saveing System + const APISettingsChildren: Setting[] = []; + createFoldingHeader(containerEl, 'API Settings', APISettingsChildren); + APISettingsChildren.push( + new Setting(containerEl) + .setName('Google Book API Key') + .setDesc('Be secure the API key.\n How the API create, please read README') + .addText(text => { + const prevValue = this.plugin.settings.apiKey; + text.setValue(prevValue).onChange(async value => { + const newValue = value; + this.plugin.settings.apiKey = newValue; + await this.plugin.saveSettings(); + }); + }), + ); } } From c49521a2a2addf366c219b177853301a42ac92a6 Mon Sep 17 00:00:00 2001 From: luckylat Date: Tue, 3 Oct 2023 23:36:44 +0900 Subject: [PATCH 2/3] feat: use safeStorage --- src/apis/google_books_api.ts | 8 +++++++- src/settings/settings.ts | 28 +++++++++++++++++++--------- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/apis/google_books_api.ts b/src/apis/google_books_api.ts index 50bfea1..ec6ebc1 100644 --- a/src/apis/google_books_api.ts +++ b/src/apis/google_books_api.ts @@ -2,6 +2,12 @@ import { Book } from '@models/book.model'; import { apiGet, BaseBooksApiImpl } from '@apis/base_api'; import { GoogleBooksResponse, VolumeInfo } from './models/google_books_response'; +// eslint-disable-next-line @typescript-eslint/no-var-requires +const Electron = require('electron'); +const { + remote: { safeStorage }, +} = Electron; + export class GoogleBooksApi implements BaseBooksApiImpl { constructor(private readonly localePreference: string, private readonly apiKey?: string) {} @@ -19,7 +25,7 @@ export class GoogleBooksApi implements BaseBooksApiImpl { params['langRestrict'] = langRestrict; } if (this.apiKey !== '') { - params['key'] = this.apiKey; + params['key'] = safeStorage.isEncryptionAvailable() ? safeStorage.decryptString(this.apiKey) : this.apiKey; } const searchResults = await apiGet('https://www.googleapis.com/books/v1/volumes', params); if (!searchResults?.totalItems) { diff --git a/src/settings/settings.ts b/src/settings/settings.ts index f3d61ce..2d1cefe 100644 --- a/src/settings/settings.ts +++ b/src/settings/settings.ts @@ -1,10 +1,11 @@ -import { App, PluginSettingTab, Setting } from 'obsidian'; +import { App, PluginSettingTab, Setting, Notice } from 'obsidian'; import { replaceDateInString } from '@utils/utils'; -//const Electron = require('electron'); -//const { -// remote: { safeStorage }, -//} = Electron; +// eslint-disable-next-line @typescript-eslint/no-var-requires +const Electron = require('electron'); +const { + remote: { safeStorage }, +} = Electron; import BookSearchPlugin from '../main'; import { FileNameFormatSuggest } from './suggesters/FileNameFormatSuggester'; @@ -278,16 +279,25 @@ export class BookSearchSettingTab extends PluginSettingTab { // TODO: More Secure Saveing System const APISettingsChildren: Setting[] = []; createFoldingHeader(containerEl, 'API Settings', APISettingsChildren); + let tempKeyValue = ''; APISettingsChildren.push( new Setting(containerEl) .setName('Google Book API Key') .setDesc('Be secure the API key.\n How the API create, please read README') .addText(text => { - const prevValue = this.plugin.settings.apiKey; - text.setValue(prevValue).onChange(async value => { - const newValue = value; - this.plugin.settings.apiKey = newValue; + text.onChange(async value => { + if (safeStorage.isEncryptionAvailable()) { + tempKeyValue = safeStorage.enctyptString(value); + } else { + tempKeyValue = value; + } + }); + }) + .addButton(button => { + button.setButtonText('Save Key').onClick(async () => { + this.plugin.settings.apiKey = tempKeyValue; await this.plugin.saveSettings(); + new Notice('Apikey Saved'); }); }), ); From e9f2a82c759101c0ccb40c5df56c852ff991ca3f Mon Sep 17 00:00:00 2001 From: luckylat Date: Tue, 3 Oct 2023 23:55:53 +0900 Subject: [PATCH 3/3] chore: improve some sentence --- src/settings/settings.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/settings/settings.ts b/src/settings/settings.ts index 2d1cefe..1dbdb49 100644 --- a/src/settings/settings.ts +++ b/src/settings/settings.ts @@ -275,15 +275,16 @@ export class BookSearchSettingTab extends PluginSettingTab { ); // API Settings - // - // TODO: More Secure Saveing System const APISettingsChildren: Setting[] = []; - createFoldingHeader(containerEl, 'API Settings', APISettingsChildren); + createFoldingHeader(containerEl, 'Google API Settings', APISettingsChildren); let tempKeyValue = ''; APISettingsChildren.push( new Setting(containerEl) + .setClass('book-search-plugin__hide') .setName('Google Book API Key') - .setDesc('Be secure the API key.\n How the API create, please read README') + .setDesc( + 'Add your Books API key. **WARNING** please use this field after you must understand Google Cloud API, such as API key security.', + ) .addText(text => { text.onChange(async value => { if (safeStorage.isEncryptionAvailable()) {