Created
February 8, 2014 14:13
-
-
Save bhathiya/8884367 to your computer and use it in GitHub Desktop.
Simple OAuth Handler for WSO2 ESB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package org.wso2.handler; | |
| import org.apache.axis2.client.Options; | |
| import org.apache.axis2.client.ServiceClient; | |
| import org.apache.axis2.context.ConfigurationContext; | |
| import org.apache.axis2.context.ConfigurationContextFactory; | |
| import org.apache.axis2.transport.http.HTTPConstants; | |
| import org.apache.axis2.transport.http.HttpTransportProperties; | |
| import org.apache.http.HttpHeaders; | |
| import org.apache.synapse.core.axis2.Axis2MessageContext; | |
| import org.wso2.carbon.identity.oauth2.stub.OAuth2TokenValidationServiceStub; | |
| import org.wso2.carbon.identity.oauth2.stub.dto.OAuth2TokenValidationRequestDTO; | |
| import org.apache.synapse.ManagedLifecycle; | |
| import org.apache.synapse.MessageContext; | |
| import org.apache.synapse.core.SynapseEnvironment; | |
| import org.apache.synapse.rest.AbstractHandler; | |
| import org.wso2.carbon.identity.oauth2.stub.dto.OAuth2TokenValidationRequestDTO_OAuth2AccessToken; | |
| import java.rmi.RemoteException; | |
| import java.util.Map; | |
| public class SimpleOauthHandler extends AbstractHandler implements ManagedLifecycle { | |
| private static final String securityHeader = HttpHeaders.AUTHORIZATION; | |
| private static final String consumerKeyHeaderSegment = "Bearer"; | |
| private static final String oauthHeaderSplitter = ","; | |
| private static final String consumerKeySegmentDelimiter = " "; | |
| private static final String oauth2TokenValidationService = "oauth2TokenValidationService"; | |
| private static final String identityServerUserName = "identityServerUserName"; | |
| private static final String identityServerPw = "identityServerPw"; | |
| private static final String BEARER_TOKEN_TYPE = "bearer"; | |
| @Override | |
| public boolean handleRequest(MessageContext messageContext) { | |
| try { | |
| ConfigurationContext configCtx = ConfigurationContextFactory.createConfigurationContextFromFileSystem(null, null); | |
| //Read parameters from axis2.xml | |
| String identityServerUrl = messageContext.getConfiguration().getAxisConfiguration().getParameter(oauth2TokenValidationService).getValue().toString(); | |
| String username = messageContext.getConfiguration().getAxisConfiguration().getParameter(identityServerUserName).getValue().toString(); | |
| String password = messageContext.getConfiguration().getAxisConfiguration().getParameter(identityServerPw).getValue().toString(); | |
| OAuth2TokenValidationServiceStub stub = new OAuth2TokenValidationServiceStub(configCtx, identityServerUrl); | |
| ServiceClient client = stub._getServiceClient(); | |
| Options options = client.getOptions(); | |
| HttpTransportProperties.Authenticator authenticator = new HttpTransportProperties.Authenticator(); | |
| authenticator.setUsername(username); | |
| authenticator.setPassword(password); | |
| authenticator.setPreemptiveAuthentication(true); | |
| options.setProperty(HTTPConstants.AUTHENTICATE, authenticator); | |
| client.setOptions(options); | |
| Map headers = (Map) ((Axis2MessageContext) messageContext).getAxis2MessageContext(). | |
| getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS); | |
| String apiKey = null; | |
| if (headers != null) { | |
| apiKey = extractCustomerKeyFromAuthHeader(headers); | |
| } | |
| OAuth2TokenValidationRequestDTO oauthReq = new OAuth2TokenValidationRequestDTO(); | |
| OAuth2TokenValidationRequestDTO_OAuth2AccessToken accessToken = | |
| new org.wso2.carbon.identity.oauth2.stub.dto.OAuth2TokenValidationRequestDTO_OAuth2AccessToken(); | |
| accessToken.setTokenType(BEARER_TOKEN_TYPE); | |
| accessToken.setIdentifier(apiKey); | |
| oauthReq.setAccessToken(accessToken); | |
| try { | |
| return stub.validate(oauthReq).getValid(); | |
| } catch (RemoteException e) { | |
| throw new Exception("Error while validating OAuth2 request", e); | |
| } | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| return false; | |
| } | |
| } | |
| public String extractCustomerKeyFromAuthHeader(Map headersMap) { | |
| String authHeader = (String) headersMap.get(securityHeader); | |
| if (authHeader == null) { | |
| return null; | |
| } | |
| if (authHeader.startsWith("OAuth ") || authHeader.startsWith("oauth ")) { | |
| authHeader = authHeader.substring(authHeader.indexOf("o")); | |
| } | |
| String[] headers = authHeader.split(oauthHeaderSplitter); | |
| if (headers != null) { | |
| for (int i = 0; i < headers.length; i++) { | |
| String[] elements = headers[i].split(consumerKeySegmentDelimiter); | |
| if (elements != null && elements.length > 1) { | |
| int j = 0; | |
| boolean isConsumerKeyHeaderAvailable = false; | |
| for (String element : elements) { | |
| if (!"".equals(element.trim())) { | |
| if (consumerKeyHeaderSegment.equals(elements[j].trim())) { | |
| isConsumerKeyHeaderAvailable = true; | |
| } else if (isConsumerKeyHeaderAvailable) { | |
| return removeLeadingAndTrailing(elements[j].trim()); | |
| } | |
| } | |
| j++; | |
| } | |
| } | |
| } | |
| } | |
| return null; | |
| } | |
| private String removeLeadingAndTrailing(String base) { | |
| String result = base; | |
| if (base.startsWith("\"") || base.endsWith("\"")) { | |
| result = base.replace("\"", ""); | |
| } | |
| return result.trim(); | |
| } | |
| @Override | |
| public boolean handleResponse(MessageContext messageContext) { | |
| return true; | |
| } | |
| @Override | |
| public void init(SynapseEnvironment synapseEnvironment) { | |
| //To change body of implemented methods use File | Settings | File Templates. | |
| } | |
| @Override | |
| public void destroy() { | |
| //To change body of implemented methods use File | Settings | File Templates. | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment