mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 18:12:04 +00:00
refactor: Move API keys into their own table (no-changelog) (#10629)
Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
This commit is contained in:
@@ -62,17 +62,30 @@ function createAgent(
|
||||
return agent;
|
||||
}
|
||||
|
||||
function publicApiAgent(
|
||||
const userDoesNotHaveApiKey = (user: User) => {
|
||||
return !user.apiKeys || !Array.from(user.apiKeys) || user.apiKeys.length === 0;
|
||||
};
|
||||
|
||||
const publicApiAgent = (
|
||||
app: express.Application,
|
||||
{ user, version = 1 }: { user: User; version?: number },
|
||||
) {
|
||||
{ user, apiKey, version = 1 }: { user?: User; apiKey?: string; version?: number },
|
||||
) => {
|
||||
if (user && apiKey) {
|
||||
throw new Error('Cannot provide both user and API key');
|
||||
}
|
||||
|
||||
if (user && userDoesNotHaveApiKey(user)) {
|
||||
throw new Error('User does not have an API key');
|
||||
}
|
||||
|
||||
const agentApiKey = apiKey ?? user?.apiKeys[0].apiKey;
|
||||
|
||||
const agent = request.agent(app);
|
||||
void agent.use(prefix(`${PUBLIC_API_REST_PATH_SEGMENT}/v${version}`));
|
||||
if (user.apiKey) {
|
||||
void agent.set({ 'X-N8N-API-KEY': user.apiKey });
|
||||
}
|
||||
if (!user && !apiKey) return agent;
|
||||
void agent.set({ 'X-N8N-API-KEY': agentApiKey });
|
||||
return agent;
|
||||
}
|
||||
};
|
||||
|
||||
export const setupTestServer = ({
|
||||
endpointGroups,
|
||||
@@ -100,6 +113,8 @@ export const setupTestServer = ({
|
||||
authlessAgent: createAgent(app),
|
||||
restlessAgent: createAgent(app, { auth: false, noRest: true }),
|
||||
publicApiAgentFor: (user) => publicApiAgent(app, { user }),
|
||||
publicApiAgentWithApiKey: (apiKey) => publicApiAgent(app, { apiKey }),
|
||||
publicApiAgentWithoutApiKey: () => publicApiAgent(app, {}),
|
||||
license: new LicenseMocker(),
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user