Search in sources :

Example 1 with BoxApiUser

use of com.box.androidsdk.content.BoxApiUser in project box-android-sdk by box.

the class BoxAuthentication method doUserRefresh.

private BoxFutureTask<BoxUser> doUserRefresh(final Context context, final BoxAuthenticationInfo info) {
    BoxSession tempSession = new BoxSession(context, info.accessToken(), null);
    BoxApiUser apiUser = new BoxApiUser(tempSession);
    BoxFutureTask<BoxUser> task = apiUser.getCurrentUserInfoRequest().setFields(BoxUser.ALL_FIELDS).toTask();
    task.addOnCompletedListener(new BoxFutureTask.OnCompletedListener<BoxUser>() {

        @Override
        public void onCompleted(BoxResponse<BoxUser> response) {
            if (response.isSuccess()) {
                info.setUser(response.getResult());
                BoxAuthentication.getInstance().onAuthenticated(info, context);
            } else {
                BoxAuthentication.getInstance().onAuthenticationFailure(info, response.getException());
            }
        }
    });
    AUTH_EXECUTOR.execute(task);
    return task;
}
Also used : BoxApiUser(com.box.androidsdk.content.BoxApiUser) BoxSession(com.box.androidsdk.content.models.BoxSession) BoxFutureTask(com.box.androidsdk.content.BoxFutureTask) BoxUser(com.box.androidsdk.content.models.BoxUser)

Example 2 with BoxApiUser

use of com.box.androidsdk.content.BoxApiUser in project box-android-sdk by box.

the class BoxUserRequestTest method testCreateEnterpriseUserRequestProperties.

public void testCreateEnterpriseUserRequestProperties() throws ParseException {
    String login = "tester@gmail.com";
    String name = "tester";
    String address = "4440 El Camino Real";
    String jobTitle = "Tester";
    String phone = "123-456-7890";
    double space = 1000;
    String timezone = "Asia/Hong_Kong";
    BoxApiUser userApi = new BoxApiUser(null);
    BoxRequestsUser.CreateEnterpriseUser request = userApi.getCreateEnterpriseUserRequest(login, name).setAddress(address).setJobTitle(jobTitle).setPhone(phone).setSpaceAmount(space).setRole(BoxUser.Role.COADMIN).setStatus(BoxUser.Status.ACTIVE).setTimezone(timezone).setCanSeeManagedUsers(true).setIsExemptFromDeviceLimits(true).setIsExemptFromLoginVerification(true).setIsSyncEnabled(true);
    Assert.assertEquals(login, request.getLogin());
    Assert.assertEquals(name, request.getName());
    Assert.assertEquals(address, request.getAddress());
    Assert.assertEquals(jobTitle, request.getJobTitle());
    Assert.assertEquals(phone, request.getPhone());
    Assert.assertEquals(space, request.getSpaceAmount());
    Assert.assertEquals(timezone, request.getTimezone());
    Assert.assertEquals(BoxUser.Status.ACTIVE, request.getStatus());
    Assert.assertEquals(BoxUser.Role.COADMIN, request.getRole());
    Assert.assertTrue(request.getCanSeeManagedUsers());
    Assert.assertTrue(request.getIsExemptFromDeviceLimits());
    Assert.assertTrue(request.getIsExemptFromLoginVerification());
    Assert.assertTrue(request.getCanSeeManagedUsers());
    Assert.assertTrue(request.getIsSyncEnabled());
}
Also used : BoxApiUser(com.box.androidsdk.content.BoxApiUser)

Example 3 with BoxApiUser

use of com.box.androidsdk.content.BoxApiUser in project box-android-sdk by box.

the class BoxAuthentication method doRefresh.

private FutureTask<BoxAuthenticationInfo> doRefresh(final BoxSession session, final BoxAuthenticationInfo info) throws BoxException {
    final boolean userUnknown = (info.getUser() == null && session.getUser() == null);
    final String taskKey = SdkUtils.isBlank(session.getUserId()) && userUnknown ? info.accessToken() : session.getUserId();
    final String userId = (info.getUser() != null) ? info.getUser().getId() : session.getUserId();
    FutureTask<BoxAuthenticationInfo> task = new FutureTask<BoxAuthenticationInfo>(new Callable<BoxAuthenticationInfo>() {

        @Override
        public BoxAuthenticationInfo call() throws Exception {
            BoxAuthenticationInfo refreshInfo = null;
            if (session.getRefreshProvider() != null) {
                try {
                    refreshInfo = session.getRefreshProvider().refreshAuthenticationInfo(info);
                } catch (BoxException e) {
                    mRefreshingTasks.remove(taskKey);
                    throw handleRefreshException(session, e, info, userId);
                }
            } else if (mRefreshProvider != null) {
                try {
                    refreshInfo = mRefreshProvider.refreshAuthenticationInfo(info);
                } catch (BoxException e) {
                    mRefreshingTasks.remove(taskKey);
                    throw handleRefreshException(session, e, info, userId);
                }
            } else {
                String refreshToken = info.refreshToken() != null ? info.refreshToken() : "";
                String clientId = session.getClientId() != null ? session.getClientId() : BoxConfig.CLIENT_ID;
                String clientSecret = session.getClientSecret() != null ? session.getClientSecret() : BoxConfig.CLIENT_SECRET;
                if (SdkUtils.isBlank(clientId) || SdkUtils.isBlank(clientSecret)) {
                    BoxException badRequest = new BoxException("client id or secret not specified", 400, "{\"error\": \"bad_request\",\n" + "  \"error_description\": \"client id or secret not specified\"}", null);
                    throw handleRefreshException(session, badRequest, info, userId);
                }
                BoxApiAuthentication.BoxRefreshAuthRequest request = new BoxApiAuthentication(session).refreshOAuth(refreshToken, clientId, clientSecret);
                try {
                    refreshInfo = request.send();
                } catch (BoxException e) {
                    mRefreshingTasks.remove(taskKey);
                    throw handleRefreshException(session, e, info, userId);
                }
            }
            if (refreshInfo != null) {
                refreshInfo.setRefreshTime(System.currentTimeMillis());
            }
            BoxAuthenticationInfo.cloneInfo(session.getAuthInfo(), refreshInfo);
            // if we using a custom refresh provider ensure we check the user, otherwise do this only if we don't know who the user is.
            if (userUnknown || session.getRefreshProvider() != null || mRefreshProvider != null) {
                BoxApiUser userApi = new BoxApiUser(session);
                info.setUser(userApi.getCurrentUserInfoRequest().setFields(BoxUser.ALL_FIELDS).send());
            }
            getAuthInfoMap(session.getApplicationContext()).put(info.getUser().getId(), refreshInfo);
            getAuthStorage().storeAuthInfoMap(mCurrentAccessInfo, session.getApplicationContext());
            // call notifyListeners() with results.
            for (WeakReference<AuthListener> reference : mListeners) {
                AuthListener rc = reference.get();
                if (rc != null) {
                    rc.onRefreshed(refreshInfo);
                }
            }
            if (!session.getUserId().equals(info.getUser().getId())) {
                session.onAuthFailure(info, new BoxException("Session User Id has changed!"));
            }
            mRefreshingTasks.remove(taskKey);
            return info;
        }
    });
    mRefreshingTasks.put(taskKey, task);
    AUTH_EXECUTOR.execute(task);
    return task;
}
Also used : BoxException(com.box.androidsdk.content.BoxException) BoxException(com.box.androidsdk.content.BoxException) FutureTask(java.util.concurrent.FutureTask) BoxFutureTask(com.box.androidsdk.content.BoxFutureTask) BoxApiUser(com.box.androidsdk.content.BoxApiUser) WeakReference(java.lang.ref.WeakReference)

Example 4 with BoxApiUser

use of com.box.androidsdk.content.BoxApiUser in project box-android-sdk by box.

the class BoxUserRequestTest method testCreateEnterpriseUserRequest.

public void testCreateEnterpriseUserRequest() throws UnsupportedEncodingException {
    String expected = "{\"login\":\"tester@gmail.com\",\"name\":\"tester\",\"address\":\"4440 El Camino Real\",\"job_title\":\"Tester\",\"phone\":\"123-456-7890\",\"space_amount\":\"1000.0\",\"role\":\"coadmin\",\"status\":\"active\",\"timezone\":\"Asia/Hong_Kong\",\"can_see_managed_users\":\"true\",\"is_exempt_from_device_limits\":\"true\",\"is_exempt_from_login_verification\":\"true\",\"is_sync_enabled\":\"true\"}";
    String login = "tester@gmail.com";
    String name = "tester";
    String address = "4440 El Camino Real";
    String jobTitle = "Tester";
    String phone = "123-456-7890";
    double space = 1000;
    String timezone = "Asia/Hong_Kong";
    BoxApiUser userApi = new BoxApiUser(null);
    BoxRequestsUser.CreateEnterpriseUser request = userApi.getCreateEnterpriseUserRequest(login, name).setAddress(address).setJobTitle(jobTitle).setPhone(phone).setSpaceAmount(space).setRole(BoxUser.Role.COADMIN).setStatus(BoxUser.Status.ACTIVE).setTimezone(timezone).setCanSeeManagedUsers(true).setIsExemptFromDeviceLimits(true).setIsExemptFromLoginVerification(true).setIsSyncEnabled(true);
    String json = request.getStringBody();
    Assert.assertEquals(expected, json);
}
Also used : BoxApiUser(com.box.androidsdk.content.BoxApiUser)

Aggregations

BoxApiUser (com.box.androidsdk.content.BoxApiUser)4 BoxFutureTask (com.box.androidsdk.content.BoxFutureTask)2 BoxException (com.box.androidsdk.content.BoxException)1 BoxSession (com.box.androidsdk.content.models.BoxSession)1 BoxUser (com.box.androidsdk.content.models.BoxUser)1 WeakReference (java.lang.ref.WeakReference)1 FutureTask (java.util.concurrent.FutureTask)1