@Override
 public void onAuthCreated(BoxAuthentication.BoxAuthenticationInfo info) {
   BoxAuthentication.BoxAuthenticationInfo.cloneInfo(mSession.mAuthInfo, info);
   mSession.setUserId(info.getUser().getId());
   mSession.onAuthCreated(info);
   authLatch.countDown();
 }
 public BoxSession send() throws BoxException {
   synchronized (mSession) {
     if (mSession.getUser() != null) {
       BoxAuthentication.getInstance().logout(mSession);
       mSession.getAuthInfo().wipeOutAuth();
     }
   }
   return mSession;
 }
Beispiel #3
0
 /**
  * Create intent to launch OAuthActivity using information from the given session.
  *
  * @param context context
  * @param session the BoxSession to use to get parameters required to authenticate via this
  *     activity.
  * @param loginViaBoxApp Whether login should be handled by the installed box android app. Set
  *     this to true only when you are sure or want to make sure user installed box android app and
  *     want to use box android app to login.
  * @return intent to launch OAuthActivity.
  */
 public static Intent createOAuthActivityIntent(
     final Context context, BoxSession session, boolean loginViaBoxApp) {
   Intent intent =
       createOAuthActivityIntent(
           context,
           session.getClientId(),
           session.getClientSecret(),
           session.getRedirectUrl(),
           loginViaBoxApp);
   intent.putExtra(EXTRA_SESSION, session);
   if (!SdkUtils.isEmptyString(session.getUserId())) {
     intent.putExtra(EXTRA_USER_ID_RESTRICTION, session.getUserId());
   }
   return intent;
 }
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.upload) {
      uploadFile();
      return true;
    } else if (id == R.id.switch_accounts) {
      switchAccounts();
      return true;
    } else if (id == R.id.logout) {
      mSession.logout();
      initialize();
      return true;
    } else if (id == R.id.logoutAll) {
      new Thread() {
        @Override
        public void run() {
          BoxAuthentication.getInstance().logoutAllUsers(getApplicationContext());
        }
      }.start();
      return true;
    }

    return super.onOptionsItemSelected(item);
  }
Beispiel #5
0
  /**
   * Start to create OAuth after getting the code.
   *
   * @param code OAuth 2 authorization code
   */
  protected void startMakingOAuthAPICall(final String code, final String baseDomain) {
    if (apiCallStarted.getAndSet(true)) {
      return;
    }
    showSpinner();
    mSession.getAuthInfo().setBaseDomain(baseDomain);
    new Thread() {
      public void run() {
        try {
          BoxAuthenticationInfo sessionAuth =
              BoxAuthentication.getInstance().create(mSession, code).get();

          String restrictedUserId = getIntent().getStringExtra(EXTRA_USER_ID_RESTRICTION);
          if (!SdkUtils.isEmptyString(restrictedUserId)
              && !sessionAuth.getUser().getId().equals(restrictedUserId)) {
            // the user logged in as does not match the user id this activity was restricted to,
            // treat this as a failure.
            throw new RuntimeException(
                "Unexpected user logged in. Expected "
                    + restrictedUserId
                    + " received "
                    + sessionAuth.getUser().getId());
          }
          dismissSpinnerAndFinishAuthenticate(sessionAuth);
        } catch (Exception e) {
          dismissSpinnerAndFailAuthenticate(getAuthCreationErrorString(e));
        }
      }
    }.start();
  }
Beispiel #6
0
 protected void startOAuth() {
   // Use already logged in accounts if not disabled in this activity and not already showing this
   // fragment.
   if (authType != AUTH_TYPE_APP
       && !getIntent().getBooleanExtra(EXTRA_DISABLE_ACCOUNT_CHOOSING, false)
       && getSupportFragmentManager().findFragmentByTag(CHOOSE_AUTH_TAG) == null) {
     Map<String, BoxAuthenticationInfo> map =
         BoxAuthentication.getInstance().getStoredAuthInfo(this);
     if (SdkUtils.isEmptyString(getIntent().getStringExtra(EXTRA_USER_ID_RESTRICTION))
         && map != null
         && map.size() > 0) {
       FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
       transaction.replace(
           R.id.oauth_container,
           ChooseAuthenticationFragment.createAuthenticationActivity(this),
           CHOOSE_AUTH_TAG);
       transaction.addToBackStack(CHOOSE_AUTH_TAG);
       transaction.commit();
     }
   }
   switch (authType) {
     case AUTH_TYPE_APP:
       Intent intent = getBoxAuthApp();
       if (intent != null) {
         intent.putExtra(BoxConstants.KEY_CLIENT_ID, mClientId);
         intent.putExtra(BoxConstants.KEY_REDIRECT_URL, mRedirectUrl);
         if (!SdkUtils.isEmptyString(getIntent().getStringExtra(EXTRA_USER_ID_RESTRICTION))) {
           intent.putExtra(
               EXTRA_USER_ID_RESTRICTION, getIntent().getStringExtra(EXTRA_USER_ID_RESTRICTION));
         }
         startActivityForResult(intent, REQUEST_BOX_APP_FOR_AUTH_CODE);
         break;
       }
     case AUTH_TYPE_WEBVIEW:
       showSpinner();
       this.oauthView = createOAuthView();
       this.oauthClient = createOAuthWebViewClient(oauthView.getStateString());
       oauthClient.setOnPageFinishedListener(this);
       oauthView.setWebViewClient(oauthClient);
       if (mSession.getBoxAccountEmail() != null) {
         oauthView.setBoxAccountEmail(mSession.getBoxAccountEmail());
       }
       oauthView.authenticate(mClientId, mRedirectUrl);
       break;
     default:
   }
 }
 private void switchAccounts() {
   mOldSession = mSession;
   mSession = new BoxSession(this, null);
   mSession.setSessionAuthListener(this);
   mSession
       .authenticate()
       .addOnCompletedListener(
           new BoxFutureTask.OnCompletedListener<BoxSession>() {
             @Override
             public void onCompleted(BoxResponse<BoxSession> response) {
               if (response.isSuccess()) {
                 clearAdapter();
                 onAuthCreated(mSession.getAuthInfo());
               }
             }
           });
 }
 @Override
 public void onAuthFailure(BoxAuthentication.BoxAuthenticationInfo info, Exception ex) {
   if (ex != null) {
     clearAdapter();
   } else if (info == null && mOldSession != null) {
     mSession = mOldSession;
     mOldSession = null;
     onAuthCreated(mSession.getAuthInfo());
   }
 }
 public BoxSession send() throws BoxException {
   try {
     // block until this session is finished refreshing.
     BoxAuthentication.BoxAuthenticationInfo refreshedInfo =
         BoxAuthentication.getInstance().refresh(mSession).get();
   } catch (Exception e) {
     BoxException r = (BoxException) e.getCause();
     if (e.getCause() instanceof BoxException) {
       throw (BoxException) e.getCause();
     } else {
       throw new BoxException("BoxSessionRefreshRequest failed", e);
     }
   }
   BoxAuthentication.BoxAuthenticationInfo.cloneInfo(
       mSession.mAuthInfo,
       BoxAuthentication.getInstance()
           .getAuthInfo(mSession.getUserId(), mSession.getApplicationContext()));
   return mSession;
 }
Beispiel #10
0
  @Override
  public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(getContentView());
    registerReceiver(mConnectedReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

    Intent intent = getIntent();
    mClientId = intent.getStringExtra(BoxConstants.KEY_CLIENT_ID);
    mClientSecret = intent.getStringExtra(BoxConstants.KEY_CLIENT_SECRET);
    mDeviceId = intent.getStringExtra(BoxConstants.KEY_BOX_DEVICE_ID);
    mDeviceName = intent.getStringExtra(BoxConstants.KEY_BOX_DEVICE_NAME);
    mRedirectUrl = intent.getStringExtra(BoxConstants.KEY_REDIRECT_URL);
    boolean loginViaBoxApp = intent.getBooleanExtra(LOGIN_VIA_BOX_APP, false);
    authType = loginViaBoxApp ? AUTH_TYPE_APP : AUTH_TYPE_WEBVIEW;
    apiCallStarted.getAndSet(false);
    mSession = (BoxSession) intent.getSerializableExtra(EXTRA_SESSION);
    if (mSession != null) {
      mSession.setApplicationContext(getApplicationContext());
    } else {
      mSession = new BoxSession(this, null, mClientId, mClientSecret, mRedirectUrl);
      mSession.setDeviceId(mDeviceId);
      mSession.setDeviceName(mDeviceName);
    }
  }
 private void initialize() {
   mAdapter.clear();
   mSession = new BoxSession(this);
   mSession.setSessionAuthListener(this);
   mSession.authenticate();
 }
    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;
      }
    }
 /**
  * Construct a new box session object based off of an existing session.
  *
  * @param session session to use as the base.
  */
 protected BoxSession(BoxSession session) {
   this.mApplicationContext = session.mApplicationContext;
   this.mAuthInfo = session.getAuthInfo();
   setupSession();
 }