Skip to content

Commit

Permalink
WIP: Start on signature generation
Browse files Browse the repository at this point in the history
  • Loading branch information
dac09 committed Aug 19, 2024
1 parent 52c57c9 commit 540bdcc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
14 changes: 14 additions & 0 deletions packages/uploads/src/__tests__/generateSignature.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { beforeAll, describe, test } from 'vitest'

import { generateSignature } from '../lib/generateSignature.js'

describe('Generate signature', () => {
beforeAll(() => {
process.env.RW_UPLOADS_SECRET = 'bazinga'
})

test('It creates a signature', () => {
const out = generateSignature('/tmp/myfile.txt', 500)
console.log(`👉 \n ~ out:`, out)
})
})
16 changes: 16 additions & 0 deletions packages/uploads/src/lib/generateSignature.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import crypto from 'node:crypto'
export const generateSignature = (filePath: string, expiresIn: number) => {
if (!process.env.RW_UPLOADS_SECRET) {
throw new Error(
'Please configure RW_UPLOADS_SECRET in your environment variables',
)
}

const expires = Math.floor(Date.now() / 1000) + expiresIn
const signature = crypto
.createHmac('sha256', process.env.RW_UPLOADS_SECRET)
.update(`${filePath}:${expires}`)
.digest('hex')

return signature
}

0 comments on commit 540bdcc

Please sign in to comment.