Java tutorial
/******************************************************************************* * Copyright (c) 2015-2016, WSO2.Telco Inc. (http://www.wso2telco.com) * * All Rights Reserved. WSO2.Telco Inc. licences this file to you under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. ******************************************************************************/ package com.wso2telco.gsma.authenticators; import com.wso2telco.core.config.service.ConfigurationService; import com.wso2telco.core.config.service.ConfigurationServiceImpl; import com.wso2telco.gsma.authenticators.model.MSSRequest; import com.wso2telco.gsma.authenticators.util.AuthenticationContextHelper; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.http.HttpResponse; import org.wso2.carbon.identity.application.authentication.framework.AbstractApplicationAuthenticator; import org.wso2.carbon.identity.application.authentication.framework.AuthenticatorFlowStatus; import org.wso2.carbon.identity.application.authentication.framework.LocalApplicationAuthenticator; import org.wso2.carbon.identity.application.authentication.framework.config.ConfigurationFacade; import org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext; import org.wso2.carbon.identity.application.authentication.framework.exception.AuthenticationFailedException; import org.wso2.carbon.identity.application.authentication.framework.exception.LogoutFailedException; import org.wso2.carbon.identity.application.authentication.framework.util.FrameworkUtils; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; // TODO: Auto-generated Javadoc /** * The Class MSSPinAuthenticator. */ public class MSSPinAuthenticator extends AbstractApplicationAuthenticator implements LocalApplicationAuthenticator { /** * The Constant serialVersionUID. */ private static final long serialVersionUID = 6817280268460894001L; /** * The log. */ private static Log log = LogFactory.getLog(MSSPinAuthenticator.class); /** * The Configuration service */ private static ConfigurationService configurationService = new ConfigurationServiceImpl(); /* (non-Javadoc) * @see org.wso2.carbon.identity.application.authentication.framework.AbstractApplicationAuthenticator#process * (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, org.wso2.carbon.identity * .application.authentication.framework.context.AuthenticationContext) */ @Override public AuthenticatorFlowStatus process(HttpServletRequest request, HttpServletResponse response, AuthenticationContext context) throws AuthenticationFailedException, LogoutFailedException { if (context.isLogoutRequest()) { return AuthenticatorFlowStatus.SUCCESS_COMPLETED; } else { return super.process(request, response, context); } } /* (non-Javadoc) * @see org.wso2.carbon.identity.application.authentication.framework * .AbstractApplicationAuthenticator#initiateAuthenticationRequest(javax.servlet.http.HttpServletRequest, javax * .servlet.http.HttpServletResponse, org.wso2.carbon.identity.application.authentication.framework.context * .AuthenticationContext) */ @Override protected void initiateAuthenticationRequest(HttpServletRequest request, HttpServletResponse response, AuthenticationContext context) throws AuthenticationFailedException { log.info("Initiating authentication request"); String loginPage = ConfigurationFacade.getInstance().getAuthenticationEndpointURL(); String queryParams = FrameworkUtils.getQueryStringWithFrameworkContextId(context.getQueryParams(), context.getCallerSessionKey(), context.getContextIdentifier()); if (log.isDebugEnabled()) { log.debug("Query parameters : " + queryParams); } try { String retryParam = ""; if (context.isRetrying()) { retryParam = "&authFailure=true&authFailureMsg=login.fail.message"; } else { // Insert entry to DB only if this is not a retry DBUtils.insertUserResponse(context.getContextIdentifier(), String.valueOf(MSSPinAuthenticator.UserResponse.PENDING)); } //MSISDN will be saved in the context in the MSISDNAuthenticator String msisdn = (String) context.getProperty("msisdn"); MSSRequest mssRequest = new MSSRequest(); mssRequest.setMsisdnNo("+" + msisdn); mssRequest.setSendString( configurationService.getDataHolder().getMobileConnectConfig().getMSS().getMssPinTest()); String contextIdentifier = context.getContextIdentifier(); MSSRestClient mssRestClient = new MSSRestClient(contextIdentifier, mssRequest); mssRestClient.start(); response.sendRedirect(response.encodeRedirectURL(loginPage + ("?" + queryParams)) + "&authenticators=" + getName() + ":" + "LOCAL" + retryParam); } catch (IOException e) { throw new AuthenticationFailedException(e.getMessage(), e); } catch (AuthenticatorException e) { throw new AuthenticationFailedException(e.getMessage(), e); } } /* (non-Javadoc) * @see org.wso2.carbon.identity.application.authentication.framework * .AbstractApplicationAuthenticator#processAuthenticationResponse(javax.servlet.http.HttpServletRequest, javax * .servlet.http.HttpServletResponse, org.wso2.carbon.identity.application.authentication.framework.context * .AuthenticationContext) */ @Override protected void processAuthenticationResponse(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationContext context) throws AuthenticationFailedException { // String msisdn = httpServletRequest.getParameter("msisdn"); log.info("Processing authentication response"); String sessionDataKey = httpServletRequest.getParameter("sessionDataKey"); String msisdn = (String) context.getProperty("msisdn"); boolean isAuthenticated = false; if (log.isDebugEnabled()) { log.debug("MSISDN : " + msisdn); log.debug("SessionDataKey : " + sessionDataKey); } try { String responseStatus = DBUtils.getUserResponse(sessionDataKey); if (responseStatus.equalsIgnoreCase(UserResponse.APPROVED.toString())) { isAuthenticated = true; } } catch (AuthenticatorException e) { log.error("SMS Authentication failed while trying to authenticate", e); throw new AuthenticationFailedException(e.getMessage(), e); } if (!isAuthenticated) { log.info("Authentication failed. MSISDN doesn't exist."); context.setProperty("faileduser", msisdn); throw new AuthenticationFailedException("Authentication Failed"); } log.info("Authentication success for MSISDN - " + msisdn); context.setProperty("msisdn", msisdn); // AuthenticatedUser user=new AuthenticatedUser(); // context.setSubject(user); AuthenticationContextHelper.setSubject(context, msisdn); String rememberMe = httpServletRequest.getParameter("chkRemember"); if (rememberMe != null && "on".equals(rememberMe)) { context.setRememberMe(true); } } /* (non-Javadoc) * @see org.wso2.carbon.identity.application.authentication.framework.ApplicationAuthenticator#canHandle(javax * .servlet.http.HttpServletRequest) */ @Override public boolean canHandle(HttpServletRequest httpServletRequest) { if (log.isDebugEnabled()) { log.debug("MSS PIN Authenticator canHandle invoked"); } return true; } /* (non-Javadoc) * @see org.wso2.carbon.identity.application.authentication.framework * .ApplicationAuthenticator#getContextIdentifier(javax.servlet.http.HttpServletRequest) */ @Override public String getContextIdentifier(HttpServletRequest httpServletRequest) { return httpServletRequest.getParameter("sessionDataKey"); } /* (non-Javadoc) * @see org.wso2.carbon.identity.application.authentication.framework.ApplicationAuthenticator#getName() */ @Override public String getName() { return Constants.MSS_PIN_AUTHENTICATOR_NAME; } /* (non-Javadoc) * @see org.wso2.carbon.identity.application.authentication.framework.ApplicationAuthenticator#getFriendlyName() */ @Override public String getFriendlyName() { return Constants.MSS_PIN_AUTHENTICATOR_FRIENDLY_NAME; } /* (non-Javadoc) * @see org.wso2.carbon.identity.application.authentication.framework * .AbstractApplicationAuthenticator#retryAuthenticationEnabled() */ @Override protected boolean retryAuthenticationEnabled() { // Setting retry to true as we need the correct continue return true; } /** * Read response entity. * * @param httpResponse the http response * @return the string * @throws IOException Signals that an I/O exception has occurred. */ private String readResponseEntity(HttpResponse httpResponse) throws IOException { String resp = ""; if (httpResponse.getEntity() != null) { BufferedReader rd = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent())); String line; while ((line = rd.readLine()) != null) { resp += line; } } return resp; } /** * The Enum UserResponse. */ private enum UserResponse { /** * The pending. */ PENDING, /** * The approved. */ APPROVED, /** * The rejected. */ REJECTED } }