Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev: sign requests sent to Transloadit #3517

Merged
merged 2 commits into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,6 @@ VITE_TUS_ENDPOINT=https://tusd.tusdemo.net/files/
VITE_XHR_ENDPOINT=https://xhr-server.herokuapp.com/upload

VITE_TRANSLOADIT_KEY=***
VITE_TRANSLOADIT_SECRET=***
VITE_TRANSLOADIT_TEMPLATE=***
VITE_TRANSLOADIT_SERVICE_URL=https://api2.transloadit.com
27 changes: 18 additions & 9 deletions private/dev/Dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,39 @@ import Audio from '@uppy/audio'
import Compressor from '@uppy/compressor'
/* eslint-enable import/no-extraneous-dependencies */

import generateSignatureIfSecret from './generateSignatureIfSecret.js'

// DEV CONFIG: create a .env file in the project root directory to customize those values.
const {
VITE_UPLOADER : UPLOADER,
VITE_COMPANION_URL : COMPANION_URL,
VITE_TUS_ENDPOINT : TUS_ENDPOINT,
VITE_XHR_ENDPOINT : XHR_ENDPOINT,
VITE_TRANSLOADIT_KEY : TRANSLOADIT_KEY,
VITE_TRANSLOADIT_SECRET : TRANSLOADIT_SECRET,
VITE_TRANSLOADIT_TEMPLATE : TRANSLOADIT_TEMPLATE,
VITE_TRANSLOADIT_SERVICE_URL : TRANSLOADIT_SERVICE_URL,
} = import.meta.env

import.meta.env.VITE_TRANSLOADIT_KEY = '***' // to avoid leaking secrets in screenshots.
import.meta.env.VITE_TRANSLOADIT_KEY &&= '***' // to avoid leaking secrets in screenshots.
import.meta.env.VITE_TRANSLOADIT_SECRET &&= '***' // to avoid leaking secrets in screenshots.
console.log(import.meta.env)

// DEV CONFIG: enable or disable Golden Retriever

const RESTORE = false

async function getAssemblyOptions () {
return generateSignatureIfSecret(TRANSLOADIT_SECRET, {
auth: {
key: TRANSLOADIT_KEY,
},
// It's more secure to use a template_id and enable
// Signature Authentication
template_id: TRANSLOADIT_TEMPLATE,
})
}

// Rest is implementation! Obviously edit as necessary...

export default () => {
Expand Down Expand Up @@ -111,21 +126,15 @@ export default () => {
uppyDashboard.use(Transloadit, {
service: TRANSLOADIT_SERVICE_URL,
waitForEncoding: true,
params: {
auth: { key: TRANSLOADIT_KEY },
template_id: TRANSLOADIT_TEMPLATE,
},
getAssemblyOptions,
})
break
case 'transloadit-s3':
uppyDashboard.use(AwsS3, { companionUrl: COMPANION_URL })
uppyDashboard.use(Transloadit, {
waitForEncoding: true,
importFromUploadURLs: true,
params: {
auth: { key: TRANSLOADIT_KEY },
template_id: TRANSLOADIT_TEMPLATE,
},
getAssemblyOptions,
})
break
case 'transloadit-xhr':
Expand Down
3 changes: 2 additions & 1 deletion private/dev/DragDrop.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const {
VITE_TUS_ENDPOINT : TUS_ENDPOINT,
} = import.meta.env

import.meta.env.VITE_TRANSLOADIT_KEY = '***' // to avoid leaking secrets in screenshots.
import.meta.env.VITE_TRANSLOADIT_KEY &&= '***' // to avoid leaking secrets in screenshots.
import.meta.env.VITE_TRANSLOADIT_SECRET &&= '***' // to avoid leaking secrets in screenshots.
console.log(import.meta.env)

export default () => {
Expand Down
34 changes: 34 additions & 0 deletions private/dev/generateSignatureIfSecret.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const enc = new TextEncoder('utf-8')
async function sign (secret, body) {
const algorithm = { name: 'HMAC', hash: 'SHA-384' }

const key = await crypto.subtle.importKey('raw', enc.encode(secret), algorithm, false, ['sign', 'verify'])
const signature = await crypto.subtle.sign(algorithm.name, key, enc.encode(body))
return `sha384:${Array.from(new Uint8Array(signature), x => x.toString(16).padStart(2, '0')).join('')}`
}
function getExpiration (future) {
return new Date(Date.now() + future)
.toISOString()
.replace('T', ' ')
.replace(/\.\d+Z$/, '+00:00')
}
/**
* Adds an expiration date and signs the params object if a secret is passed to
* it. If no secret is given, it returns the same object.
*
* @param {string | undefined} secret
* @param {object} params
* @returns {{ params: string, signature?: string }}
*/
export default async function generateSignatureIfSecret (secret, params) {
let signature
if (secret) {
// eslint-disable-next-line no-param-reassign
params.auth.expires = getExpiration(5 * 60 * 1000)
// eslint-disable-next-line no-param-reassign
params = JSON.stringify(params)
signature = await sign(secret, params)
}

return { params, signature }
}