Search in sources :

Example 1 with Token

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());
            }
        });
    }
}
Also used : LogoutResponse(io.realm.internal.network.LogoutResponse) Token(io.realm.internal.objectserver.Token) AuthenticationServer(io.realm.internal.network.AuthenticationServer) ObjectServerUser(io.realm.internal.objectserver.ObjectServerUser) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) File(java.io.File)

Example 2 with Token

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"));
}
Also used : AuthenticateRequest(io.realm.internal.network.AuthenticateRequest) JSONObject(org.json.JSONObject) Token(io.realm.internal.objectserver.Token) URI(java.net.URI) Test(org.junit.Test)

Example 3 with Token

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"));
}
Also used : AuthenticateRequest(io.realm.internal.network.AuthenticateRequest) JSONObject(org.json.JSONObject) Token(io.realm.internal.objectserver.Token) URI(java.net.URI) Test(org.junit.Test)

Example 4 with Token

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);
    }
}
Also used : JSONObject(org.json.JSONObject) ObjectServerUser(io.realm.internal.objectserver.ObjectServerUser) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Token(io.realm.internal.objectserver.Token)

Example 5 with Token

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);
    }
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) Token(io.realm.internal.objectserver.Token)

Aggregations

Token (io.realm.internal.objectserver.Token)10 JSONObject (org.json.JSONObject)8 JSONException (org.json.JSONException)6 ObjectServerUser (io.realm.internal.objectserver.ObjectServerUser)4 URI (java.net.URI)3 JSONArray (org.json.JSONArray)3 AuthenticateRequest (io.realm.internal.network.AuthenticateRequest)2 Test (org.junit.Test)2 AuthenticationServer (io.realm.internal.network.AuthenticationServer)1 LogoutResponse (io.realm.internal.network.LogoutResponse)1 NetworkStateReceiver (io.realm.internal.network.NetworkStateReceiver)1 File (java.io.File)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1