Skip to content

Commit

Permalink
feat: enable api key injection from within JWT (#1752)
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity authored Oct 31, 2024
1 parent 874c2e4 commit 8f64b73
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { safeVerifyFernJWTConfig } from "@/server/auth/FernJWT";
import { OAuth2Client } from "@/server/auth/OAuth2Client";
import { getAPIKeyInjectionConfig } from "@/server/auth/getApiKeyInjectionConfig";
import { withSecureCookie } from "@/server/auth/with-secure-cookie";
Expand All @@ -17,6 +18,19 @@ export default async function handler(req: NextRequest): Promise<NextResponse<AP
const host = getHostEdge(req);
const edgeConfig = await getAuthEdgeConfig(domain);

const fern_token = req.cookies.get(COOKIE_FERN_TOKEN)?.value;

if (fern_token != null) {
const fernUser = await safeVerifyFernJWTConfig(fern_token, edgeConfig);
if (fernUser?.api_key != null) {
return NextResponse.json({
enabled: true,
authenticated: true,
access_token: fernUser.api_key,
});
}
}

// assume that if the edge config is set for webflow, api key injection is always enabled
if (edgeConfig?.type === "oauth2" && edgeConfig.partner === "webflow") {
const accessToken = req.cookies.get("access_token")?.value;
Expand Down
1 change: 1 addition & 0 deletions packages/ui/fern-docs-auth/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const FernUserSchema = z.object({
"The roles of the token (can be a string or an array of strings) which limits what content users can access",
)
.optional(),
api_key: z.string().optional().describe("For API Playground key injection"),
});

export type FernUser = z.infer<typeof FernUserSchema>;
Expand Down

0 comments on commit 8f64b73

Please sign in to comment.