fix: add logs for invalid api key

This commit is contained in:
Alexis Tyler
2021-05-12 07:26:47 +09:30
parent 93b75a5e4c
commit 3f9c083d3d
2 changed files with 15 additions and 7 deletions

View File

@@ -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

View File

@@ -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);