diff --git a/app/core/api-manager.ts b/app/core/api-manager.ts index c86a1a105..9100a6160 100644 --- a/app/core/api-manager.ts +++ b/app/core/api-manager.ts @@ -89,7 +89,8 @@ export class ApiManager extends EventEmitter { // Update api manager with key this.replace('upc', apiKey, { // @todo: fix UPC being root - userId: '0' + userId: '0', + }); } @@ -181,10 +182,11 @@ export class ApiManager extends EventEmitter { const name = this.getNameFromKey(nameOrKey); if (!name) { + log.debug('No key found for "%s".', nameOrKey); return false; } - // We still have to run the retrieve after finding the key + // We still have to use .get() after finding the key // as this will run the cache validation check // without this the key would be "valid" even after // it's over the cache time diff --git a/app/graphql/index.ts b/app/graphql/index.ts index 1ec273c60..4460bb12a 100644 --- a/app/graphql/index.ts +++ b/app/graphql/index.ts @@ -270,16 +270,16 @@ const schema = makeExecutableSchema({ }); const ensureApiKey = async (apiKeyToCheck: string) => { - // No keys are loaded into memory - if (core.apiManager.getValidKeys().length === 0) { + // No my servers key is loaded into memory + if (core.apiManager.getValidKeys().filter(key => key.name === 'my_servers').length === 0) { const configPath = paths.get('myservers-config')!; await apiManager.checkKey(configPath, true); } - // Check there is atleast one valid key + // Check there is a valid my servers key // If there were no keys when we entered this method // the above should have tried forcefully reloading them - if (core.apiManager.getValidKeys().length !== 0) { + if (core.apiManager.getValidKeys().filter(key => key.name === 'my_servers').length !== 0) { if (!apiKeyToCheck) { throw new AppError('Missing API key.', 403); } @@ -295,7 +295,13 @@ const ensureApiKey = async (apiKeyToCheck: string) => { const debug = config.get('debug'); const apiKeyToUser = async (apiKey: string) => { - await ensureApiKey(apiKey); + try { + await ensureApiKey(apiKey); + } catch (error: unknown) { + log.debug('Failed looking up API key with "%s"', (error as Error).message); + + return { name: 'guest', role: 'guest' }; + } try { const keyName = apiManager.getNameFromKey(apiKey);