use of org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext in project carbon-apimgt by wso2.
the class TokenPasser method mediate.
public boolean mediate(MessageContext synCtx) {
AuthenticationContext authContext = APISecurityUtils.getAuthenticationContext(synCtx);
addHTTPHeader(synCtx, authContext);
return true;
}
use of org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext in project carbon-apimgt by wso2.
the class SubscriberInfoLoader method handleThrottle.
private boolean handleThrottle(WebhooksDTO subscriber, MessageContext messageContext) {
AuthenticationContext authContext = new AuthenticationContext();
populateAuthContext(subscriber.getTenantDomain(), Integer.parseInt(subscriber.getAppID()), authContext);
messageContext.setProperty(APISecurityUtils.API_AUTH_CONTEXT, authContext);
if (subscriber.isThrottled()) {
if (APIUtil.isAnalyticsEnabled()) {
String errorMessage = "Message throttled out";
String errorDescription = "You have exceeded your quota";
int errorCode = APIThrottleConstants.EVENTS_COUNT_THROTTLE_OUT_ERROR_CODE;
messageContext.setProperty(SynapseConstants.ERROR_CODE, errorCode);
messageContext.setProperty(SynapseConstants.ERROR_MESSAGE, errorMessage);
messageContext.setProperty(SynapseConstants.ERROR_DETAIL, errorDescription);
messageContext.setProperty(Constants.BACKEND_RESPONSE_CODE, APIThrottleConstants.SC_TOO_MANY_REQUESTS);
((Axis2MessageContext) messageContext).getAxis2MessageContext().setProperty(SynapseConstants.HTTP_SC, APIThrottleConstants.SC_TOO_MANY_REQUESTS);
WebhooksUtils.publishAnalyticsData(messageContext);
}
return false;
}
if (doThrottle(subscriber, messageContext, authContext)) {
messageContext.setProperty(APIConstants.Webhooks.SUBSCRIBER_CALLBACK_PROPERTY, subscriber.getCallbackURL());
String errorMessage = "Message throttled out";
String errorDescription = "You have exceeded your quota";
int errorCode = APIThrottleConstants.EVENTS_COUNT_THROTTLE_OUT_ERROR_CODE;
int httpErrorCode = APIThrottleConstants.SC_TOO_MANY_REQUESTS;
messageContext.setProperty(SynapseConstants.ERROR_CODE, errorCode);
messageContext.setProperty(SynapseConstants.ERROR_MESSAGE, errorMessage);
messageContext.setProperty(SynapseConstants.ERROR_DETAIL, errorDescription);
messageContext.setProperty(APIMgtGatewayConstants.HTTP_RESPONSE_STATUS_CODE, httpErrorCode);
org.apache.axis2.context.MessageContext axis2MC = ((Axis2MessageContext) messageContext).getAxis2MessageContext();
// This property need to be set to avoid sending the content in pass-through pipe (request message)
// as the response.
axis2MC.setProperty(PassThroughConstants.MESSAGE_BUILDER_INVOKED, Boolean.TRUE);
try {
RelayUtils.consumeAndDiscardMessage(axis2MC);
} catch (AxisFault axisFault) {
// In case of an error it is logged and the process is continued because we're setting a fault message
// in the payload.
log.error("Error occurred while consuming and discarding the message", axisFault);
}
if (APIUtil.isAnalyticsEnabled()) {
messageContext.setProperty(Constants.BACKEND_RESPONSE_CODE, httpErrorCode);
((Axis2MessageContext) messageContext).getAxis2MessageContext().setProperty(SynapseConstants.HTTP_SC, APIThrottleConstants.SC_TOO_MANY_REQUESTS);
WebhooksUtils.publishAnalyticsData(messageContext);
}
Mediator sequence = messageContext.getSequence(APIThrottleConstants.API_THROTTLE_OUT_HANDLER);
// Invoke the custom error handler specified by the user
if (sequence != null && !sequence.mediate(messageContext)) {
// logic from getting executed
return true;
}
Utils.sendFault(messageContext, httpErrorCode);
}
return true;
}
use of org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext in project carbon-apimgt by wso2.
the class SubscriberInfoLoader method populateAuthContext.
private void populateAuthContext(String tenantDomain, int appId, AuthenticationContext authContext) {
Application app = SubscriptionDataHolder.getInstance().getTenantSubscriptionStore(tenantDomain).getApplicationById(appId);
authContext.setApplicationUUID(app.getUUID());
authContext.setApplicationName(app.getName());
authContext.setSubscriber(app.getSubName());
authContext.setKeyType(app.getTokenType());
}
use of org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext in project carbon-apimgt by wso2.
the class SubscribersPersistMediator method mediate.
@Override
public boolean mediate(MessageContext messageContext) {
try {
Map<String, String> queryParams = populateQueryParamData(messageContext);
if (queryParams.isEmpty()) {
populateException("Query params must present in the request", messageContext);
}
String callback = queryParams.get(APIConstants.Webhooks.HUB_CALLBACK_QUERY_PARAM);
String topicName = queryParams.get(APIConstants.Webhooks.HUB_TOPIC_QUERY_PARAM);
String mode = queryParams.get(APIConstants.Webhooks.HUB_MODE_QUERY_PARAM);
String secret = queryParams.get(APIConstants.Webhooks.HUB_SECRET_QUERY_PARAM);
String leaseSeconds = queryParams.get(APIConstants.Webhooks.HUB_LEASE_SECONDS_QUERY_PARAM);
messageContext.setProperty(Constants.SKIP_DEFAULT_METRICS_PUBLISHING, true);
org.apache.axis2.context.MessageContext axisCtx = ((Axis2MessageContext) messageContext).getAxis2MessageContext();
axisCtx.setProperty(PassThroughConstants.SYNAPSE_ARTIFACT_TYPE, APIConstants.API_TYPE_WEBSUB);
if (StringUtils.isEmpty(callback)) {
populateException("Callback URL cannot be empty", messageContext);
}
if (StringUtils.isEmpty(mode)) {
populateException("Mode cannot be empty", messageContext);
} else if (!(APIConstants.Webhooks.SUBSCRIBE_MODE.equalsIgnoreCase(mode.trim()) || APIConstants.Webhooks.UNSUBSCRIBE_MODE.equalsIgnoreCase(mode.trim()))) {
populateException("Invalid Entry for hub.mode", messageContext);
}
AuthenticationContext authenticationContext = APISecurityUtils.getAuthenticationContext(messageContext);
String tenantDomain = (String) messageContext.getProperty(APIConstants.TENANT_DOMAIN_INFO_PROPERTY);
int tenantID = (Integer) messageContext.getProperty(APIConstants.TENANT_ID_INFO_PROPERTY);
String apiKey = WebhooksUtils.generateAPIKey(messageContext, tenantDomain);
String apiContext = (String) messageContext.getProperty(RESTConstants.REST_API_CONTEXT);
String apiVersion = (String) messageContext.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION);
String applicationID = (String) messageContext.getProperty(APIMgtGatewayConstants.APPLICATION_ID);
if (APIConstants.Webhooks.SUBSCRIBE_MODE.equalsIgnoreCase(mode) && isThrottled(applicationID, apiKey, tenantDomain)) {
WebhooksUtils.handleThrottleOutMessage(messageContext);
return false;
}
String jsonString = generateRequestBody(callback, topicName, mode, secret, leaseSeconds, apiKey, apiContext, apiVersion, applicationID, tenantDomain, tenantID, authenticationContext);
HttpResponse httpResponse = WebhooksUtils.persistData(jsonString, subscriptionDataPersisRetries, APIConstants.Webhooks.SUBSCRIPTION_EVENT_TYPE);
handleResponse(httpResponse, messageContext);
} catch (URISyntaxException | InterruptedException | IOException e) {
messageContext.setProperty(SynapseConstants.ERROR_CODE, HttpStatus.SC_INTERNAL_SERVER_ERROR);
messageContext.setProperty(SynapseConstants.ERROR_MESSAGE, "Error while persisting request");
messageContext.setProperty(SynapseConstants.ERROR_DETAIL, "Error while persisting request");
Mediator sequence = messageContext.getSequence(APISecurityConstants.BACKEND_AUTH_FAILURE_HANDLER);
if (sequence != null && !sequence.mediate(messageContext)) {
return true;
}
WebhooksUtils.sendFault(messageContext, HttpStatus.SC_INTERNAL_SERVER_ERROR);
}
return true;
}
use of org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext in project carbon-apimgt by wso2.
the class ThrottleDataPublisher method publishNonThrottledEvent.
/**
* This method used to pass message context and let it run within separate thread.
*
* @param messageContext is message context object that holds
*/
public void publishNonThrottledEvent(String applicationLevelThrottleKey, String applicationLevelTier, String apiLevelThrottleKey, String apiLevelTier, String subscriptionLevelThrottleKey, String subscriptionLevelTier, String resourceLevelThrottleKey, String resourceLevelTier, String authorizedUser, String apiContext, String apiVersion, String appTenant, String apiTenant, String appId, MessageContext messageContext, AuthenticationContext authenticationContext) {
try {
if (dataPublisherPool != null) {
DataProcessAndPublishingAgent agent = dataPublisherPool.get();
agent.setDataReference(applicationLevelThrottleKey, applicationLevelTier, apiLevelThrottleKey, apiLevelTier, subscriptionLevelThrottleKey, subscriptionLevelTier, resourceLevelThrottleKey, resourceLevelTier, authorizedUser, apiContext, apiVersion, appTenant, apiTenant, appId, messageContext, authenticationContext);
if (log.isDebugEnabled()) {
log.debug("Publishing throttle data from gateway to traffic-manager for: " + apiContext + " with ID: " + messageContext.getMessageID() + " started" + " at " + new SimpleDateFormat("[yyyy.MM.dd HH:mm:ss,SSS zzz]").format(new Date()));
}
executor.execute(agent);
if (log.isDebugEnabled()) {
log.debug("Publishing throttle data from gateway to traffic-manager for: " + apiContext + " with ID: " + messageContext.getMessageID() + " ended" + " at " + new SimpleDateFormat("[yyyy.MM.dd HH:mm:ss,SSS zzz]").format(new Date()));
}
} else {
log.debug("Throttle data publisher pool is not initialized.");
}
} catch (Exception e) {
log.error("Error while publishing throttling events to global policy server", e);
}
}
Aggregations