簡體   English   中英

如何使所有刷新令牌對獲取訪問令牌無效以使其更安全

[英]How to make all refresh tokens invalid for getting access token to make it more secure

我以某種方式設法將默認訪問令牌的生命周期減少到 30 分鍾。 這使得令牌在 30 分鍾后過期或無效。 現在的問題是,很少有用戶之前已經獲得了刷新令牌和訪問令牌,並在令牌過期后使用它們再次獲得訪問令牌,例如

POST https://login.microsoft.com/tenantid/oauth2/v2.0/token?&client_id:appid&grant_type:refresh_token&refresh_token: refresh token&client_secret: client secret

我不希望這發生。 刪除offline_access scope 將不再提供刷新令牌。 但是用戶已經獲得的刷新令牌呢? 如何使這些刷新令牌無效,以便用戶無法使用它們來獲取更安全的訪問令牌。 即使他們使用,它也應該拋出一些錯誤而不是提供訪問令牌。

如何做到這一點? 有人試過這個嗎?

要使所有刷新令牌無效,您可以使用以下查詢:

POST https://graph.microsoft.com/beta/users/<user_id>/invalidateAllRefreshTokens

我試圖在我的環境中重現相同的結果並得到以下結果:

我注冊了一個 Azure AD 應用程序,並通過如下授予同意添加了API 權限

在此處輸入圖像描述

我通過 Postman 使用以下參數獲得了刷新令牌和訪問令牌:

POST  https://login.microsoftonline.com/<tenantID>/oauth2/v2.0/token
client_id:<appID>
grant_type:authorization_code
scope: offline_access user.read.all
code:code
redirect_uri: https://jwt.ms
client_secret: secret

回復:

在此處輸入圖像描述

使用此刷新令牌,我可以獲得如下訪問令牌:

POST https://login.microsoftonline.com/<tenantID>/oauth2/v2.0/token
client_id:appID
grant_type:refresh_token
refresh_token: 0.AVYA_in0zaI3eUqOQHrbrD-FUv //paste the refresh token that I got above
client_secret:client_secret //Mandatory if client is web app

回復:

在此處輸入圖像描述

為了撤銷這些刷新令牌,我通過授予對所需權限的同意在Graph Explorer中運行以下查詢:

POST https://graph.microsoft.com/beta/users/<user_id>/invalidateAllRefreshTokens

回復:

在此處輸入圖像描述

現在,當我嘗試使用現有的刷新令牌再次獲取access token時,由於刷新令牌被撤銷,出現如下錯誤

POST https://login.microsoftonline.com/<tenantID>/oauth2/v2.0/token
client_id:appID
grant_type:refresh_token
refresh_token: 0.AVYA_in0zaI3eUqOQHrbrD-FUv //paste the refresh token that I got above
client_secret:client_secret //Mandatory if client is web app

回復:

在此處輸入圖像描述

要從PowerShell執行相同的操作,您可以使用以下命令:

Revoke-AzureADUserAllRefreshToken -ObjectId <userID>

參考: 撤銷-AzureADUserAllRefreshToken (AzureAD)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM