From dc1312c970696315cb0182b7bb49430140438ba1 Mon Sep 17 00:00:00 2001 From: "afik.cohen" Date: Sun, 10 Dec 2023 17:40:07 -0800 Subject: [PATCH] Rename some auth0 mgmt things --- js/cf-api/.prettierrc => .prettierrc | 0 apps/cf-webapp/package.json | 63 ++++++++++++++++++++ js/cf-api/{auth0-utils.ts => auth0-mgmt.ts} | 6 +- js/cf-api/index.ts | 12 ++-- js/cf-api/package.json | 3 +- js/cf-webapp/{.eslintrc.js => .eslintrc.cjs} | 0 js/common/package.json | 2 +- js/common/token-functions.ts | 12 ++-- 8 files changed, 84 insertions(+), 14 deletions(-) rename js/cf-api/.prettierrc => .prettierrc (100%) create mode 100644 apps/cf-webapp/package.json rename js/cf-api/{auth0-utils.ts => auth0-mgmt.ts} (67%) rename js/cf-webapp/{.eslintrc.js => .eslintrc.cjs} (100%) diff --git a/js/cf-api/.prettierrc b/.prettierrc similarity index 100% rename from js/cf-api/.prettierrc rename to .prettierrc diff --git a/apps/cf-webapp/package.json b/apps/cf-webapp/package.json new file mode 100644 index 000000000..4c5c0ba53 --- /dev/null +++ b/apps/cf-webapp/package.json @@ -0,0 +1,63 @@ +{ + "name": "codeflash-webapp", + "version": "0.1.0", + "private": true, + "scripts": { + "dev": "next dev", + "build": "npm install --omit=dev --loglevel verbose && npx prisma generate && next build", + "deploy": "az webapp up -n codeflash-webapp-2 --sku P1V2 --runtime NODE:20-lts", + "start": "next start", + "lint": "next lint" + }, + "dependencies": { + "@auth0/nextjs-auth0": "^3.3.0", + "@hookform/resolvers": "^3.3.2", + "@prisma/client": "^5.6.0", + "@radix-ui/react-dialog": "^1.0.5", + "@radix-ui/react-dropdown-menu": "^2.0.6", + "@radix-ui/react-label": "^2.0.2", + "@radix-ui/react-navigation-menu": "^1.1.4", + "@radix-ui/react-select": "^2.0.0", + "@radix-ui/react-separator": "^1.0.3", + "@radix-ui/react-slot": "^1.0.2", + "@radix-ui/react-toast": "^1.1.5", + "@types/node": "^20", + "@types/pg": "^8.10.9", + "@types/react": "^18", + "@types/react-dom": "^18", + "@typescript-eslint/eslint-plugin": "^6.13.1", + "autoprefixer": "^10.0.1", + "class-variance-authority": "^0.7.0", + "clsx": "^2.0.0", + "@codeflash-ai/common": "1.0.0", + "eslint": "^8.54.0", + "eslint-config-next": "14.0.3", + "eslint-config-prettier": "^9.1.0", + "eslint-config-standard-with-typescript": "^40.0.0", + "eslint-plugin-import": "^2.29.0", + "eslint-plugin-n": "^16.3.1", + "eslint-plugin-promise": "^6.1.1", + "eslint-plugin-react": "^7.33.2", + "lucide-react": "^0.293.0", + "next": "14.0.4", + "next-themes": "^0.2.1", + "pg": "^8.11.3", + "postcss": "^8", + "prisma": "^5.6.0", + "react": "^18", + "react-dom": "^18", + "react-hook-form": "^7.48.2", + "react-markdown": "^9.0.1", + "tailwind-merge": "^2.0.0", + "tailwindcss": "^3.3.0", + "tailwindcss-animate": "^1.0.7", + "typescript": "^5.3.2", + "zod": "^3.22.4" + }, + "devDependencies": { + "prettier": "3.1.0" + }, + "engines": { + "node": ">=20.0.0 < 21.0.0" + } +} diff --git a/js/cf-api/auth0-utils.ts b/js/cf-api/auth0-mgmt.ts similarity index 67% rename from js/cf-api/auth0-utils.ts rename to js/cf-api/auth0-mgmt.ts index 41abb57ac..a27c9d427 100644 --- a/js/cf-api/auth0-utils.ts +++ b/js/cf-api/auth0-mgmt.ts @@ -2,9 +2,9 @@ import { ManagementClient } from "auth0" export async function userNickname(userId: string): Promise { const m = new ManagementClient({ - domain: process.env.AUTH0_ISSUER_BASE_URL || "", - clientId: process.env.AUTH0_CLIENT_ID_MANAGEMENT || "", - clientSecret: process.env.AUTH0_CLIENT_SECRET_MANAGEMENT || "", + domain: process.env.AUTH0_ISSUER_BASE_URL ?? "", + clientId: process.env.AUTH0_MANAGEMENT_CLIENT_ID ?? "", + clientSecret: process.env.AUTH0_MANAGEMENT_CLIENT_SECRET ?? "", }) try { const user = await m.users.get({ id: userId, fields: "nickname" }) diff --git a/js/cf-api/index.ts b/js/cf-api/index.ts index 6be1a3f16..0897d371f 100644 --- a/js/cf-api/index.ts +++ b/js/cf-api/index.ts @@ -1,3 +1,4 @@ +import dotenv from "dotenv" import { createNodeMiddleware } from "octokit" import express from "express" import suggester from "code-suggester" @@ -6,18 +7,20 @@ import { type FileDiffContent } from "code-suggester/build/src/types" import { determineValidHunks, objectToMap } from "./utils" import { createPullRequestFromDiffContents } from "./create-pr-from-diffcontents" import { githubApp, webhookApiPath } from "./github-app" +import { userNickname } from "./auth0-mgmt" + import functions from "@codeflash-ai/common/token-functions" -import dotenv from "dotenv" -import { userNickname } from "./auth0-utils" +import * as console from "console" const userForAPIKey = functions.userForAPIKey -const port = process.env.PORT || 3001 - if (process.env.NODE_ENV !== "production") { + console.log("Using .env.local file to supply config environment variables") dotenv.config({ path: ".env.local" }) } +const port = process.env.PORT || 3001 + const appExpress = express() appExpress.use((req, res, next) => { @@ -52,6 +55,7 @@ appExpress.use(async (req, res, next) => { const apiKey = authHeader.replace(/^Bearer\s+/, "") const userId = await userForAPIKey(apiKey) if (userId == null) { + console.log(`Userid null for API key ${apiKey}. Returning 403`) return res.status(403).send("Invalid API key") } req.userId = userId diff --git a/js/cf-api/package.json b/js/cf-api/package.json index ef26c16e2..98c87a68b 100644 --- a/js/cf-api/package.json +++ b/js/cf-api/package.json @@ -5,6 +5,7 @@ "type": "module", "scripts": { "npx": "npx", + "build": "npm install --loglevel verbose && npx prisma generate", "deploy": "az webapp up -n codeflash-api --sku P1V2 --runtime NODE:20-lts", "start": "npx tsx index.ts" }, @@ -12,7 +13,7 @@ "@azure/identity": "^4.0.0", "@azure/keyvault-keys": "^4.7.2", "@azure/keyvault-secrets": "^4.7.0", - "@codeflash-ai/common": "1.0.1", + "@codeflash-ai/common": "1.0.2", "@prisma/client": "^5.6.0", "auth0": "^4.2.0", "body-parser": "^1.20.2", diff --git a/js/cf-webapp/.eslintrc.js b/js/cf-webapp/.eslintrc.cjs similarity index 100% rename from js/cf-webapp/.eslintrc.js rename to js/cf-webapp/.eslintrc.cjs diff --git a/js/common/package.json b/js/common/package.json index 8b896bb74..594602026 100644 --- a/js/common/package.json +++ b/js/common/package.json @@ -1,6 +1,6 @@ { "name": "@codeflash-ai/common", - "version": "1.0.1", + "version": "1.0.2", "main": "index.ts", "types": "index.d.ts", "repository": "https://github.com/codeflash-ai/codeflash.git", diff --git a/js/common/token-functions.ts b/js/common/token-functions.ts index fa91aa6bb..83ee11c85 100644 --- a/js/common/token-functions.ts +++ b/js/common/token-functions.ts @@ -1,8 +1,8 @@ "use server" import util from "util" -import {createHash, type Hash, randomBytes} from "crypto" -import {prisma} from "./index" -import {cf_api_keys} from "@prisma/client"; +import { createHash, type Hash, randomBytes } from "crypto" +import { prisma } from "./index" +import { cf_api_keys } from "@prisma/client" // const prisma = new PrismaClient() async function generateRandomAPIToken(): Promise { @@ -55,20 +55,22 @@ export async function userForAPIKey(key: string): Promise { }, }) if (result == null) { + console.log(`No user found for API key ${key}`) return null } else { return result.user_id } } catch (e) { + console.log(`Error in userForAPIKey: ${e}`) return null } } -export async function deleteAPIKeyById(id: number, userId: string): Promise { +export async function deleteAPIKeyById(id: number, userId: string): Promise { return prisma.cf_api_keys.delete({ where: { id, user_id: userId, }, - }); + }) }