Advertisement
Guest User

Untitled

a guest
Feb 2nd, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.10 KB | None | 0 0
  1. import java.util.Map;
  2. import org.apache.axis2.client.Options;
  3. import org.apache.axis2.client.ServiceClient;
  4. import org.apache.axis2.context.ConfigurationContext;
  5. import org.apache.axis2.context.ConfigurationContextFactory;
  6. import org.apache.axis2.transport.http.HTTPConstants;
  7. import org.apache.axis2.transport.http.HttpTransportProperties;
  8. import org.apache.http.HttpHeaders;
  9. import org.apache.synapse.ManagedLifecycle;
  10. import org.apache.synapse.MessageContext;
  11. import org.apache.synapse.core.SynapseEnvironment;
  12. import org.apache.synapse.core.axis2.Axis2MessageContext;
  13. import org.apache.synapse.rest.AbstractHandler;
  14. import
  15. org.wso2.carbon.identity.oauth2.stub.OAuth2TokenValidationServiceStub;
  16. import
  17. org.wso2.carbon.identity.oauth2.stub.dto.OAuth2TokenValidationRequestDTO;
  18.  
  19. public class SimpleOauthHandler extends AbstractHandler implements ManagedLifecycle {
  20.  
  21. private String securityHeader = HttpHeaders.AUTHORIZATION;
  22. private String consumerKeyHeaderSegment = "Bearer";
  23. private String oauthHeaderSplitter = ",";
  24. private String consumerKeySegmentDelimiter = " ";
  25. private String oauth2TokenValidationService = "oauth2TokenValidationService";
  26. private String identityServerUserName = "identityServerUserName";
  27. private String identityServerPw = "identityServerPw";
  28.  
  29.  
  30. public boolean handleRequest(MessageContext messageContext) {
  31. try{
  32. ConfigurationContext configCtx = ConfigurationContextFactory.createConfigurationContextFromFileSystem(null, null);
  33. //Read parameters from axis2.xml
  34. String identityServerUrl = messageContext.getConfiguration().getAxisConfiguration().getParameter(oauth2TokenValidationService).getValue().toString();
  35. String username = messageContext.getConfiguration().getAxisConfiguration().getParameter(identityServerUserName).getValue().toString();
  36. String password = messageContext.getConfiguration().getAxisConfiguration().getParameter(identityServerPw).getValue().toString();
  37.  
  38. OAuth2TokenValidationServiceStub stub = new OAuth2TokenValidationServiceStub(configCtx,identityServerUrl);
  39. ServiceClient client = stub._getServiceClient();
  40. Options options = client.getOptions();
  41. HttpTransportProperties.Authenticator authenticator = new HttpTransportProperties.Authenticator();
  42. authenticator.setUsername(username);
  43. authenticator.setPassword(password);
  44. authenticator.setPreemptiveAuthentication(true);
  45.  
  46. options.setProperty(HTTPConstants.AUTHENTICATE, authenticator);
  47. client.setOptions(options);
  48. OAuth2TokenValidationRequestDTO dto = new OAuth2TokenValidationRequestDTO();
  49. dto.setTokenType("bearer");
  50. Map headers = (Map) ((Axis2MessageContext) messageContext).getAxis2MessageContext().
  51. getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
  52. String apiKey = null;
  53. if (headers != null) {
  54. apiKey = extractCustomerKeyFromAuthHeader(headers);
  55. }
  56. dto.setAccessToken(apiKey);
  57. //validate passed apiKey(token)
  58. if(stub.validate(dto).getValid()){
  59. return true;
  60. }else{
  61. return false;
  62. }
  63. }catch(Exception e){
  64. e.printStackTrace();
  65. return false;
  66. }
  67. }
  68.  
  69. public String extractCustomerKeyFromAuthHeader(Map headersMap) {
  70.  
  71. //From 1.0.7 version of this component onwards remove the OAuth authorization header from
  72. // the message is configurable. So we dont need to remove headers at this point.
  73. String authHeader = (String) headersMap.get(securityHeader);
  74. if (authHeader == null) {
  75. return null;
  76. }
  77.  
  78. if (authHeader.startsWith("OAuth ") || authHeader.startsWith("oauth ")) {
  79. authHeader = authHeader.substring(authHeader.indexOf("o"));
  80. }
  81.  
  82. String[] headers = authHeader.split(oauthHeaderSplitter);
  83. if (headers != null) {
  84. for (int i = 0; i < headers.length; i++) {
  85. String[] elements = headers[i].split(consumerKeySegmentDelimiter);
  86. if (elements != null && elements.length > 1) {
  87. int j = 0;
  88. boolean isConsumerKeyHeaderAvailable = false;
  89. for (String element : elements) {
  90. if (!"".equals(element.trim())) {
  91. if (consumerKeyHeaderSegment.equals(elements[j].trim())) {
  92. isConsumerKeyHeaderAvailable = true;
  93. } else if (isConsumerKeyHeaderAvailable) {
  94. return removeLeadingAndTrailing(elements[j].trim());
  95. }
  96. }
  97. j++;
  98. }
  99. }
  100. }
  101. }
  102. return null;
  103. }
  104.  
  105. private String removeLeadingAndTrailing(String base) {
  106. String result = base;
  107.  
  108. if (base.startsWith(""") || base.endsWith(""")) {
  109. result = base.replace(""", "");
  110. }
  111. return result.trim();
  112. }
  113.  
  114.  
  115. public boolean handleResponse(MessageContext messageContext) {
  116. return true;
  117. }
  118.  
  119. public void init(SynapseEnvironment synapseEnvironment) {
  120. //To change body of implemented methods use File | Settings | File Templates.
  121. }
  122.  
  123.  
  124. public void destroy() {
  125. //To change body of implemented methods use File | Settings | File Templates.
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement