Skip to content

Commit

Permalink
Encrypt fileKey
Browse files Browse the repository at this point in the history
  • Loading branch information
yunusefendi52 committed May 4, 2024
1 parent fe8acbb commit e9219bb
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@tanstack/vue-query": "^5.28.9",
"@xmldom/xmldom": "^0.8.10",
"chart.js": "3.3.2",
"crypto-js": "^4.2.0",
"drizzle-orm": "^0.30.7",
"h3": "^1.11.1",
"jose": "^5.2.4",
Expand All @@ -39,6 +40,7 @@
},
"devDependencies": {
"@babel/eslint-parser": "^7.18.9",
"@types/crypto-js": "^4.2.2",
"@types/lodash": "^4.17.0",
"@types/pg": "^8.11.4",
"@types/uuid": "^9.0.8",
Expand Down
5 changes: 3 additions & 2 deletions server/api/artifacts/upload-artifact-url.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import { getStorageKeys, s3BucketName } from "~/server/utils/utils"
import { takeUniqueOrThrow } from "../detail-app.get"
import { CopyObjectCommand, DeleteObjectCommand, GetObjectCommand } from "@aws-sdk/client-s3"
import { S3AppClient } from "~/server/services/S3AppClient"
import { verifyToken } from "~/server/utils/token-utils"
import { decryptText, verifyToken } from "~/server/utils/token-utils"

export default defineEventHandler(async (event) => {
const { token, appName, orgName, releaseNotes, packageMetadata, } = await readBody(event)
const fileKey = (await verifyToken(event, token)).fileKey as string
const fileKey = (decryptText(event, token)).fileKey as string

const userId = event.context.auth.userId
const db = event.context.drizzle

const userOrg = await db.select({
organizationsId: organizations.id,
})
Expand Down
4 changes: 2 additions & 2 deletions server/api/artifacts/upload-artifact.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { organizations, organizationsPeople } from "~/server/db/schema";
import { and, eq } from "drizzle-orm";
import { takeUniqueOrThrow } from "../detail-app.get";
import { S3AppClient } from "~/server/services/S3AppClient";
import { generateToken } from "~/server/utils/token-utils";
import { encryptText } from "~/server/utils/token-utils";

export default defineEventHandler(async (event) => {
const { orgName, appName } = await readBody(event)
Expand All @@ -26,7 +26,7 @@ export default defineEventHandler(async (event) => {
}).then(takeUniqueOrThrow)

const key = generateRandomPassword()
const token = await generateToken(event, {
const token = encryptText(event, {
fileKey: key,
})
var expires = 500;
Expand Down
17 changes: 17 additions & 0 deletions server/utils/token-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { EventHandlerRequest, H3Event } from 'h3'
import * as jose from 'jose'
import Crypto from 'crypto-js'

const alg = 'HS256'

Expand All @@ -22,3 +23,19 @@ export const verifyToken = async (
return undefined
}
}

export const encryptText = (
event: H3Event<EventHandlerRequest>,
data: any) => {
const config = useRuntimeConfig(event)
return Crypto.AES.encrypt(JSON.stringify(data), config.JWT_KEY).toString()
}

export const decryptText = (
event: H3Event<EventHandlerRequest>,
token: string) => {
const config = useRuntimeConfig(event)
config.JWT_KEY
const value = Crypto.AES.decrypt(token, config.JWT_KEY).toString(Crypto.enc.Utf8)
return JSON.parse(value)
}

0 comments on commit e9219bb

Please sign in to comment.