Skip to content

Authentication and sessions

Add login to a Chromium extension

Add RemiliaNET login to a Chromium extension with the downloadable example, and read the connected account.

Chromium's identity API opens the login window for a public Login client. The background worker stores tokens, and the popup receives only the handle, the connection status, and the granted scopes.

The example has no client secret, no content scripts, and no access to website cookies. It reads identity from the /me response and discards ID tokens without decoding their claims. An extension that uses ID token claims validates them with a maintained OpenID Connect (OIDC) library.

Prerequisites

  • Chrome or another Chromium browser.
  • A RemiliaNET account that can register a Login client in the developer portal.

The callback has the form https://<extension-id>.chromiumapp.org/callback. During development, keep the extension folder and ID unchanged. A public manifest key can fix the development ID. A release uses your signed distribution identity, and the private signing key stays out of the archive. If the development ID and the release ID differ, register a separate client for each callback.

Steps

  1. Download the extension example.
  2. Extract the archive to a folder.
  3. Open chrome://extensions in Chrome.
  4. Enable Developer mode.
  5. Select Load unpacked for the extracted folder.
  6. Copy the extension ID into extensionId in config.js.
  7. Open the service worker inspector of the extension.
  8. Evaluate chrome.identity.getRedirectURL("callback").
  9. Register the returned URL on a Login client in the developer portal.
  10. Wait for client approval.
  11. Copy the client ID into clientId in config.js.
  12. Reload the extension.
  13. Select Log in with RemiliaNET in the popup.
  14. Select Check account to read the connected account.

Result

The popup shows the handle of the connected account. Log out removes the stored tokens and the account display of the extension.

The example makes no provider requests until you configure the client ID and the extension ID. It requests openid and calls only the profile endpoint of the logged-in user. Other features need their API scopes.

Behavior of the example

The example implements each stage of the connection:

StageBehavior
LoginChecks the configured extension ID, loads issuer discovery, and generates a fresh state, an S256 verifier, and its challenge.
CallbackChecks the origin, the path, the state, and any returned issuer. Rejects duplicate or conflicting response parameters and consumes the transaction.
Token exchangeSends the original callback and verifier. Reads expiry from expires_in. Saves a refresh token when the provider issues one.
Account checkSends the access token to /api/v1/me. Returns only the handle, the connection status, and the granted scopes to the popup.
RenewalRuns one renewal at a time and keeps any replacement refresh token. A temporary failure keeps the refresh token. invalid_grant clears the connection.
Log outClears tokens and pending state. Late login, refresh, and account responses cannot restore the connection.

The archive includes the official login control. The source files are manifest.json, config.js, background.js, popup.html, popup.js, and popup.css.

The background worker accepts three actions from its own popup. An extension that adds content scripts or other views checks the caller of each message. It returns only the data that the caller needs and keeps tokens out of messages.

Connection lifetime

The example stores tokens in chrome.storage.session, which only trusted contexts can read. Tokens survive a closed popup and a recreated worker during a browser session. A reload, an update, or a disable of the extension clears that storage. A browser restart clears it too, and the user must log in again.

After an interrupted authorization window, the user might need a fresh login attempt. Storage duration does not set token expiry. The provider can ask for consent when the user connects again. For more information about these limits, see the Chromium storage reference.

Browser checks

Each check uses your registered test client and account and a fresh /me request. A cached name is not evidence of an active connection. Tokens and raw private responses stay out of screenshots and logs.

CheckExpected result
Close and reopen the popupCheck account reads the connected account while the session stays valid.
Stop the worker, then reopen the popupStored tokens are available. Account checks and renewal work without tokens from the page.
Reload or update the extensionWith this storage choice, the user must log in again.
Quit and reopen the browserWith this storage choice, the user must log in again.
Let the access token approach expiryAn account check uses renewal when a refresh token is available. Without one, it requests another connection.
Close the authorization windowThe popup says that login was not completed. It does not claim that the provider denied a permission.
Log out while an account check or renewal is pendingNo late result restores tokens or the logged-in display.
Log out, then log in againEarlier requests cannot overwrite the later connection.

The behavior can differ between Chromium versions. The identity API describes how the browser handles the authorization window. Log out in this example performs no remote revocation and no provider logout.

Troubleshooting

The popup shows one of these results when a connection fails:

ResultCause and fix
Configure clientconfig.js lacks the approved Login client ID, or the extension ID does not match the installed extension. Correct config.js.
Login not completedThe window closed or authorization failed. Start a fresh attempt. This result alone does not show that the user denied consent.
Invalid callbackThe registered callback differs from getRedirectURL("callback"), or the response belongs to another login attempt. Register the exact callback.
Token errorCheck client approval, the exact callback, and the PKCE verifier. Read the error code of the endpoint. Keep tokens and token responses out of logs.
Network errorCheck the network connection and the extension permissions for the issuer and API hosts. Retry without deleting a valid refresh token.
Reconnect requiredThe session expired, has no usable refresh token, or session storage was cleared. Log in again.

Connection status and issued permissions are separate states. For missing scopes, see App permissions.

Next steps