public BoxSession send() throws BoxException { synchronized (mSession) { if (mSession.getUser() == null) { if (mSession.getAuthInfo() != null && !SdkUtils.isBlank(mSession.getAuthInfo().accessToken())) { // if we have an access token, but no user try to repair by making the call to user // endpoint. try { // TODO: show some ui while requestion user info BoxApiUser apiUser = new BoxApiUser(mSession); BoxUser user = apiUser.getCurrentUserInfoRequest().send(); mSession.setUserId(user.getId()); mSession.getAuthInfo().setUser(user); mSession.onAuthCreated(mSession.getAuthInfo()); return mSession; } catch (BoxException e) { BoxLogUtils.e("BoxSession", "Unable to repair user", e); if (e instanceof BoxException.RefreshFailure && ((BoxException.RefreshFailure) e).isErrorFatal()) { // if the refresh failure is unrecoverable have the user login again. toastString(mSession.getApplicationContext(), R.string.boxsdk_error_fatal_refresh); } else if (e.getErrorType() == BoxException.ErrorType.TERMS_OF_SERVICE_REQUIRED) { toastString( mSession.getApplicationContext(), R.string.boxsdk_error_terms_of_service); } else { mSession.onAuthFailure(null, e); throw e; } } // at this point we were unable to repair. } BoxAuthentication.getInstance().addListener(this); launchAuthUI(); return mSession; } else { BoxAuthentication.BoxAuthenticationInfo info = BoxAuthentication.getInstance() .getAuthInfo(mSession.getUserId(), mSession.getApplicationContext()); if (info != null) { BoxAuthentication.BoxAuthenticationInfo.cloneInfo(mSession.mAuthInfo, info); mSession.onAuthCreated(mSession.getAuthInfo()); } else { // Fail to get information of current user. current use no longer valid. mSession.mAuthInfo.setUser(null); launchAuthUI(); } } return mSession; } }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (RESULT_OK == resultCode && REQUEST_BOX_APP_FOR_AUTH_CODE == requestCode) { String userId = data.getStringExtra(USER_ID); String authCode = data.getStringExtra(AUTH_CODE); if (SdkUtils.isBlank(authCode) && !SdkUtils.isBlank(userId)) { Map<String, BoxAuthenticationInfo> authenticatedMap = BoxAuthentication.getInstance().getStoredAuthInfo(this); BoxAuthenticationInfo info = authenticatedMap.get(userId); if (info != null) { onAuthenticationChosen(info); } else { onAuthFailure(new AuthFailure(AuthFailure.TYPE_USER_INTERACTION, "")); } } else if (!SdkUtils.isBlank(authCode)) { startMakingOAuthAPICall(authCode, null); } } else if (resultCode == RESULT_CANCELED) { finish(); } }
/** * This is an advanced constructor that can be used when implementing an authentication flow that * differs from the default oauth 2 flow. * * @param context current context. * @param authInfo authentication information that should be used. (Must at the minimum provide an * access token). * @param refreshProvider the refresh provider to use when the access token expires and needs to * be refreshed. */ public BoxSession( Context context, BoxAuthentication.BoxAuthenticationInfo authInfo, BoxAuthentication.AuthenticationRefreshProvider refreshProvider) { mApplicationContext = context.getApplicationContext(); mAuthInfo = authInfo; mRefreshProvider = refreshProvider; if (authInfo.getUser() != null) { if (!SdkUtils.isBlank(authInfo.getUser().getId())) { mUserId = authInfo.getUser().getId(); } } }