oauth2.0 declarativeNetRequest.updateDynamicRules没有更新,如何正确地删除和添加规则?

yhxst69z  于 2023-02-11  发布在  其他
关注(0)|答案(1)|浏览(189)

**更新:**我注意到在我第一次运行ITM时传递了令牌,但是没有新的令牌被附加到我的规则中。

我在Gmail上安装了这个chrome扩展,用户登录后,它会返回一个访问令牌,该令牌通过HTTP请求传递给我们的API。第一次传递访问令牌时,它工作正常,但如果我不刷新Gmail,1小时后,访问令牌过期,我的应用程序出现401错误。我有一个函数interceptURL,它将匹配URL并在HTTP请求发出之前给予一个刷新的令牌(或者我是这么认为的)。
可能在1小时后,访问令牌过期,因此调用刷新令牌不会生成新令牌?

背景脚本

function interceptURL(requestDetails: chrome.webRequest.WebRequestBodyDetails) {
    console.log('intercepted: ' + requestDetails.url);
    if (requestDetails.url.includes(liveApiUrl) || requestDetails.url.includes(testApiUrl)) { 
        chrome.runtime.sendMessage({ "message": "refresh_token" }, (token: string) => { 
            if (token == undefined) {
                chrome.runtime.sendMessage({ "message": "get_token" });
            }
        });
    }
}

chrome.webRequest.onBeforeRequest.addListener(
    interceptURL,
    { urls: [liveApiUrl, testApiUrl] }
)

"这是我的规则"

function GetInterceptRules(token: string) {
    const allResourceTypes =
        Object.values(chrome.declarativeNetRequest.ResourceType);

    return [
        {
            id: 1,
            priority: 1,
            action: {
                type: chrome.declarativeNetRequest.RuleActionType.MODIFY_HEADERS,
                requestHeaders: [
                    {
                        operation: chrome.declarativeNetRequest.HeaderOperation.SET,
                        header: 'Authorization',
                        value: 'Bearer ' + token,
                    },
                ]
            },
            condition: {
                urlFilter: liveApiUrl,
                initiatorDomains: ["mail.google.com"],
                resourceTypes: allResourceTypes,
            }
        },
        {
            id: 2,
            priority: 1,
            action: {
                type: chrome.declarativeNetRequest.RuleActionType.MODIFY_HEADERS,
                requestHeaders: [
                    {
                        operation: chrome.declarativeNetRequest.HeaderOperation.SET,
                        header: 'Authorization',
                        value: 'Bearer ' + token,
                    },
                ]
            },
            condition: {
                urlFilter: testApiUrl,
                initiatorDomains: ["mail.google.com"],
                resourceTypes: allResourceTypes,
            }
        }
    ];

我的想法是:1 -我在每个HTTP请求之前给予它一个刷新令牌,这样当我更新动态规则时,它会传递新的令牌。(这是我目前拥有的)2 -我可以检查访问令牌是何时创建的,并确保在1小时结束之前运行获取令牌的代码。(可能不是最好的方法?)

获取访问令牌

chrome.identity.launchWebAuthFlow(
        {
            url: azureTokenAuthUrl,
            interactive: isInteractive
        },
        (redirectURL: string) => {
            let token: string = '';
            if (redirectURL != null) {
                let params = new URLSearchParams(redirectURL);
                token = params.get("access_token");
            }
            console.log("acces_token", token);
            console.log(redirectURL)
            UpdateIntercept(token)
            callback(token)
        }

清单版本3

"permissions": [
    "webRequest",
    "declarativeNetRequest",
    "declarativeNetRequestWithHostAccess",
    "identity",
    "identity.email"
  ],
  "background": {
    "service_worker": "/static/js/Background.js"
  },
  "content_scripts": [
    {
      "matches": [ "<all_urls>" ],
      "css": [ "/css/bootstrap-iso.css" ],
      "js": [ "react.production.min.js", "react-bootstrap.min.js", "react-dom.production.min.js" ]
    },
    {
      "matches": [ "*://mail.google.com/*" ],
      "css": [ "/css/AuthButton.css" ],
      "js": [ "/static/js/AuthButton.js" ]
    },
    {
      "matches": [ "*://mail.google.com/*" ],
      "js": [ "/static/js/PushNotification.js" ]

    }
  ],

我一直在寻找,但似乎不能找到一个解决我的问题。我尝试使用 JWT 解码,所以我知道它已经过期。

2w2cym1i

2w2cym1i1#

将交互性为false的launchAuthFlow添加到我的侦听器中,我在侦听器中检查电子邮件是否已打开,如果已打开,则触发launchAuthFlow,将令牌附加到HTTP请求

相关问题