Skip to content

Commit

Permalink
Fix authentication for some auth paths
Browse files Browse the repository at this point in the history
fixes ovh#20
  • Loading branch information
Aurélien GASTON committed Sep 13, 2018
1 parent 3037170 commit 9432a89
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
51 changes: 51 additions & 0 deletions lib/includes.polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// https://tc39.github.io/ecma262/#sec-array.prototype.includes
if (!Array.prototype.includes) {
Object.defineProperty(Array.prototype, 'includes', {
value: function(searchElement, fromIndex) {

if (this == null) {
throw new TypeError('"this" is null or not defined');
}

// 1. Let O be ? ToObject(this value).
var o = Object(this);

// 2. Let len be ? ToLength(? Get(O, "length")).
var len = o.length >>> 0;

// 3. If len is 0, return false.
if (len === 0) {
return false;
}

// 4. Let n be ? ToInteger(fromIndex).
// (If fromIndex is undefined, this step produces the value 0.)
var n = fromIndex | 0;

// 5. If n ≥ 0, then
// a. Let k be n.
// 6. Else n < 0,
// a. Let k be len + n.
// b. If k < 0, let k be 0.
var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);

function sameValueZero(x, y) {
return x === y || (typeof x === 'number' && typeof y === 'number' && isNaN(x) && isNaN(y));
}

// 7. Repeat, while k < len
while (k < len) {
// a. Let elementK be the result of ? Get(O, ! ToString(k)).
// b. If SameValueZero(searchElement, elementK) is true, return true.
if (sameValueZero(o[k], searchElement)) {
return true;
}
// c. Increase k by 1.
k++;
}

// 8. Return false
return false;
}
});
}
6 changes: 5 additions & 1 deletion lib/ovh.es5.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
*/
'use strict';

// String.includes polyfill
require('./includes.polyfill');

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
Expand All @@ -51,6 +54,7 @@ var Ovh = function () {
this.timeout = params.timeout;
this.apiTimeDiff = params.apiTimeDiff || null;
this.endpoint = params.endpoint || null;
this.noAuthPaths = params.noAuthPaths || ['/auth/credential', '/auth/time'];

// Preconfigured API endpoints
if (this.endpoint) {
Expand Down Expand Up @@ -363,7 +367,7 @@ var Ovh = function () {
}
}

if (path.indexOf('/auth') < 0) {
if (!this.noAuthPaths.includes(path)) {
options.headers['X-Ovh-Timestamp'] = Math.round(Date.now() / 1000) + this.apiTimeDiff;

// Sign request
Expand Down
8 changes: 6 additions & 2 deletions lib/ovh.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
*/
'use strict';

// String.includes polyfill
require('./includes.polyfill');

let https = require('https'),
Bluebird = require('bluebird'),
querystring = require('querystring'),
Expand All @@ -43,6 +46,7 @@ class Ovh {
this.timeout = params.timeout;
this.apiTimeDiff = params.apiTimeDiff || null;
this.endpoint = params.endpoint || null;
this.noAuthPaths = params.noAuthPaths || ['/auth/credential', '/auth/time'];

// Preconfigured API endpoints
if (this.endpoint) {
Expand Down Expand Up @@ -356,7 +360,7 @@ class Ovh {
}
}

if (path.indexOf('/auth') < 0) {
if (!this.noAuthPaths.includes(path)) {
options.headers['X-Ovh-Timestamp'] = Math.round(Date.now() / 1000) + this.apiTimeDiff;

// Sign request
Expand Down Expand Up @@ -487,4 +491,4 @@ class Ovh {

module.exports = function (params) {
return new Ovh(params || {});
};
};

0 comments on commit 9432a89

Please sign in to comment.