diff --git a/docs/sync.js b/docs/sync.js index 770d8583..d2ae2327 100644 --- a/docs/sync.js +++ b/docs/sync.js @@ -168,7 +168,7 @@ class User { /** * Create an admin user for the given authentication server with an existing 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 */ static adminUser(adminToken, server) {} diff --git a/lib/user-methods.js b/lib/user-methods.js index 98977b6c..7a11fd38 100644 --- a/lib/user-methods.js +++ b/lib/user-methods.js @@ -61,6 +61,10 @@ function print_error() { } function refreshAccessToken(user, localRealmPath, realmUrl) { + if (!user.server) { + throw new Error("Server for user must be specified"); + } + let parsedRealmUrl = url_parse(realmUrl); const url = auth_url(user.server); const options = { @@ -157,12 +161,8 @@ module.exports = { }, adminUser(token, server) { - checkTypes(arguments, ['string']); - const 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); - }); - return this.createUser(server || '', uuid, token, true); + checkTypes(arguments, ['string', 'string']); + return this._adminUser(server, token); }, register(server, username, password, callback) { diff --git a/src/js_sync.hpp b/src/js_sync.hpp index 20218dcb..bd081b7e 100644 --- a/src/js_sync.hpp +++ b/src/js_sync.hpp @@ -71,9 +71,11 @@ public: }; 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 const static_methods = { - {"createUser", wrap} + {"createUser", wrap}, + {"_adminUser", wrap} }; /*static void current_user(ContextType ctx, ObjectType object, ReturnValue &return_value);*/ @@ -134,6 +136,16 @@ void UserClass::create_user(ContextType ctx, FunctionType, ObjectType this_ob return_value.set(create_object>(ctx, user)); } +template +void UserClass::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>(ctx, user)); +} + template void UserClass::all_users(ContextType ctx, ObjectType object, ReturnValue &return_value) { auto users = Object::create_empty(ctx); diff --git a/tests/js/admin-user-helper.js b/tests/js/admin-user-helper.js index a9d1f18b..0bd90141 100644 --- a/tests/js/admin-user-helper.js +++ b/tests/js/admin-user-helper.js @@ -23,25 +23,26 @@ function random(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } -let newAdminName = 'admin' + random(1, 100000); -let password = '123'; - +const newAdminName = 'admin' + random(1, 100000); +const password = '123'; exports.createAdminUser = function () { + let nonTokenUser, userIdentity, admin_token_user return new Promise((resolve, reject) => { Realm.Sync.User.register('http://localhost:9080', newAdminName, password, (error, user) => { if (error) { reject(error); return; } - let userIdentity = user.identity; + nonTokenUser = user + userIdentity = user.identity; user.logout(); - let admin_token_user = Realm.Sync.User.adminUser(getAdminToken()); + admin_token_user = Realm.Sync.User.adminUser(getAdminToken(), 'http://localhost:9080'); const config = { sync: { user: admin_token_user, - url: `realm://localhost:9080/__admin`, + url: 'realm://localhost:9080/__admin', error: err => { 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; - user.logout(); + nonTokenUser.logout(); if (!isAdmin) { setTimeout(waitForServerToUpdateAdminUser, 500); return; diff --git a/tests/js/encryption-tests.js b/tests/js/encryption-tests.js index f5d3974b..8b3c6b94 100644 --- a/tests/js/encryption-tests.js +++ b/tests/js/encryption-tests.js @@ -76,7 +76,7 @@ if (Realm.Sync) { new Realm({ encryptionKey: new Int8Array(64), sync: { - user: Realm.Sync.User.adminUser('fake-token'), + user: Realm.Sync.User.adminUser('fake-token', 'http://fake-server'), url: 'realm://fake-server' } });