Validate user-methods parameters (#924)
* Validate user-methods parameters * Switch homemade typeof operator for real one. * Fix array prototype access
This commit is contained in:
parent
78caae7b17
commit
5bece3d5c4
|
@ -24,6 +24,15 @@ function node_require(module) {
|
||||||
return require(module);
|
return require(module);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function checkTypes(args, types) {
|
||||||
|
args = Array.prototype.slice.call(args);
|
||||||
|
for (var i = 0; i < types.length; ++i) {
|
||||||
|
if (typeof args[i] !== types[i]) {
|
||||||
|
throw new TypeError('param ' + i + ' must be of type ' + types[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const performFetch = typeof fetch === 'undefined' ? node_require('node-fetch') : fetch;
|
const performFetch = typeof fetch === 'undefined' ? node_require('node-fetch') : fetch;
|
||||||
|
|
||||||
const url_parse = require('url-parse');
|
const url_parse = require('url-parse');
|
||||||
|
@ -133,6 +142,7 @@ module.exports = {
|
||||||
},
|
},
|
||||||
|
|
||||||
adminUser(token) {
|
adminUser(token) {
|
||||||
|
checkTypes(arguments, ['string']);
|
||||||
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
||||||
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
|
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
|
||||||
return v.toString(16);
|
return v.toString(16);
|
||||||
|
@ -142,6 +152,7 @@ module.exports = {
|
||||||
},
|
},
|
||||||
|
|
||||||
register(server, username, password, callback) {
|
register(server, username, password, callback) {
|
||||||
|
checkTypes(arguments, ['string', 'string', 'string', 'function']);
|
||||||
_authenticate(this, server, {
|
_authenticate(this, server, {
|
||||||
provider: 'password',
|
provider: 'password',
|
||||||
user_info: { password: password, register: true },
|
user_info: { password: password, register: true },
|
||||||
|
@ -150,6 +161,7 @@ module.exports = {
|
||||||
},
|
},
|
||||||
|
|
||||||
login(server, username, password, callback) {
|
login(server, username, password, callback) {
|
||||||
|
checkTypes(arguments, ['string', 'string', 'string', 'function']);
|
||||||
_authenticate(this, server, {
|
_authenticate(this, server, {
|
||||||
provider: 'password',
|
provider: 'password',
|
||||||
user_info: { password: password },
|
user_info: { password: password },
|
||||||
|
@ -162,11 +174,14 @@ module.exports = {
|
||||||
// Compatibility with previous signature:
|
// Compatibility with previous signature:
|
||||||
// registerWithProvider(server, provider, providerToken, callback)
|
// registerWithProvider(server, provider, providerToken, callback)
|
||||||
if (arguments.length === 4) {
|
if (arguments.length === 4) {
|
||||||
|
checkTypes(arguments, ['string', 'string', 'string', 'function']);
|
||||||
options = {
|
options = {
|
||||||
provider: arguments[1],
|
provider: arguments[1],
|
||||||
providerToken: arguments[2]
|
providerToken: arguments[2]
|
||||||
};
|
};
|
||||||
callback = arguments[3];
|
callback = arguments[3];
|
||||||
|
} else {
|
||||||
|
checkTypes(arguments, ['string', 'object', 'function']);
|
||||||
}
|
}
|
||||||
|
|
||||||
let reqOptions = {
|
let reqOptions = {
|
||||||
|
|
|
@ -134,17 +134,21 @@ module.exports = {
|
||||||
},
|
},
|
||||||
|
|
||||||
testRegisterMissingUsername() {
|
testRegisterMissingUsername() {
|
||||||
return callbackTest((callback) => Realm.Sync.User.register('http://localhost:9080', undefined, 'password', callback), (error, user) => {
|
return new Promise((resolve, _reject) => {
|
||||||
assertIsAuthError(error, 602, 'https://realm.io/docs/object-server/problems/missing-parameters');
|
TestCase.assertThrows(() => {
|
||||||
TestCase.assertUndefined(user);
|
Realm.Sync.User.register('http://localhost:9080', undefined, 'password', () => {});
|
||||||
|
});
|
||||||
|
resolve();
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
testRegisterMissingPassword() {
|
testRegisterMissingPassword() {
|
||||||
var username = uuid();
|
var username = uuid();
|
||||||
return callbackTest((callback) => Realm.Sync.User.register('http://localhost:9080', username, undefined, callback), (error, user) => {
|
return new Promise((resolve, _reject) => {
|
||||||
assertIsAuthError(error, 602, 'https://realm.io/docs/object-server/problems/missing-parameters');
|
TestCase.assertThrows(() => {
|
||||||
TestCase.assertUndefined(user);
|
Realm.Sync.User.register('http://localhost:9080', username, undefined, () => {});
|
||||||
|
});
|
||||||
|
resolve();
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -180,17 +184,21 @@ module.exports = {
|
||||||
},
|
},
|
||||||
|
|
||||||
testLoginMissingUsername() {
|
testLoginMissingUsername() {
|
||||||
return callbackTest((callback) => Realm.Sync.User.login('http://localhost:9080', undefined, 'password', callback), (error, user) => {
|
return new Promise((resolve, _reject) => {
|
||||||
assertIsAuthError(error, 602, 'https://realm.io/docs/object-server/problems/missing-parameters');
|
TestCase.assertThrows(() => {
|
||||||
TestCase.assertUndefined(user);
|
Realm.Sync.User.login('http://localhost:9080', undefined, 'password', () => {});
|
||||||
|
});
|
||||||
|
resolve();
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
testLoginMissingPassword() {
|
testLoginMissingPassword() {
|
||||||
var username = uuid();
|
var username = uuid();
|
||||||
return callbackTest((callback) => Realm.Sync.User.login('http://localhost:9080', username, undefined, callback), (error, user) => {
|
return new Promise((resolve, _reject) => {
|
||||||
assertIsAuthError(error, 602, 'https://realm.io/docs/object-server/problems/missing-parameters');
|
TestCase.assertThrows(() => {
|
||||||
TestCase.assertUndefined(user);
|
Realm.Sync.User.login('http://localhost:9080', username, undefined, () => {});
|
||||||
|
});
|
||||||
|
resolve();
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue