[英]Validating Authorization token in incoming requests with Azure API management and third party Authorization Server
我必須使用第三方授權服務器在我的項目中實現OAuth 2.0。 我的客戶端和服務器都已在AS上注冊。 我已經在Azure上創建了一個API管理實例,並導入了swagger API。 我希望針對我的AS驗證每個傳入的請求,因此我只需要將該請求重定向到https://my-as.com/as/introspect.oauth2 ,並驗證令牌。 如果令牌有效,則讓其繼續執行,否則發送401。我正嘗試使用“入站處理”實現此任務,並參考以下文檔: https : //docs.microsoft.com/zh-cn/azure/api-管理/ api管理如何使用aad#configure保護ajwt驗證策略以預先授權請求的后端 。
唯一的問題是,我使用的是第三方AS,而不是Azure AD。 我嘗試用我的URL替換示例XML代碼中的URL,但是它不起作用。
如何將請求重定向到授權服務器以驗證訪問令牌?
添加以下入站策略有效:
<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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.