From 6beac736efcf7b9102e02e43b75d91a9a158cd22 Mon Sep 17 00:00:00 2001 From: sct Date: Fri, 18 Dec 2020 19:40:21 +0900 Subject: [PATCH] feat: api key regeneration --- overseerr-api.yml | 13 +++++++ server/lib/settings.ts | 2 +- server/routes/settings.ts | 35 ++++++++++++++---- src/components/Settings/CopyButton.tsx | 5 ++- src/components/Settings/SettingsMain.tsx | 46 ++++++++++++++++++++++-- src/i18n/locale/en.json | 4 +++ 6 files changed, 94 insertions(+), 11 deletions(-) diff --git a/overseerr-api.yml b/overseerr-api.yml index 752a4fe7e..e6faa7f21 100644 --- a/overseerr-api.yml +++ b/overseerr-api.yml @@ -1028,6 +1028,19 @@ paths: application/json: schema: $ref: '#/components/schemas/MainSettings' + /settings/main/regenerate: + get: + summary: Returns main settings with newly generated API Key + description: Retreives all main settings in JSON format with new API Key + tags: + - settings + responses: + '200': + description: OK + content: + application/json: + schema: + $ref: '#/components/schemas/MainSettings' /settings/plex: get: summary: Returns plex settings diff --git a/server/lib/settings.ts b/server/lib/settings.ts index f618615cb..327563631 100644 --- a/server/lib/settings.ts +++ b/server/lib/settings.ts @@ -213,7 +213,7 @@ class Settings { } private generateApiKey(): string { - return Buffer.from(`${Date.now()}${this.clientId}`).toString('base64'); + return Buffer.from(`${Date.now()}${uuidv4()})`).toString('base64'); } /** diff --git a/server/routes/settings.ts b/server/routes/settings.ts index 25a702b07..f90ae9b62 100644 --- a/server/routes/settings.ts +++ b/server/routes/settings.ts @@ -23,16 +23,27 @@ import { getAppVersion } from '../utils/appVersion'; const settingsRoutes = Router(); -settingsRoutes.get('/main', (req, res) => { +const filteredMainSettings = ( + user: User, + main: MainSettings +): Partial => { + if (!user?.hasPermission(Permission.ADMIN)) { + return { + applicationUrl: main.applicationUrl, + }; + } + + return main; +}; + +settingsRoutes.get('/main', (req, res, next) => { const settings = getSettings(); - if (!req.user?.hasPermission(Permission.ADMIN)) { - return res.status(200).json({ - applicationUrl: settings.main.applicationUrl, - } as Partial); + if (!req.user) { + return next({ status: 500, message: 'User missing from request' }); } - res.status(200).json(settings.main); + res.status(200).json(filteredMainSettings(req.user, settings.main)); }); settingsRoutes.post('/main', (req, res) => { @@ -44,6 +55,18 @@ settingsRoutes.post('/main', (req, res) => { return res.status(200).json(settings.main); }); +settingsRoutes.get('/main/regenerate', (req, res, next) => { + const settings = getSettings(); + + const main = settings.regenerateApiKey(); + + if (!req.user) { + return next({ status: 500, message: 'User missing from request' }); + } + + return res.status(200).json(filteredMainSettings(req.user, main)); +}); + settingsRoutes.get('/plex', (_req, res) => { const settings = getSettings(); diff --git a/src/components/Settings/CopyButton.tsx b/src/components/Settings/CopyButton.tsx index 717667d59..0ddf4dbfb 100644 --- a/src/components/Settings/CopyButton.tsx +++ b/src/components/Settings/CopyButton.tsx @@ -25,7 +25,10 @@ const CopyButton: React.FC<{ textToCopy: string }> = ({ textToCopy }) => { return (