Comparing version 1.0.1 to 1.0.2
# Changelog | ||
## 1.0.2 | ||
* Fix noAuthenticated calls | ||
* Optionnal consumer key, now checked only on debug | ||
* Fix unicode (thanks to @naholyr #4) | ||
## 1.0.1 | ||
@@ -4,0 +10,0 @@ |
56
ovh.js
@@ -330,2 +330,10 @@ // | ||
if (typeof(this.consumerKey) !== 'string' && | ||
!api._api.operations[i].noAuthentication) { | ||
return this.warn( | ||
'[OVH] The API call ' + pathStr + ' requires an authentication' + | ||
' with a consumer key.' | ||
); | ||
} | ||
return true; | ||
@@ -357,3 +365,3 @@ } | ||
} | ||
// Time drift | ||
@@ -376,6 +384,2 @@ if (this.apiTimeDiff === null && path !== '/auth/time') { | ||
if (path.indexOf('/auth') < 0 && typeof(this.consumerKey) !== 'string') { | ||
return callback('[OVH] No consumerKey defined'); | ||
} | ||
// Replace "{str}", used for $call() | ||
@@ -418,5 +422,10 @@ if (path.indexOf('{') >= 0) { | ||
var reqBody = null; | ||
if (typeof(params) === 'object' && Object.keys(params).length > 0) { | ||
if (httpMethod === 'PUT' || httpMethod === 'POST') { | ||
options.headers['Content-Length'] = JSON.stringify(params).length; | ||
// Escape unicode | ||
reqBody = JSON.stringify(params).replace(/[\u0080-\uFFFF]/g, function(m) { | ||
return "\\u" + ("0000" + m.charCodeAt(0).toString(16)).slice(-4); | ||
}); | ||
options.headers['Content-Length'] = reqBody.length; | ||
} | ||
@@ -428,12 +437,15 @@ else { | ||
// Sign request | ||
if (path.indexOf('/auth') < 0) { | ||
options.headers['X-Ovh-Consumer'] = this.consumerKey; | ||
options.headers['X-Ovh-Timestamp'] = | ||
Math.round(Date.now() / 1000) + this.apiTimeDiff; | ||
options.headers['X-Ovh-Signature'] = | ||
this.signRequest( | ||
httpMethod, 'https://' + options.host + options.path, | ||
params, options.headers['X-Ovh-Timestamp'] | ||
); | ||
// Sign request | ||
if (typeof(this.consumerKey) === 'string') { | ||
options.headers['X-Ovh-Consumer'] = this.consumerKey; | ||
options.headers['X-Ovh-Signature'] = | ||
this.signRequest( | ||
httpMethod, 'https://' + options.host + options.path, | ||
reqBody, options.headers['X-Ovh-Timestamp'] | ||
); | ||
} | ||
} | ||
@@ -445,7 +457,4 @@ | ||
options.method, options.path, | ||
(httpMethod === 'PUT' || httpMethod === 'POST' && | ||
typeof(params) === 'object' && Object.keys(params).length > 0) ? | ||
JSON.stringify(params) : '' | ||
); | ||
reqBody || '' | ||
); | ||
} | ||
@@ -512,5 +521,4 @@ | ||
if (httpMethod === 'PUT' || httpMethod === 'POST' && | ||
typeof(params) === 'object' && Object.keys(params).length > 0) { | ||
req.write(JSON.stringify(params)); | ||
if (reqBody !== null) { | ||
req.write(reqBody); | ||
} | ||
@@ -521,3 +529,3 @@ | ||
Ovh.prototype.signRequest = function (httpMethod, url, params, timestamp) { | ||
Ovh.prototype.signRequest = function (httpMethod, url, body, timestamp) { | ||
var s = [ | ||
@@ -528,5 +536,3 @@ this.appSecret, | ||
url, | ||
(httpMethod === 'PUT' || httpMethod === 'POST') && | ||
typeof(params) === 'object' && | ||
Object.keys(params).length > 0 ? JSON.stringify(params) : '', | ||
body || '', | ||
timestamp | ||
@@ -533,0 +539,0 @@ ]; |
{ | ||
"name": "ovh", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "A helper library to use OVH.com REST API (v6)", | ||
"homepage": "https://github.com/gierschv/node-ovh", | ||
"homepage": "http://gierschv.github.io/node-ovh", | ||
"author": "Vincent Giersch <mail@vincent.sh>", | ||
@@ -7,0 +7,0 @@ "contributors": [], |
@@ -34,1 +34,5 @@ node-ovh | ||
``` | ||
[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/gierschv/node-ovh/trend.png)](https://bitdeli.com/free "Bitdeli Badge") | ||
@@ -43,4 +43,4 @@ // | ||
var rest = ovh({ appKey: 'XXX', appSecret: 'XXX', apis: [] }); | ||
rest.request('GET', '/me', function (err) { | ||
assert.equal(err, '[OVH] No consumerKey defined'); | ||
rest.request('GET', '/me', function (err, message) { | ||
assert.equal(err, 401); | ||
done(); | ||
@@ -47,0 +47,0 @@ }); |
@@ -64,3 +64,20 @@ // | ||
}); | ||
} | ||
}, | ||
'Call method without CK': function (done) { | ||
"use strict"; | ||
var rest = ovh({ | ||
appKey: APP_KEY, | ||
appSecret: APP_SECRET, | ||
apis: ['me'], | ||
warn: function (err) { | ||
assert.equal(err, '[OVH] The API call /me requires an authentication with a consumer key.'); | ||
done(); | ||
} | ||
}); | ||
rest.request('GET', '/me', function (err) { | ||
assert.ok(err); | ||
}); | ||
}, | ||
}; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
39861
15
882
38
1
4