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: } }
/** * Create a BoxSession using a specific box clientId, secret, and redirectUrl. This constructor is * not necessary unless an application uses multiple api keys. Note: When setting the userId to * null ui will be shown to ask which user to authenticate as if at least one user is logged in. * If no user has been stored will show login ui. * * @param context current context. * @param clientId the developer's client id to access the box api. * @param clientSecret the developer's secret used to interpret the response coming from Box. * @param redirectUrl the developer's redirect url to use for authenticating via Box. * @param userId user id to login as or null to login as a new user. */ public BoxSession( Context context, String userId, String clientId, String clientSecret, String redirectUrl) { mClientId = clientId; mClientSecret = clientSecret; mClientRedirectUrl = redirectUrl; if (SdkUtils.isEmptyString(mClientId) || SdkUtils.isEmptyString(mClientSecret)) { throw new RuntimeException( "Session must have a valid client id and client secret specified."); } mApplicationContext = context.getApplicationContext(); if (!SdkUtils.isEmptyString(userId)) { mAuthInfo = BoxAuthentication.getInstance().getAuthInfo(userId, context); mUserId = userId; } if (mAuthInfo == null) { mUserId = userId; mAuthInfo = new BoxAuthentication.BoxAuthenticationInfo(); } mAuthInfo.setClientId(mClientId); setupSession(); }
/** * 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; }
/** * Create intent to launch OAuthActivity. Notes about redirect url parameter: If you already set * redirect url in <a href="https://cloud.app.box.com/developers/services">box dev console</a>, * you should pass in the same redirect url or use null for redirect url. If you didn't set it in * box dev console, you should pass in a url. In case you don't have a redirect server you can * simply use "http://localhost". * * @param context context * @param clientId your box client id * @param clientSecret your box client secret * @param redirectUrl redirect url, if you already set redirect url in <a * href="https://cloud.app.box.com/developers/services">box dev console</a>, leave this null * or use the same url, otherwise this field is required. You can use "http://localhost" if * you don't have a redirect server. * @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, final String clientId, final String clientSecret, String redirectUrl, boolean loginViaBoxApp) { Intent intent = new Intent(context, OAuthActivity.class); intent.putExtra(BoxConstants.KEY_CLIENT_ID, clientId); intent.putExtra(BoxConstants.KEY_CLIENT_SECRET, clientSecret); if (!SdkUtils.isEmptyString(redirectUrl)) { intent.putExtra(BoxConstants.KEY_REDIRECT_URL, redirectUrl); } intent.putExtra(LOGIN_VIA_BOX_APP, loginViaBoxApp); return intent; }
/** * @return the user id associated with the only logged in user. If no user is logged in or * multiple users are logged in returns null. */ private static String getBestStoredUserId(final Context context) { String lastAuthenticatedUserId = BoxAuthentication.getInstance().getLastAuthenticatedUserId(context); Map<String, BoxAuthentication.BoxAuthenticationInfo> authInfoMap = BoxAuthentication.getInstance().getStoredAuthInfo(context); if (authInfoMap != null) { if (!SdkUtils.isEmptyString(lastAuthenticatedUserId) && authInfoMap.get(lastAuthenticatedUserId) != null) { return lastAuthenticatedUserId; } if (authInfoMap.size() == 1) { for (String authUserId : authInfoMap.keySet()) { return authUserId; } } } return null; }
/** Callback method to be called when authentication failed. */ public boolean onAuthFailure(AuthFailure failure) { if (failure.type == OAuthWebView.AuthFailure.TYPE_WEB_ERROR) { if (failure.mWebException.getErrorCode() == WebViewClient.ERROR_CONNECT || failure.mWebException.getErrorCode() == WebViewClient.ERROR_HOST_LOOKUP || failure.mWebException.getErrorCode() == WebViewClient.ERROR_TIMEOUT) { return false; } Resources resources = this.getResources(); Toast.makeText( this, String.format( "%s\n%s: %s", resources.getString(com.box.sdk.android.R.string.boxsdk_Authentication_fail), resources.getString(com.box.sdk.android.R.string.boxsdk_details), failure.mWebException.getErrorCode() + " " + failure.mWebException.getDescription()), Toast.LENGTH_LONG) .show(); } else if (SdkUtils.isEmptyString(failure.message)) { Toast.makeText(this, R.string.boxsdk_Authentication_fail, Toast.LENGTH_LONG).show(); } else { switch (failure.type) { case AuthFailure.TYPE_URL_MISMATCH: Resources resources = this.getResources(); Toast.makeText( this, String.format( "%s\n%s: %s", resources.getString(R.string.boxsdk_Authentication_fail), resources.getString(R.string.boxsdk_details), resources.getString(R.string.boxsdk_Authentication_fail_url_mismatch)), Toast.LENGTH_LONG) .show(); break; default: Toast.makeText(this, R.string.boxsdk_Authentication_fail, Toast.LENGTH_LONG).show(); } } finish(); return true; }