简体   繁体   English

从 cognito 获取令牌并在 wso2 Enterprise Intigrator 中缓存

[英]getting token from cognito and caching in wso2 Enterprise Intigrator

I need to get a token from Cognito but I don't know how to get it through enterprise integrator.我需要从 Cognito 获取令牌,但我不知道如何通过企业集成商获取它。 I have client_id and client_secet and endpoint also.我也有client_idclient_secet和端点。 But I 'm not able to get the token seems I'm not calling the endpoint properly with proper parameter.但是我无法获得令牌似乎我没有使用正确的参数正确调用端点。

After getting the token I need to cache it till the token expires so I don't have to call again and again获得令牌后,我需要缓存它直到令牌过期,这样我就不必一次又一次地调用

here is the code I'm trying这是我正在尝试的代码

<cache id="cache-sample" scope="per-host" collector="false" hashGenerator="org.wso2.carbon.mediator.cache.digest.DOMHASHGenerator" timeout="5000">
    <implementation type="memory" maxSize="1000"/>
</cache>
<header name="client_id" scope="transport" value="XXXXXXXXXXXXXXXXX"/>
<header name="client_secret" scope="transport" value="XXXXXXXXXXXXXXXXXXX"/>
<header name="Content-Type" scope="transport" value="application/json"/>
<property name="DISABLE_CHUNKING" scope="axis2" type="STRING" value="true"/>
<call>
    <endpoint>
       <address uri="https://xcxxxxxxxxxx.auth.us-east-1.amazoncognito.com/oauth2/token"/>
    </endpoint>
</call>
<property name="RESPONSE" value="true" scope="default" type="STRING"/>
<cache scope="per-host" collector="true"/>
<respond/>
<?xml version="1.0" encoding="UTF-8"?>
<api context="/api/myService" name="myService-api" xmlns="http://ws.apache.org/ns/synapse">
    <resource methods="POST" uri-template="/getToken">
        <inSequence>
            <script language="js"><![CDATA[var payload = mc.getPayloadXML();
                var log = mc.getServiceLog();
                var client_id = payload..*::client_id.toString();
                var client_secret = payload..*::client_secret.toString();
                mc.setProperty("client_id", client_id);
                mc.setProperty("client_secret", client_secret);]]>
            </script>
            <payloadFactory media-type="json">
                <format/>
                <args/>
            </payloadFactory>
            <property name="ContentType" scope="axis2" type="STRING" value="application/x-www-form-urlencoded"/>
            <property expression="fn:concat($ctx:client_id,':',$ctx:client_secret)" name="credentials" scope="default" type="STRING"/>
            <property expression="fn:concat('Basic ', base64Encode($ctx:credentials))" name="Authorization" scope="transport" type="STRING" xmlns:ns="http://org.apache.synapse/xsd"/>
            <property name="FORCE_POST_PUT_NOBODY" scope="axis2" type="BOOLEAN" value="true"/>
            <property name="DISABLE_CHUNKING" scope="axis2" type="STRING" value="true"/>
            <cache 
                collector="false" 
                hashGenerator="org.wso2.carbon.mediator.cache.digest.DOMHASHGenerator" 
                id="cognito-token" 
                scope="per-host" 
                timeout="3600">
                <onCacheHit>
                    <log level="custom">
                        <property expression="/" name="[usage-service-api]:: CHACHE HIT"/>
                    </log>
                    <respond/>
                </onCacheHit>
                <implementation maxSize="50" type="memory"/>
            </cache>
            <call>
                <endpoint>
                    <http method="post" uri-template="https://xxxxxxxxxxx.amazoncognito.com/oauth2/token?grant_type=client_credentials"/>
                </endpoint>
            </call>
            <property name="RESPONSE" scope="default" type="STRING" value="true"/>
            <cache collector="true" id="cognito-token" scope="per-host"/>
            <respond/>
        </inSequence>
        <outSequence/>
        <faultSequence/>
    </resource>
</api>

For first Call cache miss will happen in that case it will call the endpoint and store it in the cache for the second call and onwards till the time expires for the same call it will hit the cache ie Cache hit then It will respond from onCacheHit mediator对于第一次调用缓存未命中,在这种情况下,它将调用端点并将其存储在缓存中以用于第二次调用,直到相同调用的时间到期,它将命中缓存,即缓存命中,然后它将从onCacheHit中介器响应

<onCacheHit>
    <respond/>
</onCacheHit>

and

But after cache hit it will skip any execution of any statement written after cache Mediator.但是在缓存命中后,它将跳过cache中介器之后写入的任何语句的任何执行。 So So keep it in some other sequence if you are writing in some sequence You can have more than one resources in API.所以所以如果你按某种顺序编写,请保持其他顺序你可以在 API 中拥有多个resources

If you want to know how to call this API then see this getting cognito acces_token with wso2 esb如果您想知道如何调用此 API,请参阅使用 wso2 esb 获取 cognito acces_token

<log level="custom">
    <property expression="/" name="[usage-service-api]:: CHACHE HIT"/>
</log>

The above block of code logs the content of the cache on CACHE HIT上面的代码块将缓存的内容记录在CACHE HIT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM