use of io.realm.internal.objectserver.Token in project realm-java by realm.
the class SyncUser method logout.
/**
* Logs out the user from the Realm Object Server. Once the Object Server has confirmed the logout any registered
* {@link AuthenticationListener} will be notified and user credentials will be deleted from this device.
*
* @throws IllegalStateException if any Realms owned by this user is still open. They should be closed before
* logging out.
*/
/* FIXME: Add this back to the javadoc when enable SyncConfiguration.Builder#deleteRealmOnLogout()
<p>
Any Realms owned by the user will be deleted if {@link SyncConfiguration.Builder#deleteRealmOnLogout()} is
also set.
*/
public void logout() {
// Acquire lock to prevent users creating new instances
synchronized (Realm.class) {
if (!syncUser.isLoggedIn()) {
// Already local/global logout status
return;
}
// Ensure that we can log out. If any Realm file is still open we should abort before doing anything
// else.
Collection<SyncSession> sessions = syncUser.getSessions();
for (SyncSession session : sessions) {
SyncConfiguration config = session.getConfiguration();
if (Realm.getGlobalInstanceCount(config) > 0) {
throw new IllegalStateException("A Realm controlled by this user is still open. Close all Realms " + "before logging out: " + config.getPath());
}
}
SyncManager.getUserStore().remove(syncUser.getIdentity());
// Delete all Realms if needed.
for (ObjectServerUser.AccessDescription desc : syncUser.getRealms()) {
// disabled. Make sure this works for Realm opened in the client thread/other processes.
if (desc.deleteOnLogout) {
File realmFile = new File(desc.localPath);
if (realmFile.exists() && !Util.deleteRealm(desc.localPath, realmFile.getParentFile(), realmFile.getName())) {
RealmLog.error("Could not delete Realm when user logged out: " + desc.localPath);
}
}
}
// Remove all local tokens, preventing further connections.
final Token userToken = syncUser.getUserToken();
syncUser.clearTokens();
syncUser.localLogout();
// Finally revoke server token. The local user is logged out in any case.
final AuthenticationServer server = SyncManager.getAuthServer();
ThreadPoolExecutor networkPoolExecutor = SyncManager.NETWORK_POOL_EXECUTOR;
networkPoolExecutor.submit(new ExponentialBackoffTask<LogoutResponse>() {
@Override
protected LogoutResponse execute() {
return server.logout(userToken, syncUser.getAuthenticationUrl());
}
@Override
protected void onSuccess(LogoutResponse response) {
SyncManager.notifyUserLoggedOut(SyncUser.this);
}
@Override
protected void onError(LogoutResponse response) {
RealmLog.error("Failed to log user out.\n" + response.getError().toString());
}
});
}
}
use of io.realm.internal.objectserver.Token in project realm-java by realm.
the class AuthenticateRequestTests method realmLogin.
// Tests based on the schemas described here: https://github.com/realm/realm-sync-services/blob/master/doc/index.apib
@Test
public void realmLogin() throws URISyntaxException, JSONException {
Token t = SyncTestUtils.createTestUser().getSyncUser().getUserToken();
AuthenticateRequest request = AuthenticateRequest.realmLogin(t, new URI("realm://objectserver/" + t.identity() + "/default"));
JSONObject obj = new JSONObject(request.toJson());
assertEquals("/" + t.identity() + "/default", obj.get("path"));
assertEquals(t.value(), obj.get("data"));
assertEquals("realm", obj.get("provider"));
}
use of io.realm.internal.objectserver.Token in project realm-java by realm.
the class AuthenticateRequestTests method userRefresh.
@Test
public void userRefresh() throws URISyntaxException, JSONException {
Token t = SyncTestUtils.createTestUser().getSyncUser().getUserToken();
AuthenticateRequest request = AuthenticateRequest.userRefresh(t, new URI("realm://objectserver/" + t.identity() + "/default"));
JSONObject obj = new JSONObject(request.toJson());
assertTrue(obj.has("path"));
assertEquals(t.value(), obj.get("data"));
assertEquals("realm", obj.get("provider"));
}
use of io.realm.internal.objectserver.Token in project realm-java by realm.
the class SyncTestUtils method createTestUser.
public static SyncUser createTestUser(String userTokenValue, String realmTokenValue, String userIdentifier, String authUrl, long expires) {
Token userToken = new Token(userTokenValue, userIdentifier, null, expires, null);
Token accessToken = new Token(realmTokenValue, userIdentifier, "/foo", expires, new Token.Permission[] { Token.Permission.DOWNLOAD });
ObjectServerUser.AccessDescription desc = new ObjectServerUser.AccessDescription(accessToken, "/data/data/myapp/files/default", false);
JSONObject obj = new JSONObject();
try {
JSONArray realmList = new JSONArray();
JSONObject realmDesc = new JSONObject();
realmDesc.put("uri", "realm://objectserver.realm.io/default");
realmDesc.put("description", desc.toJson());
realmList.put(realmDesc);
obj.put("authUrl", authUrl);
obj.put("userToken", userToken.toJson());
obj.put("realms", realmList);
return SyncUser.fromJson(obj.toString());
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
use of io.realm.internal.objectserver.Token in project realm-java by realm.
the class SyncTestUtils method createLoginResponse.
public static AuthenticateResponse createLoginResponse(String userTokenValue, String userIdentity, long expires) {
try {
Token userToken = new Token(userTokenValue, userIdentity, null, expires, null);
JSONObject response = new JSONObject();
response.put("refresh_token", userToken.toJson());
return AuthenticateResponse.from(response.toString());
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
Aggregations