azure - 使用 Azure API 管理和第三方授权服务器验证传入请求中的授权 token

标签 azure oauth-2.0 authorization azure-active-directory azure-api-management

我必须在我的项目中使用第三方授权服务器实现 OAuth 2.0。我的客户端和服务器都已经在AS上注册了。我已在 Azure 上创建了一个 API 管理实例并导入了 swagger API。我希望每个传入请求都针对我的 AS 进行验证,因此我只需将请求重定向到 https://my-as.com/as/introspect.oauth2 ,并验证 token 。如果 token 有效,则让它继续,否则发送 401。我试图使用“入站处理”来实现此目的,并引用以下文档: https://learn.microsoft.com/en-us/azure/api-management/api-management-howto-protect-backend-with-aad#configure-a-jwt-validation-policy-to-pre-authorize-requests .

唯一的问题是我使用的是第三方 AS,而不是 Azure AD。我尝试用我的 URL 替换示例 XML 代码中的 URL,但它不起作用。

如何将请求重定向到授权服务器以验证访问 token ?

最佳答案

添加以下入站策略有效:

<inbound>
        <!-- Extract Token from Authorization header parameter -->
        <set-variable name="token" value="@(context.Request.Headers.GetValueOrDefault("Authorization","scheme param").Split(' ').Last())" />
        <!-- Send request to Token Server to validate token (see RFC 7662) -->
        <send-request mode="new" response-variable-name="tokenstate" timeout="20" ignore-error="true">
            <set-url>https://my-as.com/as/introspect.oauth2</set-url>
            <set-method>POST</set-method>
            <set-header name="Content-Type" exists-action="override">
                <value>application/x-www-form-urlencoded</value>
            </set-header>
            <set-body>@($"grant_type=urn:pingidentity.com:oauth2:grant_type:validate_bearer&client_id=UoM&client_secret=somesecret&token={(string)context.Variables["token"]}")</set-body>
        </send-request>
        <choose>
            <!-- Check active property in response -->
            <when condition="@((bool)((IResponse)context.Variables["tokenstate"]).Body.As<JObject>()["active"] == false)">
                <!-- Return 401 Unauthorized with http-problem payload -->
                <return-response response-variable-name="existing response variable">
                    <set-status code="401" reason="Unauthorized" />
                    <set-header name="WWW-Authenticate" exists-action="override">
                        <value>Bearer error="invalid_token"</value>
                    </set-header>
                </return-response>
            </when>
        </choose>
        <base />
    </inbound>

关于azure - 使用 Azure API 管理和第三方授权服务器验证传入请求中的授权 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54624213/

相关文章:

azure - 如何访问 Azure 上的自定义脚本扩展脚本中的环境变量

azure - 在 Azure Web 应用程序中部署自签名证书

mysql - 尝试向 MySQL Innodb Cluster 添加节点

java - Spring 安全oauth2 : grant_type=password Authentication

Symfony 2.3 getRequest()-> headers 不显示授权承载 token

java - 使用 Apache Karaf 容器和 Play 框架在标准 OSGi 上插入用户授权的最佳方法

c# - Azure函数连接到本地sql访问被拒绝

java - 是否可以使用普通 Java 使用 okHttp 客户端执行 OAuth2 请求

c# - 使用 C# 实现 SoundCloud OAuth 登录

vue.js - 在 vuejs 应用程序中运行时 axios 响应头丢失数据