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

Allow for policy to be remotely generated. #55

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-aws3",
"version": "0.0.8",
"name": "@timbrandin/react-native-aws3",
"version": "0.0.9",
"description": "Pure JavaScript react native library for uploading to AWS S3",
"author": {
"name": "Ben Reinhart"
Expand Down
39 changes: 9 additions & 30 deletions src/RNS3.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,23 @@
*/

import { Request } from './Request'
import { setBodyAsParsedXML } from './utils';
import { S3Policy } from './S3Policy'

const AWS_DEFAULT_S3_HOST = 's3.amazonaws.com'

const EXPECTED_RESPONSE_KEY_VALUE_RE = {
key: /<Key>(.*)<\/Key>/,
etag: /<ETag>"?([^"]*)"?<\/ETag>/,
bucket: /<Bucket>(.*)<\/Bucket>/,
location: /<Location>(.*)<\/Location>/,
}

const entries = o =>
Object.keys(o).map(k => [k, o[k]])

const extractResponseValues = (responseText) =>
entries(EXPECTED_RESPONSE_KEY_VALUE_RE).reduce((result, [key, regex]) => {
const match = responseText.match(regex)
return { ...result, [key]: match && match[1] }
}, {})

const setBodyAsParsedXML = (response) =>
({
...response,
body: { postResponse: response.text == null ? null : extractResponseValues(response.text) }
})

export class RNS3 {
static put(file, options) {
options = {
...options,
key: (options.keyPrefix || '') + file.name,
date: new Date,
contentType: file.type
static put(file, options, policy) {
if (!policy) {
policy = S3Policy.generate({
...options,
key: (options.keyPrefix || '') + file.name,
date: new Date,
contentType: file.type
})
}

const url = `https://${options.bucket}.${options.awsUrl || AWS_DEFAULT_S3_HOST}`
const method = "POST"
const policy = S3Policy.generate(options)

return Request.create(url, method, policy)
.set("file", file)
Expand Down
23 changes: 23 additions & 0 deletions src/RNS3Client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* RNS3
*/

import { Request } from './Request'
import { setBodyAsParsedXML } from './utils';

const AWS_DEFAULT_S3_HOST = 's3.amazonaws.com'

export class RNS3Client {
static put(file, options, policy) {
if (!policy) {
throw new Error('missing policy');
}
const url = `https://${options.bucket}.${options.awsUrl || AWS_DEFAULT_S3_HOST}`
const method = "POST"

return Request.create(url, method, policy)
.set("file", file)
.send()
.then(setBodyAsParsedXML)
}
}
15 changes: 8 additions & 7 deletions src/S3Policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
* S3Policy
*/

const CryptoJS = require('crypto-js');
const HmacSHA256 = require('crypto-js').HmacSHA256;
const Hex = require('crypto-js').enc.Hex;
const Buffer = global.Buffer || require('buffer').Buffer;
const { dateToString } = require('./DateUtils');

Expand Down Expand Up @@ -100,17 +101,17 @@ const getEncodedPolicy = (policy) => {
}

const getSignature = (base64EncodedPolicy, options) => {
return CryptoJS.HmacSHA256(
return HmacSHA256(
base64EncodedPolicy,
getSignatureKey(options)
).toString(CryptoJS.enc.Hex);
).toString(Hex);
}

const getSignatureKey = (options) => {
const kDate = CryptoJS.HmacSHA256(options.yyyymmddDate, "AWS4" + options.secretKey);
const kRegion = CryptoJS.HmacSHA256(options.region, kDate);
const kService = CryptoJS.HmacSHA256(AWS_SERVICE_NAME, kRegion);
const kSigning = CryptoJS.HmacSHA256(AWS_REQUEST_POLICY_VERSION, kService);
const kDate = HmacSHA256(options.yyyymmddDate, "AWS4" + options.secretKey);
const kRegion = HmacSHA256(options.region, kDate);
const kService = HmacSHA256(AWS_SERVICE_NAME, kRegion);
const kSigning = HmacSHA256(AWS_REQUEST_POLICY_VERSION, kService);

return kSigning;
}
21 changes: 21 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const EXPECTED_RESPONSE_KEY_VALUE_RE = {
key: /<Key>(.*)<\/Key>/,
etag: /<ETag>"?([^"]*)"?<\/ETag>/,
bucket: /<Bucket>(.*)<\/Bucket>/,
location: /<Location>(.*)<\/Location>/,
}

const entries = o =>
Object.keys(o).map(k => [k, o[k]])

const extractResponseValues = (responseText) =>
entries(EXPECTED_RESPONSE_KEY_VALUE_RE).reduce((result, [key, regex]) => {
const match = responseText.match(regex)
return { ...result, [key]: match && match[1] }
}, {})

export const setBodyAsParsedXML = (response) =>
({
...response,
body: { postResponse: response.text == null ? null : extractResponseValues(response.text) }
})