Exemplo n.º 1
0
    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;
      }
    }
Exemplo n.º 2
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();
  }
Exemplo n.º 3
0
 public BoxSession send() throws BoxException {
   synchronized (mSession) {
     if (mSession.getUser() != null) {
       BoxAuthentication.getInstance().logout(mSession);
       mSession.getAuthInfo().wipeOutAuth();
     }
   }
   return mSession;
 }
Exemplo n.º 4
0
 @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());
   }
 }
Exemplo n.º 5
0
 /**
  * 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();
 }