1
1
#!/usr/bin/env node
2
2
3
- const crypto = require ( 'crypto' ) ;
3
+ const crypto = require ( 'crypto-js' ) ;
4
+ const Hex = require ( 'crypto-js/enc-hex' ) ;
4
5
const fs = require ( 'fs' ) ;
5
6
7
+ // from: https://docs.aws.amazon.com/general/latest/gr/signature-v4-examples.html#signature-v4-examples-javascript
8
+ function getSignatureKey ( key , dateStamp , regionName , serviceName ) {
9
+ const kDate = crypto . HmacSHA256 ( dateStamp , `AWS4${ key } ` ) ;
10
+ const kRegion = crypto . HmacSHA256 ( regionName , kDate ) ;
11
+ const kService = crypto . HmacSHA256 ( serviceName , kRegion ) ;
12
+ const kSigning = crypto . HmacSHA256 ( 'aws4_request' , kService ) ;
13
+ return kSigning ;
14
+ }
15
+
6
16
const awsAccessKeyId = '<your access key id>' ;
7
17
const awsSecretAccessKey = '<your secret access key>' ;
8
18
const bucketName = '<your bucket name>' ;
19
+ const region = '<your region name>' ;
9
20
10
21
const msPerDay = 24 * 60 * 60 * 1000 ;
11
22
const expiration = new Date ( Date . now ( ) + msPerDay ) . toISOString ( ) ;
12
23
const bucketUrl = `https://${ bucketName } .s3.amazonaws.com` ;
24
+ const date = new Date ( ) . toISOString ( ) . slice ( 0 , 10 ) . replace ( / - / g, '' ) ;
25
+ const credentials = `${ awsAccessKeyId } /${ date } /${ region } /s3/aws4_request` ;
13
26
27
+ // Sample policy and form: https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-post-example.html
14
28
const policy = {
15
29
expiration,
16
30
conditions : [
17
- [ 'starts-with' , '$key' , 'uploads/' ] ,
18
31
{ bucket : bucketName } ,
32
+ [ 'starts-with' , '$key' , 'uploads/' ] ,
19
33
{ acl : 'public-read' } ,
20
34
[ 'starts-with' , '$Content-Type' , 'image/png' ] ,
21
- { success_action_status : '201' } ,
35
+ // ['starts-with', '$success_action_redirect', ''],
36
+ [ 'starts-with' , '$success_action_status' , '' ] ,
37
+
38
+ { 'x-amz-credential' : credentials } ,
39
+ { 'x-amz-algorithm' : 'AWS4-HMAC-SHA256' } ,
40
+ { 'x-amz-date' : `${ date } T000000Z` } ,
22
41
] ,
23
42
} ;
24
43
25
- const policyB64 = Buffer ( JSON . stringify ( policy ) , 'utf-8' ) . toString ( 'base64' ) ;
44
+ const policyB64 = Buffer . from ( JSON . stringify ( policy ) , 'utf-8' ) . toString ( 'base64' ) ;
26
45
27
- const hmac = crypto . createHmac ( 'sha1' , awsSecretAccessKey ) ;
28
- hmac . update ( new Buffer ( policyB64 , 'utf-8' ) ) ;
46
+ const sigKey = getSignatureKey ( awsSecretAccessKey , date , region , 's3' ) ;
29
47
30
- const signature = hmac . digest ( 'base64' ) ;
48
+ const signature = Hex . stringify ( crypto . HmacSHA256 ( policyB64 , sigKey ) ) ;
31
49
32
50
fs . readFile ( 'frontend/index.template.html' , 'utf8' , ( err , input ) => {
33
51
if ( err ) {
@@ -36,8 +54,9 @@ fs.readFile('frontend/index.template.html', 'utf8', (err, input) => {
36
54
37
55
const data = input
38
56
. replace ( / % B U C K E T _ U R L % / g, bucketUrl )
39
- . replace ( / % A W S _ A C C E S S _ K E Y % / g, awsAccessKeyId )
40
57
. replace ( / % P O L I C Y _ B A S E 6 4 % / g, policyB64 )
58
+ . replace ( / % C R E D E N T I A L % / g, credentials )
59
+ . replace ( / % D A T E % / g, `${ date } T000000Z` )
41
60
. replace ( / % S I G N A T U R E % / g, signature ) ;
42
61
43
62
fs . writeFile ( 'frontend/index.html' , data , 'utf8' , ( e ) => {
0 commit comments