如何通过 WSO2-API 管理器传递 CKAN API 授权令牌

How to pass CKAN API authorization token via the WSO2-API Manager

我正在使用 WSO2 API Manager(版本 3.0.1)作为前端,通过 API 调用 CKAN(版本 2.8.2)来访问数据。

私有 CKAN 数据集需要授权令牌,如 here 所述。

"When calling an API function that requires authorization, you must authenticate yourself by providing your API key with your HTTP request."

这在 WSO2 中是如何实现的?具体来说,需要更改什么配置 files/settings 才能实现此目的?

我从 this documentation 中了解到,如果使用工具插件在 Eclipse 中进行配置,则可以通过以下方式完成(步骤 23):

curl -k -H "Authorization: Bearer api-key-for-WSO2-goes-here" -H "Custom: Bearer api-key-for-CKAN-goes-here" https://my-wso2-host-goes-here:8243/test/1.0.0

但是,这些说明需要使用 Eclipse。但是如果没有 Eclipse,这怎么能完成呢? IE。需要在服务器或 WSO2 API 发布者 and/or WSO2 API 开发门户中修改哪些配置 files/settings 以通过 WSO2 传递 CKAN 的授权令牌 API-M?

您不必为此使用 Eclipse。在本教程中,Eclispse 插件用作轻松生成序列的工具。在那个教程中我们需要一个序列 首先是因为后端需要的授权header是"Authorization"。在 WSO2 APIM 中,这是一个保留的 header 以传递内部生成的令牌。因此,我们首先使用不同的 header 名称在自定义 header 中传递后端令牌,然后将此值复制到 in-sequence 中的授权 header。 该序列如下所示。

<sequence xmlns="http://ws.apache.org/ns/synapse" name="authorization_header_exchange">
<property name="X-Authorization" expression="get-property('transport', 'X-Authorization')" scope="default" type="STRING"/>
<property name="Authorization" expression="get-property('X-Authorization')" scope="transport" type="STRING" description=""/>
<property name="X-Authorization" scope="transport" action="remove"/>

参考[1]了解更多信息。

然而,在您的情况下,您可以在 X-CKAN-API-Key 中发送 api 密钥以及请求本身,而无需使用中介序列。

[1]。 https://docs.wso2.com/display/APICloud/Sample+Mediation+Sequences#SampleMediationSequences-Passinganauthorizationheadertoyourbackend

我在回答我自己的问题...

TLDR

上面@naoko 的回答是正确的:通过 WSO2 传递 CKAN 授权 API-M 包括 X-CKAN-API-Key 作为 header 并将值设置为您的 CKAN 用户的私有 CKAN API键。

长版

像这样传递 CKAN API 密钥:

curl -k -H "Authorization: Bearer wso2-app-key-here" -H "X-CKAN-API-Key: ckan-authorization-key-here" https://myWso2DeveloperPortal.com:8243/daas/3.0.1/action/resource_show?id=resource-id-of-CKAN-dataset-here

(如果主机有 self-signed https 证书,则使用 -k

在哪里...

  • wso2-app-key-here 是在 WSO2 开发者门户中找到的应用程序密钥。
  • ckan-authorization-key-here 是您的用户账号在 CKAN 中的私钥。 (可以在 CKAN UI 的用户个人资料页面上找到它。)
  • resource-id-of-CKAN-dataset-here是你要查询的数据集的资源id。

此示例中的 resource_show 方法将为给定的 CKAN 资源 return 元数据。以类似的方式调用其他 CKAN 方法。

这都要感谢 CKAN 提供了一种替代方法,可以将密钥传递到名为 Authorize 的 header 中。在 CKAN 的情况下,可以使用变量 X-CKAN-API-Key。而且这个可以很容易的通过如上图

一开始就没有在 CKAN 文档中捕捉到它(它就在我自己上面的屏幕截图中!)...如果我仔细阅读它会保存一个 SO post好还是坏;)

如果 CKAN 没有提供 X-CKAN-API-Key 的替代方案,那么这可以在 3.0.0 版中完成,如这些页面所述:

"Passing a Custom Authorization Token to the Backend"

https://apim.docs.wso2.com/en/latest/Learn/APIGateway/MessageMediation/passing-a-custom-authorization-token-to-the-backend/

FWIW,我实际上在尝试 X-CKAN-API-Key 解决方案之前尝试过它,但它没有用。也许我做错了什么。但由于 X-CKAN-API-Key 解决方案对我有效,所以我称之为完成。