From 69cd92f974d6e2fa8cf190e536bfe8a4de669491 Mon Sep 17 00:00:00 2001 From: Eli Bosley Date: Wed, 29 Jan 2025 12:55:10 -0500 Subject: [PATCH] fix: create api key for connect on startup --- api/src/index.ts | 7 ++-- api/src/store/modules/config.ts | 9 ++++- api/src/unraid-api/auth/api-key.service.ts | 41 +++++++++++----------- 3 files changed, 31 insertions(+), 26 deletions(-) diff --git a/api/src/index.ts b/api/src/index.ts index 68dc9c134..387505925 100644 --- a/api/src/index.ts +++ b/api/src/index.ts @@ -83,17 +83,14 @@ try { // Start listening to dynamix config file changes setupDynamixConfigWatch(); - // Disabled until we need the access token to work - // TokenRefresh.init(); - // If port is unix socket, delete old socket before starting http server unlinkUnixPort(); + startMiddlewareListeners(); + // Start webserver server = await bootstrapNestServer(); - startMiddlewareListeners(); - // On process exit stop HTTP server exitHook(async (signal) => { console.log('exithook', signal); diff --git a/api/src/store/modules/config.ts b/api/src/store/modules/config.ts index 13261afca..631116985 100644 --- a/api/src/store/modules/config.ts +++ b/api/src/store/modules/config.ts @@ -229,6 +229,11 @@ export const config = createSlice({ const stateAsArray = state.remote.ssoSubIds.split(',').filter((id) => id !== action.payload); state.remote.ssoSubIds = stateAsArray.join(','); }, + setLocalApiKey(state, action: PayloadAction) { + if (action.payload) { + state.remote.localApiKey = action.payload; + } + }, }, extraReducers(builder) { builder.addCase(loadConfigFile.pending, (state) => { @@ -310,6 +315,7 @@ export const { setWanPortToValue, setWanAccess, removeSsoUser, + setLocalApiKey, } = actions; /** @@ -326,7 +332,8 @@ export const configUpdateActionsFlash = isAnyOf( setupRemoteAccessThunk.fulfilled, logoutUser.fulfilled, loginUser.fulfilled, - removeSsoUser + removeSsoUser, + setLocalApiKey ); /** diff --git a/api/src/unraid-api/auth/api-key.service.ts b/api/src/unraid-api/auth/api-key.service.ts index 872f69756..9c54084f0 100644 --- a/api/src/unraid-api/auth/api-key.service.ts +++ b/api/src/unraid-api/auth/api-key.service.ts @@ -21,7 +21,7 @@ import { Role, } from '@app/graphql/generated/api/types'; import { getters, store } from '@app/store'; -import { updateUserConfig } from '@app/store/modules/config'; +import { setLocalApiKey } from '@app/store/modules/config'; import { FileLoadStatus } from '@app/store/types'; @Injectable() @@ -151,33 +151,34 @@ export class ApiKeyService implements OnModuleInit { if (!environment.IS_MAIN_PROCESS) { return; } + const { remote, status } = getters.config(); - if (getters.config().status !== FileLoadStatus.LOADED) { + if (status !== FileLoadStatus.LOADED) { this.logger.error('Config file not loaded, cannot create local API key'); return; } + if (!remote.apikey) { + return; + } - const { remote } = getters.config(); // If the remote API Key is set and the local key is either not set or not found on disk, create a key - if (remote.apikey && (!remote.localApiKey || !this.findByKey(remote.localApiKey))) { - const hasExistingKey = this.findByField('name', 'Connect'); + if (!remote.localApiKey || !this.findByKey(remote.localApiKey)) { + const existingKey = this.findByField('name', 'Connect'); - if (hasExistingKey) { - return; - } - // Create local API key - const localApiKey = await this.createLocalConnectApiKey(); - - if (localApiKey?.key) { - store.dispatch( - updateUserConfig({ - remote: { - localApiKey: localApiKey.key, - }, - }) - ); + if (existingKey) { + this.logger.debug('Found existing Connect key, not set in config, setting'); + store.dispatch(setLocalApiKey(existingKey.key)); } else { - this.logger.error('Failed to create local API key - no key returned'); + this.logger.debug('Creating a new key for Connect'); + + // Create local API key + const localApiKey = await this.createLocalConnectApiKey(); + + if (localApiKey?.key) { + store.dispatch(setLocalApiKey(localApiKey.key)); + } else { + this.logger.error('Failed to create local API key - no key returned'); + } } } }