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);
}
}
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);
}
}
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;
}
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);
}
}
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);
}
}
Aggregations