realm-js/lib/user-methods.js

143 lines
4.3 KiB
JavaScript
Raw Normal View History

2016-10-20 00:55:46 +00:00
'use strict';
2016-11-08 16:59:30 +00:00
const AuthError = require('./errors').AuthError;
2016-10-20 00:55:46 +00:00
function node_require(module) {
return require(module);
}
var post;
if (typeof fetch != 'undefined') {
post = function(options, callback) {
options.method = 'POST';
fetch(options.url, options)
.then((response) => {
if (response.status != 200) {
callback(undefined, {statusCode: response.status});
}
else {
return response.text();
}
})
.then((body) => {
2016-11-08 17:21:27 +00:00
callback(undefined, {statusCode: 200}, body)
2016-10-20 00:55:46 +00:00
})
.catch((error) => {
callback(error);
});
}
}
else {
2016-10-27 20:49:41 +00:00
post = function(options, callback) {
node_require('needle').post(options.url, options.body, options, callback);
}
2016-10-20 00:55:46 +00:00
}
2016-11-08 16:59:30 +00:00
const url_parse = require("url-parse");
2016-10-20 00:55:46 +00:00
const postHeaders = {
'content-type': 'application/json;charset=utf-8',
'accept': 'application/json'
};
2016-11-08 16:59:30 +00:00
function auth_url(server) {
if (server.charAt(server.length-1) != '/') {
return server + '/auth';
}
return server + 'auth';
}
2016-10-20 00:55:46 +00:00
module.exports = function(realmConstructor) {
function _authenticate(server, json, callback) {
json.app_id = '';
var options = {
2016-11-08 16:59:30 +00:00
url: auth_url(server),
2016-10-20 00:55:46 +00:00
body: JSON.stringify(json),
2016-11-08 22:07:04 +00:00
headers: postHeaders,
open_timeout: 5000
2016-10-20 00:55:46 +00:00
};
post(options, function(error, response, body) {
if (error) {
callback(error);
}
else if (response.statusCode != 200) {
2016-11-08 17:21:27 +00:00
callback(new AuthError(JSON.parse(body)));
2016-10-20 00:55:46 +00:00
}
else {
// TODO: validate JSON
2016-11-08 16:59:30 +00:00
const token = body.refresh_token.token;
const identity = body.refresh_token.token_data.identity;
2016-10-20 00:55:46 +00:00
callback(undefined, realmConstructor.Sync.User.createUser(server, identity, token, false));
}
});
}
var methods = {};
methods['adminUser'] = function(token) {
2016-10-20 00:55:46 +00:00
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);
return v.toString(16);
});
var user = realmConstructor.Sync.User.createUser('', uuid, token, true);
2016-10-20 00:55:46 +00:00
return user;
}
2016-10-27 20:49:41 +00:00
methods['register'] = function(server, username, password, callback) {
2016-10-20 00:55:46 +00:00
_authenticate(server, {
provider: 'password',
2016-10-27 20:49:41 +00:00
user_info: { password: password, register: true },
2016-10-20 00:55:46 +00:00
data: username
}, callback);
}
2016-11-08 16:59:30 +00:00
methods['login'] = function(server, username, password, callback) {
2016-10-20 00:55:46 +00:00
_authenticate(server, {
2016-10-27 20:49:41 +00:00
provider: 'password',
user_info: { password: password },
data: username
2016-10-20 00:55:46 +00:00
}, callback);
}
2016-10-27 20:49:41 +00:00
methods['registerWithProvider'] = function(server, provider, providerToken, callback) {
2016-10-20 00:55:46 +00:00
_authenticate(server, {
2016-10-27 20:49:41 +00:00
provider: provider,
data: providerToken
2016-10-20 00:55:46 +00:00
}, callback);
}
methods['_authenticateRealm'] = function(fileUrl, realmUrl, callback) {
var options = {
2016-11-08 16:59:30 +00:00
url: auth_url(this.server),
2016-10-20 00:55:46 +00:00
body: JSON.stringify({
data: this.token,
2016-11-08 16:59:30 +00:00
path: url_parse(realmUrl).pathname,
2016-10-20 00:55:46 +00:00
provider: 'realm',
app_id: ''
}),
headers: postHeaders
};
post(options, function(error, response, body) {
if (error) {
callback(error);
}
else if (response.statusCode != 200) {
2016-11-08 16:59:30 +00:00
callback(new AuthError('Bad response: ' + response.statusCode));
2016-10-20 00:55:46 +00:00
}
else {
// TODO: validate JSON
callback(undefined, {
2016-11-08 16:59:30 +00:00
token: body.access_token.token,
file_url: url_parse(fileUrl).pathname,
resolved_realm_url: 'realm://' + url_parse(realmUrl).host + body.access_token.token_data.path
2016-10-20 00:55:46 +00:00
});
}
});
}
for (var name in methods) {
methods[name] = {value: methods[name], configurable: true, writable: true}
}
return methods;
}