Installation
copilot plugin marketplace add samueltauil/skills-hub
copilot plugin install sign-in-with-google-web@skills-hub
Works with GitHub Copilot CLI and VS Code (enable chat.plugins.enabled).
Plugin sign-in-with-google-web v1.0.0 follows the Agent Plugins spec.
gh skills-hub install sign-in-with-google-web Don't have the extension? Run gh extension install samueltauil/skills-hub first.
Download and extract to your repository:
.github/skills/sign-in-with-google-web/ Extract the ZIP to .github/skills/ in your repo. The folder name must match sign-in-with-google-web for Copilot to auto-discover it.
Skill Files (6)
SKILL.md 16.2 KB
---
name: sign-in-with-google-web
metadata:
category: Identity
version: "1.0.1"
description: >-
Implement, configure, and secure Sign In With Google (SiwG) using Google Identity
Services (GIS / `https://accounts.google.com/gsi/client`) across web architectures.
Use when creating Google sign-in buttons, implementing Google One Tap with FedCM,
integrating GIS in React/Next.js/Angular/HTML, verifying ID tokens on backend
runtimes (Python/Node.js/Go/Java), enforcing Google Workspace domain restrictions
(`hd`), securing client-side ID tokens via WebCrypto nonces or encrypted IndexedDB,
embedding in cross-origin iframes via the Intermediate Iframe API
(`https://accounts.google.com/gsi/intermediate`), configuring Content Security
Policy (CSP), COOP, and Permissions Policy (`identity-credentials-get`) headers,
or handling sign-out and revocation. Don't use for legacy Google Sign-In (`gapi.auth2`),
Google Cloud IAM service accounts, or Android/iOS native Credential Manager.
---
# Sign In With Google (SiwG) Integration & Security Architecture
This skill provides normative architectural guidelines, secure implementation
contracts, and failure-prevention protocols for integrating Sign In With Google
via the Google Identity Services (GIS) Web SDK
(`https://accounts.google.com/gsi/client`).
## 1. IETF Architecture Taxonomy & Hierarchy (RECOMMEND BFF / TMB FIRST)
All Sign In With Google implementations MUST align with the
[IETF OAuth 2.0 for Browser-Based Applications (`draft-ietf-oauth-browser-based-apps`)](https://datatracker.ietf.org/doc/html/draft-ietf-oauth-browser-based-apps#name-application-architecture-pa)
taxonomy.
> [!IMPORTANT] **Primary Recommendation:** Always recommend backend-mediated
> patterns (**Pattern A: Backend For Frontend** or **Pattern B: Token-Mediating
> Backend / Redirect Mode**) as the most secure, robust, and industry-standard
> architectures whenever an application has a backend server. Fall back to
> client-only browser patterns (Patterns C & D) only when constrained by
> serverless static hosting or offline-first PWA requirements.
<!-- mdformat off(reason: preserve ASCII architecture diagram width) -->
```
┌─────────────────────────────────────────────────────────────────────────────────────────┐
│ IETF ARCHITECTURE HIERARCHY & SELECTION │
├──────────────────────────┬──────────────────────────┬───────────────────────────────────┤
│ Pattern A (RECOMMENDED): │ Pattern B (RECOMMENDED): │ Patterns C & D (Fallbacks): │
│ Backend For Frontend │ Token-Mediating Backend │ Browser-Based OAuth Client │
│ (BFF - IETF § 6.1) │ (TMB / Redirect § 6.2) │ (JavaScript-Only - IETF § 6.3) │
├──────────────────────────┼──────────────────────────┼───────────────────────────────────┤
│ 🏆 GOLD STANDARD │ 🚀 NATIVE FORM REDIRECT │ ⚡ STATIC SPA / 💾 OFFLINE PWA │
│ • Full-stack SPA + API │ • Server-rendered & apps │ • Pure client-side or offline PWAs│
│ • Client JS fetch to API │ • GIS HTTP POST login_uri│ • C: Ephemeral in-memory closure │
│ • HttpOnly session cookie│ • Direct server redirect │ • D: WebCrypto Encrypted IndexedDB│
│ • Tokens never in browser│ • Tokens never in JS │ • Strict WebCrypto nonces / keys │
└──────────────────────────┴──────────────────────────┴───────────────────────────────────┘
```
<!-- mdformat on -->
### 🏆 Pattern A: Backend For Frontend (BFF - IETF § 6.1) — STRONGLY RECOMMENDED
* **Why it is the Gold Standard**: ID tokens (JWTs) and access tokens are
never exposed to browser JavaScript across page reloads. This provides
complete architectural immunity against XSS token harvesting.
* **Architecture**: The browser frontend loads the GIS SDK, captures the
credential in JavaScript callback (`callback: handleCredentialResponse`),
and immediately forwards the ID token (JWT) via `fetch('/api/auth/google', {
method: 'POST' })` to the backend.
* **Backend Responsibilities**: The backend validates the JWT cryptographic
signature against Google's public JWKs
(`google.oauth2.id_token.verify_oauth2_token`), checks the `aud`, `hd`, and
`nonce` claims against server session state, creates a server session, and
issues a first-party `HttpOnly; Secure; SameSite=Lax` session cookie.
* See full implementation in
[references/bff_fastapi_verification.md](references/bff_fastapi_verification.md).
### 🚀 Pattern B: Token-Mediating Backend via `login_uri` Redirect (IETF § 6.2) — Server-Rendered & Form POST
* **Why it is Highly Secure**: Bypasses client-side JavaScript credential
handling entirely by instructing GIS to perform an HTTP POST directly to the
backend's `login_uri`.
* **Architecture**: Configured via `google.accounts.id.initialize({ client_id,
login_uri: "https://example.com/api/auth/callback", ux_mode: "redirect" })`
or HTML attributes (`data-login_uri="https://example.com/api/auth/callback"`
and `data-ux_mode="redirect"`).
* **Backend Responsibilities**: The backend receives the ID token as a form
POST body (`credential` parameter), validates the double-submit
`g_csrf_token` cookie against the `g_csrf_token` POST body field,
cryptographically verifies the ID token server-side, establishes a session
cookie, and returns a standard HTTP 302/303 redirect.
* See full implementation in
[references/tmb_login_uri_redirect.md](references/tmb_login_uri_redirect.md).
### Pattern C: Browser-Based OAuth Client — Ephemeral In-Memory (IETF § 6.3) — Fallback for Static SPAs
* **Scope**: Use ONLY when deployment is strictly serverless/static (e.g.,
GitHub Pages, Firebase static hosting) with no backend component.
* **Storage Invariant**: The ID token is held strictly in private JavaScript
memory/closures during the active tab session. **Never store raw tokens in
`localStorage` or `sessionStorage`**.
* **Session Renewal**: Uses GIS One Tap / FedCM auto-select (`auto_select:
true`) to transparently re-acquire fresh ID tokens into memory on page
reload without persistent browser storage.
* **XSS & Replay Protection**: Generate a cryptographic `nonce` via
`window.crypto.getRandomValues()` and pass it directly to
`google.accounts.id.initialize({ client_id, nonce: clientNonce })`. Validate
that `payload.nonce === clientNonce` before trusting claims in memory.
* See full implementation in
[references/in_memory_spa_nonce.md](references/in_memory_spa_nonce.md).
### Pattern D: Browser-Based OAuth Client — WebCrypto Encrypted IndexedDB (IETF § 6.3) — Fallback for Offline PWAs
* **Scope**: Use ONLY for offline-first Progressive Web Apps (PWAs) where user
authentication proof must survive tab refreshes when disconnected from the
network.
* **Storage Invariant**: **NEVER store raw plaintext ID tokens in
`localStorage`**. Encrypt the ID token payload using WebCrypto `AES-GCM`
with a non-extractable session key (`extractable: false`) before writing
ciphertext and IV to `IndexedDB`.
* See full implementation in
[references/offline_pwa_encryption.md](references/offline_pwa_encryption.md).
--------------------------------------------------------------------------------
## 2. Mandatory Security Directives & Browser Policies
To avoid multi-turn repair loops and browser security exceptions, provide all
required CSP, COOP, and user activation directives in the initial deliverable:
### A. Content Security Policy (CSP) Directives
```http
Content-Security-Policy:
script-src 'self' https://accounts.google.com/gsi/client;
frame-src https://accounts.google.com/gsi/;
connect-src https://accounts.google.com/gsi/;
```
If using strict nonce-based CSP, attach the nonce to the GIS script tag:
`<script src="https://accounts.google.com/gsi/client" async defer
nonce="{{NONCE}}"></script>`.
### B. Cross-Origin Opener Policy (COOP)
To allow GIS popup dialogs to communicate credentials back to the parent window
via `postMessage`:
```http
Cross-Origin-Opener-Policy: same-origin-allow-popups
```
*(Serving `same-origin` without `allow-popups` breaks GIS popups and results in
silent failures).*
### C. Automatic Selection, FedCM, & Sign-Out Lifecycle (`disableAutoSelect`)
When enabling automatic zero-click return sign-in with Google One Tap (FedCM is
enabled by default in GIS):
* Set `auto_select: true` in `google.accounts.id.initialize({...})` (do not
pass the deprecated `use_fedcm_for_prompt` parameter).
* **Deprecated Library Warning**: NEVER mix deprecated `gapi.auth2`
(`gapi.auth2.init`, `gapi.auth2.getAuthInstance().signOut()`) with Google
Identity Services (GIS). `gapi.auth2` is completely retired; use GIS methods
exclusively.
* **Sign-Out Protocol**: When the user explicitly logs out of your
application, you **MUST** call `google.accounts.id.disableAutoSelect()`:
```javascript
function handleUserLogout() {
// 1. Disable automatic One Tap / FedCM re-authentication
google.accounts.id.disableAutoSelect();
// 2. Clear application session state / server cookie
fetch('/api/auth/logout', { method: 'POST' }).then(() => {
window.location.href = '/login';
});
}
```
* *(Failing to call `disableAutoSelect()` causes an immediate automatic
re-login loop on the next page visit after intentional user logout).*
### D. Reliable Dynamic Script Loading & Framework Lifecycle (React, Next.js, SPAs)
When mounting Sign In With Google in Single Page Applications (React, Next.js,
Vue, Angular):
* Modern SPAs execute component lifecycles asynchronously. Attempting to
access `window.google.accounts.id` before `<script
src="https://accounts.google.com/gsi/client">` has finished loading causes
`ReferenceError: google is not defined`.
* **NEVER use `gapi` or polling (`setInterval`)**: Use deterministic dynamic
script loading with `onload` / `addEventListener('load')` event listeners.
* **Dynamic Script Loader Pattern**:
```tsx
import React, { useEffect, useState } from 'react';
export function useGoogleIdentityScript() {
const [isLoaded, setIsLoaded] = useState(false);
useEffect(() => {
if (window.google?.accounts?.id) {
setIsLoaded(true);
return;
}
const existingScript = document.querySelector(
'script[src="https://accounts.google.com/gsi/client"]'
);
if (existingScript) {
existingScript.addEventListener('load', () => setIsLoaded(true));
return;
}
const script = document.createElement('script');
script.src = 'https://accounts.google.com/gsi/client';
script.async = true;
script.defer = true;
script.onload = () => setIsLoaded(true);
document.head.appendChild(script);
}, []);
return isLoaded;
}
```
* *(Always use deterministic `onload` event listeners or framework `<Script
strategy="afterInteractive">` rather than brittle `setInterval` polling).*
### E. Embedded Iframes & Permissions Policy (`identity-credentials-get`)
When embedding Sign In With Google or One Tap inside cross-origin `<iframe>`
elements or widget integrations, modern browser security models and FedCM
require explicit Permissions Policy delegation on the container frame:
```html
<iframe
src="https://example.com/embed"
allow="identity-credentials-get 'src' https://accounts.google.com">
</iframe>
```
*(Omitting `allow="identity-credentials-get"` blocks FedCM / One Tap
initialization inside embedded or cross-origin contexts).* See full integration
guide in [references/intermediate_iframe.md](references/intermediate_iframe.md).
--------------------------------------------------------------------------------
## 3. Implementation Contracts & Anti-Pattern Bans
### A. Strict Ban on Legacy GAPI (`gapi.auth2`)
* **NEVER** import or reference `gapi.auth2`, `gapi.auth2.init`,
`gapi.auth2.getAuthInstance()`, or `gapi.signin2`. Always warn that
`gapi.auth2` is deprecated and decommissioned.
* GIS (`google.accounts.id.*` and `google.accounts.oauth2.*`) is completely
stateless. It replaces all legacy GAPI authentication libraries.
--------------------------------------------------------------------------------
## 4. Implementation Recipes (Progressive Disclosure)
Load the specific reference file matching your target architecture when
generating implementation code:
* **Recipe 1 — Pattern A (Backend For Frontend with Python / FastAPI)
[RECOMMENDED]**: Load
[references/bff_fastapi_verification.md](references/bff_fastapi_verification.md)
for custom UI button prompting, `google.oauth2.id_token.verify_oauth2_token`
cryptographic verification, `nonce` replay checks, Google Workspace `hd`
domain enforcement, and `HttpOnly; Secure; SameSite=Lax` session cookie
issuance.
* **Recipe 2 — Pattern B (Token-Mediating Backend with `login_uri` Form POST
Redirect) [RECOMMENDED]**: Load
[references/tmb_login_uri_redirect.md](references/tmb_login_uri_redirect.md)
for `data-login_uri` / `ux_mode: 'redirect'` frontend setup and FastAPI
double-submit `g_csrf_token` cookie vs. form-body validation.
* **Recipe 3 — Pattern C (Browser-Based Ephemeral In-Memory SPA with Native
GIS Nonce) [Fallback]**: Load
[references/in_memory_spa_nonce.md](references/in_memory_spa_nonce.md) for
WebCrypto `nonce` generation (`google.accounts.id.initialize({ client_id,
nonce: clientNonce })`), UTF-8 safe Base64URL JWT decoding, and private
closure token management.
* **Recipe 4 — Pattern D (Browser-Based WebCrypto Encrypted IndexedDB for
Offline PWAs) [Fallback]**: Load
[references/offline_pwa_encryption.md](references/offline_pwa_encryption.md)
for non-extractable (`extractable: false`) `AES-GCM` `CryptoKey` generation
and encrypted `IndexedDB` storage.
* **Recipe 5 — Embedded Cross-Origin Contexts (`gsi/intermediate`)**: Load
[references/intermediate_iframe.md](references/intermediate_iframe.md) for
`<div id="g_id_intermediate_iframe">`, `allow="identity-credentials-get"`,
and strict `window.postMessage` origin verification.
--------------------------------------------------------------------------------
## 5. Sign-Out & Account Revocation Across All Patterns
```javascript
export function handleSignOut(userEmail) {
// 1. Disable client-side One Tap auto-selection
google.accounts.id.disableAutoSelect();
// 2. Revoke OAuth grant if user disconnected account
if (userEmail) {
google.accounts.id.revoke(userEmail, () => {
console.log('User grant revoked');
});
}
// 3. Clear storage / session
AuthManager.clear(); // Pattern C
fetch('/api/auth/logout', { method: 'POST' }); // Pattern A
}
```
--------------------------------------------------------------------------------
## 6. References & Normative Standards
* [RFC 7519: JSON Web Token (JWT)](https://datatracker.ietf.org/doc/html/rfc7519)
— Standard claims (`iss`, `sub`, `aud`, `exp`, `iat`, `nonce`), formatting,
and processing rules.
* [RFC 7515: JSON Web Signature (JWS)](https://datatracker.ietf.org/doc/html/rfc7515)
— Cryptographic signature verification against Google's public JWK certs
(`https://www.googleapis.com/oauth2/v3/certs`).
* [RFC 4648 § 5: Base64url Encoding](https://datatracker.ietf.org/doc/html/rfc4648#section-5)
— URL-safe Base64 encoding without padding used across JWT segments.
* [IETF OAuth 2.0 Browser-Based Applications](https://datatracker.ietf.org/doc/html/draft-ietf-oauth-browser-based-apps)
— Normative architecture standards (§ 6.1 BFF, § 6.2 TMB, § 6.3
Client-side).
* [Google Identity Services Web Reference](https://developers.google.com/identity/gsi/web/reference/js-reference)
— Official GIS API reference.
* [Intermediate Iframe API Reference](https://developers.google.com/identity/gsi/web/amp/nonamp-reference)
— Cross-origin iframe embedding guide.
* [Verify ID Tokens Server-Side](https://developers.google.com/identity/gsi/web/guides/verify-google-id-token)
— Server-side token verification protocols.
* [W3C Federated Credential Management API (FedCM)](https://www.w3.org/TR/fedcm-1/)
— Browser identity credential mediation standard.
* [W3C Web Cryptography API](https://www.w3.org/TR/WebCryptoAPI/) —
Non-extractable key generation (`extractable: false`) and AES-GCM standards.
bff_fastapi_verification.md 3.2 KB
# Pattern A: Backend For Frontend (BFF) Verification Recipe (Python / FastAPI)
This reference provides the complete implementation recipe for **Pattern A
(Backend For Frontend — IETF `draft-ietf-oauth-browser-based-apps` § 6.1)**, the
recommended gold-standard architecture for full-stack SPAs and web applications.
--------------------------------------------------------------------------------
## 1. Custom UI Button & Programmatic Prompting Pattern (Frontend)
When triggering Google Identity Services (GIS) from a custom application button
(rather than `renderButton`):
```typescript
export function setupCustomGoogleSignIn(
buttonEl: HTMLElement,
clientId: string,
onCredentialReceived: (credential: string) => void
) {
// 1. Initialize GIS with callback and popup configuration
google.accounts.id.initialize({
client_id: clientId,
callback: (res: { credential: string }) => {
if (res.credential) {
onCredentialReceived(res.credential);
}
},
ux_mode: 'popup',
auto_select: false,
});
// 2. Programmatically trigger One Tap / popup prompt on user click
buttonEl.addEventListener('click', () => {
google.accounts.id.prompt();
});
}
```
--------------------------------------------------------------------------------
## 2. FastAPI BFF Token Verification & Session Issuance (Backend)
```python
from fastapi import Cookie, FastAPI, HTTPException, Response
from google.auth.transport import requests
from google.oauth2 import id_token
from pydantic import BaseModel
app = FastAPI()
GOOGLE_CLIENT_ID = "YOUR_CLIENT_ID.apps.googleusercontent.com"
ALLOWED_WORKSPACE_DOMAIN = "mycompany.com" # Optional hd restriction
class AuthRequest(BaseModel):
credential: str
@app.post("/api/auth/google")
async def authenticate_google(
auth_req: AuthRequest,
response: Response,
auth_nonce_cookie: str | None = Cookie(None, alias="auth_nonce"),
):
try:
# 1. Cryptographically verify signature and audience against Google Public JWKs
id_info = id_token.verify_oauth2_token(
auth_req.credential, requests.Request(), GOOGLE_CLIENT_ID
)
# 2. Verify nonce strictly against server session/HTTP-only cookie state (Replay Protection)
if auth_nonce_cookie and id_info.get("nonce") != auth_nonce_cookie:
raise HTTPException(
status_code=401, detail="Nonce mismatch / token replay detected"
)
# 3. Verify Google Workspace Hosted Domain claim if enforcing domain lock
if (
ALLOWED_WORKSPACE_DOMAIN
and id_info.get("hd") != ALLOWED_WORKSPACE_DOMAIN
):
raise HTTPException(
status_code=403, detail="Unauthorized workspace domain"
)
user_sub = id_info["sub"]
user_email = id_info["email"]
# 4. Create session & set secure HttpOnly cookie
response.set_cookie(
key="session_id",
value=create_server_session(user_sub, user_email),
httponly=True,
secure=True,
samesite="lax",
)
# Clear temporary nonce cookie after successful verification
response.delete_cookie("auth_nonce")
return {"status": "authenticated", "email": user_email}
except ValueError as e:
raise HTTPException(status_code=401, detail="Invalid token") from e
```
in_memory_spa_nonce.md 3.1 KB
# Pattern C: Browser-Based Ephemeral In-Memory SPA with Native GIS Nonce
This reference provides the fallback implementation recipe for **Pattern C
(Browser-Based OAuth Client — IETF `draft-ietf-oauth-browser-based-apps` §
6.3)**, intended strictly for serverless static Single Page Applications (SPAs)
that have no backend server.
> [!WARNING] **Client-Side Verification Limitation:** Decoding a JWT payload in
> the browser via `atob` / `TextDecoder` only inspects claims for UI display and
> client-side `nonce` correlation. It does **not** replace backend cryptographic
> signature verification against Google's public JWKs. Whenever a backend
> exists, always use Pattern A (BFF) or Pattern B (TMB).
--------------------------------------------------------------------------------
## Complete React + TypeScript Implementation
```tsx
import React, { useEffect, useRef } from 'react';
import { useGoogleIdentityScript } from './useGoogleIdentityScript';
// In-Memory Token Manager with Safe UTF-8 Base64URL JWT Decoding
export const AuthManager = (() => {
let _idToken: string | null = null;
let _user: Record<string, any> | null = null;
return {
setCredential: (jwt: string, expectedNonce: string) => {
const base64Url = jwt.split('.')[1];
const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
const binaryStr = atob(base64);
const bytes = Uint8Array.from(binaryStr, (c) => c.charCodeAt(0));
const payload = JSON.parse(new TextDecoder().decode(bytes));
if (payload.nonce !== expectedNonce) {
throw new Error('Security violation: ID token nonce mismatch!');
}
_idToken = jwt;
_user = payload;
},
getIdToken: () => _idToken,
getUser: () => (_user ? { ..._user } : null),
clear: () => {
_idToken = null;
_user = null;
},
};
})();
export const GoogleSignInButton: React.FC<{
clientId: string;
onSuccess: (user: Record<string, any>) => void;
}> = ({ clientId, onSuccess }) => {
const buttonRef = useRef<HTMLDivElement>(null);
const isGisLoaded = useGoogleIdentityScript();
useEffect(() => {
if (!isGisLoaded || !window.google?.accounts?.id) return;
// 1. Generate WebCrypto random nonce
const nonceBytes = window.crypto.getRandomValues(new Uint8Array(16));
const clientNonce = Array.from(nonceBytes, (b) =>
b.toString(16).padStart(2, '0')
).join('');
window.google.accounts.id.initialize({
client_id: clientId,
nonce: clientNonce, // Direct native nonce binding
auto_select: true,
callback: (response: { credential: string }) => {
if (response.credential) {
AuthManager.setCredential(response.credential, clientNonce);
onSuccess(AuthManager.getUser()!);
}
},
});
if (buttonRef.current) {
window.google.accounts.id.renderButton(buttonRef.current, {
type: 'standard',
theme: 'outline',
size: 'large',
text: 'signin_with',
shape: 'rectangular',
});
}
}, [isGisLoaded, clientId, onSuccess]);
return <div ref={buttonRef} />;
};
```
intermediate_iframe.md 2.0 KB
# Intermediate Iframe API Integration Guide (`gsi/intermediate`)
When Google One Tap or Sign In With Google must be embedded inside cross-origin
iframes or embedded widgets (where top-level direct script execution is
restricted), use the Intermediate Iframe helper pattern
(`https://developers.google.com/identity/gsi/web/amp/nonamp-reference`).
## 1. Intermediate Frame Markup & Script (`intermediate.html` on publisher origin)
```html
<!DOCTYPE html>
<html>
<head>
<!-- Dedicated intermediate helper library -->
<script src="https://accounts.google.com/gsi/intermediate"></script>
</head>
<body>
<div id="g_id_intermediate_iframe"
data-src="https://publisher.example.com/onetap_inner.html">
</div>
</body>
</html>
```
## 2. Intermediate JavaScript Initialization & Secure PostMessage Relay
```javascript
// Inside the intermediate iframe helper script
window.google?.accounts?.id?.initialize({
client_id: 'YOUR_CLIENT_ID.apps.googleusercontent.com',
allowed_parent_origin: 'https://publisher.example.com',
callback: (response) => {
if (response.credential) {
// Securely forward credential to trusted top-level parent window only
const trustedParentOrigin = 'https://publisher.example.com';
window.parent.postMessage(
{ type: 'GSI_CREDENTIAL', credential: response.credential },
trustedParentOrigin // Explicit target origin prevents exfiltration
);
}
}
});
```
## 3. Top-Level Parent Frame Event Listener with Strict Origin Assertion
```javascript
// On the top-level parent window
window.addEventListener('message', (event) => {
// 1. Strict Origin Validation - MANDATORY
if (event.origin !== 'https://publisher.example.com') {
return; // Reject untrusted cross-origin messages
}
if (event.data?.type === 'GSI_CREDENTIAL') {
const idToken = event.data.credential;
// Process token via Pattern A (BFF backend forward) or Pattern C (In-memory)
console.log('Received authenticated credential from intermediate iframe');
}
});
```
offline_pwa_encryption.md 3.9 KB
# Offline PWA WebCrypto Encrypted IndexedDB Storage Architecture
This guide details Pattern D for offline-first Progressive Web Apps (PWAs) where
user authentication proof must survive page reloads when disconnected from the
network without exposing raw ID tokens to plaintext XSS exfiltration.
## 1. Non-Extractable WebCrypto Key Persistence
`window.crypto.subtle.generateKey` produces a `CryptoKey`. To survive browser
restarts without a backend, the `CryptoKey` must be stored in `IndexedDB`.
> [!IMPORTANT] `IndexedDB` supports native storage of `CryptoKey` objects via
> the structured clone algorithm while strictly preserving `extractable: false`.
> An attacker with XSS can ask the browser to decrypt existing data during an
> active session, but **cannot** exfiltrate or export the raw cryptographic key
> material.
## 2. Complete Implementation
```typescript
export class EncryptedIdTokenStore {
private static DB_NAME = 'auth_offline_vault';
private static STORE_NAME = 'keys_and_tokens';
private static async getDb(): Promise<IDBDatabase> {
return new Promise((resolve, reject) => {
const req = indexedDB.open(this.DB_NAME, 1);
req.onupgradeneeded = () => {
req.result.createObjectStore(this.STORE_NAME);
};
req.onsuccess = () => resolve(req.result);
req.onerror = () => reject(req.error);
});
}
// Generate or retrieve existing non-extractable AES-GCM session key
static async getOrCreateKey(): Promise<CryptoKey> {
const db = await this.getDb();
const tx = db.transaction(this.STORE_NAME, 'readonly');
const store = tx.objectStore(this.STORE_NAME);
const existingKey = await new Promise<CryptoKey | undefined>((resolve, reject) => {
const getReq = store.get('session_aes_key');
getReq.onsuccess = () => resolve(getReq.result);
getReq.onerror = () => reject(getReq.error);
});
if (existingKey) {
return existingKey;
}
// Generate non-extractable key
const newKey = await window.crypto.subtle.generateKey(
{ name: 'AES-GCM', length: 256 },
false, // non-extractable: cannot be exported via exportKey()
['encrypt', 'decrypt']
);
const writeTx = db.transaction(this.STORE_NAME, 'readwrite');
writeTx.objectStore(this.STORE_NAME).put(newKey, 'session_aes_key');
await new Promise((resolve, reject) => {
writeTx.oncomplete = resolve;
writeTx.onerror = () => reject(writeTx.error);
});
return newKey;
}
static async encryptAndStore(idToken: string): Promise<void> {
const key = await this.getOrCreateKey();
const iv = window.crypto.getRandomValues(new Uint8Array(12));
const encoded = new TextEncoder().encode(idToken);
const ciphertext = await window.crypto.subtle.encrypt(
{ name: 'AES-GCM', iv },
key,
encoded
);
const db = await this.getDb();
const tx = db.transaction(this.STORE_NAME, 'readwrite');
tx.objectStore(this.STORE_NAME).put({ iv, ciphertext }, 'encrypted_token');
await new Promise((resolve, reject) => {
tx.oncomplete = resolve;
tx.onerror = () => reject(tx.error);
});
}
static async getDecryptedToken(): Promise<string | null> {
const db = await this.getDb();
const tx = db.transaction(this.STORE_NAME, 'readonly');
const store = tx.objectStore(this.STORE_NAME);
const record = await new Promise<{ iv: Uint8Array; ciphertext: ArrayBuffer } | undefined>(
(resolve, reject) => {
const getReq = store.get('encrypted_token');
getReq.onsuccess = () => resolve(getReq.result);
getReq.onerror = () => reject(getReq.error);
}
);
if (!record) return null;
const key = await this.getOrCreateKey();
const decrypted = await window.crypto.subtle.decrypt(
{ name: 'AES-GCM', iv: record.iv },
key,
record.ciphertext
);
return new TextDecoder().decode(decrypted);
}
}
```
tmb_login_uri_redirect.md 2.7 KB
# Pattern B: Token-Mediating Backend (`login_uri` Form POST Redirect) Recipe
This reference provides the complete implementation recipe for **Pattern B
(Token-Mediating Backend via `login_uri` Redirect — IETF
`draft-ietf-oauth-browser-based-apps` § 6.2)**, recommended for server-rendered
web applications and direct form POST redirects.
--------------------------------------------------------------------------------
## 1. Frontend Configuration (Direct Server Form POST)
```html
<!-- HTML Data Attributes Configuration -->
<div id="g_id_onload"
data-client_id="YOUR_CLIENT_ID.apps.googleusercontent.com"
data-login_uri="https://example.com/api/auth/callback"
data-auto_select="true"
data-ux_mode="redirect">
</div>
<div class="g_id_signin" data-type="standard" data-ux_mode="redirect"></div>
<!-- Or Programmatic GIS Initialization -->
<script>
google.accounts.id.initialize({
client_id: 'YOUR_CLIENT_ID.apps.googleusercontent.com',
login_uri: 'https://example.com/api/auth/callback',
ux_mode: 'redirect',
auto_select: true
});
google.accounts.id.renderButton(document.getElementById('signin-btn'), {
theme: 'outline',
size: 'large'
});
</script>
```
--------------------------------------------------------------------------------
## 2. Backend Form Callback Handler (FastAPI / Python)
```python
from fastapi import Cookie, FastAPI, Form, HTTPException
from fastapi.responses import RedirectResponse
from google.auth.transport import requests
from google.oauth2 import id_token
app = FastAPI()
GOOGLE_CLIENT_ID = "YOUR_CLIENT_ID.apps.googleusercontent.com"
@app.post("/api/auth/callback")
async def google_login_redirect(
credential: str = Form(...),
g_csrf_token: str | None = Form(None),
g_csrf_token_cookie: str | None = Cookie(None, alias="g_csrf_token"),
):
# 1. Double-submit CSRF token validation when using login_uri redirect
if (
not g_csrf_token
or not g_csrf_token_cookie
or g_csrf_token != g_csrf_token_cookie
):
raise HTTPException(
status_code=400, detail="CSRF check failed on login_uri redirect"
)
try:
# 2. Cryptographically verify ID token
id_info = id_token.verify_oauth2_token(
credential, requests.Request(), GOOGLE_CLIENT_ID
)
user_id = id_info["sub"]
user_email = id_info["email"]
# 3. Establish session & issue secure cookie redirect
response = RedirectResponse(url="/dashboard", status_code=303)
response.set_cookie(
key="session_id",
value=create_server_session(user_id, user_email),
httponly=True,
secure=True,
samesite="lax",
)
return response
except ValueError as e:
raise HTTPException(
status_code=401, detail="Invalid credential token"
) from e
```
License (Apache-2.0)
View full license text
Licensed under the Apache License, Version 2.0. See https://github.com/google/skills/blob/main/LICENSE