/* * Copyright 2012 by A-SIT, Secure Information Technology Center Austria * * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by * the European Commission - subsequent versions of the EUPL (the "Licence"); * You may not use this work except in compliance with the Licence. * You may obtain a copy of the Licence at: * http://joinup.ec.europa.eu/software/page/eupl * * Unless required by applicable law or agreed to in writing, software * distributed under the Licence is distributed on an "AS IS" basis, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the Licence for the specific language governing permissions and * limitations under the Licence. */ package at.asit.pdfover.gui.bku.mobile; // Imports import java.io.IOException; import java.net.URL; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import at.asit.pdfover.gui.controls.Dialog; import at.asit.pdfover.gui.controls.Dialog.BUTTONS; import at.asit.pdfover.gui.controls.Dialog.ICON; import at.asit.pdfover.gui.utils.Messages; import at.asit.pdfover.gui.workflow.states.MobileBKUState; import at.asit.pdfover.signator.SLResponse; /** * */ public class IAIKHandler extends MobileBKUHandler { /** * SLF4J Logger instance **/ private static final Logger log = LoggerFactory .getLogger(IAIKHandler.class); Shell shell; /** * Constructor * @param state the MobileBKUState * @param shell the Shell */ public IAIKHandler(MobileBKUState state, Shell shell) { super(state); this.shell = shell; } /* (non-Javadoc) * @see at.asit.pdfover.gui.workflow.states.mobilebku.MobileBKUHandler#handleSLRequestResponse(java.lang.String) */ @Override public void handleSLRequestResponse(String responseData) throws Exception { IAIKStatus status = getStatus(); // Extract infos: String credentialURL = MobileBKUHelper.extractSubstring(responseData, "name=\"userCredLogon\" method=\"post\" action=\"", "\""); //$NON-NLS-1$ //$NON-NLS-2$ URL baseURL = new URL(status.getBaseURL()); int p = baseURL.getPort(); String port = ((p != -1) && (p != baseURL.getDefaultPort())) ? ":" + p : ""; //$NON-NLS-1$ //$NON-NLS-2$ credentialURL = baseURL.getProtocol() + "://" + baseURL.getHost() + port + //$NON-NLS-1$ (credentialURL.startsWith("/") ? "" : "/") + credentialURL; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ String viewState = MobileBKUHelper.extractSubstring(responseData, "id=\"javax.faces.ViewState\" value=\"", "\""); //$NON-NLS-1$ //$NON-NLS-2$ String sessionID = credentialURL.substring(credentialURL.indexOf("jsessionid=") + 11); //$NON-NLS-1$ log.info("credentialURL: " + credentialURL); //$NON-NLS-1$ log.info("sessionID: " + sessionID); //$NON-NLS-1$ log.info("viewState: " + viewState); //$NON-NLS-1$ status.setBaseURL(credentialURL); status.setSessionID(sessionID); status.setViewState(viewState); } /* (non-Javadoc) * @see at.asit.pdfover.gui.workflow.states.mobilebku.MobileBKUHandler#postCredentials() */ @Override public String postCredentials() throws Exception { IAIKStatus status = getStatus(); MobileBKUHelper.registerTrustedSocketFactory(); HttpClient client = MobileBKUHelper.getHttpClient(status); PostMethod post = new PostMethod(status.getBaseURL()); post.getParams().setContentCharset("utf-8"); //$NON-NLS-1$ post.addParameter("javax.faces.ViewState", status.getViewState()); //$NON-NLS-1$ post.addParameter("userCredLogon:phoneNR", status.getPhoneNumber()); //$NON-NLS-1$ post.addParameter("userCredLogon:pwd", status.getMobilePassword()); //$NON-NLS-1$ post.addParameter("userCredLogon:logonButton", "userCredLogon:logonButton"); //$NON-NLS-1$ //$NON-NLS-2$ post.addParameter("javax.faces.partial.ajax", "true"); //$NON-NLS-1$ //$NON-NLS-2$ post.addParameter("javax.faces.source", "userCredLogon:logonButton"); //$NON-NLS-1$ //$NON-NLS-2$ post.addParameter("javax.faces.partial.execute", "@all"); //$NON-NLS-1$ //$NON-NLS-2$ post.addParameter("javax.faces.partial.render", "userCredLogon:errorMessagePanel userCredLogon:errorMessage"); //$NON-NLS-1$ //$NON-NLS-2$ post.addParameter("userCredLogon", "userCredLogon"); //$NON-NLS-1$ //$NON-NLS-2$ post.addParameter("userCredLogon:j_idt20_input", "de"); //$NON-NLS-1$ //$NON-NLS-2$ return executePost(client, post); } /* (non-Javadoc) * @see at.asit.pdfover.gui.workflow.states.mobilebku.MobileBKUHandler#handleCredentialsResponse(java.lang.String) */ @Override public void handleCredentialsResponse(String responseData) throws Exception { IAIKStatus status = getStatus(); String refVal = null; String signatureDataURL = null; status.setErrorMessage(null); if (!responseData.contains("referenceValueLogon.jsf")) { //$NON-NLS-1$ // Assume that an error occurred String errorMessage; try { errorMessage = MobileBKUHelper.extractSubstring(responseData, ":errorMessage\">", ""); //$NON-NLS-1$ //$NON-NLS-2$ } catch (Exception e) { errorMessage = Messages.getString("error.Unexpected"); //$NON-NLS-1$ } status.setErrorMessage(errorMessage); // force UI again! status.setMobilePassword(null); return; } HttpClient client = MobileBKUHelper.getHttpClient(status); String redirectURL = status.getBaseURL().substring(0, status.getBaseURL().lastIndexOf('/', status.getBaseURL().lastIndexOf('/') - 1) + 1); //Cut off last directory redirectURL += MobileBKUHelper.extractSubstring(responseData, "redirection_url\":\"", "\""); //$NON-NLS-1$ //$NON-NLS-2$ redirectURL = status.ensureSessionID(redirectURL); responseData = getRedirect(client, redirectURL); refVal = MobileBKUHelper.extractSubstring(responseData, "id=\"j_idt6:refValue\" class=\"strong\">", "", ""); //$NON-NLS-1$ //$NON-NLS-2$ getStatus().setErrorMessage(errorMessage); } catch (Exception e) { // Assume that wrong TAN was entered too many times Display.getDefault().syncExec(new Runnable() { @Override public void run() { Dialog dialog = new Dialog(IAIKHandler.this.shell, Messages.getString("common.warning"), //$NON-NLS-1$ Messages.getString("mobileBKU.tan_tries_exceeded"), //$NON-NLS-1$ BUTTONS.OK_CANCEL, ICON.QUESTION); if (dialog.open() == SWT.CANCEL) { // Go back to BKU Selection getStatus().setTanTries(-1); } else { // Start signature process over getStatus().setTanTries(-2); } } }); } } } @Override public IAIKStatus getStatus() { return (IAIKStatus) getState().getStatus(); } private String getRedirect(HttpClient client, String redirectURL) throws HttpException, IOException { redirectURL = getStatus().ensureSessionID(redirectURL); log.debug("Sending get request to URL " + redirectURL); //$NON-NLS-1$ GetMethod get = new GetMethod(redirectURL); int returnCode = client.executeMethod(get); if (returnCode != HttpStatus.SC_OK) { throw new HttpException(HttpStatus.getStatusText(returnCode)); } String responseData = get.getResponseBodyAsString(); log.debug("Response: " + responseData); //$NON-NLS-1$ return responseData; } /* (non-Javadoc) * @see at.asit.pdfover.gui.bku.mobile.MobileBKUHandler#useBase64Request() */ @Override public boolean useBase64Request() { return false; } }