Fix creating admin token users

This commit is contained in:
Thomas Goyne 2017-08-31 12:38:10 -07:00
parent 26b4cdf33f
commit 736277084e
5 changed files with 29 additions and 16 deletions

View File

@ -168,7 +168,7 @@ class User {
/** /**
* Create an admin user for the given authentication server with an existing token * Create an admin user for the given authentication server with an existing token
* @param {string} adminToken - existing admin token * @param {string} adminToken - existing admin token
* @param {string} [server] - authentication server * @param {string} server - authentication server
* @return {User} - admin user populated with the given token and server * @return {User} - admin user populated with the given token and server
*/ */
static adminUser(adminToken, server) {} static adminUser(adminToken, server) {}

View File

@ -61,6 +61,10 @@ function print_error() {
} }
function refreshAccessToken(user, localRealmPath, realmUrl) { function refreshAccessToken(user, localRealmPath, realmUrl) {
if (!user.server) {
throw new Error("Server for user must be specified");
}
let parsedRealmUrl = url_parse(realmUrl); let parsedRealmUrl = url_parse(realmUrl);
const url = auth_url(user.server); const url = auth_url(user.server);
const options = { const options = {
@ -157,12 +161,8 @@ module.exports = {
}, },
adminUser(token, server) { adminUser(token, server) {
checkTypes(arguments, ['string']); checkTypes(arguments, ['string', 'string']);
const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { return this._adminUser(server, token);
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
return this.createUser(server || '', uuid, token, true);
}, },
register(server, username, password, callback) { register(server, username, password, callback) {

View File

@ -71,9 +71,11 @@ public:
}; };
static void create_user(ContextType, FunctionType, ObjectType, size_t, const ValueType[], ReturnValue &); static void create_user(ContextType, FunctionType, ObjectType, size_t, const ValueType[], ReturnValue &);
static void admin_user(ContextType, FunctionType, ObjectType, size_t, const ValueType[], ReturnValue &);
MethodMap<T> const static_methods = { MethodMap<T> const static_methods = {
{"createUser", wrap<create_user>} {"createUser", wrap<create_user>},
{"_adminUser", wrap<admin_user>}
}; };
/*static void current_user(ContextType ctx, ObjectType object, ReturnValue &return_value);*/ /*static void current_user(ContextType ctx, ObjectType object, ReturnValue &return_value);*/
@ -134,6 +136,16 @@ void UserClass<T>::create_user(ContextType ctx, FunctionType, ObjectType this_ob
return_value.set(create_object<T, UserClass<T>>(ctx, user)); return_value.set(create_object<T, UserClass<T>>(ctx, user));
} }
template<typename T>
void UserClass<T>::admin_user(ContextType ctx, FunctionType, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
validate_argument_count(argc, 2, 2);
SharedUser *user = new SharedUser(SyncManager::shared().get_admin_token_user(
Value::validated_to_string(ctx, arguments[0], "authServerUrl"),
Value::validated_to_string(ctx, arguments[1], "refreshToken")
));
return_value.set(create_object<T, UserClass<T>>(ctx, user));
}
template<typename T> template<typename T>
void UserClass<T>::all_users(ContextType ctx, ObjectType object, ReturnValue &return_value) { void UserClass<T>::all_users(ContextType ctx, ObjectType object, ReturnValue &return_value) {
auto users = Object::create_empty(ctx); auto users = Object::create_empty(ctx);

View File

@ -23,25 +23,26 @@ function random(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min; return Math.floor(Math.random() * (max - min + 1)) + min;
} }
let newAdminName = 'admin' + random(1, 100000); const newAdminName = 'admin' + random(1, 100000);
let password = '123'; const password = '123';
exports.createAdminUser = function () { exports.createAdminUser = function () {
let nonTokenUser, userIdentity, admin_token_user
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
Realm.Sync.User.register('http://localhost:9080', newAdminName, password, (error, user) => { Realm.Sync.User.register('http://localhost:9080', newAdminName, password, (error, user) => {
if (error) { if (error) {
reject(error); reject(error);
return; return;
} }
let userIdentity = user.identity; nonTokenUser = user
userIdentity = user.identity;
user.logout(); user.logout();
let admin_token_user = Realm.Sync.User.adminUser(getAdminToken()); admin_token_user = Realm.Sync.User.adminUser(getAdminToken(), 'http://localhost:9080');
const config = { const config = {
sync: { sync: {
user: admin_token_user, user: admin_token_user,
url: `realm://localhost:9080/__admin`, url: 'realm://localhost:9080/__admin',
error: err => { error: err => {
reject(new Error('Error opening __admin realm error:' + err.user + ' url:' + err.url + ' state:' + err.state)); reject(new Error('Error opening __admin realm error:' + err.user + ' url:' + err.url + ' state:' + err.state));
} }
@ -74,7 +75,7 @@ exports.createAdminUser = function () {
} }
let isAdmin = newAdminUser.isAdmin; let isAdmin = newAdminUser.isAdmin;
user.logout(); nonTokenUser.logout();
if (!isAdmin) { if (!isAdmin) {
setTimeout(waitForServerToUpdateAdminUser, 500); setTimeout(waitForServerToUpdateAdminUser, 500);
return; return;

View File

@ -76,7 +76,7 @@ if (Realm.Sync) {
new Realm({ new Realm({
encryptionKey: new Int8Array(64), encryptionKey: new Int8Array(64),
sync: { sync: {
user: Realm.Sync.User.adminUser('fake-token'), user: Realm.Sync.User.adminUser('fake-token', 'http://fake-server'),
url: 'realm://fake-server' url: 'realm://fake-server'
} }
}); });