@docyrus/signin 0.1.0
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 +410 -0
- package/dist/core-index-CZoxBFk9.d.ts +198 -0
- package/dist/core-index.d.ts +3 -0
- package/dist/core-index.js +509 -0
- package/dist/core-index.js.map +1 -0
- package/dist/electron/index.d.ts +34 -0
- package/dist/electron/index.js +59 -0
- package/dist/electron/index.js.map +1 -0
- package/dist/index.d.ts +73 -0
- package/dist/index.js +679 -0
- package/dist/index.js.map +1 -0
- package/dist/types-C4-_L2Z-.d.ts +62 -0
- package/dist/vue/index.d.ts +177 -0
- package/dist/vue/index.js +678 -0
- package/dist/vue/index.js.map +1 -0
- package/package.json +66 -0
package/README.md
ADDED
|
@@ -0,0 +1,410 @@
|
|
|
1
|
+
# @docyrus/signin
|
|
2
|
+
|
|
3
|
+
Authentication provider for Docyrus apps. Provides "Sign in with Docyrus" experience with automatic environment detection — standalone apps use OAuth2 Authorization Code + PKCE via page redirect, while iframe-embedded apps receive tokens via `window.postMessage` from the host.
|
|
4
|
+
|
|
5
|
+
Supports **React**, **Vue**, and **Electron** via separate entrypoints.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- **Auto Environment Detection**: Detects standalone vs iframe mode automatically
|
|
10
|
+
- **OAuth2 PKCE**: Full Authorization Code flow with PKCE (S256) for standalone apps
|
|
11
|
+
- **Iframe Auth**: Receives tokens via `postMessage` from `*.docyrus.app` hosts
|
|
12
|
+
- **Token Auto-Refresh**: Proactive token refresh before expiry in both modes
|
|
13
|
+
- **React Hooks**: `useDocyrusAuth()` and `useDocyrusClient()` for easy integration
|
|
14
|
+
- **Vue Composables**: `useDocyrusAuth()` and `useDocyrusClient()` for Vue 3
|
|
15
|
+
- **Electron Support**: `ElectronTokenStorage` for IPC-based token storage + `getAuthorizationUrl()` for external browser login
|
|
16
|
+
- **Ready-to-Use API Client**: Exposes a configured `RestApiClient` from `@docyrus/api-client`
|
|
17
|
+
- **SignInButton**: Unstyled, customizable sign-in button component (React + Vue)
|
|
18
|
+
- **SSR Support**: Sync access token to cookie for server-side rendering (Next.js)
|
|
19
|
+
- **TypeScript**: Full type definitions included
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
npm install @docyrus/signin
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pnpm add @docyrus/signin
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### Peer Dependencies
|
|
32
|
+
|
|
33
|
+
- `@docyrus/api-client` >= 0.0.4
|
|
34
|
+
- `react` >= 19.2.0 (optional — required for React/Next.js entrypoint)
|
|
35
|
+
- `vue` >= 3.5.0 (optional — required for Vue entrypoint)
|
|
36
|
+
|
|
37
|
+
## Entrypoints
|
|
38
|
+
|
|
39
|
+
| Import Path | Description | Framework |
|
|
40
|
+
|-------------|-------------|-----------|
|
|
41
|
+
| `@docyrus/signin` | React provider, hooks, components | React, Next.js |
|
|
42
|
+
| `@docyrus/signin/vue` | Vue provider, composables, components | Vue 3 |
|
|
43
|
+
| `@docyrus/signin/electron` | Electron token storage | Electron |
|
|
44
|
+
| `@docyrus/signin/core` | Pure TypeScript core (no framework dependency) | Any |
|
|
45
|
+
|
|
46
|
+
## Quick Start (React)
|
|
47
|
+
|
|
48
|
+
```tsx
|
|
49
|
+
import { DocyrusAuthProvider, useDocyrusAuth, useDocyrusClient, SignInButton } from '@docyrus/signin';
|
|
50
|
+
|
|
51
|
+
function App() {
|
|
52
|
+
return (
|
|
53
|
+
<DocyrusAuthProvider
|
|
54
|
+
apiUrl="https://alpha-api.docyrus.com"
|
|
55
|
+
clientId="your-oauth2-client-id"
|
|
56
|
+
redirectUri="http://localhost:3000/callback"
|
|
57
|
+
scopes={['offline_access', 'Read.All', 'Users.Read']}
|
|
58
|
+
callbackPath="/callback">
|
|
59
|
+
<Dashboard />
|
|
60
|
+
</DocyrusAuthProvider>
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function Dashboard() {
|
|
65
|
+
const { status, signOut } = useDocyrusAuth();
|
|
66
|
+
const client = useDocyrusClient();
|
|
67
|
+
|
|
68
|
+
if (status === 'loading') return <div>Loading...</div>;
|
|
69
|
+
if (status === 'unauthenticated') return <SignInButton />;
|
|
70
|
+
|
|
71
|
+
return (
|
|
72
|
+
<div>
|
|
73
|
+
<p>Authenticated!</p>
|
|
74
|
+
<button onClick={() => client!.get('/v1/users/me').then(console.log)}>
|
|
75
|
+
Fetch user
|
|
76
|
+
</button>
|
|
77
|
+
<button onClick={signOut}>Sign out</button>
|
|
78
|
+
</div>
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Quick Start (Vue)
|
|
84
|
+
|
|
85
|
+
```vue
|
|
86
|
+
<!-- App.vue -->
|
|
87
|
+
<script setup lang="ts">
|
|
88
|
+
import { RouterView } from 'vue-router';
|
|
89
|
+
import { DocyrusAuthProvider } from '@docyrus/signin/vue';
|
|
90
|
+
</script>
|
|
91
|
+
|
|
92
|
+
<template>
|
|
93
|
+
<DocyrusAuthProvider
|
|
94
|
+
apiUrl="https://alpha-api.docyrus.com"
|
|
95
|
+
clientId="your-oauth2-client-id"
|
|
96
|
+
redirectUri="http://localhost:5173/callback"
|
|
97
|
+
:scopes="['offline_access', 'Read.All', 'Users.Read']"
|
|
98
|
+
callbackPath="/callback">
|
|
99
|
+
<RouterView />
|
|
100
|
+
</DocyrusAuthProvider>
|
|
101
|
+
</template>
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
```vue
|
|
105
|
+
<!-- Dashboard.vue -->
|
|
106
|
+
<script setup lang="ts">
|
|
107
|
+
import { useDocyrusAuth } from '@docyrus/signin/vue';
|
|
108
|
+
|
|
109
|
+
const { status, client, signIn, signOut } = useDocyrusAuth();
|
|
110
|
+
</script>
|
|
111
|
+
|
|
112
|
+
<template>
|
|
113
|
+
<div v-if="status === 'loading'">Loading...</div>
|
|
114
|
+
<button v-else-if="status === 'unauthenticated'" @click="signIn">Sign in</button>
|
|
115
|
+
<div v-else>
|
|
116
|
+
<p>Authenticated!</p>
|
|
117
|
+
<button @click="signOut">Sign out</button>
|
|
118
|
+
</div>
|
|
119
|
+
</template>
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Quick Start (Electron)
|
|
123
|
+
|
|
124
|
+
```tsx
|
|
125
|
+
import { DocyrusAuthProvider, useDocyrusAuth } from '@docyrus/signin';
|
|
126
|
+
import { ElectronTokenStorage } from '@docyrus/signin/electron';
|
|
127
|
+
|
|
128
|
+
const tokenStorage = new ElectronTokenStorage();
|
|
129
|
+
|
|
130
|
+
function App() {
|
|
131
|
+
return (
|
|
132
|
+
<DocyrusAuthProvider
|
|
133
|
+
apiUrl="https://alpha-api.docyrus.com"
|
|
134
|
+
clientId="your-oauth2-client-id"
|
|
135
|
+
redirectUri="docyrus://callback"
|
|
136
|
+
scopes={['offline_access', 'Read.All', 'Users.Read']}
|
|
137
|
+
callbackPath="/callback"
|
|
138
|
+
tokenStorage={tokenStorage}>
|
|
139
|
+
<LoginPage />
|
|
140
|
+
</DocyrusAuthProvider>
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function LoginPage() {
|
|
145
|
+
const { status, getAuthorizationUrl } = useDocyrusAuth();
|
|
146
|
+
|
|
147
|
+
const handleLogin = async () => {
|
|
148
|
+
const url = await getAuthorizationUrl();
|
|
149
|
+
if (url) {
|
|
150
|
+
// Open in system browser instead of navigating
|
|
151
|
+
await window.electronAPI.openExternal(url);
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
return <button onClick={handleLogin} disabled={status === 'loading'}>Sign in</button>;
|
|
156
|
+
}
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
## Configuration
|
|
160
|
+
|
|
161
|
+
Pass props to `DocyrusAuthProvider` to override defaults:
|
|
162
|
+
|
|
163
|
+
```tsx
|
|
164
|
+
<DocyrusAuthProvider
|
|
165
|
+
apiUrl="https://alpha-api.docyrus.com"
|
|
166
|
+
clientId="your-oauth2-client-id"
|
|
167
|
+
redirectUri="http://localhost:3000/callback"
|
|
168
|
+
scopes={['offline_access', 'Read.All', 'Users.Read']}
|
|
169
|
+
callbackPath="/callback"
|
|
170
|
+
forceMode="standalone"
|
|
171
|
+
>
|
|
172
|
+
<App />
|
|
173
|
+
</DocyrusAuthProvider>
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
| Prop | Type | Default | Description |
|
|
177
|
+
|------|------|---------|-------------|
|
|
178
|
+
| `apiUrl` | `string` | `https://alpha-api.docyrus.com` | Docyrus API base URL |
|
|
179
|
+
| `clientId` | `string` | **Required** | OAuth2 client ID (throws if missing) |
|
|
180
|
+
| `redirectUri` | `string` | `window.location.origin + callbackPath` | OAuth2 redirect URI |
|
|
181
|
+
| `scopes` | `string[]` | `['offline_access', 'Read.All', ...]` | OAuth2 scopes |
|
|
182
|
+
| `callbackPath` | `string` | `/auth/callback` | Path to detect OAuth callback |
|
|
183
|
+
| `forceMode` | `'standalone' \| 'iframe'` | Auto-detected | Force a specific auth mode |
|
|
184
|
+
| `storageKeyPrefix` | `string` | `docyrus_oauth2_` | localStorage key prefix |
|
|
185
|
+
| `tokenStorage` | `OAuth2TokenStorage` | Browser localStorage | Custom token storage (e.g., `ElectronTokenStorage`) |
|
|
186
|
+
| `allowedHostOrigins` | `string[]` | `undefined` | Extra trusted iframe origins |
|
|
187
|
+
| `ssr` | `boolean` | `false` | Sync access token to cookie for server components |
|
|
188
|
+
| `ssrCookieKey` | `string` | `docyrus-token` | Cookie name for SSR token sync |
|
|
189
|
+
|
|
190
|
+
## Hooks / Composables
|
|
191
|
+
|
|
192
|
+
### useDocyrusAuth()
|
|
193
|
+
|
|
194
|
+
Returns the full authentication context. Available in both React and Vue (`@docyrus/signin/vue`).
|
|
195
|
+
|
|
196
|
+
```tsx
|
|
197
|
+
const {
|
|
198
|
+
status, // 'loading' | 'authenticated' | 'unauthenticated'
|
|
199
|
+
mode, // 'standalone' | 'iframe'
|
|
200
|
+
client, // RestApiClient | null
|
|
201
|
+
tokens, // { accessToken, refreshToken, ... } | null
|
|
202
|
+
signIn, // () => void (redirects to Docyrus login)
|
|
203
|
+
getAuthorizationUrl, // () => Promise<string | null> (returns URL without navigating — for Electron)
|
|
204
|
+
signOut, // () => Promise<void>
|
|
205
|
+
error // Error | null
|
|
206
|
+
} = useDocyrusAuth();
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
### useDocyrusClient()
|
|
210
|
+
|
|
211
|
+
Shorthand hook/composable that returns just the API client:
|
|
212
|
+
|
|
213
|
+
```tsx
|
|
214
|
+
const client = useDocyrusClient(); // RestApiClient | null
|
|
215
|
+
|
|
216
|
+
if (client) {
|
|
217
|
+
const response = await client.get('/v1/users/me');
|
|
218
|
+
}
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
## Components
|
|
222
|
+
|
|
223
|
+
### SignInButton
|
|
224
|
+
|
|
225
|
+
Unstyled button that triggers sign-in. Automatically hidden when authenticated or in iframe mode. Available in both React and Vue.
|
|
226
|
+
|
|
227
|
+
**React:**
|
|
228
|
+
```tsx
|
|
229
|
+
// Basic
|
|
230
|
+
<SignInButton />
|
|
231
|
+
|
|
232
|
+
// With custom styling
|
|
233
|
+
<SignInButton className="btn btn-primary" label="Log in with Docyrus" />
|
|
234
|
+
|
|
235
|
+
// Render prop for full customization
|
|
236
|
+
<SignInButton>
|
|
237
|
+
{({ signIn, isLoading }) => (
|
|
238
|
+
<button onClick={signIn} disabled={isLoading}>
|
|
239
|
+
{isLoading ? 'Redirecting...' : 'Sign in'}
|
|
240
|
+
</button>
|
|
241
|
+
)}
|
|
242
|
+
</SignInButton>
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
**Vue:**
|
|
246
|
+
```vue
|
|
247
|
+
<!-- Basic -->
|
|
248
|
+
<SignInButton />
|
|
249
|
+
|
|
250
|
+
<!-- With custom styling -->
|
|
251
|
+
<SignInButton label="Login" class="btn-primary" />
|
|
252
|
+
|
|
253
|
+
<!-- Scoped slot for custom rendering -->
|
|
254
|
+
<SignInButton v-slot="{ signIn, status }">
|
|
255
|
+
<MyCustomButton @click="signIn" :loading="status === 'loading'" />
|
|
256
|
+
</SignInButton>
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
## Electron Token Storage
|
|
260
|
+
|
|
261
|
+
`ElectronTokenStorage` stores OAuth2 tokens via IPC bridge to the Electron main process. Falls back to localStorage if the Electron API is not available.
|
|
262
|
+
|
|
263
|
+
```tsx
|
|
264
|
+
import { ElectronTokenStorage } from '@docyrus/signin/electron';
|
|
265
|
+
|
|
266
|
+
// Auto-detects window.electronAPI
|
|
267
|
+
const storage = new ElectronTokenStorage();
|
|
268
|
+
|
|
269
|
+
// Or pass a custom IPC bridge
|
|
270
|
+
const storage = new ElectronTokenStorage(myElectronAPI);
|
|
271
|
+
|
|
272
|
+
// Custom key prefix
|
|
273
|
+
const storage = new ElectronTokenStorage(undefined, 'my_app_oauth2');
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
The Electron preload script must expose `storeGet`, `storeSet`, and `storeDelete` IPC methods via `contextBridge`.
|
|
277
|
+
|
|
278
|
+
## Auth Modes
|
|
279
|
+
|
|
280
|
+
### Standalone Mode (OAuth2 PKCE)
|
|
281
|
+
|
|
282
|
+
Used when the app runs directly in the browser (not in an iframe). The flow:
|
|
283
|
+
|
|
284
|
+
1. User clicks sign-in
|
|
285
|
+
2. Page redirects to Docyrus authorization endpoint
|
|
286
|
+
3. After login, redirects back with authorization code
|
|
287
|
+
4. Provider automatically exchanges code for tokens
|
|
288
|
+
5. Tokens stored in localStorage (or custom `tokenStorage`), auto-refreshed before expiry
|
|
289
|
+
|
|
290
|
+
### Iframe Mode (postMessage)
|
|
291
|
+
|
|
292
|
+
Used when the app is embedded in an iframe on `*.docyrus.app`. The flow:
|
|
293
|
+
|
|
294
|
+
1. Provider detects iframe environment and validates host origin
|
|
295
|
+
2. Host sends `{ type: 'signin', accessToken, refreshToken }` via `postMessage`
|
|
296
|
+
3. Provider creates API client with received tokens
|
|
297
|
+
4. When tokens expire, provider sends `{ type: 'token-refresh-request' }` to host
|
|
298
|
+
5. Host responds with fresh tokens
|
|
299
|
+
|
|
300
|
+
## SSR Support (Next.js)
|
|
301
|
+
|
|
302
|
+
Enable `ssr` to sync the access token to a non-httpOnly cookie. Server components can then read the token via `cookies()` for server-side data fetching.
|
|
303
|
+
|
|
304
|
+
**Client — Enable SSR in AuthProvider:**
|
|
305
|
+
|
|
306
|
+
```tsx
|
|
307
|
+
<DocyrusAuthProvider
|
|
308
|
+
apiUrl="https://alpha-api.docyrus.com"
|
|
309
|
+
clientId="your-oauth2-client-id"
|
|
310
|
+
scopes={['offline_access', 'Read.All', 'Users.Read']}
|
|
311
|
+
callbackPath="/auth/callback"
|
|
312
|
+
ssr={true}>
|
|
313
|
+
{children}
|
|
314
|
+
</DocyrusAuthProvider>
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
The cookie is automatically:
|
|
318
|
+
- **Set** on login and token refresh
|
|
319
|
+
- **Cleared** on sign out
|
|
320
|
+
|
|
321
|
+
**Server — Read token in server components:**
|
|
322
|
+
|
|
323
|
+
```tsx
|
|
324
|
+
import { cookies } from 'next/headers';
|
|
325
|
+
|
|
326
|
+
export default async function Layout({ children }: { children: React.ReactNode }) {
|
|
327
|
+
const cookieStore = await cookies();
|
|
328
|
+
const token = cookieStore.get('docyrus-token')?.value;
|
|
329
|
+
|
|
330
|
+
// Use token for server-side API calls (e.g., i18n translations)
|
|
331
|
+
const translations = token
|
|
332
|
+
? await fetch('https://alpha-api.docyrus.com/v1/tenant/translations', {
|
|
333
|
+
headers: { Authorization: `Bearer ${token}` }
|
|
334
|
+
}).then(r => r.json())
|
|
335
|
+
: null;
|
|
336
|
+
|
|
337
|
+
return <>{children}</>;
|
|
338
|
+
}
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
**Cookie helper (client-side):**
|
|
342
|
+
|
|
343
|
+
```tsx
|
|
344
|
+
import { getTokenFromCookie } from '@docyrus/signin';
|
|
345
|
+
|
|
346
|
+
const token = getTokenFromCookie(); // reads 'docyrus-token' cookie
|
|
347
|
+
const token = getTokenFromCookie('custom-key'); // custom cookie name
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
## Advanced Usage
|
|
351
|
+
|
|
352
|
+
### Core (Framework-Agnostic)
|
|
353
|
+
|
|
354
|
+
Core classes are exported for advanced use cases without any framework dependency:
|
|
355
|
+
|
|
356
|
+
```tsx
|
|
357
|
+
import {
|
|
358
|
+
AuthManager,
|
|
359
|
+
StandaloneOAuth2Auth,
|
|
360
|
+
IframeAuth,
|
|
361
|
+
detectAuthMode,
|
|
362
|
+
} from '@docyrus/signin/core';
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
### React Entrypoint
|
|
366
|
+
|
|
367
|
+
```tsx
|
|
368
|
+
import {
|
|
369
|
+
DocyrusAuthProvider,
|
|
370
|
+
useDocyrusAuth,
|
|
371
|
+
useDocyrusClient,
|
|
372
|
+
SignInButton,
|
|
373
|
+
AuthManager,
|
|
374
|
+
detectAuthMode
|
|
375
|
+
} from '@docyrus/signin';
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
### Vue Entrypoint
|
|
379
|
+
|
|
380
|
+
```tsx
|
|
381
|
+
import {
|
|
382
|
+
DocyrusAuthProvider,
|
|
383
|
+
useDocyrusAuth,
|
|
384
|
+
useDocyrusClient,
|
|
385
|
+
SignInButton
|
|
386
|
+
} from '@docyrus/signin/vue';
|
|
387
|
+
```
|
|
388
|
+
|
|
389
|
+
## Development
|
|
390
|
+
|
|
391
|
+
```bash
|
|
392
|
+
# Install dependencies
|
|
393
|
+
pnpm install
|
|
394
|
+
|
|
395
|
+
# Development mode with watch
|
|
396
|
+
pnpm dev
|
|
397
|
+
|
|
398
|
+
# Build the package
|
|
399
|
+
pnpm build
|
|
400
|
+
|
|
401
|
+
# Run linting
|
|
402
|
+
pnpm lint
|
|
403
|
+
|
|
404
|
+
# Type checking
|
|
405
|
+
pnpm typecheck
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
## License
|
|
409
|
+
|
|
410
|
+
MIT
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { OAuth2Tokens, RestApiClient } from '@docyrus/api-client';
|
|
2
|
+
import { D as DocyrusAuthConfig, A as AuthMode, a as AuthStatus } from './types-C4-_L2Z-.js';
|
|
3
|
+
|
|
4
|
+
type AuthStateListener = (state: {
|
|
5
|
+
status: AuthStatus;
|
|
6
|
+
tokens: OAuth2Tokens | null;
|
|
7
|
+
error: Error | null;
|
|
8
|
+
}) => void;
|
|
9
|
+
/**
|
|
10
|
+
* Unified authentication manager.
|
|
11
|
+
* Orchestrates standalone OAuth2 and iframe postMessage modes,
|
|
12
|
+
* exposes a pre-configured RestApiClient, and manages token lifecycle.
|
|
13
|
+
*/
|
|
14
|
+
declare class AuthManager {
|
|
15
|
+
private mode;
|
|
16
|
+
private status;
|
|
17
|
+
private tokens;
|
|
18
|
+
private error;
|
|
19
|
+
private client;
|
|
20
|
+
private standaloneAuth;
|
|
21
|
+
private iframeAuth;
|
|
22
|
+
private tokenRefreshTimer;
|
|
23
|
+
private listeners;
|
|
24
|
+
private config;
|
|
25
|
+
constructor(config?: DocyrusAuthConfig);
|
|
26
|
+
getMode(): AuthMode;
|
|
27
|
+
getStatus(): AuthStatus;
|
|
28
|
+
getTokens(): OAuth2Tokens | null;
|
|
29
|
+
getClient(): RestApiClient | null;
|
|
30
|
+
getError(): Error | null;
|
|
31
|
+
subscribe(listener: AuthStateListener): () => void;
|
|
32
|
+
private notify;
|
|
33
|
+
/** Initialize the auth manager. Must be called once on mount. */
|
|
34
|
+
initialize(): Promise<void>;
|
|
35
|
+
/**
|
|
36
|
+
* Standalone mode initialization.
|
|
37
|
+
* 1. Check if we're returning from an OAuth callback.
|
|
38
|
+
* 2. If not, check for existing tokens in localStorage.
|
|
39
|
+
* 3. If tokens are expired, try to refresh.
|
|
40
|
+
*/
|
|
41
|
+
private initializeStandaloneMode;
|
|
42
|
+
/**
|
|
43
|
+
* Iframe mode initialization.
|
|
44
|
+
* Listen for postMessage tokens from the host.
|
|
45
|
+
* Status remains 'loading' until the host sends the first signin message.
|
|
46
|
+
*/
|
|
47
|
+
private initializeIframeMode;
|
|
48
|
+
/** Initiate sign-in. Only meaningful in standalone mode. */
|
|
49
|
+
signIn(): Promise<void>;
|
|
50
|
+
/**
|
|
51
|
+
* Get the OAuth2 authorization URL without navigating.
|
|
52
|
+
* Use this in Electron apps to open the URL in an external browser.
|
|
53
|
+
* Returns null if not in standalone mode.
|
|
54
|
+
*/
|
|
55
|
+
getAuthorizationUrl(): Promise<string | null>;
|
|
56
|
+
/**
|
|
57
|
+
* Sign out.
|
|
58
|
+
* Standalone: revoke token, clear localStorage, reset state.
|
|
59
|
+
* Iframe: clear local state (host manages the actual session).
|
|
60
|
+
*/
|
|
61
|
+
signOut(): Promise<void>;
|
|
62
|
+
/**
|
|
63
|
+
* Called when valid tokens are received (either mode).
|
|
64
|
+
* Creates/updates the RestApiClient and schedules token refresh.
|
|
65
|
+
*/
|
|
66
|
+
private setAuthenticated;
|
|
67
|
+
/**
|
|
68
|
+
* Create a RestApiClient with a custom TokenManager that proactively
|
|
69
|
+
* refreshes the token in getToken(). This is necessary because
|
|
70
|
+
* BaseApiClient.getAccessToken() only calls tokenManager.getToken()
|
|
71
|
+
* and does NOT auto-call refreshToken() when the token is expired.
|
|
72
|
+
*/
|
|
73
|
+
private createClient;
|
|
74
|
+
/**
|
|
75
|
+
* Get a valid access token, proactively refreshing if expired.
|
|
76
|
+
* Called by the RestApiClient on every request via tokenManager.getToken().
|
|
77
|
+
*/
|
|
78
|
+
private getValidToken;
|
|
79
|
+
/**
|
|
80
|
+
* Handle token refresh depending on mode.
|
|
81
|
+
* Standalone: use OAuth2Client.getValidAccessToken() which auto-refreshes.
|
|
82
|
+
* Iframe: send postMessage to host requesting new tokens.
|
|
83
|
+
*/
|
|
84
|
+
private handleTokenRefresh;
|
|
85
|
+
/** Schedule proactive token refresh before expiry. */
|
|
86
|
+
private scheduleTokenRefresh;
|
|
87
|
+
private clearTokenRefreshTimer;
|
|
88
|
+
/** Cleanup: remove listeners, timers, stop iframe auth. */
|
|
89
|
+
destroy(): void;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Standalone OAuth2 authorization code flow with PKCE via page redirect.
|
|
94
|
+
* Wraps api-client's OAuth2Client and BrowserOAuth2TokenStorage.
|
|
95
|
+
*/
|
|
96
|
+
declare class StandaloneOAuth2Auth {
|
|
97
|
+
private oauth2Client;
|
|
98
|
+
private storage;
|
|
99
|
+
private callbackPath;
|
|
100
|
+
constructor(config?: DocyrusAuthConfig);
|
|
101
|
+
/**
|
|
102
|
+
* Check if the current page URL is the OAuth2 callback.
|
|
103
|
+
* Called on app initialization to detect returning from the auth server.
|
|
104
|
+
*/
|
|
105
|
+
isCallbackUrl(): boolean;
|
|
106
|
+
/**
|
|
107
|
+
* Initiate the OAuth2 authorization code flow.
|
|
108
|
+
* Stores the current URL for post-auth redirect, then navigates
|
|
109
|
+
* the page to the authorization endpoint.
|
|
110
|
+
*
|
|
111
|
+
* PKCE state (codeVerifier, state) is stored in localStorage
|
|
112
|
+
* by the OAuth2Client and survives the page redirect.
|
|
113
|
+
*/
|
|
114
|
+
initiateLogin(): Promise<void>;
|
|
115
|
+
/**
|
|
116
|
+
* Get the authorization URL without navigating.
|
|
117
|
+
* Useful for Electron apps that open the URL in an external browser.
|
|
118
|
+
* PKCE state is stored in storage just like initiateLogin().
|
|
119
|
+
*/
|
|
120
|
+
getAuthorizationUrl(): Promise<string>;
|
|
121
|
+
/**
|
|
122
|
+
* Handle the OAuth2 callback URL.
|
|
123
|
+
* Reads PKCE state from localStorage, validates the state param,
|
|
124
|
+
* exchanges the authorization code for tokens, and stores them.
|
|
125
|
+
*/
|
|
126
|
+
handleCallback(): Promise<OAuth2Tokens>;
|
|
127
|
+
/**
|
|
128
|
+
* Get the URL the user was on before the OAuth redirect.
|
|
129
|
+
* Clears the stored value after retrieval.
|
|
130
|
+
*/
|
|
131
|
+
getReturnUrl(): string | null;
|
|
132
|
+
/** Get currently stored tokens (e.g., on page reload). */
|
|
133
|
+
getStoredTokens(): Promise<OAuth2Tokens | null>;
|
|
134
|
+
/** Check if the stored token is expired (with 60s buffer). */
|
|
135
|
+
isTokenExpired(): Promise<boolean>;
|
|
136
|
+
/** Refresh the access token using the stored refresh token. */
|
|
137
|
+
refreshToken(): Promise<OAuth2Tokens>;
|
|
138
|
+
/** Get a valid access token, refreshing if needed. */
|
|
139
|
+
getValidAccessToken(): Promise<string>;
|
|
140
|
+
/** Logout: revoke token and clear storage. */
|
|
141
|
+
logout(): Promise<void>;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
type IframeAuthCallback = (tokens: OAuth2Tokens) => void;
|
|
145
|
+
/**
|
|
146
|
+
* Iframe authentication via postMessage.
|
|
147
|
+
*
|
|
148
|
+
* Protocol:
|
|
149
|
+
* - Host → App: { type: "signin", accessToken, refreshToken }
|
|
150
|
+
* - App → Host: { type: "token-refresh-request" }
|
|
151
|
+
*
|
|
152
|
+
* Every incoming message's event.origin is validated against
|
|
153
|
+
* the allowed host pattern before processing.
|
|
154
|
+
*/
|
|
155
|
+
declare class IframeAuth {
|
|
156
|
+
private allowedOrigins;
|
|
157
|
+
private allowedPattern;
|
|
158
|
+
private onTokensReceived;
|
|
159
|
+
private refreshPromise;
|
|
160
|
+
private messageHandler;
|
|
161
|
+
constructor(allowedOrigins?: string[], allowedPattern?: RegExp);
|
|
162
|
+
/**
|
|
163
|
+
* Start listening for postMessage events from the host.
|
|
164
|
+
* The callback is invoked every time valid tokens are received.
|
|
165
|
+
*/
|
|
166
|
+
start(onTokensReceived: IframeAuthCallback): void;
|
|
167
|
+
/** Stop listening for postMessage events. */
|
|
168
|
+
stop(): void;
|
|
169
|
+
/**
|
|
170
|
+
* Request fresh tokens from the host.
|
|
171
|
+
* Posts a "token-refresh-request" to the host, then waits for
|
|
172
|
+
* a "signin" response.
|
|
173
|
+
*
|
|
174
|
+
* Only one refresh request can be in-flight at a time.
|
|
175
|
+
* Additional callers share the same promise.
|
|
176
|
+
*/
|
|
177
|
+
requestTokenRefresh(): Promise<OAuth2Tokens>;
|
|
178
|
+
private isOriginAllowed;
|
|
179
|
+
private handleMessage;
|
|
180
|
+
private rejectPendingRefresh;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Detect whether the app is inside an iframe from a trusted *.docyrus.app host
|
|
185
|
+
* or running as a standalone page.
|
|
186
|
+
*
|
|
187
|
+
* If we are in an iframe but NOT from a trusted origin, we fall back
|
|
188
|
+
* to standalone mode (graceful degradation).
|
|
189
|
+
*/
|
|
190
|
+
declare function detectAuthMode(allowedOrigins?: string[], allowedPattern?: RegExp): AuthMode;
|
|
191
|
+
|
|
192
|
+
declare const DEFAULT_API_URL = "https://alpha-api.docyrus.com";
|
|
193
|
+
declare const DEFAULT_OAUTH_SCOPES: string[];
|
|
194
|
+
declare const DEFAULT_CALLBACK_PATH = "/auth/callback";
|
|
195
|
+
/** Default cookie name for SSR token sync */
|
|
196
|
+
declare const DEFAULT_SSR_COOKIE_KEY = "docyrus-token";
|
|
197
|
+
|
|
198
|
+
export { AuthManager as A, DEFAULT_API_URL as D, IframeAuth as I, StandaloneOAuth2Auth as S, DEFAULT_CALLBACK_PATH as a, DEFAULT_OAUTH_SCOPES as b, DEFAULT_SSR_COOKIE_KEY as c, detectAuthMode as d, type AuthStateListener as e };
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { A as AuthManager, e as AuthStateListener, D as DEFAULT_API_URL, a as DEFAULT_CALLBACK_PATH, b as DEFAULT_OAUTH_SCOPES, I as IframeAuth, S as StandaloneOAuth2Auth, d as detectAuthMode } from './core-index-CZoxBFk9.js';
|
|
2
|
+
export { A as AuthMode, a as AuthStatus, D as DocyrusAuthConfig, b as DocyrusAuthContextValue, H as HostSignInMessage, T as TokenRefreshRequestMessage } from './types-C4-_L2Z-.js';
|
|
3
|
+
import '@docyrus/api-client';
|