Fix: Now supports AWS signature version 4. (#949)

* intial commiy

* cleaning code

* added makeCredential to s3.js and removed extra module dependecy

* lint fixes

* minor fix

* minor fixes

* changed encoding type from string to any

* added new env var to env.sample
This commit is contained in:
Himanshu Agarwal
2019-06-19 13:03:38 +05:30
committed by Tom Moor
parent ebd9535cb4
commit 0079593446
4 changed files with 50 additions and 13 deletions

View File

@ -1,7 +1,13 @@
// @flow
import uuid from 'uuid';
import Router from 'koa-router';
import { makePolicy, signPolicy, publicS3Endpoint } from '../utils/s3';
import format from 'date-fns/format';
import {
makePolicy,
getSignature,
publicS3Endpoint,
makeCredential,
} from '../utils/s3';
import { ValidationError } from '../errors';
import { Event, User, Team } from '../models';
import auth from '../middlewares/authentication';
@ -63,7 +69,9 @@ router.post('users.s3Upload', auth(), async ctx => {
const s3Key = uuid.v4();
const key = `uploads/${ctx.state.user.id}/${s3Key}/${filename}`;
const policy = makePolicy();
const credential = makeCredential();
const longDate = format(new Date(), 'YYYYMMDDTHHmmss\\Z');
const policy = makePolicy(credential, longDate);
const endpoint = publicS3Endpoint();
const url = `${endpoint}/${key}`;
@ -84,13 +92,15 @@ router.post('users.s3Upload', auth(), async ctx => {
maxUploadSize: process.env.AWS_S3_UPLOAD_MAX_SIZE,
uploadUrl: endpoint,
form: {
AWSAccessKeyId: process.env.AWS_ACCESS_KEY_ID,
'Cache-Control': 'max-age=31557600',
'Content-Type': kind,
key,
acl: 'public-read',
signature: signPolicy(policy),
key,
policy,
'x-amz-algorithm': 'AWS4-HMAC-SHA256',
'x-amz-credential': credential,
'x-amz-date': longDate,
'x-amz-signature': getSignature(policy),
},
asset: {
contentType: kind,