From a8393707fec85a9262af5ba8c03d205190b2235b Mon Sep 17 00:00:00 2001 From: sct Date: Tue, 8 Dec 2020 05:04:15 +0000 Subject: [PATCH] feat: generate real api key This also hides the api key from users without the ADMIN permission. It will not be returned from the api for them. Regenerate functionality is not in the commit. --- server/lib/settings.ts | 16 +++++- server/routes/settings.ts | 9 +++- src/components/Settings/SettingsMain.tsx | 69 ++++++++++++------------ src/pages/settings/index.tsx | 2 +- src/pages/settings/jobs.tsx | 2 +- src/pages/settings/main.tsx | 2 +- src/pages/settings/plex.tsx | 2 +- src/pages/settings/services.tsx | 2 +- 8 files changed, 64 insertions(+), 40 deletions(-) diff --git a/server/lib/settings.ts b/server/lib/settings.ts index b64ed4245..1fcb48bfd 100644 --- a/server/lib/settings.ts +++ b/server/lib/settings.ts @@ -102,7 +102,7 @@ class Settings { this.data = { clientId: '', main: { - apiKey: 'temp', + apiKey: '', applicationUrl: '', }, plex: { @@ -144,6 +144,10 @@ class Settings { } get main(): MainSettings { + if (!this.data.main.apiKey) { + this.data.main.apiKey = this.generateApiKey(); + this.save(); + } return this.data.main; } @@ -200,6 +204,16 @@ class Settings { return this.data.clientId; } + public regenerateApiKey(): MainSettings { + this.main.apiKey = this.generateApiKey(); + this.save(); + return this.main; + } + + private generateApiKey(): string { + return Buffer.from(`${Date.now()}${this.clientId}`).toString('base64'); + } + /** * Settings Load * diff --git a/server/routes/settings.ts b/server/routes/settings.ts index d4194a85a..422510f2e 100644 --- a/server/routes/settings.ts +++ b/server/routes/settings.ts @@ -4,6 +4,7 @@ import { RadarrSettings, SonarrSettings, Library, + MainSettings, } from '../lib/settings'; import { getRepository } from 'typeorm'; import { User } from '../entity/User'; @@ -19,9 +20,15 @@ import { merge } from 'lodash'; const settingsRoutes = Router(); -settingsRoutes.get('/main', (_req, res) => { +settingsRoutes.get('/main', (req, res) => { const settings = getSettings(); + if (!req.user?.hasPermission(Permission.ADMIN)) { + return res.status(200).json({ + applicationUrl: settings.main.applicationUrl, + } as Partial); + } + res.status(200).json(settings.main); }); diff --git a/src/components/Settings/SettingsMain.tsx b/src/components/Settings/SettingsMain.tsx index 77fc69c4d..611e11b95 100644 --- a/src/components/Settings/SettingsMain.tsx +++ b/src/components/Settings/SettingsMain.tsx @@ -7,6 +7,7 @@ import { Form, Formik, Field } from 'formik'; import axios from 'axios'; import Button from '../Common/Button'; import { defineMessages, useIntl } from 'react-intl'; +import { useUser, Permission } from '../../hooks/useUser'; const messages = defineMessages({ generalsettings: 'General Settings', @@ -19,6 +20,7 @@ const messages = defineMessages({ }); const SettingsMain: React.FC = () => { + const { hasPermission } = useUser(); const intl = useIntl(); const { data, error, revalidate } = useSWR( '/api/v1/settings/main' @@ -41,7 +43,6 @@ const SettingsMain: React.FC = () => {
{ @@ -59,40 +60,42 @@ const SettingsMain: React.FC = () => { {({ isSubmitting }) => { return (
-
- -
-
- - - + {hasPermission(Permission.ADMIN) && ( +
+ +
+
+ + + +
-
+ )}