Skip to content

Commit 9432a89

Browse files
author
Aurélien GASTON
committed
Fix authentication for some auth paths
fixes ovh#20
1 parent 3037170 commit 9432a89

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed

lib/includes.polyfill.js

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// https://tc39.github.io/ecma262/#sec-array.prototype.includes
2+
if (!Array.prototype.includes) {
3+
Object.defineProperty(Array.prototype, 'includes', {
4+
value: function(searchElement, fromIndex) {
5+
6+
if (this == null) {
7+
throw new TypeError('"this" is null or not defined');
8+
}
9+
10+
// 1. Let O be ? ToObject(this value).
11+
var o = Object(this);
12+
13+
// 2. Let len be ? ToLength(? Get(O, "length")).
14+
var len = o.length >>> 0;
15+
16+
// 3. If len is 0, return false.
17+
if (len === 0) {
18+
return false;
19+
}
20+
21+
// 4. Let n be ? ToInteger(fromIndex).
22+
// (If fromIndex is undefined, this step produces the value 0.)
23+
var n = fromIndex | 0;
24+
25+
// 5. If n ≥ 0, then
26+
// a. Let k be n.
27+
// 6. Else n < 0,
28+
// a. Let k be len + n.
29+
// b. If k < 0, let k be 0.
30+
var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
31+
32+
function sameValueZero(x, y) {
33+
return x === y || (typeof x === 'number' && typeof y === 'number' && isNaN(x) && isNaN(y));
34+
}
35+
36+
// 7. Repeat, while k < len
37+
while (k < len) {
38+
// a. Let elementK be the result of ? Get(O, ! ToString(k)).
39+
// b. If SameValueZero(searchElement, elementK) is true, return true.
40+
if (sameValueZero(o[k], searchElement)) {
41+
return true;
42+
}
43+
// c. Increase k by 1.
44+
k++;
45+
}
46+
47+
// 8. Return false
48+
return false;
49+
}
50+
});
51+
}

lib/ovh.es5.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
*/
2727
'use strict';
2828

29+
// String.includes polyfill
30+
require('./includes.polyfill');
31+
2932
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; };
3033

3134
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; }; }();
@@ -51,6 +54,7 @@ var Ovh = function () {
5154
this.timeout = params.timeout;
5255
this.apiTimeDiff = params.apiTimeDiff || null;
5356
this.endpoint = params.endpoint || null;
57+
this.noAuthPaths = params.noAuthPaths || ['/auth/credential', '/auth/time'];
5458

5559
// Preconfigured API endpoints
5660
if (this.endpoint) {
@@ -363,7 +367,7 @@ var Ovh = function () {
363367
}
364368
}
365369

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

369373
// Sign request

lib/ovh.es6.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
*/
2727
'use strict';
2828

29+
// String.includes polyfill
30+
require('./includes.polyfill');
31+
2932
let https = require('https'),
3033
Bluebird = require('bluebird'),
3134
querystring = require('querystring'),
@@ -43,6 +46,7 @@ class Ovh {
4346
this.timeout = params.timeout;
4447
this.apiTimeDiff = params.apiTimeDiff || null;
4548
this.endpoint = params.endpoint || null;
49+
this.noAuthPaths = params.noAuthPaths || ['/auth/credential', '/auth/time'];
4650

4751
// Preconfigured API endpoints
4852
if (this.endpoint) {
@@ -356,7 +360,7 @@ class Ovh {
356360
}
357361
}
358362

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

362366
// Sign request
@@ -487,4 +491,4 @@ class Ovh {
487491

488492
module.exports = function (params) {
489493
return new Ovh(params || {});
490-
};
494+
};

0 commit comments

Comments
 (0)