title: What is the best way to authenticate users with auth0 (oauth2) in a chrome extension that runs content scripts across multiple origins?
body: <p>I've seen a few posts on this but I want to highlight some specific questions I have yet to see properly answered.</p>
  <p>My current chrome extension contains the following:</p>
  <ul>
  <li>background service worker</li>
  <li>html pages to handle login / logout (although doing this in a popup would be great)</li>
  <li>content scripts that run a SPA on certain domains</li>
  </ul>
  <p>What I would like is for a user to be able to authenticate with auth0, and then any content script running on any domain can then use an access token to hit my API.</p>
  <p>The current challenges I've been seeing that I'm not sure how to tackle:</p>
  <ol>
  <li><p>Ideally each running content script has its own token. This involves using the auth0 session to silently get an access token. However, since auth0 checks the origin when hitting <code>/authorize</code> it would mean registering every domain as an &quot;allowed origin&quot; which is not possible for me due to volume. Currently if I try just setting the <code>redirectURI</code> to my chrome extension URL, it ends up timing out. I have seen some users report this approach working, so I'm not sure if I'm doing something wrong or not, but this approach feels unsafe in retrospect.</p>
  </li>
  <li><p>If I instead funnel my requests through the background script, so all running content scripts effectively use a single access token, how do I refresh that access token? The documentation recommends making a call to <code>/oauth/token</code> which involves the client secret. My guess is this is not something I should be putting into my javascript as all of that is visible to anyone who inspects the bundle. Is that a valid concern? If so, what other approach do I have?</p>
  </li>
  <li><p>If I do use a manually stored refresh_token, what is the best way to keep that available? The chrome storage API says not to use it for sensitive information. Is it preferred then to keep it in local storage?</p>
  </li>
  <li><p>If the best option is to have the background script make all the requests on behalf of the content scripts, what is the safest way for the content scripts to make a request through the background script? I would rely on <code>chrome.runtime.sendMessage</code> but it seems like the API supports arbitrarily sending messages to any extension, which means other code that isn't part of the extension could also funnel requests through the background script.</p>
  </li>
  </ol>
  <p>More generally, I would love to hear some guidance on a <em>safe</em> architecture to authenticate users for a multi-domain extension.</p>
  <p>I am also not adverse to using another service, but from what I've seen so far, auth0 offers relatively good UX/DX.</p>