How to set tenant in header in mediator using WSO2 API Manager

I have an API that requires a tenant as a header.

If I create a custom sequence:

<sequence name="WSO2AM--Ext--In"> <header name="X-Tenant-Id" scope="transport" action="set" expression="???????????????????" /> </sequence> 

Is there an expression that I can use to achieve this? Or should I resort to creating a media intermediary for the API?

PS: Looking at the WSO2 source code ( CarbonTenantInfoConfigurator.java ), I found this snippet that may be useful as a hint:

 PrivilegedCarbonContext cc = PrivilegedCarbonContext.getThreadLocalCarbonContext(); String tenantDomain = cc.getTenantDomain(); int tenantId = cc.getTenantId(); messageContext.setProperty("tenant.info.domain", tenantDomain); messageContext.setProperty("tenant.info.id", tenantId); 

But I do not know how to access these properties in a user sequence, if possible.

+7
wso2 wso2-am wso2carbon
source share
2 answers

After checking the output of ApiManager debugging, I noticed that user sequences are executed immediately after the handlers. Fortunately, the OAuthAuthenticator class (used by APIAuthenticationHandler) sets some convenient properties, such as END_USER_NAME and APPLICATION_NAME .

END_USER_NAME contains the name and tenant of the caller ( user@tenant.com ).

This custom sequence worked for me:

 <sequence name="add_service_header" trace="enable" statistics="enable" xmlns="http://ws.apache.org/ns/synapse"> <log/> <property name="tenant" expression="fn:substring-after(get-property('END_USER_NAME'), '@')" /> <header name="X-Tenant" scope="transport" expression="get-property('tenant')"/> <header name="X-AppName" scope="transport" expression="get-property('APPLICATION_NAME')"/> </sequence> 

I could not find the documentation for a property other than the source code, and this other question

+3
source share

As shown in the code, they are configured for the MessageContext synapse. You can get these properties using the following expressions.

Get property ('tenant.info.domain')

Get property ('tenant.info.id')

thanks

Tishan

0
source share

All Articles