@bigio/better-auth-electron 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,35 @@
1
+ ## I'm currently studying the official implementation; the documentation is still rough but will be improved soon. I'm eager and looking forward to exchanging ideas with everyone.
2
+
3
+ Based on my study of the official implementation code, I have decided on the following to-do list:
4
+
5
+ **1. Architecture: The "Silent Handoff" (Stateless & Secure)**
6
+
7
+ - [ ] **Server-Side Cookie Interception**: Modify `electron-server-plugin` to intercept the OAuth callback response.
8
+ - _Action_: Strip the `Set-Cookie` header (specifically the session token) from the response to prevent overwriting the user's browser session.
9
+ - _Goal_: Achieve strict physical isolation between Web Session and Electron Session.
10
+
11
+ - [ ] **Stateless OAuth Flow**: Ensure the OAuth flow relies solely on the encrypted `Ticket` mechanism, making the browser a purely stateless transport layer for Electron authentication.
12
+
13
+ **2. Security & Hardening**
14
+
15
+ - [ ] **Secure Persistence**: Implement `safeStorage` (DPAPI/Keychain) for encrypting the persisted PKCE Verifier on disk.
16
+ - _Reason_: Prevent plaintext credentials from resting on the file system.
17
+
18
+ - [ ] **User-Agent Scrubbing**: Global removal of "Electron" tokens from the `User-Agent` string at the `app.on('ready')` stage.
19
+ - _Reason_: Bypass WAF/Anti-Bot protections that block Electron-based requests during the ticket exchange phase.
20
+
21
+ - [done] ~~**Automated CSP Injection**: Implement `onHeadersReceived` interceptor in the Main Process.~~
22
+ - ~~_Action_: Automatically append the backend API URL to the `connect-src` directive.~~
23
+ - ~~_Goal_: Provide a "Zero-Config" experience by preventing CSP violations without requiring users to manually edit `index.html`.~~
24
+
25
+ **3. Developer Experience (DX) & API**
26
+
27
+ - [ ] **Enhanced Renderer API**: Refactor `getActions` to introduce a dedicated `authClient.bigio` namespace.
28
+ - _Feature_: Implement `authClient.bigio.signIn({ provider: 'github' })` wrapper.
29
+ - _Implementation_: Utilize `window.open` (intercepted by Main) or IPC to trigger the flow, keeping the API consistent with the official web client style.
30
+
31
+ - [ ] **Smart Web Handoff UI (Optional/Next)**: Update the web-side confirmation page to detect and display the currently logged-in web user, offering a "Continue as [User]" button for a seamless transition.
32
+
1
33
  # @bigio/better-auth-electron
2
34
 
3
35
  > **Work In Progress:** This library is actively being developed. Detailed documentation and architecture diagrams are coming soon.
@@ -14,7 +46,7 @@ Designed for production-grade applications, this library provides a secure, "bat
14
46
  - ** Secure PKCE Flow:**
15
47
  Implements the standard **Proof Key for Code Exchange** protocol out-of-the-box. Ensures enterprise-grade security for your OAuth exchanges without exposing secrets.
16
48
 
17
- - ** Preact SSR Ready:**
49
+ - ** Preact SSR Coming soon:**
18
50
  Includes a dedicated, lightweight Preact entry point optimized for Server-Side Rendering (SSR) in login windows.
19
51
  _(React 19 supported. Vue/Svelte support coming soon!)_
20
52
 
@@ -64,6 +96,28 @@ export const auth = betterAuth({
64
96
 
65
97
  Use `mainInjection` to setup IPC handlers and deep linking strategies. This automatically handles the "protocol" opening events.
66
98
 
99
+ ### Security & CSP Configuration
100
+
101
+ ** IMPORTANT: Clean up your `index.html`**
102
+
103
+ This plugin automatically injects a rigorous, production-ready **Content Security Policy (CSP)** via the Main Process.
104
+
105
+ **You CAN remove** any manual CSP `<meta>` tags from your `index.html` (renderer). Leaving them in will cause the browser to enforce the "intersection" of both policies, likely breaking your Auth flow (e.g., blocking the OAuth popup or API connection).
106
+
107
+ **DELETE this from your `index.html`:**
108
+
109
+ ```html
110
+ <meta
111
+ http-equiv="Content-Security-Policy"
112
+ content="
113
+ default-src 'self';
114
+ script-src 'self';
115
+ style-src 'self' 'unsafe-inline';
116
+ img-src 'self' data:;
117
+ connect-src 'self' http://localhost;
118
+ " />
119
+ ```
120
+
67
121
  ```typescript
68
122
  import { app, BrowserWindow } from 'electron'
69
123
  import { mainInjection } from '@bigio/better-auth-electron/main'
@@ -76,8 +130,14 @@ const { windowInjection, whenReadyInjection } = mainInjection({
76
130
  PROVIDERS: ['github', 'google'],
77
131
  BETTER_AUTH_BASEURL: 'http://localhost:3002',
78
132
  FRONTEND_URL: 'http://localhost:3001/oauth',
79
- // Use the classic 'onBeforeRequest' filter approach for auth code capture if true
80
- OLD_SCHOOL_ONBEFORE_WAY: false,
133
+ CONTENT_SECURITY_POLICY: '',
134
+ /**
135
+ * [Optional] Content Security Policy (CSP) Configuration
136
+ * * Strategy: "All-or-Nothing"
137
+ * - undefined (Default): The plugin automatically injects a secure, production-ready CSP (The "MVP" Fallback).
138
+ * - string: The plugin uses YOUR string exactly. No merging, no magic. You take full control.
139
+ */
140
+ CONTENT_SECURITY_POLICY: "default-src 'self'; ...", // override completely
81
141
  })
82
142
 
83
143
  function createWindow(): void {
@@ -96,6 +156,21 @@ app.whenReady().then(() => {
96
156
  })
97
157
  ```
98
158
 
159
+ **If CONTENT_SECURITY_POLICY is not provided, the plugin applies the following strictly secure rules to the Main Frame (index.html) automatically. This ensures Auth works out-of-the-box while keeping your app secure.**
160
+
161
+ ```http
162
+ default-src 'self';
163
+ script-src 'self';
164
+ style-src 'self' 'unsafe-inline';
165
+ # Allows loading images from 'self', OAuth providers (https:), and your Auth Server
166
+ img-src 'self' data: blob: https: ${BETTER_AUTH_BASEURL};
167
+ # Strictly restricts API connections to 'self' and your Auth Server
168
+ connect-src 'self' ${BETTER_AUTH_BASEURL};
169
+ font-src 'self' data:;
170
+ # Prevents clickjacking attacks
171
+ frame-ancestors 'none';
172
+ ```
173
+
99
174
  ### 3. Web Client Initialization (`src/web/client.ts`)
100
175
 
101
176
  Configure the client-side plugin. Note the usage of `setLazyClient` to handle circular dependencies or lazy initialization patterns effectively.