Skip to content

Commit

Permalink
storage: allow using custom domain name when creating signed resources
Browse files Browse the repository at this point in the history
  • Loading branch information
AVVS authored and stephenplusplus committed Jul 6, 2016
1 parent 24810b9 commit 0a6da60
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 17 deletions.
34 changes: 17 additions & 17 deletions lib/storage/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -1219,6 +1219,8 @@ File.prototype.getSignedPolicy = function(options, callback) {
* @param {object} config - Configuration object.
* @param {string} config.action - "read" (HTTP: GET), "write" (HTTP: PUT), or
* "delete" (HTTP: DELETE).
* @param {string=} config.cname - The cname for this bucket, i.e.,
* "https://cdn.example.com".
* @param {string=} config.contentMd5 - The MD5 digest value in base64. If you
* provide this, the client must provide this HTTP header with this same
* value in its request.
Expand Down Expand Up @@ -1288,6 +1290,7 @@ File.prototype.getSignedPolicy = function(options, callback) {
* });
*/
File.prototype.getSignedUrl = function(config, callback) {
var self = this;
var expires = new Date(config.expires);
var expiresInSeconds = Math.round(expires / 1000); // The API expects seconds.

Expand All @@ -1304,8 +1307,7 @@ File.prototype.getSignedUrl = function(config, callback) {
}[config.action];

var name = encodeURIComponent(this.name);
var targetGeneration = this.generation;

var host = config.cname || STORAGE_DOWNLOAD_BASE_URL + '/' + self.bucket.name;
config.resource = '/' + this.bucket.name + '/' + name;

this.storage.getCredentials(function(err, credentials) {
Expand Down Expand Up @@ -1345,14 +1347,14 @@ File.prototype.getSignedUrl = function(config, callback) {
].join('\n'));
var signature = sign.sign(credentials.private_key, 'base64');

var responseContentType = '';
var responseContentType;
if (is.string(config.responseType)) {
responseContentType =
'&response-content-type=' +
encodeURIComponent(config.responseType);
}

var responseContentDisposition = '';
var responseContentDisposition;
if (is.string(config.promptSaveAs)) {
responseContentDisposition =
'&response-content-disposition=attachment; filename="' +
Expand All @@ -1364,20 +1366,18 @@ File.prototype.getSignedUrl = function(config, callback) {
encodeURIComponent(config.responseDisposition);
}

var generation = '';
if (!is.undefined(targetGeneration)) {
generation = '&generation=' + targetGeneration;
}
var signedUrl = format('{host}/{name}{id}{exp}{sig}{type}{disp}{gen}', {
host: host.replace(/\/*$/, ''), // Remove trailing slashes.
name: name,
id: '?GoogleAccessId=' + credentials.client_email,
exp: '&Expires=' + expiresInSeconds,
sig: '&Signature=' + encodeURIComponent(signature),
type: responseContentType || '',
disp: responseContentDisposition || '',
gen: self.generation ? '&generation=' + self.generation : ''
});

callback(null, [
'https://storage.googleapis.com' + config.resource,
'?GoogleAccessId=' + credentials.client_email,
'&Expires=' + expiresInSeconds,
'&Signature=' + encodeURIComponent(signature),
responseContentType,
responseContentDisposition,
generation
].join(''));
callback(null, signedUrl);
});
};

Expand Down
31 changes: 31 additions & 0 deletions test/storage/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -1832,6 +1832,37 @@ describe('File', function() {
});
});

describe('cname', function() {
it('should use a provided cname', function(done) {
var host = 'http://www.example.com';

file.getSignedUrl({
action: 'read',
cname: host,
expires: Date.now() + 5
}, function(err, signedUrl) {
assert.ifError(err);
assert.strictEqual(signedUrl.indexOf(host), 0);
done();
});
});

it('should remove trailing slashes from cname', function(done) {
var host = 'http://www.example.com//';

file.getSignedUrl({
action: 'read',
cname: host,
expires: Date.now() + 5
}, function(err, signedUrl) {
assert.ifError(err);
assert.strictEqual(signedUrl.indexOf(host), -1);
assert.strictEqual(signedUrl.indexOf(host.substr(0, -1)), 0);
done();
});
});
});

describe('promptSaveAs', function() {
it('should add response-content-disposition', function(done) {
var disposition = 'attachment; filename="fname.ext"';
Expand Down

0 comments on commit 0a6da60

Please sign in to comment.