public void authenticate(IAuthFlowListener listener) {
// TODO run this on an Executor to make it async
// create HtmlUnit client
final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_24);
final WebClientOptions options = webClient.getOptions();
options.setRedirectEnabled(true);
options.setJavaScriptEnabled(false);
options.setThrowExceptionOnFailingStatusCode(true);
options.setThrowExceptionOnScriptError(true);
options.setPrintContentOnFailingStatusCode(LOG.isDebugEnabled());
try {
// use default SSP to create supported non-SSL protocols list
final SSLContext sslContext = new SSLContextParameters().createSSLContext();
options.setSSLClientProtocols(sslContext.createSSLEngine().getEnabledProtocols());
} catch (GeneralSecurityException e) {
throw ObjectHelper.wrapRuntimeCamelException(e);
} catch (IOException e) {
throw ObjectHelper.wrapRuntimeCamelException(e);
}
// add HTTP proxy if set
final Map<String, Object> httpParams = configuration.getHttpParams();
if (httpParams != null && httpParams.get(ConnRoutePNames.DEFAULT_PROXY) != null) {
final HttpHost proxyHost = (HttpHost) httpParams.get(ConnRoutePNames.DEFAULT_PROXY);
final Boolean socksProxy = (Boolean) httpParams.get("http.route.socks-proxy");
final ProxyConfig proxyConfig = new ProxyConfig(proxyHost.getHostName(), proxyHost.getPort(),
socksProxy != null ? socksProxy : false);
options.setProxyConfig(proxyConfig);
}
// authorize application on user's behalf
try {
final String csrfId = String.valueOf(new SecureRandom().nextLong());
OAuthWebViewData viewData = new OAuthWebViewData(boxClient.getOAuthDataController());
viewData.setOptionalState(String.valueOf(csrfId));
final HtmlPage authPage = webClient.getPage(viewData.buildUrl().toString());
// submit login credentials
final HtmlForm loginForm = authPage.getFormByName("login_form");
final HtmlTextInput login = loginForm.getInputByName("login");
login.setText(configuration.getUserName());
final HtmlPasswordInput password = loginForm.getInputByName("password");
password.setText(configuration.getUserPassword());
final HtmlSubmitInput submitInput = loginForm.getInputByName("login_submit");
// submit consent
final HtmlPage consentPage = submitInput.click();
final HtmlForm consentForm = consentPage.getFormByName("consent_form");
final HtmlButton consentAccept = consentForm.getButtonByName("consent_accept");
// disable redirect to avoid loading redirect URL
webClient.getOptions().setRedirectEnabled(false);
// validate CSRF and get authorization code
String redirectQuery;
try {
final Page redirectPage = consentAccept.click();