Skip to content

Commit

Permalink
Refactor & Severe Fix Bug
Browse files Browse the repository at this point in the history
Signed-off-by: Peng Jingwen <pengsrc@icloud.com>
  • Loading branch information
pengsrc committed Aug 19, 2016
1 parent ac6a3d9 commit b093a42
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 75 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ QingStor Authorization Signature Dynamic Value Extension for [Paw](https://paw.c

## Screenshot

![Version 1.0](screenshots/version_1.0.png)
![Version 1.5.0](screenshots/version_1.5.0.png)



Expand Down Expand Up @@ -60,7 +60,9 @@ make install
* v1.0.2 (2016-08-19)
* Change 'QingStor Access Key' type to String


* v1.5.0 (2016-08-19)
* Severe Bug Fix
* Refactor


Contributing
Expand Down
File renamed without changes
Binary file added screenshots/version_1.5.0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
133 changes: 73 additions & 60 deletions src/QingStorAuthorizationSignature.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,47 @@
import CryptoJS from 'crypto-js';

export default class QingStorAuthorizationSignature {
constructor(accessKey, secretAccessKey, signOptions) {
this.accessKey = accessKey;
this.secretAccessKey = secretAccessKey;
this.signOptions = signOptions;
static buildCanonicalizedHeaders(requestHeaders) {

const canonicalizedHeaderKeys = [];
for (const canonicalizedHeaderKey in requestHeaders) {
if (requestHeaders.hasOwnProperty(canonicalizedHeaderKey)) {
if (canonicalizedHeaderKey.startsWith('X-QS-')) {
canonicalizedHeaderKeys.push(canonicalizedHeaderKey);
}
}
}
canonicalizedHeaderKeys.sort();

const canonicalizedHeaders = [];
for (let i = 0; i < canonicalizedHeaderKeys.length; i++) {
canonicalizedHeaders.push(
`${canonicalizedHeaderKeys[i].toLowerCase()}:${requestHeaders[canonicalizedHeaderKeys[i]]}`
);
}

const canonicalizedHeadersString = canonicalizedHeaders.join(`\n`);

return canonicalizedHeadersString === '' ? '' : `${canonicalizedHeadersString}\n`;
}

parseCanonicalizedHeaders(canonicalizedHeaders) {
if (canonicalizedHeaders) {
const raws = canonicalizedHeaders;
static buildCanonicalizedResource(request, locationStyle) {
const originalParamsString = new DynamicValue('com.luckymarmot.RequestURLDynamicValue', {
request: request.id,
includeScheme: false,
includeHost: false,
includeParameters: true,
}).getEvaluatedString();

let paramsString = originalParamsString;
if (originalParamsString.includes('?')) {
const resources = originalParamsString.split('?');
const subResource = resources[1];
const raws = subResource.split('&');
const map = {};
for (let i = 0; i < raws.length; i++) {
const raw = raws[i].toString();
if (raw.endsWith(',true')) {
let piece = raw.replace(',true', '');
piece = piece.replace(',', 'SPECIAL_CHARACTER');
const parts = piece.split('SPECIAL_CHARACTER');
map[parts[0].toLowerCase()] = parts[1];
}
const pieces = raws[i].split('=');
map[pieces[0]] = pieces[1];
}

const keys = [];
Expand All @@ -29,66 +52,56 @@ export default class QingStorAuthorizationSignature {
}
keys.sort();

const headers = [];
const values = [];
for (let i = 0; i < keys.length; i++) {
headers.push(`${keys[i]}:${map[keys[i]]}`);
if (map[keys[i]] === undefined) {
values.push(`${keys[i]}`);
} else {
values.push(`${keys[i]}=${map[keys[i]]}`);
}
}

const headersString = headers.join("\n");

if (headersString !== '') {
return headersString + "\n";
}
paramsString = `${resources[0]}?${values.join('&')}`;
}

return '';
}
if (locationStyle === 'virtual_host_style') {
const hostString = new DynamicValue('com.luckymarmot.RequestURLDynamicValue', {
request: request.id,
includeScheme: false,
includeHost: true,
includeParameters: false,
}).getEvaluatedString();
const bucketName = hostString.split('.')[0];

parseCanonicalizedResource(canonicalizedResource) {
if (canonicalizedResource) {
if (canonicalizedResource.includes('?')) {
const resources = canonicalizedResource.split('?');
const subResource = resources[1];
const raws = subResource.split('&');
const map = {};
for (let i = 0; i < raws.length; i++) {
const pieces = raws[i].split('=');
map[pieces[0]] = pieces[1];
}
return `/${bucketName}${paramsString.replace(/^\/\?/g, '?')}`;
}

const keys = [];
for (const key in map) {
if (map.hasOwnProperty(key)) {
keys.push(key);
}
}
keys.sort();

const values = [];
for (let i = 0; i < keys.length; i++) {
if (map[keys[i]] === undefined) {
values.push(`${keys[i]}`);
} else {
values.push(`${keys[i]}=${map[keys[i]]}`);
}
}
return `${resources[0]}?${values.join('&')}`;
}
return canonicalizedResource;
if (locationStyle === 'path_style') {
return paramsString === '' ? '/' : paramsString;
}

return '';
}

constructor(request, accessKey, secretAccessKey, locationStyle) {
this.request = request;
this.accessKey = accessKey;
this.secretAccessKey = secretAccessKey;
this.locationStyle = locationStyle;
}

buildStringToSign() {
const options = this.signOptions;
const request = this.request;
const headers = request.headers;
const style = this.locationStyle;

return (
`${options.Verb}\n` +
`${options['Content-MD5'] ? options['Content-MD5'] : ''}\n` +
`${options['Content-Type'] ? options['Content-Type'] : ''}\n` +
`${options.Date ? options.Date : ''}\n` +
`${this.parseCanonicalizedHeaders(options['Canonicalized Headers'])}` +
`${this.parseCanonicalizedResource(options['Canonicalized Resource'])}`
`${request.method}\n` +
`${headers['Content-MD5'] ? headers['Content-MD5'] : ''}\n` +
`${headers['Content-Type'] ? headers['Content-Type'] : ''}\n` +
`${headers.Date ? headers.Date : ''}\n` +
`${QingStorAuthorizationSignature.buildCanonicalizedHeaders(headers)}` +
`${QingStorAuthorizationSignature.buildCanonicalizedResource(request, style)}`
);
}

Expand Down
23 changes: 10 additions & 13 deletions src/QingStorAuthorizationSignatureDynamicValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,13 @@ class QingStorAuthorizationSignatureDynamicValue {
return '** signature is only generated during request send **';
}

const request = context.getCurrentRequest();
const headers = request.headers;

const signature = new QingStorAuthorizationSignature(
context.getCurrentRequest(),
this.accessKey,
this.secretAccessKey,
{
'Verb': request.method,
'Content-MD5': headers['Content-MD5'],
'Content-Type': headers['Content-Type'],
'Date': headers.Date,
'Canonicalized Headers': this.canonicalizedHeaders,
'Canonicalized Resource': this.canonicalizedResource,
}
this.locationStyle || 'virtual_host_style'
);

return signature.sign();
}
}
Expand All @@ -32,8 +24,13 @@ Object.assign(QingStorAuthorizationSignatureDynamicValue, {
inputs: [
DynamicValueInput('accessKey', 'QingStor Access Key', 'String'),
DynamicValueInput('secretAccessKey', 'QingStor Secret Access Key', 'SecureValue'),
DynamicValueInput('canonicalizedHeaders', 'Canonicalized Headers', 'KeyValueList'),
DynamicValueInput('canonicalizedResource', 'Canonicalized Resource', 'String'),
DynamicValueInput('locationStyle', 'Location Style', 'Select', {
choices: {
'virtual_host_style': 'Virtual-Host Style',
'path_style': 'Path Style',
},
persisted: true,
}),
],
});

Expand Down

0 comments on commit b093a42

Please sign in to comment.