Search in sources :

Example 6 with Token

use of io.realm.internal.objectserver.Token in project realm-java by realm.

the class SyncTestUtils method createRefreshResponse.

public static AuthenticateResponse createRefreshResponse() {
    try {
        Token userToken = new Token(USER_TOKEN, "JohnDoe", null, Long.MAX_VALUE, 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)

Example 7 with Token

use of io.realm.internal.objectserver.Token in project realm-java by realm.

the class MainActivity method createTestUser.

private static SyncUser createTestUser(long expires) {
    Token userToken = new Token(USER_TOKEN, "JohnDoe", null, expires, null);
    Token accessToken = new Token(REALM_TOKEN, "JohnDoe", "/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", "http://objectserver.realm.io/auth");
        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 8 with Token

use of io.realm.internal.objectserver.Token in project realm-java by realm.

the class SyncSession method accessToken.

String accessToken(final AuthenticationServer authServer) {
    // check first if there's a valid access_token we can return immediately
    if (getUser().getSyncUser().isAuthenticated(configuration)) {
        Token accessToken = getUser().getSyncUser().getAccessToken(configuration.getServerUrl());
        // start refreshing this token if a refresh is not going on
        if (!onGoingAccessTokenQuery.getAndSet(true)) {
            scheduleRefreshAccessToken(authServer, accessToken.expiresMs());
        }
        return accessToken.value();
    } else {
        if (!onGoingAccessTokenQuery.getAndSet(true)) {
            if (NetworkStateReceiver.isOnline(SyncObjectServerFacade.getApplicationContext())) {
                authenticateRealm(authServer);
            } else {
                // Wait for connection to become available, before trying again.
                // The Session might potentially stay in this state for the lifetime of the application.
                // This is acceptable.
                networkListener = new NetworkStateReceiver.ConnectionListener() {

                    @Override
                    public void onChange(boolean connectionAvailable) {
                        if (connectionAvailable) {
                            if (!onGoingAccessTokenQuery.getAndSet(true)) {
                                authenticateRealm(authServer);
                            }
                            NetworkStateReceiver.removeListener(this);
                        }
                    }
                };
                NetworkStateReceiver.addListener(networkListener);
            }
        }
    }
    return null;
}
Also used : Token(io.realm.internal.objectserver.Token) NetworkStateReceiver(io.realm.internal.network.NetworkStateReceiver)

Example 9 with Token

use of io.realm.internal.objectserver.Token in project realm-java by realm.

the class SyncUser method fromJson.

/**
     * Loads a user that has previously been serialized using {@link #toJson()}.
     *
     * @param user JSON string representing the user.
     *
     * @return the user object.
     * @throws IllegalArgumentException if the JSON couldn't be converted to a valid {@link SyncUser} object.
     */
public static SyncUser fromJson(String user) {
    try {
        JSONObject obj = new JSONObject(user);
        URL authUrl = new URL(obj.getString("authUrl"));
        //TODO rename to refresh_token
        Token userToken = Token.from(obj.getJSONObject("userToken"));
        ObjectServerUser syncUser = new ObjectServerUser(userToken, authUrl);
        JSONArray realmTokens = obj.getJSONArray("realms");
        for (int i = 0; i < realmTokens.length(); i++) {
            JSONObject token = realmTokens.getJSONObject(i);
            URI uri = new URI(token.getString("uri"));
            ObjectServerUser.AccessDescription realmDesc = ObjectServerUser.AccessDescription.fromJson(token.getJSONObject("description"));
            syncUser.addRealm(uri, realmDesc);
        }
        return new SyncUser(syncUser);
    } catch (JSONException e) {
        throw new IllegalArgumentException("Could not parse user json: " + user, e);
    } catch (MalformedURLException e) {
        throw new IllegalArgumentException("URL in JSON not valid: " + user, e);
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException("URI is not valid: " + user, e);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Token(io.realm.internal.objectserver.Token) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URL(java.net.URL) JSONObject(org.json.JSONObject) ObjectServerUser(io.realm.internal.objectserver.ObjectServerUser)

Example 10 with Token

use of io.realm.internal.objectserver.Token in project realm-java by realm.

the class AuthenticateResponse method createValidResponseWithUser.

/**
     * Helper method for creating a valid user login response. The user returned will be assumed to have all permissions
     * and doesn't expire.
     *
     * @param identifier user identifier.
     * @param refreshToken user's refresh token.
     */
public static AuthenticateResponse createValidResponseWithUser(String identifier, String refreshToken) {
    try {
        JSONObject response = new JSONObject();
        response.put(JSON_FIELD_REFRESH_TOKEN, new Token(refreshToken, identifier, null, Long.MAX_VALUE, Token.Permission.ALL).toJson());
        return new AuthenticateResponse(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