Chrome Extensions - token from "launchWebAuthFlow" expires after an hour and need user interaction to get new one

Viewed 170

I am using launchWebAuthFlow in a service worker to authenticate users who choose to backup the extension settings in their Google Drive. When a user clicks the login button, it sends a message to the service worker (MV3, perviously "background script"), who does this:

const redirectUrl = await browser.identity.launchWebAuthFlow({
        'url': _createAuthEndpoint(),
        'interactive': true
    })
    const url = new URL(redirectUrl);
    const urlParams = new URLSearchParams(url.hash.slice(1));
    const params = Object.fromEntries(urlParams.entries());
    await browser.storage.local.set({googleToken: params.access_token});

Helper method to construct auth url:

function _createAuthEndpoint() {
  const redirectURL = browser.identity.getRedirectURL();
  const { oauth2 } = browser.runtime.getManifest();
  const clientId = oauth2.client_id;
  const authParams = new URLSearchParams({
    client_id: clientId,
    response_type: 'token',
    redirect_uri: redirectURL,
    scope: 'openid ' + oauth2.scopes.join(' '),
  });
  return `https://accounts.google.com/o/oauth2/auth?${authParams.toString()}`;
}

It works well for about an hour, after that the token get invalidated and I need to get a new token. If i try to use launchWebAuthFlow with interactive: false I get an error "user interaction required"

Is there a way to have the token refresh without user interaction?

0 Answers
Related