From 961a73f712e2558276d54b98f2d2553109f31c98 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Wed, 13 Mar 2024 10:51:20 +0100 Subject: [PATCH] Fix: Re-add API key support --- .../server/plugins/auth/inject-identity.ts | 36 +++++++++++-------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/backend/src/server/plugins/auth/inject-identity.ts b/backend/src/server/plugins/auth/inject-identity.ts index 95deb78a5..5fcd3635b 100644 --- a/backend/src/server/plugins/auth/inject-identity.ts +++ b/backend/src/server/plugins/auth/inject-identity.ts @@ -19,13 +19,14 @@ export type TAuthMode = orgId?: string; authMethod: AuthMethod; } - // | { - // authMode: AuthMode.API_KEY; - // actor: ActorType.USER; - // userId: string; - // user: TUsers; - // orgId?: string; - // } + | { + authMode: AuthMode.API_KEY; + authMethod: null; + actor: ActorType.USER; + userId: string; + user: TUsers; + orgId: string; + } | { authMode: AuthMode.SERVICE_TOKEN; serviceToken: TServiceTokens & { createdByEmail: string }; @@ -78,8 +79,8 @@ const extractAuth = async (req: FastifyRequest, jwtSecret: string) => { actor: ActorType.USER } as const; case AuthTokenType.API_KEY: - throw new Error("API Key auth is no longer supported."); - // return { authMode: AuthMode.API_KEY, token: decodedToken, actor: ActorType.USER } as const; + // throw new Error("API Key auth is no longer supported."); + return { authMode: AuthMode.API_KEY, token: decodedToken, actor: ActorType.USER } as const; case AuthTokenType.IDENTITY_ACCESS_TOKEN: return { authMode: AuthMode.IDENTITY_ACCESS_TOKEN, @@ -148,11 +149,18 @@ export const injectIdentity = fp(async (server: FastifyZodProvider) => { }; break; } - // case AuthMode.API_KEY: { - // const user = await server.services.apiKey.fnValidateApiKey(token as string); - // req.auth = { authMode: AuthMode.API_KEY as const, userId: user.id, actor, user }; - // break; - // } + case AuthMode.API_KEY: { + const user = await server.services.apiKey.fnValidateApiKey(token as string); + req.auth = { + authMode: AuthMode.API_KEY as const, + userId: user.id, + actor, + user, + orgId: "API_KEY", + authMethod: null + }; // We set the orgId to an arbitrary value, since we can't link an API key to a specific org. We have to deprecate API keys soon! + break; + } case AuthMode.SCIM_TOKEN: { const { orgId, scimTokenId } = await server.services.scim.fnValidateScimToken(token); req.auth = { authMode: AuthMode.SCIM_TOKEN, actor, scimTokenId, orgId, authMethod: null };