|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.synapse.rest.cors; |
| 21 | + |
| 22 | +import org.apache.commons.logging.Log; |
| 23 | +import org.apache.commons.logging.LogFactory; |
| 24 | +import org.apache.http.HttpStatus; |
| 25 | +import org.apache.synapse.MessageContext; |
| 26 | +import org.apache.synapse.core.axis2.Axis2MessageContext; |
| 27 | +import org.apache.synapse.rest.RESTConstants; |
| 28 | +import org.apache.synapse.transport.passthru.PassThroughConstants; |
| 29 | + |
| 30 | +import java.util.Map; |
| 31 | +import java.util.Set; |
| 32 | + |
| 33 | +/** |
| 34 | + * This class provides util functions for all CORS related activities. |
| 35 | + */ |
| 36 | +public class CORSHelper { |
| 37 | + |
| 38 | + private static final Log log = LogFactory.getLog(CORSHelper.class); |
| 39 | + |
| 40 | + /** |
| 41 | + * Function to retrieve allowed origin header string |
| 42 | + * |
| 43 | + * @param origin Received origin |
| 44 | + * @param allowedOrigins allowed origin set |
| 45 | + * @return |
| 46 | + */ |
| 47 | + public static String getAllowedOrigins(String origin, Set<String> allowedOrigins) { |
| 48 | + |
| 49 | + if (allowedOrigins.contains("*")) { |
| 50 | + return "*"; |
| 51 | + } else if (allowedOrigins.contains(origin)) { |
| 52 | + return origin; |
| 53 | + } else { |
| 54 | + return ""; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Functions to handle CORS Headers |
| 60 | + * |
| 61 | + * @param synCtx Synapse message context |
| 62 | + * @param corsConfiguration of the API |
| 63 | + * @param supportedMethods |
| 64 | + * @param updateHeaders Boolean |
| 65 | + */ |
| 66 | + public static void handleCORSHeaders(CORSConfiguration corsConfiguration, MessageContext synCtx, |
| 67 | + String supportedMethods, boolean updateHeaders) { |
| 68 | + |
| 69 | + if (corsConfiguration.isEnabled()) { |
| 70 | + org.apache.axis2.context.MessageContext msgCtx = ((Axis2MessageContext) synCtx).getAxis2MessageContext(); |
| 71 | + Map<String, String> transportHeaders = (Map<String, String>) msgCtx.getProperty( |
| 72 | + org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS); |
| 73 | + if (transportHeaders != null) { |
| 74 | + String allowedOrigin = getAllowedOrigins(transportHeaders.get(RESTConstants.CORS_HEADER_ORIGIN), |
| 75 | + corsConfiguration.getAllowedOrigins()); |
| 76 | + if (updateHeaders) { |
| 77 | + transportHeaders.put(RESTConstants.CORS_HEADER_ACCESS_CTL_ALLOW_METHODS, supportedMethods); |
| 78 | + transportHeaders.put(RESTConstants.CORS_HEADER_ACCESS_CTL_ALLOW_ORIGIN, allowedOrigin); |
| 79 | + transportHeaders.put(RESTConstants.CORS_HEADER_ACCESS_CTL_ALLOW_HEADERS, |
| 80 | + corsConfiguration.getAllowedHeaders()); |
| 81 | + } |
| 82 | + |
| 83 | + synCtx.setProperty(RESTConstants.INTERNAL_CORS_HEADER_ACCESS_CTL_ALLOW_METHODS, supportedMethods); |
| 84 | + synCtx.setProperty(RESTConstants.INTERNAL_CORS_HEADER_ACCESS_CTL_ALLOW_ORIGIN, allowedOrigin); |
| 85 | + synCtx.setProperty(RESTConstants.INTERNAL_CORS_HEADER_ACCESS_CTL_ALLOW_HEADERS, |
| 86 | + corsConfiguration.getAllowedHeaders()); |
| 87 | + synCtx.setProperty(RESTConstants.INTERNAL_CORS_HEADER_ORIGIN, |
| 88 | + transportHeaders.get(RESTConstants.CORS_HEADER_ORIGIN)); |
| 89 | + |
| 90 | + // If the request origin is not allowed, set the status code to 403 |
| 91 | + if (isOptionsRequest(synCtx) && allowedOrigin.isEmpty()) { |
| 92 | + ((Axis2MessageContext) synCtx).getAxis2MessageContext() |
| 93 | + .setProperty(PassThroughConstants.HTTP_SC, HttpStatus.SC_FORBIDDEN); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Function to set CORS headers to response message transport headers extracting from synapse message context |
| 102 | + * |
| 103 | + * @param synCtx |
| 104 | + * @param corsConfiguration of the API |
| 105 | + */ |
| 106 | + public static void handleCORSHeadersForResponse(CORSConfiguration corsConfiguration, MessageContext synCtx) { |
| 107 | + |
| 108 | + if (corsConfiguration.isEnabled()) { |
| 109 | + org.apache.axis2.context.MessageContext msgCtx = ((Axis2MessageContext) synCtx).getAxis2MessageContext(); |
| 110 | + Map<String, String> transportHeaders = (Map<String, String>) msgCtx.getProperty( |
| 111 | + org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS); |
| 112 | + if (transportHeaders != null) { |
| 113 | + if (synCtx.getProperty(RESTConstants.INTERNAL_CORS_HEADER_ACCESS_CTL_ALLOW_METHODS) != null) { |
| 114 | + transportHeaders.put(RESTConstants.CORS_HEADER_ACCESS_CTL_ALLOW_METHODS, |
| 115 | + (String) synCtx.getProperty(RESTConstants.INTERNAL_CORS_HEADER_ACCESS_CTL_ALLOW_METHODS)); |
| 116 | + } |
| 117 | + |
| 118 | + if (synCtx.getProperty(RESTConstants.INTERNAL_CORS_HEADER_ACCESS_CTL_ALLOW_ORIGIN) != null) { |
| 119 | + transportHeaders.put(RESTConstants.CORS_HEADER_ACCESS_CTL_ALLOW_ORIGIN, |
| 120 | + (String) synCtx.getProperty(RESTConstants.INTERNAL_CORS_HEADER_ACCESS_CTL_ALLOW_ORIGIN)); |
| 121 | + } |
| 122 | + |
| 123 | + if (synCtx.getProperty(RESTConstants.INTERNAL_CORS_HEADER_ACCESS_CTL_ALLOW_HEADERS) != null) { |
| 124 | + transportHeaders.put(RESTConstants.CORS_HEADER_ACCESS_CTL_ALLOW_HEADERS, |
| 125 | + (String) synCtx.getProperty(RESTConstants.INTERNAL_CORS_HEADER_ACCESS_CTL_ALLOW_HEADERS)); |
| 126 | + } |
| 127 | + |
| 128 | + if (synCtx.getProperty(RESTConstants.INTERNAL_CORS_HEADER_ORIGIN) != null) { |
| 129 | + transportHeaders.put(RESTConstants.CORS_HEADER_ORIGIN, |
| 130 | + (String) synCtx.getProperty(RESTConstants.INTERNAL_CORS_HEADER_ORIGIN)); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + private static boolean isOptionsRequest(MessageContext synCtx) { |
| 137 | + |
| 138 | + String method = (String) synCtx.getProperty(RESTConstants.REST_METHOD); |
| 139 | + return RESTConstants.METHOD_OPTIONS.equals(method); |
| 140 | + } |
| 141 | +} |
0 commit comments