2017-01-31 21:56:09 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Copyright 2016 Realm Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-10-20 00:55:46 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-11-08 16:59:30 +00:00
|
|
|
const AuthError = require('./errors').AuthError;
|
2017-08-29 13:23:22 +00:00
|
|
|
const permissionApis = require('./permission-api');
|
2016-11-08 16:59:30 +00:00
|
|
|
|
2018-05-30 10:54:51 +00:00
|
|
|
const merge = require('deepmerge');
|
2017-09-26 04:20:28 +00:00
|
|
|
const require_method = require;
|
2018-05-30 10:54:51 +00:00
|
|
|
const URL = require('url-parse');
|
2017-09-26 04:20:28 +00:00
|
|
|
|
2018-10-15 12:20:19 +00:00
|
|
|
const refreshTimers = {};
|
|
|
|
const retryInterval = 5 * 1000;
|
|
|
|
const refreshBuffer = 20 * 1000;
|
|
|
|
|
2016-10-20 00:55:46 +00:00
|
|
|
function node_require(module) {
|
2017-09-26 04:20:28 +00:00
|
|
|
return require_method(module);
|
2016-10-20 00:55:46 +00:00
|
|
|
}
|
|
|
|
|
2017-03-20 11:52:41 +00:00
|
|
|
function checkTypes(args, types) {
|
|
|
|
args = Array.prototype.slice.call(args);
|
|
|
|
for (var i = 0; i < types.length; ++i) {
|
2017-09-12 13:17:59 +00:00
|
|
|
if (args.length > i && typeof args[i] !== types[i]) {
|
2017-03-20 11:52:41 +00:00
|
|
|
throw new TypeError('param ' + i + ' must be of type ' + types[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-28 13:01:32 +00:00
|
|
|
function checkObjectTypes(obj, types) {
|
|
|
|
for (const name of Object.getOwnPropertyNames(types)) {
|
|
|
|
const actualType = typeof obj[name];
|
|
|
|
let targetType = types[name];
|
|
|
|
const isOptional = targetType[targetType.length - 1] === '?';
|
|
|
|
if (isOptional) {
|
|
|
|
targetType = targetType.slice(0, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isOptional && actualType === 'undefined') {
|
|
|
|
throw new Error(`${name} is required, but a value was not provided.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (actualType !== targetType) {
|
|
|
|
throw new TypeError(`${name} must be of type '${targetType}' but was of type '${actualType}' instead.`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-15 23:08:48 +00:00
|
|
|
// Perform a HTTP request, enqueuing it if too many requests are already in
|
|
|
|
// progress to avoid hammering the server.
|
|
|
|
const performFetch = (function() {
|
|
|
|
const doFetch = typeof fetch === 'undefined' ? node_require('node-fetch') : fetch;
|
|
|
|
const queue = [];
|
|
|
|
let count = 0;
|
|
|
|
const maxCount = 5;
|
|
|
|
const next = () => {
|
|
|
|
if (count >= maxCount) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const req = queue.shift();
|
|
|
|
if (!req) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const [url, options, resolve, reject] = req;
|
|
|
|
++count;
|
|
|
|
// node doesn't support Promise.prototype.finally until 10
|
|
|
|
doFetch(url, options)
|
|
|
|
.then(response => {
|
|
|
|
--count;
|
|
|
|
next();
|
|
|
|
resolve(response);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
--count;
|
|
|
|
next();
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
return (url, options) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
queue.push([url, options, resolve, reject]);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
})();
|
2016-10-20 00:55:46 +00:00
|
|
|
|
2017-01-31 21:56:09 +00:00
|
|
|
const url_parse = require('url-parse');
|
2016-10-20 00:55:46 +00:00
|
|
|
|
2017-07-06 09:27:01 +00:00
|
|
|
const postHeaders = {
|
2016-10-20 00:55:46 +00:00
|
|
|
'content-type': 'application/json;charset=utf-8',
|
|
|
|
'accept': 'application/json'
|
|
|
|
};
|
|
|
|
|
2018-01-11 13:47:54 +00:00
|
|
|
function append_url(server, path) {
|
|
|
|
return server + (server.charAt(server.length - 1) != '/' ? '/' : '') + path;
|
2016-11-08 16:59:30 +00:00
|
|
|
}
|
|
|
|
|
2017-02-03 15:40:13 +00:00
|
|
|
function scheduleAccessTokenRefresh(user, localRealmPath, realmUrl, expirationDate) {
|
2018-10-15 12:20:19 +00:00
|
|
|
let userTimers = refreshTimers[user.identity];
|
|
|
|
if (!userTimers) {
|
|
|
|
refreshTimers[user.identity] = userTimers = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// We assume that access tokens have ~ the same expiration time, so if someone already
|
|
|
|
// scheduled a refresh, it's likely to complete before the one we would have scheduled
|
|
|
|
if (!userTimers[localRealmPath]) {
|
|
|
|
const timeout = expirationDate - Date.now() - refreshBuffer;
|
|
|
|
userTimers[localRealmPath] = setTimeout(() => {
|
|
|
|
delete userTimers[localRealmPath];
|
|
|
|
refreshAccessToken(user, localRealmPath, realmUrl);
|
|
|
|
}, timeout);
|
|
|
|
}
|
2017-02-03 15:40:13 +00:00
|
|
|
}
|
|
|
|
|
2017-06-27 18:32:34 +00:00
|
|
|
function print_error() {
|
|
|
|
(console.error || console.log).apply(console, arguments);
|
|
|
|
}
|
|
|
|
|
2018-01-11 13:47:54 +00:00
|
|
|
function validateRefresh(user, localRealmPath, response, json) {
|
|
|
|
let session = user._sessionForOnDiskPath(localRealmPath);
|
|
|
|
if (!session) {
|
2018-03-21 17:18:37 +00:00
|
|
|
print_error(`Unhandled session token refresh error: could not look up session for user ${user.identity} at path ${localRealmPath}`);
|
2018-01-11 13:47:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const errorHandler = session.config.error;
|
|
|
|
if (response.status != 200) {
|
|
|
|
let error = new AuthError(json);
|
|
|
|
if (errorHandler) {
|
|
|
|
errorHandler(session, error);
|
|
|
|
} else {
|
2018-03-21 17:18:37 +00:00
|
|
|
print_error(`Unhandled session token refresh error for user ${user.identity} at path ${localRealmPath}`, error);
|
2018-01-11 13:47:54 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (session.state === 'invalid') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return session;
|
|
|
|
}
|
|
|
|
|
|
|
|
function refreshAdminToken(user, localRealmPath, realmUrl) {
|
|
|
|
const token = user.token;
|
|
|
|
const server = user.server;
|
|
|
|
|
|
|
|
// We don't need to actually refresh the token, but we need to let ROS know
|
|
|
|
// we're accessing the file and get the sync label for multiplexing
|
|
|
|
let parsedRealmUrl = url_parse(realmUrl);
|
|
|
|
const url = append_url(user.server, 'realms/files/' + encodeURIComponent(parsedRealmUrl.pathname));
|
|
|
|
performFetch(url, {method: 'GET', timeout: 10000.0, headers: {Authorization: user.token}})
|
2018-01-13 09:02:08 +00:00
|
|
|
.then((response) => {
|
|
|
|
// There may not be a Realm Directory Service running on the server
|
|
|
|
// we're talking to. If we're talking directly to the sync service
|
|
|
|
// we'll get a 404, and if we're running inside ROS we'll get a 503 if
|
|
|
|
// the directory service hasn't started yet (perhaps because we got
|
|
|
|
// called due to the directory service itself opening some Realms).
|
|
|
|
//
|
|
|
|
// In both of these cases we can just pretend we got a valid response.
|
|
|
|
if (response.status === 404 || response.status === 503) {
|
|
|
|
return {response: {status: 200}, json: {path: parsedRealmUrl.pathname, syncLabel: '_direct'}};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return response.json().then((json) => { return { response, json }; });
|
|
|
|
}
|
|
|
|
})
|
2018-01-11 13:47:54 +00:00
|
|
|
.then((responseAndJson) => {
|
|
|
|
const response = responseAndJson.response;
|
|
|
|
const json = responseAndJson.json;
|
|
|
|
|
2018-10-08 13:01:12 +00:00
|
|
|
const credentials = credentialsMethods.adminToken(token)
|
|
|
|
const newUser = user.constructor.login(server, credentials);
|
2018-01-11 13:47:54 +00:00
|
|
|
const session = validateRefresh(newUser, localRealmPath, response, json);
|
|
|
|
if (session) {
|
|
|
|
parsedRealmUrl.set('pathname', json.path);
|
|
|
|
session._refreshAccessToken(user.token, parsedRealmUrl.href, json.syncLabel);
|
|
|
|
}
|
2018-01-18 12:26:14 +00:00
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
print_error(e);
|
2018-10-15 12:20:19 +00:00
|
|
|
setTimeout(() => refreshAccessToken(user, localRealmPath, realmUrl), retryInterval);
|
2018-01-11 13:47:54 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-02-03 15:40:13 +00:00
|
|
|
function refreshAccessToken(user, localRealmPath, realmUrl) {
|
2017-08-31 19:38:10 +00:00
|
|
|
if (!user.server) {
|
|
|
|
throw new Error("Server for user must be specified");
|
|
|
|
}
|
|
|
|
|
2018-10-23 22:33:34 +00:00
|
|
|
const parsedRealmUrl = url_parse(realmUrl);
|
|
|
|
const path = parsedRealmUrl.pathname;
|
|
|
|
if (!path) {
|
|
|
|
throw new Error(`Unexpected Realm path inferred from url '${realmUrl}'. The path section of the url should be a non-empty string.`);
|
|
|
|
}
|
|
|
|
|
2018-01-11 13:47:54 +00:00
|
|
|
if (user.isAdminToken) {
|
|
|
|
return refreshAdminToken(user, localRealmPath, realmUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
const url = append_url(user.server, 'auth');
|
2017-02-01 13:18:59 +00:00
|
|
|
const options = {
|
2017-01-31 13:07:29 +00:00
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify({
|
|
|
|
data: user.token,
|
2018-10-23 22:33:34 +00:00
|
|
|
path,
|
2017-01-31 13:07:29 +00:00
|
|
|
provider: 'realm',
|
|
|
|
app_id: ''
|
|
|
|
}),
|
2017-09-27 20:52:14 +00:00
|
|
|
headers: postHeaders,
|
|
|
|
// FIXME: This timeout appears to be necessary in order for some requests to be sent at all.
|
|
|
|
// See https://github.com/realm/realm-js-private/issues/338 for details.
|
2017-12-21 13:14:07 +00:00
|
|
|
timeout: 10000.0
|
2017-01-31 13:07:29 +00:00
|
|
|
};
|
2017-02-01 13:18:59 +00:00
|
|
|
performFetch(url, options)
|
2017-02-03 15:40:13 +00:00
|
|
|
.then((response) => response.json().then((json) => { return { response, json }; }))
|
|
|
|
.then((responseAndJson) => {
|
|
|
|
const response = responseAndJson.response;
|
|
|
|
const json = responseAndJson.json;
|
2017-02-07 10:01:26 +00:00
|
|
|
// Look up a fresh instance of the user.
|
|
|
|
// We do this because in React Native Remote Debugging
|
|
|
|
// `Realm.clearTestState()` will have invalidated the user object
|
2018-01-11 15:00:31 +00:00
|
|
|
let newUser = user.constructor._getExistingUser(user.server, user.identity);
|
2017-11-15 00:09:50 +00:00
|
|
|
if (!newUser) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-11 13:47:54 +00:00
|
|
|
const session = validateRefresh(newUser, localRealmPath, response, json);
|
|
|
|
if (!session) {
|
2017-11-15 00:09:50 +00:00
|
|
|
return;
|
2017-02-01 13:18:59 +00:00
|
|
|
}
|
2017-11-15 00:09:50 +00:00
|
|
|
|
|
|
|
const tokenData = json.access_token.token_data;
|
|
|
|
|
|
|
|
parsedRealmUrl.set('pathname', tokenData.path);
|
|
|
|
session._refreshAccessToken(json.access_token.token, parsedRealmUrl.href, tokenData.sync_label);
|
|
|
|
|
2018-01-11 13:47:54 +00:00
|
|
|
const errorHandler = session.config.error;
|
2017-11-15 00:09:50 +00:00
|
|
|
if (errorHandler && errorHandler._notifyOnAccessTokenRefreshed) {
|
|
|
|
errorHandler(session, errorHandler._notifyOnAccessTokenRefreshed)
|
|
|
|
}
|
|
|
|
|
|
|
|
const tokenExpirationDate = new Date(tokenData.expires * 1000);
|
|
|
|
scheduleAccessTokenRefresh(newUser, localRealmPath, realmUrl, tokenExpirationDate);
|
2017-08-31 17:39:48 +00:00
|
|
|
})
|
2017-08-31 17:50:23 +00:00
|
|
|
.catch((e) => {
|
|
|
|
print_error(e);
|
|
|
|
// in case something lower in the HTTP stack breaks, try again in 10 seconds
|
2018-10-15 12:20:19 +00:00
|
|
|
setTimeout(() => refreshAccessToken(user, localRealmPath, realmUrl), retryInterval);
|
2017-08-31 17:50:23 +00:00
|
|
|
})
|
2017-01-31 13:07:29 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 18:01:12 +00:00
|
|
|
/**
|
|
|
|
* The base authentication method. It fires a JSON POST to the server parameter plus the auth url
|
|
|
|
* For example, if the server parameter is `http://myapp.com`, this url will post to `http://myapp.com/auth`
|
2017-11-15 00:09:50 +00:00
|
|
|
* @param {object} userConstructor
|
|
|
|
* @param {string} server the http or https server url
|
2017-08-24 18:01:12 +00:00
|
|
|
* @param {object} json the json to post to the auth endpoint
|
2017-11-15 00:09:50 +00:00
|
|
|
* @param {Function} callback an optional callback with an error and user parameter
|
2017-08-24 18:01:12 +00:00
|
|
|
* @returns {Promise} only returns a promise if the callback parameter was omitted
|
|
|
|
*/
|
2018-09-13 06:45:06 +00:00
|
|
|
function _authenticate(userConstructor, server, json) {
|
2017-01-31 21:56:09 +00:00
|
|
|
json.app_id = '';
|
2018-01-11 13:47:54 +00:00
|
|
|
const url = append_url(server, 'auth');
|
2017-01-31 21:56:09 +00:00
|
|
|
const options = {
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify(json),
|
|
|
|
headers: postHeaders,
|
|
|
|
open_timeout: 5000
|
|
|
|
};
|
2017-09-12 20:04:20 +00:00
|
|
|
|
2018-09-13 06:45:06 +00:00
|
|
|
return performFetch(url, options)
|
2017-07-06 09:27:01 +00:00
|
|
|
.then((response) => {
|
2017-12-07 09:36:24 +00:00
|
|
|
const contentType = response.headers.get('Content-Type');
|
|
|
|
if (contentType.indexOf('application/json') === -1) {
|
|
|
|
return response.text().then((body) => {
|
|
|
|
throw new AuthError({
|
|
|
|
title: `Could not authenticate: Realm Object Server didn't respond with valid JSON`,
|
|
|
|
body,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else if (!response.ok) {
|
2017-09-12 20:04:20 +00:00
|
|
|
return response.json().then((body) => Promise.reject(new AuthError(body)));
|
2017-01-31 21:56:09 +00:00
|
|
|
} else {
|
|
|
|
return response.json().then(function (body) {
|
|
|
|
// TODO: validate JSON
|
|
|
|
const token = body.refresh_token.token;
|
|
|
|
const identity = body.refresh_token.token_data.identity;
|
2017-06-17 14:59:15 +00:00
|
|
|
const isAdmin = body.refresh_token.token_data.is_admin;
|
2017-09-12 20:04:20 +00:00
|
|
|
return userConstructor.createUser(server, identity, token, false, isAdmin);
|
|
|
|
});
|
2017-01-31 21:56:09 +00:00
|
|
|
}
|
2017-08-24 18:01:12 +00:00
|
|
|
});
|
2017-01-31 21:56:09 +00:00
|
|
|
}
|
2016-10-20 00:55:46 +00:00
|
|
|
|
2018-04-25 09:23:47 +00:00
|
|
|
function _updateAccount(userConstructor, server, json) {
|
|
|
|
const url = append_url(server, 'auth/password/updateAccount');
|
|
|
|
const options = {
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify(json),
|
|
|
|
headers: postHeaders,
|
|
|
|
open_timeout: 5000
|
|
|
|
};
|
|
|
|
|
|
|
|
return performFetch(url, options)
|
|
|
|
.then((response) => {
|
|
|
|
const contentType = response.headers.get('Content-Type');
|
|
|
|
if (contentType.indexOf('application/json') === -1) {
|
|
|
|
return response.text().then((body) => {
|
|
|
|
throw new AuthError({
|
|
|
|
title: `Could not update user account: Realm Object Server didn't respond with valid JSON`,
|
|
|
|
body,
|
|
|
|
});
|
|
|
|
});
|
2018-08-28 13:01:32 +00:00
|
|
|
}
|
2018-04-25 09:23:47 +00:00
|
|
|
if (!response.ok) {
|
|
|
|
return response.json().then((body) => Promise.reject(new AuthError(body)));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-13 06:45:06 +00:00
|
|
|
const credentialsMethods = {
|
|
|
|
usernamePassword(username, password, createUser) {
|
|
|
|
checkTypes(arguments, ['string', 'string', 'boolean']);
|
|
|
|
return new Credentials('password', username, { register: createUser, password });
|
|
|
|
},
|
|
|
|
|
|
|
|
facebook(token) {
|
|
|
|
checkTypes(arguments, ['string']);
|
|
|
|
return new Credentials('facebook', token);
|
|
|
|
},
|
|
|
|
|
|
|
|
google(token) {
|
|
|
|
checkTypes(arguments, ['string']);
|
|
|
|
return new Credentials('google', token);
|
|
|
|
},
|
|
|
|
|
|
|
|
anonymous() {
|
|
|
|
return new Credentials('anonymous');
|
|
|
|
},
|
|
|
|
|
|
|
|
nickname(value, isAdmin) {
|
|
|
|
checkTypes(arguments, ['string', 'boolean']);
|
|
|
|
return new Credentials('nickname', value, { is_admin: isAdmin || false });
|
|
|
|
},
|
|
|
|
|
|
|
|
azureAD(token) {
|
|
|
|
checkTypes(arguments, ['string']);
|
|
|
|
return new Credentials('azuread', token)
|
|
|
|
},
|
|
|
|
|
|
|
|
jwt(token, providerName) {
|
|
|
|
checkTypes(arguments, ['string', 'string']);
|
|
|
|
return new Credentials(providerName || 'jwt', token);
|
|
|
|
},
|
|
|
|
|
|
|
|
adminToken(token) {
|
|
|
|
checkTypes(arguments, ['string']);
|
|
|
|
return new Credentials('adminToken', token);
|
|
|
|
},
|
|
|
|
|
|
|
|
custom(providerName, token, userInfo) {
|
2018-10-08 13:01:12 +00:00
|
|
|
if (userInfo) {
|
|
|
|
checkTypes(arguments, ['string', 'string', 'object']);
|
|
|
|
} else {
|
|
|
|
checkTypes(arguments, ['string', 'string']);
|
|
|
|
}
|
|
|
|
|
2018-09-13 06:45:06 +00:00
|
|
|
return new Credentials(providerName, token, userInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-29 13:23:22 +00:00
|
|
|
const staticMethods = {
|
2018-09-13 06:45:06 +00:00
|
|
|
get current() {
|
|
|
|
const allUsers = this.all;
|
|
|
|
const keys = Object.keys(allUsers);
|
|
|
|
if (keys.length === 0) {
|
|
|
|
return undefined;
|
|
|
|
} else if (keys.length > 1) {
|
|
|
|
throw new Error("Multiple users are logged in");
|
|
|
|
}
|
2017-01-31 13:07:29 +00:00
|
|
|
|
2018-09-13 06:45:06 +00:00
|
|
|
return allUsers[keys[0]];
|
|
|
|
},
|
2017-11-15 00:09:50 +00:00
|
|
|
|
2018-09-13 06:45:06 +00:00
|
|
|
login(server, credentials) {
|
|
|
|
if (arguments.length === 3) {
|
|
|
|
// Deprecated legacy signature.
|
|
|
|
checkTypes(arguments, ['string', 'string', 'string']);
|
|
|
|
console.warn("User.login is deprecated. Please use User.login(server, Credentials.usernamePassword(...)) instead.");
|
|
|
|
const newCredentials = credentialsMethods.usernamePassword(arguments[1], arguments[2], /* createUser */ false);
|
|
|
|
return this.login(server, newCredentials);
|
|
|
|
}
|
2017-01-31 21:56:09 +00:00
|
|
|
|
2018-09-13 06:45:06 +00:00
|
|
|
checkTypes(arguments, ['string', 'object']);
|
|
|
|
if (credentials.identityProvider === 'adminToken') {
|
2018-09-21 12:24:37 +00:00
|
|
|
let u = this._adminUser(server, credentials.token);
|
2018-09-24 10:43:44 +00:00
|
|
|
return u;
|
2018-09-13 06:45:06 +00:00
|
|
|
}
|
2017-11-15 00:09:50 +00:00
|
|
|
|
2018-09-13 06:45:06 +00:00
|
|
|
return _authenticate(this, server, credentials);
|
|
|
|
},
|
2017-09-12 20:04:20 +00:00
|
|
|
|
2018-09-13 06:45:06 +00:00
|
|
|
deserialize(serialized) {
|
|
|
|
checkObjectTypes(serialized, {
|
|
|
|
server: 'string',
|
|
|
|
identity: 'string',
|
|
|
|
refreshToken: 'string',
|
|
|
|
isAdmin: 'boolean',
|
|
|
|
});
|
2017-01-31 21:56:09 +00:00
|
|
|
|
2018-09-13 06:45:06 +00:00
|
|
|
return this.createUser(serialized.server, serialized.identity, serialized.refreshToken, false, serialized.isAdmin || false);
|
|
|
|
},
|
2017-03-17 13:13:03 +00:00
|
|
|
|
2018-09-13 06:45:06 +00:00
|
|
|
requestPasswordReset(server, email) {
|
|
|
|
checkTypes(arguments, ['string', 'string']);
|
|
|
|
const json = {
|
|
|
|
provider_id: email,
|
|
|
|
data: { action: 'reset_password' }
|
|
|
|
};
|
2017-03-17 13:13:03 +00:00
|
|
|
|
2018-09-13 06:45:06 +00:00
|
|
|
return _updateAccount(this, server, json);
|
|
|
|
},
|
2017-03-17 13:13:03 +00:00
|
|
|
|
2018-09-13 06:45:06 +00:00
|
|
|
completePasswordReset(server, resetToken, newPassword) {
|
|
|
|
checkTypes(arguments, ['string', 'string']);
|
|
|
|
const json = {
|
|
|
|
data: {
|
|
|
|
action: 'complete_reset',
|
|
|
|
token: resetToken,
|
|
|
|
new_password: newPassword
|
2017-03-17 13:13:03 +00:00
|
|
|
}
|
2018-09-13 06:45:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return _updateAccount(this, server, json);
|
|
|
|
},
|
|
|
|
|
|
|
|
requestEmailConfirmation(server, email) {
|
|
|
|
checkTypes(arguments, ['string', 'string']);
|
|
|
|
const json = {
|
|
|
|
provider_id: email,
|
|
|
|
data: { action: 'request_email_confirmation' }
|
|
|
|
};
|
|
|
|
|
|
|
|
return _updateAccount(this, server, json);
|
|
|
|
},
|
2017-03-17 13:13:03 +00:00
|
|
|
|
2018-09-13 06:45:06 +00:00
|
|
|
confirmEmail(server, confirmationToken) {
|
|
|
|
checkTypes(arguments, ['string', 'string']);
|
|
|
|
const json = {
|
|
|
|
data: {
|
|
|
|
action: 'confirm_email',
|
|
|
|
token: confirmationToken
|
2017-09-12 17:38:43 +00:00
|
|
|
}
|
2018-09-13 06:45:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return _updateAccount(this, server, json);
|
|
|
|
},
|
|
|
|
|
|
|
|
_refreshAccessToken: refreshAccessToken,
|
2017-11-15 00:09:50 +00:00
|
|
|
|
2018-09-13 06:45:06 +00:00
|
|
|
// Deprecated...
|
|
|
|
adminUser(token, server) {
|
|
|
|
checkTypes(arguments, ['string', 'string']);
|
|
|
|
console.warn("User.adminUser is deprecated. Please use User.login(server, Credentials.adminToken(token)) instead.");
|
|
|
|
const credentials = credentialsMethods.adminToken(token);
|
|
|
|
return this.login(server, credentials);
|
|
|
|
},
|
2017-01-31 21:56:09 +00:00
|
|
|
|
2018-09-13 06:45:06 +00:00
|
|
|
register(server, username, password) {
|
|
|
|
checkTypes(arguments, ['string', 'string', 'string']);
|
|
|
|
console.warn("User.register is deprecated. Please use User.login(server, Credentials.usernamePassword(...)) instead.");
|
|
|
|
const credentials = credentialsMethods.usernamePassword(username, password, /* createUser */ true);
|
|
|
|
return this.login(server, credentials);
|
|
|
|
},
|
|
|
|
|
|
|
|
registerWithProvider(server, options) {
|
|
|
|
checkTypes(arguments, ['string', 'object']);
|
|
|
|
console.warn("User.registerWithProvider is deprecated. Please use User.login(server, Credentials.SOME-PROVIDER(...)) instead.");
|
|
|
|
const credentials = credentialsMethods.custom(options.provider, options.providerToken, options.userInfo);
|
|
|
|
return this.login(server, credentials);
|
|
|
|
},
|
2018-01-05 08:38:53 +00:00
|
|
|
|
2018-09-13 06:45:06 +00:00
|
|
|
authenticate(server, provider, options) {
|
|
|
|
checkTypes(arguments, ['string', 'string', 'object'])
|
|
|
|
console.warn("User.authenticate is deprecated. Please use User.login(server, Credentials.SOME-PROVIDER(...)) instead.");
|
|
|
|
|
|
|
|
let credentials;
|
|
|
|
switch (provider.toLowerCase()) {
|
2018-01-05 08:38:53 +00:00
|
|
|
case 'jwt':
|
2018-09-13 06:45:06 +00:00
|
|
|
credentials = credentialsMethods.jwt(options.token, 'jwt');
|
2018-01-05 08:38:53 +00:00
|
|
|
break
|
|
|
|
case 'password':
|
2018-09-13 06:45:06 +00:00
|
|
|
credentials = credentialsMethods.usernamePassword(options.username, options.password);
|
2018-01-05 08:38:53 +00:00
|
|
|
break
|
|
|
|
default:
|
2018-09-13 06:45:06 +00:00
|
|
|
credentials = credentialsMethods.custom(provider, options.data, options.user_info || options.userInfo);
|
|
|
|
break;
|
|
|
|
}
|
2018-04-25 09:23:47 +00:00
|
|
|
|
2018-09-13 06:45:06 +00:00
|
|
|
return this.login(server, credentials);
|
|
|
|
},
|
2018-01-05 08:38:53 +00:00
|
|
|
};
|
|
|
|
|
2017-08-29 13:23:22 +00:00
|
|
|
const instanceMethods = {
|
2018-03-09 14:51:45 +00:00
|
|
|
logout() {
|
|
|
|
this._logout();
|
2018-10-15 12:20:19 +00:00
|
|
|
const userTimers = refreshTimers[this.identity];
|
|
|
|
if (userTimers) {
|
|
|
|
Object.keys(userTimers).forEach((key) => {
|
|
|
|
clearTimeout(userTimers[key]);
|
|
|
|
});
|
|
|
|
|
|
|
|
delete refreshTimers[this.identity];
|
|
|
|
}
|
|
|
|
|
2018-03-09 14:51:45 +00:00
|
|
|
const url = url_parse(this.server);
|
|
|
|
url.set('pathname', '/auth/revoke');
|
|
|
|
const headers = {
|
|
|
|
Authorization: this.token
|
|
|
|
};
|
2018-04-17 13:01:02 +00:00
|
|
|
const body = JSON.stringify({
|
2018-03-09 14:51:45 +00:00
|
|
|
token: this.token
|
2018-04-17 13:01:02 +00:00
|
|
|
});
|
2018-03-09 14:51:45 +00:00
|
|
|
const options = {
|
|
|
|
method: 'POST',
|
|
|
|
headers,
|
|
|
|
body: body,
|
|
|
|
open_timeout: 5000
|
|
|
|
};
|
|
|
|
|
2018-10-15 12:20:19 +00:00
|
|
|
return performFetch(url.href, options)
|
|
|
|
.catch((e) => print_error('An error occurred while logging out a user', e));
|
2018-03-09 14:51:45 +00:00
|
|
|
},
|
2018-08-28 13:01:32 +00:00
|
|
|
serialize() {
|
|
|
|
return {
|
|
|
|
server: this.server,
|
|
|
|
refreshToken: this.token,
|
|
|
|
identity: this.identity,
|
|
|
|
isAdmin: this.isAdmin,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
openManagementRealm() {
|
|
|
|
let url = url_parse(this.server);
|
|
|
|
if (url.protocol === 'http:') {
|
|
|
|
url.set('protocol', 'realm:');
|
|
|
|
} else if (url.protocol === 'https:') {
|
|
|
|
url.set('protocol', 'realms:');
|
|
|
|
} else {
|
|
|
|
throw new Error(`Unexpected user auth url: ${this.server}`);
|
|
|
|
}
|
2017-01-31 13:07:29 +00:00
|
|
|
|
2018-08-28 13:01:32 +00:00
|
|
|
url.set('pathname', '/~/__management');
|
2017-01-31 21:56:09 +00:00
|
|
|
|
2018-08-28 13:01:32 +00:00
|
|
|
return new this.constructor._realmConstructor({
|
|
|
|
schema: require('./management-schema'),
|
|
|
|
sync: {
|
|
|
|
user: this,
|
|
|
|
url: url.href
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
retrieveAccount(provider, provider_id) {
|
|
|
|
checkTypes(arguments, ['string', 'string']);
|
|
|
|
const url = url_parse(this.server);
|
|
|
|
url.set('pathname', `/auth/users/${provider}/${provider_id}`);
|
|
|
|
const headers = {
|
|
|
|
Authorization: this.token
|
|
|
|
};
|
|
|
|
const options = {
|
|
|
|
method: 'GET',
|
|
|
|
headers,
|
|
|
|
open_timeout: 5000
|
|
|
|
};
|
|
|
|
return performFetch(url.href, options)
|
|
|
|
.then((response) => {
|
|
|
|
if (response.status !== 200) {
|
|
|
|
return response.json()
|
|
|
|
.then(body => {
|
|
|
|
throw new AuthError(body);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return response.json();
|
2017-01-31 21:56:09 +00:00
|
|
|
}
|
|
|
|
});
|
2018-08-28 13:01:32 +00:00
|
|
|
},
|
|
|
|
createConfiguration(config) {
|
2018-05-30 10:54:51 +00:00
|
|
|
|
2018-08-28 13:01:32 +00:00
|
|
|
if (config && config.sync) {
|
|
|
|
if (config.sync.user && console.warn !== undefined) {
|
|
|
|
console.warn(`'user' property will be overridden by ${this.identity}`);
|
|
|
|
}
|
|
|
|
if (config.sync.partial !== undefined && config.sync.fullSynchronization !== undefined) {
|
|
|
|
throw new Error("'partial' and 'fullSynchronization' were both set. 'partial' has been deprecated, use only 'fullSynchronization'");
|
2018-05-30 10:54:51 +00:00
|
|
|
}
|
2018-08-28 13:01:32 +00:00
|
|
|
}
|
2018-05-30 10:54:51 +00:00
|
|
|
|
2018-08-28 13:01:32 +00:00
|
|
|
// Create default config
|
|
|
|
let url = new URL(this.server);
|
|
|
|
let secure = (url.protocol === 'https:')?'s':'';
|
|
|
|
let port = (url.port === undefined)?'9080':url.port;
|
|
|
|
let realmUrl = `realm${secure}://${url.hostname}:${port}/default`;
|
|
|
|
|
|
|
|
let defaultConfig = {
|
|
|
|
sync: {
|
|
|
|
user: this,
|
|
|
|
url: realmUrl,
|
|
|
|
},
|
|
|
|
};
|
2018-05-30 10:54:51 +00:00
|
|
|
|
2018-08-28 13:01:32 +00:00
|
|
|
// Set query-based as the default setting if the user doesn't specified any other behaviour.
|
|
|
|
if (!(config && config.sync && config.sync.partial)) {
|
|
|
|
defaultConfig.sync.fullSynchronization = false;
|
|
|
|
}
|
2018-08-17 08:10:20 +00:00
|
|
|
|
2018-08-28 13:01:32 +00:00
|
|
|
// Merge default configuration with user provided config. User defined properties should aways win.
|
|
|
|
// Doing the naive merge in JS break objects that are backed by native objects, so these needs to
|
|
|
|
// be merged manually. This is currently only `sync.user`.
|
|
|
|
let mergedConfig = (config === undefined) ? defaultConfig : merge(defaultConfig, config);
|
|
|
|
mergedConfig.sync.user = this;
|
|
|
|
return mergedConfig;
|
|
|
|
},
|
|
|
|
};
|
2017-08-29 13:23:22 +00:00
|
|
|
|
2018-09-13 06:45:06 +00:00
|
|
|
class Credentials {
|
|
|
|
constructor(identityProvider, token, userInfo) {
|
|
|
|
this.identityProvider = identityProvider;
|
|
|
|
this.token = token;
|
2018-10-08 13:01:12 +00:00
|
|
|
this.userInfo = userInfo || {};
|
2018-09-13 06:45:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
toJSON() {
|
|
|
|
return {
|
|
|
|
data: this.token,
|
|
|
|
provider: this.identityProvider,
|
2018-10-08 13:01:12 +00:00
|
|
|
user_info: this.userInfo,
|
2018-09-13 06:45:06 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-29 13:23:22 +00:00
|
|
|
// Append the permission apis
|
|
|
|
Object.assign(instanceMethods, permissionApis);
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
static: staticMethods,
|
2018-09-13 06:45:06 +00:00
|
|
|
instance: instanceMethods,
|
|
|
|
credentials: credentialsMethods,
|
2017-08-29 13:23:22 +00:00
|
|
|
};
|