Search in sources :

Example 1 with ObjectServerUser

use of io.realm.internal.objectserver.ObjectServerUser 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 2 with ObjectServerUser

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

the class SyncUser method login.

/**
     * Logs in the user to the Realm Object Server. This is done synchronously, so calling this method on the Android
     * UI thread will always crash. A logged in user is required to be able to create a {@link SyncConfiguration}.
     *
     * @param credentials credentials to use.
     * @param authenticationUrl server that can authenticate against.
     * @throws ObjectServerError if the login failed.
     * @throws IllegalArgumentException if the URL is malformed.
     */
public static SyncUser login(final SyncCredentials credentials, final String authenticationUrl) throws ObjectServerError {
    final URL authUrl;
    try {
        authUrl = new URL(authenticationUrl);
    } catch (MalformedURLException e) {
        throw new IllegalArgumentException("Invalid URL " + authenticationUrl + ".", e);
    }
    ObjectServerError error;
    try {
        AuthenticateResponse result;
        if (credentials.getIdentityProvider().equals(SyncCredentials.IdentityProvider.ACCESS_TOKEN)) {
            // Credentials using ACCESS_TOKEN as IdentityProvider are optimistically assumed to be valid already.
            // So log them in directly without contacting the authentication server. This is done by mirroring
            // the JSON response expected from the server.
            String userIdentifier = credentials.getUserIdentifier();
            String token = (String) credentials.getUserInfo().get("_token");
            result = AuthenticateResponse.createValidResponseWithUser(userIdentifier, token);
        } else {
            final AuthenticationServer server = SyncManager.getAuthServer();
            result = server.loginUser(credentials, authUrl);
        }
        if (result.isValid()) {
            ObjectServerUser syncUser = new ObjectServerUser(result.getRefreshToken(), authUrl);
            SyncUser user = new SyncUser(syncUser);
            RealmLog.info("Succeeded authenticating user.\n%s", user);
            SyncManager.getUserStore().put(user);
            SyncManager.notifyUserLoggedIn(user);
            return user;
        } else {
            RealmLog.info("Failed authenticating user.\n%s", result.getError());
            error = result.getError();
        }
    } catch (Throwable e) {
        throw new ObjectServerError(ErrorCode.UNKNOWN, e);
    }
    throw error;
}
Also used : AuthenticateResponse(io.realm.internal.network.AuthenticateResponse) MalformedURLException(java.net.MalformedURLException) AuthenticationServer(io.realm.internal.network.AuthenticationServer) ObjectServerUser(io.realm.internal.objectserver.ObjectServerUser) URL(java.net.URL)

Aggregations

ObjectServerUser (io.realm.internal.objectserver.ObjectServerUser)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 AuthenticateResponse (io.realm.internal.network.AuthenticateResponse)1 AuthenticationServer (io.realm.internal.network.AuthenticationServer)1 Token (io.realm.internal.objectserver.Token)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 JSONArray (org.json.JSONArray)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1