Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4dd4ea7

Browse files
Merge pull request #27 from code-with-antonio/28-encrypting-credentials
28: encrypting credentials
2 parents f098040 + f9fc483 commit 4dd4ea7

8 files changed

Lines changed: 23 additions & 6 deletions

File tree

package-lock.json

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"client-only": "^0.0.1",
6161
"clsx": "^2.1.1",
6262
"cmdk": "^1.1.1",
63+
"cryptr": "^6.4.0",
6364
"date-fns": "^4.1.0",
6465
"embla-carousel-react": "^8.6.0",
6566
"handlebars": "^4.7.8",

src/features/credentials/server/routers.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { createTRPCRouter, premiumProcedure, protectedProcedure } from "@/trpc/i
33
import z from "zod";
44
import { PAGINATION } from "@/config/constants";
55
import { CredentialType } from "@/generated/prisma";
6+
import { encrypt } from "@/lib/encryption";
67

78
export const credentialsRouter = createTRPCRouter({
89
create: premiumProcedure
@@ -21,7 +22,7 @@ export const credentialsRouter = createTRPCRouter({
2122
name,
2223
userId: ctx.auth.user.id,
2324
type,
24-
value, // TODO: Consider encrypting in production
25+
value: encrypt(value),
2526
},
2627
});
2728
}),
@@ -52,7 +53,7 @@ export const credentialsRouter = createTRPCRouter({
5253
data: {
5354
name,
5455
type,
55-
value, // TODO: Consider encrypting in production
56+
value: encrypt(value),
5657
}
5758
});
5859
}),

src/features/executions/components/anthropic/executor.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { createAnthropic } from "@ai-sdk/anthropic";
55
import type { NodeExecutor } from "@/features/executions/types";
66
import { anthropicChannel } from "@/inngest/channels/anthropic";
77
import prisma from "@/lib/db";
8+
import { decrypt } from "@/lib/encryption";
89

910
Handlebars.registerHelper("json", (context) => {
1011
const jsonString = JSON.stringify(context, null, 2);
@@ -90,7 +91,7 @@ export const anthropicExecutor: NodeExecutor<AnthropicData> = async ({
9091
}
9192

9293
const anthropic = createAnthropic({
93-
apiKey: credential.value,
94+
apiKey: decrypt(credential.value),
9495
});
9596

9697
try {

src/features/executions/components/gemini/executor.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { createGoogleGenerativeAI } from "@ai-sdk/google";
55
import type { NodeExecutor } from "@/features/executions/types";
66
import { geminiChannel } from "@/inngest/channels/gemini";
77
import prisma from "@/lib/db";
8+
import { decrypt } from "@/lib/encryption";
89

910
Handlebars.registerHelper("json", (context) => {
1011
const jsonString = JSON.stringify(context, null, 2);
@@ -90,7 +91,7 @@ export const geminiExecutor: NodeExecutor<GeminiData> = async ({
9091
}
9192

9293
const google = createGoogleGenerativeAI({
93-
apiKey: credential.value,
94+
apiKey: decrypt(credential.value),
9495
});
9596

9697
try {

src/features/executions/components/http-request/dialog.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ const formSchema = z.object({
4545
body: z
4646
.string()
4747
.optional()
48-
// .refine() TODO JSON5
4948
});
5049

5150
export type HttpRequestFormValues = z.infer<typeof formSchema>;

src/features/executions/components/openai/executor.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { createOpenAI } from "@ai-sdk/openai";
55
import type { NodeExecutor } from "@/features/executions/types";
66
import { openAiChannel } from "@/inngest/channels/openai";
77
import prisma from "@/lib/db";
8+
import { decrypt } from "@/lib/encryption";
89

910
Handlebars.registerHelper("json", (context) => {
1011
const jsonString = JSON.stringify(context, null, 2);
@@ -90,7 +91,7 @@ export const openAiExecutor: NodeExecutor<OpenAiData> = async ({
9091
}
9192

9293
const openai = createOpenAI({
93-
apiKey: credential.value,
94+
apiKey: decrypt(credential.value),
9495
});
9596

9697
try {

src/lib/encryption.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import Cryptr from "cryptr";
2+
3+
const cryptr = new Cryptr(process.env.ENCRYPTION_KEY!);
4+
5+
export const encrypt = (text: string) => cryptr.encrypt(text);
6+
export const decrypt = (text: string) => cryptr.decrypt(text);

0 commit comments

Comments
 (0)