local-lite 0.1.4 → 0.2.1
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/dist/auth/apple.d.ts +3 -4
- package/dist/auth/apple.js +26 -3
- package/dist/auth/github.d.ts +3 -5
- package/dist/auth/github.js +18 -4
- package/dist/auth/google.d.ts +3 -6
- package/dist/auth/google.js +19 -5
- package/dist/auth/identity.d.ts +42 -0
- package/dist/auth/identity.js +63 -0
- package/dist/auth/index.d.ts +2 -0
- package/dist/auth/index.js +2 -0
- package/dist/auth/microsoft.d.ts +3 -5
- package/dist/auth/microsoft.js +16 -3
- package/dist/auth/session.d.ts +4 -0
- package/dist/auth/session.js +19 -1
- package/dist/auth/unified.d.ts +73 -0
- package/dist/auth/unified.js +188 -0
- package/package.json +1 -1
package/dist/auth/apple.d.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
+
import { type AuthUser } from './identity.js';
|
|
1
2
|
export declare const APPLE_AUTHORIZATION_ENDPOINT = "https://appleid.apple.com/auth/authorize";
|
|
2
3
|
export declare const APPLE_TOKEN_ENDPOINT = "https://appleid.apple.com/auth/token";
|
|
3
4
|
export declare const DEFAULT_APPLE_AUTH_SCOPES: string[];
|
|
4
|
-
export interface AppleUser {
|
|
5
|
-
|
|
6
|
-
email: string;
|
|
7
|
-
name: string;
|
|
5
|
+
export interface AppleUser extends AuthUser {
|
|
6
|
+
provider: 'apple';
|
|
8
7
|
givenName?: string;
|
|
9
8
|
familyName?: string;
|
|
10
9
|
identityToken?: string;
|
package/dist/auth/apple.js
CHANGED
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.AppleAuthClient = exports.DEFAULT_APPLE_AUTH_SCOPES = exports.APPLE_TOKEN_ENDPOINT = exports.APPLE_AUTHORIZATION_ENDPOINT = void 0;
|
|
4
4
|
exports.createInMemoryAppleTokenStore = createInMemoryAppleTokenStore;
|
|
5
5
|
exports.createAppleAuthTokenStore = createAppleAuthTokenStore;
|
|
6
|
+
const identity_js_1 = require("./identity.js");
|
|
6
7
|
exports.APPLE_AUTHORIZATION_ENDPOINT = 'https://appleid.apple.com/auth/authorize';
|
|
7
8
|
exports.APPLE_TOKEN_ENDPOINT = 'https://appleid.apple.com/auth/token';
|
|
8
9
|
exports.DEFAULT_APPLE_AUTH_SCOPES = ['name', 'email'];
|
|
@@ -200,14 +201,24 @@ class AppleAuthClient {
|
|
|
200
201
|
expiresAt: claims.exp ? claims.exp * 1000 : undefined,
|
|
201
202
|
};
|
|
202
203
|
await this.tokenStore.setTokens(tokens);
|
|
204
|
+
const canonicalId = (0, identity_js_1.resolveUnifiedUserId)({ id, email, emailVerified: Boolean(claims.email_verified ?? true) }, 'apple');
|
|
203
205
|
const user = {
|
|
204
|
-
id,
|
|
206
|
+
id: canonicalId,
|
|
207
|
+
userId: canonicalId,
|
|
205
208
|
email,
|
|
209
|
+
emailVerified: Boolean(claims.email_verified ?? true),
|
|
206
210
|
name,
|
|
211
|
+
displayName: name,
|
|
212
|
+
avatarUrl: null,
|
|
213
|
+
picture: null,
|
|
214
|
+
provider: 'apple',
|
|
215
|
+
providerAccountId: id,
|
|
207
216
|
givenName,
|
|
208
217
|
familyName,
|
|
209
218
|
identityToken: authorization.id_token,
|
|
210
219
|
authorizationCode: authorization.code,
|
|
220
|
+
rawProfile: claims,
|
|
221
|
+
createdAt: Date.now(),
|
|
211
222
|
};
|
|
212
223
|
await this.tokenStore.setUser(user);
|
|
213
224
|
this.notify(user);
|
|
@@ -226,14 +237,26 @@ class AppleAuthClient {
|
|
|
226
237
|
const resolvedGivenName = givenName || priorUser?.givenName;
|
|
227
238
|
const resolvedFamilyName = familyName || priorUser?.familyName;
|
|
228
239
|
const resolvedName = name !== email.split('@')[0] ? name : priorUser?.name || name;
|
|
240
|
+
const rawId = String(cred.user || claims.sub || 'apple_user');
|
|
241
|
+
const finalEmail = cred.email || priorUser?.email || email;
|
|
242
|
+
const canonicalId = (0, identity_js_1.resolveUnifiedUserId)({ id: rawId, email: finalEmail, emailVerified: true }, 'apple');
|
|
229
243
|
const user = {
|
|
230
|
-
id,
|
|
231
|
-
|
|
244
|
+
id: canonicalId,
|
|
245
|
+
userId: canonicalId,
|
|
246
|
+
email: finalEmail,
|
|
247
|
+
emailVerified: true,
|
|
232
248
|
name: resolvedName,
|
|
249
|
+
displayName: resolvedName,
|
|
250
|
+
avatarUrl: null,
|
|
251
|
+
picture: null,
|
|
252
|
+
provider: 'apple',
|
|
253
|
+
providerAccountId: rawId,
|
|
233
254
|
givenName: resolvedGivenName,
|
|
234
255
|
familyName: resolvedFamilyName,
|
|
235
256
|
identityToken: cred.identityToken || undefined,
|
|
236
257
|
authorizationCode: cred.authorizationCode || undefined,
|
|
258
|
+
rawProfile: claims,
|
|
259
|
+
createdAt: Date.now(),
|
|
237
260
|
};
|
|
238
261
|
if (cred.identityToken) {
|
|
239
262
|
await this.tokenStore.setTokens({
|
package/dist/auth/github.d.ts
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
|
+
import { type AuthUser } from './identity.js';
|
|
1
2
|
export declare const GITHUB_AUTHORIZATION_ENDPOINT = "https://github.com/login/oauth/authorize";
|
|
2
3
|
export declare const GITHUB_TOKEN_ENDPOINT = "https://github.com/login/oauth/access_token";
|
|
3
4
|
export declare const GITHUB_API_ENDPOINT = "https://api.github.com";
|
|
4
5
|
export declare const DEFAULT_GITHUB_AUTH_SCOPES: string[];
|
|
5
|
-
export interface GitHubUser {
|
|
6
|
-
|
|
6
|
+
export interface GitHubUser extends AuthUser {
|
|
7
|
+
provider: 'github';
|
|
7
8
|
login: string;
|
|
8
|
-
name: string;
|
|
9
|
-
email: string;
|
|
10
|
-
avatarUrl?: string;
|
|
11
9
|
bio?: string;
|
|
12
10
|
company?: string;
|
|
13
11
|
location?: string;
|
package/dist/auth/github.js
CHANGED
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.GitHubAuthClient = exports.DEFAULT_GITHUB_AUTH_SCOPES = exports.GITHUB_API_ENDPOINT = exports.GITHUB_TOKEN_ENDPOINT = exports.GITHUB_AUTHORIZATION_ENDPOINT = void 0;
|
|
4
4
|
exports.createInMemoryGitHubTokenStore = createInMemoryGitHubTokenStore;
|
|
5
5
|
exports.createGitHubAuthTokenStore = createGitHubAuthTokenStore;
|
|
6
|
+
const identity_js_1 = require("./identity.js");
|
|
6
7
|
exports.GITHUB_AUTHORIZATION_ENDPOINT = 'https://github.com/login/oauth/authorize';
|
|
7
8
|
exports.GITHUB_TOKEN_ENDPOINT = 'https://github.com/login/oauth/access_token';
|
|
8
9
|
exports.GITHUB_API_ENDPOINT = 'https://api.github.com';
|
|
@@ -273,15 +274,28 @@ class GitHubAuthClient {
|
|
|
273
274
|
// Fallback to username generated placeholder if emails restricted
|
|
274
275
|
}
|
|
275
276
|
}
|
|
277
|
+
const rawId = String(raw.id ?? raw.sub ?? 'github_user');
|
|
278
|
+
const normalizedEmail = email ? email.trim().toLowerCase() : `${raw.login}@users.noreply.github.com`;
|
|
279
|
+
const id = (0, identity_js_1.resolveUnifiedUserId)({ id: rawId, email: normalizedEmail, emailVerified: true }, 'github');
|
|
280
|
+
const name = raw.name || raw.login || 'GitHub User';
|
|
281
|
+
const avatarUrl = raw.avatar_url || null;
|
|
276
282
|
return {
|
|
277
|
-
id
|
|
283
|
+
id,
|
|
284
|
+
userId: id,
|
|
278
285
|
login: raw.login,
|
|
279
|
-
name
|
|
280
|
-
|
|
281
|
-
|
|
286
|
+
name,
|
|
287
|
+
displayName: name,
|
|
288
|
+
email: normalizedEmail,
|
|
289
|
+
emailVerified: true,
|
|
290
|
+
provider: 'github',
|
|
291
|
+
providerAccountId: rawId,
|
|
292
|
+
avatarUrl,
|
|
293
|
+
picture: avatarUrl,
|
|
282
294
|
bio: raw.bio,
|
|
283
295
|
company: raw.company,
|
|
284
296
|
location: raw.location,
|
|
297
|
+
rawProfile: raw,
|
|
298
|
+
createdAt: Date.now(),
|
|
285
299
|
};
|
|
286
300
|
}
|
|
287
301
|
}
|
package/dist/auth/google.d.ts
CHANGED
|
@@ -1,16 +1,13 @@
|
|
|
1
|
+
import { type AuthUser } from './identity.js';
|
|
1
2
|
import { GoogleDriveBackupStorage, GoogleDriveBackupClient, type GoogleDriveBackupClientOptions } from '../backups/cloud-storage.js';
|
|
2
3
|
export declare const GOOGLE_AUTHORIZATION_ENDPOINT = "https://accounts.google.com/o/oauth2/v2/auth";
|
|
3
4
|
export declare const GOOGLE_TOKEN_ENDPOINT = "https://oauth2.googleapis.com/token";
|
|
4
5
|
export declare const GOOGLE_USERINFO_ENDPOINT = "https://www.googleapis.com/oauth2/v3/userinfo";
|
|
5
6
|
export declare const DEFAULT_GOOGLE_AUTH_SCOPES: string[];
|
|
6
|
-
export interface GoogleUser {
|
|
7
|
-
|
|
8
|
-
email: string;
|
|
9
|
-
emailVerified: boolean;
|
|
10
|
-
name: string;
|
|
7
|
+
export interface GoogleUser extends AuthUser {
|
|
8
|
+
provider: 'google';
|
|
11
9
|
givenName?: string;
|
|
12
10
|
familyName?: string;
|
|
13
|
-
picture?: string;
|
|
14
11
|
locale?: string;
|
|
15
12
|
}
|
|
16
13
|
export interface GoogleAuthTokens {
|
package/dist/auth/google.js
CHANGED
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.GoogleAuthClient = exports.DEFAULT_GOOGLE_AUTH_SCOPES = exports.GOOGLE_USERINFO_ENDPOINT = exports.GOOGLE_TOKEN_ENDPOINT = exports.GOOGLE_AUTHORIZATION_ENDPOINT = void 0;
|
|
4
4
|
exports.createInMemoryGoogleTokenStore = createInMemoryGoogleTokenStore;
|
|
5
5
|
exports.createGoogleAuthTokenStore = createGoogleAuthTokenStore;
|
|
6
|
+
const identity_js_1 = require("./identity.js");
|
|
6
7
|
const sha2_js_1 = require("@noble/hashes/sha2.js");
|
|
7
8
|
const utils_js_1 = require("@noble/hashes/utils.js");
|
|
8
9
|
const cloud_storage_js_1 = require("../backups/cloud-storage.js");
|
|
@@ -309,15 +310,28 @@ class GoogleAuthClient {
|
|
|
309
310
|
throw new Error(`Failed to fetch Google user profile (${res.status}).`);
|
|
310
311
|
}
|
|
311
312
|
const raw = (await res.json());
|
|
313
|
+
const email = raw.email ? String(raw.email).trim().toLowerCase() : '';
|
|
314
|
+
const emailVerified = Boolean(raw.email_verified ?? true);
|
|
315
|
+
const sub = String(raw.sub || raw.id || 'google_user');
|
|
316
|
+
const id = (0, identity_js_1.resolveUnifiedUserId)({ id: sub, email, emailVerified }, 'google');
|
|
317
|
+
const name = raw.name || raw.email || 'Google User';
|
|
318
|
+
const avatarUrl = raw.picture || null;
|
|
312
319
|
return {
|
|
313
|
-
id
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
320
|
+
id,
|
|
321
|
+
userId: id,
|
|
322
|
+
email,
|
|
323
|
+
emailVerified,
|
|
324
|
+
name,
|
|
325
|
+
displayName: name,
|
|
326
|
+
avatarUrl,
|
|
327
|
+
picture: avatarUrl,
|
|
328
|
+
provider: 'google',
|
|
329
|
+
providerAccountId: sub,
|
|
317
330
|
givenName: raw.given_name,
|
|
318
331
|
familyName: raw.family_name,
|
|
319
|
-
picture: raw.picture,
|
|
320
332
|
locale: raw.locale,
|
|
333
|
+
rawProfile: raw,
|
|
334
|
+
createdAt: Date.now(),
|
|
321
335
|
};
|
|
322
336
|
}
|
|
323
337
|
createDriveBackupStorage() {
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export declare const DEFAULT_UNIFIED_EMAIL_NAMESPACE = "6ba7b810-9dad-11d1-80b4-00c04fd430c8";
|
|
2
|
+
export type SupportedAuthProvider = 'google' | 'github' | 'microsoft' | 'apple' | 'local';
|
|
3
|
+
/**
|
|
4
|
+
* Standardized first-class user identity across all auth providers in local-lite.
|
|
5
|
+
* Every provider client natively resolves and returns this canonical model.
|
|
6
|
+
*/
|
|
7
|
+
export interface AuthUser {
|
|
8
|
+
/** Canonical deterministic UUID across all providers with the same verified email */
|
|
9
|
+
id: string;
|
|
10
|
+
/** Alias for id to ensure compatibility with session records and sync workers */
|
|
11
|
+
userId: string;
|
|
12
|
+
email: string;
|
|
13
|
+
emailVerified: boolean;
|
|
14
|
+
name: string;
|
|
15
|
+
displayName: string;
|
|
16
|
+
avatarUrl?: string | null;
|
|
17
|
+
picture?: string | null;
|
|
18
|
+
provider: SupportedAuthProvider;
|
|
19
|
+
providerAccountId: string;
|
|
20
|
+
rawProfile?: Record<string, any>;
|
|
21
|
+
createdAt?: number;
|
|
22
|
+
}
|
|
23
|
+
export type UnifiedAuthUser = AuthUser;
|
|
24
|
+
export interface IdentityResolverOptions {
|
|
25
|
+
namespace?: string;
|
|
26
|
+
requireVerifiedEmail?: boolean;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Resolves a canonical UUID for an incoming profile.
|
|
30
|
+
* If email is verified, hashes the normalized email against the UUIDv5 namespace.
|
|
31
|
+
* Falls back to provider-scoped deterministic ID if unverified or email is missing.
|
|
32
|
+
*/
|
|
33
|
+
export declare function resolveUnifiedUserId(profile: {
|
|
34
|
+
id: string | number;
|
|
35
|
+
email?: string | null;
|
|
36
|
+
emailVerified?: boolean;
|
|
37
|
+
}, provider: SupportedAuthProvider, options?: IdentityResolverOptions): string;
|
|
38
|
+
/**
|
|
39
|
+
* Standardizes raw user records from Google, GitHub, Microsoft, Apple, or local auth
|
|
40
|
+
* into a single unified user object with a canonical `id` / `userId`.
|
|
41
|
+
*/
|
|
42
|
+
export declare function createUnifiedAuthUser(rawUser: Record<string, any>, provider: SupportedAuthProvider, options?: IdentityResolverOptions): AuthUser;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DEFAULT_UNIFIED_EMAIL_NAMESPACE = void 0;
|
|
4
|
+
exports.resolveUnifiedUserId = resolveUnifiedUserId;
|
|
5
|
+
exports.createUnifiedAuthUser = createUnifiedAuthUser;
|
|
6
|
+
const uuid_1 = require("uuid");
|
|
7
|
+
exports.DEFAULT_UNIFIED_EMAIL_NAMESPACE = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
|
|
8
|
+
/**
|
|
9
|
+
* Resolves a canonical UUID for an incoming profile.
|
|
10
|
+
* If email is verified, hashes the normalized email against the UUIDv5 namespace.
|
|
11
|
+
* Falls back to provider-scoped deterministic ID if unverified or email is missing.
|
|
12
|
+
*/
|
|
13
|
+
function resolveUnifiedUserId(profile, provider, options) {
|
|
14
|
+
const ns = options?.namespace || exports.DEFAULT_UNIFIED_EMAIL_NAMESPACE;
|
|
15
|
+
const requireVerified = options?.requireVerifiedEmail ?? true;
|
|
16
|
+
const isVerified = !requireVerified || profile.emailVerified === true;
|
|
17
|
+
if (isVerified && profile.email && profile.email.trim().length > 0) {
|
|
18
|
+
return (0, uuid_1.v5)(profile.email.trim().toLowerCase(), ns);
|
|
19
|
+
}
|
|
20
|
+
return (0, uuid_1.v5)(`${provider}:${profile.id}`, ns);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Standardizes raw user records from Google, GitHub, Microsoft, Apple, or local auth
|
|
24
|
+
* into a single unified user object with a canonical `id` / `userId`.
|
|
25
|
+
*/
|
|
26
|
+
function createUnifiedAuthUser(rawUser, provider, options) {
|
|
27
|
+
const emailRaw = typeof rawUser.email === 'string' ? rawUser.email : undefined;
|
|
28
|
+
const email = emailRaw ? emailRaw.trim().toLowerCase() : '';
|
|
29
|
+
const emailVerified = rawUser.emailVerified === true ||
|
|
30
|
+
rawUser.email_verified === true ||
|
|
31
|
+
provider === 'microsoft' ||
|
|
32
|
+
provider === 'github';
|
|
33
|
+
const providerAccountId = String(rawUser.providerAccountId ?? rawUser.id ?? rawUser.sub ?? rawUser.userId ?? '');
|
|
34
|
+
const id = resolveUnifiedUserId({ id: providerAccountId, email, emailVerified }, provider, options);
|
|
35
|
+
const name = typeof rawUser.name === 'string' && rawUser.name.trim()
|
|
36
|
+
? rawUser.name.trim()
|
|
37
|
+
: typeof rawUser.displayName === 'string' && rawUser.displayName.trim()
|
|
38
|
+
? rawUser.displayName.trim()
|
|
39
|
+
: typeof rawUser.login === 'string'
|
|
40
|
+
? rawUser.login
|
|
41
|
+
: email.split('@')[0] || 'User';
|
|
42
|
+
const avatarUrl = typeof rawUser.picture === 'string'
|
|
43
|
+
? rawUser.picture
|
|
44
|
+
: typeof rawUser.avatarUrl === 'string'
|
|
45
|
+
? rawUser.avatarUrl
|
|
46
|
+
: typeof rawUser.avatar_url === 'string'
|
|
47
|
+
? rawUser.avatar_url
|
|
48
|
+
: null;
|
|
49
|
+
return {
|
|
50
|
+
id,
|
|
51
|
+
userId: id,
|
|
52
|
+
email,
|
|
53
|
+
emailVerified,
|
|
54
|
+
name,
|
|
55
|
+
displayName: name,
|
|
56
|
+
avatarUrl,
|
|
57
|
+
picture: avatarUrl,
|
|
58
|
+
provider,
|
|
59
|
+
providerAccountId,
|
|
60
|
+
rawProfile: rawUser,
|
|
61
|
+
createdAt: typeof rawUser.createdAt === 'number' ? rawUser.createdAt : Date.now(),
|
|
62
|
+
};
|
|
63
|
+
}
|
package/dist/auth/index.d.ts
CHANGED
package/dist/auth/index.js
CHANGED
|
@@ -20,3 +20,5 @@ __exportStar(require("./github.js"), exports);
|
|
|
20
20
|
__exportStar(require("./apple.js"), exports);
|
|
21
21
|
__exportStar(require("./session.js"), exports);
|
|
22
22
|
__exportStar(require("./local.js"), exports);
|
|
23
|
+
__exportStar(require("./identity.js"), exports);
|
|
24
|
+
__exportStar(require("./unified.js"), exports);
|
package/dist/auth/microsoft.d.ts
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
|
+
import { type AuthUser } from './identity.js';
|
|
1
2
|
import { OneDriveBackupStorage, OneDriveBackupClient, type OneDriveBackupClientOptions } from '../backups/onedrive.js';
|
|
2
3
|
export declare const MICROSOFT_GRAPH_ENDPOINT = "https://graph.microsoft.com/v1.0";
|
|
3
4
|
export declare const DEFAULT_MICROSOFT_AUTH_SCOPES: string[];
|
|
4
|
-
export interface MicrosoftUser {
|
|
5
|
-
|
|
6
|
-
email: string;
|
|
7
|
-
name: string;
|
|
5
|
+
export interface MicrosoftUser extends AuthUser {
|
|
6
|
+
provider: 'microsoft';
|
|
8
7
|
givenName?: string;
|
|
9
8
|
familyName?: string;
|
|
10
|
-
picture?: string;
|
|
11
9
|
userPrincipalName?: string;
|
|
12
10
|
}
|
|
13
11
|
export interface MicrosoftAuthTokens {
|
package/dist/auth/microsoft.js
CHANGED
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.MicrosoftAuthClient = exports.DEFAULT_MICROSOFT_AUTH_SCOPES = exports.MICROSOFT_GRAPH_ENDPOINT = void 0;
|
|
4
4
|
exports.createInMemoryMicrosoftTokenStore = createInMemoryMicrosoftTokenStore;
|
|
5
5
|
exports.createMicrosoftAuthTokenStore = createMicrosoftAuthTokenStore;
|
|
6
|
+
const identity_js_1 = require("./identity.js");
|
|
6
7
|
const sha2_js_1 = require("@noble/hashes/sha2.js");
|
|
7
8
|
const utils_js_1 = require("@noble/hashes/utils.js");
|
|
8
9
|
const onedrive_js_1 = require("../backups/onedrive.js");
|
|
@@ -299,14 +300,26 @@ class MicrosoftAuthClient {
|
|
|
299
300
|
throw new Error(`Failed to fetch Microsoft user profile (${res.status}).`);
|
|
300
301
|
}
|
|
301
302
|
const raw = (await res.json());
|
|
302
|
-
const email = raw.mail || raw.userPrincipalName ||
|
|
303
|
+
const email = (raw.mail || raw.userPrincipalName || '').trim().toLowerCase();
|
|
304
|
+
const sub = String(raw.id || raw.sub || 'microsoft_user');
|
|
305
|
+
const id = (0, identity_js_1.resolveUnifiedUserId)({ id: sub, email, emailVerified: true }, 'microsoft');
|
|
306
|
+
const name = raw.displayName || email || 'Microsoft User';
|
|
303
307
|
return {
|
|
304
|
-
id
|
|
308
|
+
id,
|
|
309
|
+
userId: id,
|
|
305
310
|
email,
|
|
306
|
-
|
|
311
|
+
emailVerified: true,
|
|
312
|
+
name,
|
|
313
|
+
displayName: name,
|
|
314
|
+
avatarUrl: null,
|
|
315
|
+
picture: null,
|
|
316
|
+
provider: 'microsoft',
|
|
317
|
+
providerAccountId: sub,
|
|
307
318
|
givenName: raw.givenName,
|
|
308
319
|
familyName: raw.surname,
|
|
309
320
|
userPrincipalName: raw.userPrincipalName,
|
|
321
|
+
rawProfile: raw,
|
|
322
|
+
createdAt: Date.now(),
|
|
310
323
|
};
|
|
311
324
|
}
|
|
312
325
|
createOneDriveBackupStorage() {
|
package/dist/auth/session.d.ts
CHANGED
|
@@ -75,6 +75,10 @@ export declare class SessionManager {
|
|
|
75
75
|
* Retrieves the authenticated user profile object, or null if signed out.
|
|
76
76
|
*/
|
|
77
77
|
getUser<TUser = any>(): Promise<TUser | null>;
|
|
78
|
+
/**
|
|
79
|
+
* Retrieves a session record by its canonical userId.
|
|
80
|
+
*/
|
|
81
|
+
getSessionByUserId<TUser = any>(userId: string): Promise<SessionRecord<TUser> | null>;
|
|
78
82
|
/**
|
|
79
83
|
* Updates last active timestamp.
|
|
80
84
|
*/
|
package/dist/auth/session.js
CHANGED
|
@@ -84,7 +84,16 @@ class SessionManager {
|
|
|
84
84
|
* Creates or updates a user session, persisting it to local SQLite so it remains available offline.
|
|
85
85
|
*/
|
|
86
86
|
async createSession(options) {
|
|
87
|
-
|
|
87
|
+
let id = options.id;
|
|
88
|
+
if (!id) {
|
|
89
|
+
const existing = await this.db.execute(`SELECT id FROM ${this.sqlTableName} WHERE user_id = ? LIMIT 1;`, [options.userId]);
|
|
90
|
+
if (existing && existing.length > 0) {
|
|
91
|
+
id = String(existing[0].id);
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
id = `sess_${(0, uuid_js_1.generateUUID)()}`;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
88
97
|
const now = Date.now();
|
|
89
98
|
const offlineDuration = options.offlineDurationMs !== undefined
|
|
90
99
|
? options.offlineDurationMs
|
|
@@ -179,6 +188,15 @@ class SessionManager {
|
|
|
179
188
|
const session = await this.getActiveSession();
|
|
180
189
|
return session ? session.user : null;
|
|
181
190
|
}
|
|
191
|
+
/**
|
|
192
|
+
* Retrieves a session record by its canonical userId.
|
|
193
|
+
*/
|
|
194
|
+
async getSessionByUserId(userId) {
|
|
195
|
+
const rows = await this.db.execute(`SELECT * FROM ${this.sqlTableName} WHERE user_id = ? LIMIT 1;`, [userId]);
|
|
196
|
+
if (!rows || rows.length === 0)
|
|
197
|
+
return null;
|
|
198
|
+
return mapSessionRow(rows[0]);
|
|
199
|
+
}
|
|
182
200
|
/**
|
|
183
201
|
* Updates last active timestamp.
|
|
184
202
|
*/
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { GoogleAuthClient } from './google.js';
|
|
2
|
+
import { GitHubAuthClient } from './github.js';
|
|
3
|
+
import { MicrosoftAuthClient } from './microsoft.js';
|
|
4
|
+
import { AppleAuthClient } from './apple.js';
|
|
5
|
+
import { LocalAuthEngine } from './local.js';
|
|
6
|
+
import { SessionManager, SessionRecord } from './session.js';
|
|
7
|
+
import { SupportedAuthProvider, UnifiedAuthUser } from './identity.js';
|
|
8
|
+
export interface UnifiedAuthConfig {
|
|
9
|
+
google?: GoogleAuthClient;
|
|
10
|
+
github?: GitHubAuthClient;
|
|
11
|
+
microsoft?: MicrosoftAuthClient;
|
|
12
|
+
apple?: AppleAuthClient;
|
|
13
|
+
local?: LocalAuthEngine;
|
|
14
|
+
sessionManager?: SessionManager;
|
|
15
|
+
/**
|
|
16
|
+
* Whether to unify user identity across providers by verified email.
|
|
17
|
+
* Default: true
|
|
18
|
+
*/
|
|
19
|
+
unifyByEmail?: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Custom UUIDv5 namespace for deriving email-based user IDs.
|
|
22
|
+
* Default: DEFAULT_UNIFIED_EMAIL_NAMESPACE ('6ba7b810-9dad-11d1-80b4-00c04fd430c8')
|
|
23
|
+
*/
|
|
24
|
+
emailNamespace?: string;
|
|
25
|
+
}
|
|
26
|
+
export type AuthStateChangeListener = (user: UnifiedAuthUser | null) => void;
|
|
27
|
+
/**
|
|
28
|
+
* High-level orchestrator that unifies multiple OAuth providers (Google, GitHub,
|
|
29
|
+
* Microsoft, Apple) and local auth into a single consistent user identity model.
|
|
30
|
+
*/
|
|
31
|
+
export declare class UnifiedAuthManager {
|
|
32
|
+
private google?;
|
|
33
|
+
private github?;
|
|
34
|
+
private microsoft?;
|
|
35
|
+
private apple?;
|
|
36
|
+
private local?;
|
|
37
|
+
private sessionManager?;
|
|
38
|
+
private unifyByEmail;
|
|
39
|
+
private emailNamespace?;
|
|
40
|
+
private listeners;
|
|
41
|
+
private unsubscribers;
|
|
42
|
+
constructor(config: UnifiedAuthConfig);
|
|
43
|
+
private bindProviderListeners;
|
|
44
|
+
private notify;
|
|
45
|
+
/**
|
|
46
|
+
* Subscribes to global unified authentication state changes.
|
|
47
|
+
*/
|
|
48
|
+
onAuthStateChange(listener: AuthStateChangeListener): () => void;
|
|
49
|
+
/**
|
|
50
|
+
* Authenticates using the specified provider and automatically establishes a unified session.
|
|
51
|
+
*/
|
|
52
|
+
signIn(provider: SupportedAuthProvider, credentials?: any): Promise<UnifiedAuthUser | null>;
|
|
53
|
+
/**
|
|
54
|
+
* Retrieves the currently authenticated unified user.
|
|
55
|
+
*/
|
|
56
|
+
getUser(): Promise<UnifiedAuthUser | null>;
|
|
57
|
+
/**
|
|
58
|
+
* Retrieves the active SQLite session record.
|
|
59
|
+
*/
|
|
60
|
+
getSession(): Promise<SessionRecord<UnifiedAuthUser> | null>;
|
|
61
|
+
/**
|
|
62
|
+
* Checks whether there is an active session or signed-in provider.
|
|
63
|
+
*/
|
|
64
|
+
isAuthenticated(): Promise<boolean>;
|
|
65
|
+
/**
|
|
66
|
+
* Signs out of the active session and all configured providers.
|
|
67
|
+
*/
|
|
68
|
+
signOut(): Promise<void>;
|
|
69
|
+
/**
|
|
70
|
+
* Cleans up all event listeners.
|
|
71
|
+
*/
|
|
72
|
+
destroy(): void;
|
|
73
|
+
}
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.UnifiedAuthManager = void 0;
|
|
4
|
+
const identity_js_1 = require("./identity.js");
|
|
5
|
+
/**
|
|
6
|
+
* High-level orchestrator that unifies multiple OAuth providers (Google, GitHub,
|
|
7
|
+
* Microsoft, Apple) and local auth into a single consistent user identity model.
|
|
8
|
+
*/
|
|
9
|
+
class UnifiedAuthManager {
|
|
10
|
+
google;
|
|
11
|
+
github;
|
|
12
|
+
microsoft;
|
|
13
|
+
apple;
|
|
14
|
+
local;
|
|
15
|
+
sessionManager;
|
|
16
|
+
unifyByEmail;
|
|
17
|
+
emailNamespace;
|
|
18
|
+
listeners = new Set();
|
|
19
|
+
unsubscribers = [];
|
|
20
|
+
constructor(config) {
|
|
21
|
+
this.google = config.google;
|
|
22
|
+
this.github = config.github;
|
|
23
|
+
this.microsoft = config.microsoft;
|
|
24
|
+
this.apple = config.apple;
|
|
25
|
+
this.local = config.local;
|
|
26
|
+
this.sessionManager = config.sessionManager;
|
|
27
|
+
this.unifyByEmail = config.unifyByEmail !== false;
|
|
28
|
+
this.emailNamespace = config.emailNamespace || identity_js_1.DEFAULT_UNIFIED_EMAIL_NAMESPACE;
|
|
29
|
+
this.bindProviderListeners();
|
|
30
|
+
}
|
|
31
|
+
bindProviderListeners() {
|
|
32
|
+
if (this.google) {
|
|
33
|
+
this.unsubscribers.push(this.google.onAuthStateChange(user => {
|
|
34
|
+
if (!user)
|
|
35
|
+
return;
|
|
36
|
+
const unified = (0, identity_js_1.createUnifiedAuthUser)(user, 'google', {
|
|
37
|
+
namespace: this.emailNamespace,
|
|
38
|
+
requireVerifiedEmail: this.unifyByEmail,
|
|
39
|
+
});
|
|
40
|
+
this.notify(unified);
|
|
41
|
+
}));
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
notify(user) {
|
|
45
|
+
for (const listener of this.listeners) {
|
|
46
|
+
try {
|
|
47
|
+
listener(user);
|
|
48
|
+
}
|
|
49
|
+
catch (err) {
|
|
50
|
+
console.warn('[UnifiedAuthManager] Error in auth state listener:', err);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Subscribes to global unified authentication state changes.
|
|
56
|
+
*/
|
|
57
|
+
onAuthStateChange(listener) {
|
|
58
|
+
this.listeners.add(listener);
|
|
59
|
+
return () => {
|
|
60
|
+
this.listeners.delete(listener);
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Authenticates using the specified provider and automatically establishes a unified session.
|
|
65
|
+
*/
|
|
66
|
+
async signIn(provider, credentials) {
|
|
67
|
+
let rawUser = null;
|
|
68
|
+
let token = null;
|
|
69
|
+
let refreshToken = null;
|
|
70
|
+
if (provider === 'google') {
|
|
71
|
+
if (!this.google)
|
|
72
|
+
throw new Error('GoogleAuthClient is not configured on UnifiedAuthManager.');
|
|
73
|
+
rawUser = await this.google.signIn();
|
|
74
|
+
token = await this.google.getValidAccessToken().catch(() => null);
|
|
75
|
+
}
|
|
76
|
+
else if (provider === 'github') {
|
|
77
|
+
if (!this.github)
|
|
78
|
+
throw new Error('GitHubAuthClient is not configured on UnifiedAuthManager.');
|
|
79
|
+
rawUser = await this.github.signIn();
|
|
80
|
+
token = await this.github.getValidAccessToken().catch(() => null);
|
|
81
|
+
}
|
|
82
|
+
else if (provider === 'microsoft') {
|
|
83
|
+
if (!this.microsoft)
|
|
84
|
+
throw new Error('MicrosoftAuthClient is not configured on UnifiedAuthManager.');
|
|
85
|
+
rawUser = await this.microsoft.signIn();
|
|
86
|
+
token = await this.microsoft.getValidAccessToken().catch(() => null);
|
|
87
|
+
}
|
|
88
|
+
else if (provider === 'apple') {
|
|
89
|
+
if (!this.apple)
|
|
90
|
+
throw new Error('AppleAuthClient is not configured on UnifiedAuthManager.');
|
|
91
|
+
rawUser = await this.apple.signIn();
|
|
92
|
+
}
|
|
93
|
+
else if (provider === 'local') {
|
|
94
|
+
if (!this.local)
|
|
95
|
+
throw new Error('LocalAuthEngine is not configured on UnifiedAuthManager.');
|
|
96
|
+
const identifier = credentials?.email || credentials?.identifier;
|
|
97
|
+
const password = credentials?.password;
|
|
98
|
+
if (!identifier || !password) {
|
|
99
|
+
throw new Error('Local authentication requires identifier/email and password credentials.');
|
|
100
|
+
}
|
|
101
|
+
const result = await this.local.signIn({ identifier, password });
|
|
102
|
+
rawUser = result.user;
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
throw new Error(`Unsupported auth provider: ${provider}`);
|
|
106
|
+
}
|
|
107
|
+
if (!rawUser) {
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
const unifiedUser = (0, identity_js_1.createUnifiedAuthUser)(rawUser, provider, {
|
|
111
|
+
namespace: this.emailNamespace,
|
|
112
|
+
requireVerifiedEmail: this.unifyByEmail,
|
|
113
|
+
});
|
|
114
|
+
if (this.sessionManager) {
|
|
115
|
+
await this.sessionManager.createSession({
|
|
116
|
+
userId: unifiedUser.userId,
|
|
117
|
+
user: unifiedUser,
|
|
118
|
+
provider,
|
|
119
|
+
token,
|
|
120
|
+
refreshToken,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
this.notify(unifiedUser);
|
|
124
|
+
return unifiedUser;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Retrieves the currently authenticated unified user.
|
|
128
|
+
*/
|
|
129
|
+
async getUser() {
|
|
130
|
+
if (this.sessionManager) {
|
|
131
|
+
const session = await this.sessionManager.getActiveSession();
|
|
132
|
+
if (session?.user)
|
|
133
|
+
return session.user;
|
|
134
|
+
}
|
|
135
|
+
if (this.google && (await this.google.isSignedIn())) {
|
|
136
|
+
const gUser = await this.google.getUser();
|
|
137
|
+
if (gUser) {
|
|
138
|
+
return (0, identity_js_1.createUnifiedAuthUser)(gUser, 'google', {
|
|
139
|
+
namespace: this.emailNamespace,
|
|
140
|
+
requireVerifiedEmail: this.unifyByEmail,
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Retrieves the active SQLite session record.
|
|
148
|
+
*/
|
|
149
|
+
async getSession() {
|
|
150
|
+
if (!this.sessionManager)
|
|
151
|
+
return null;
|
|
152
|
+
return await this.sessionManager.getActiveSession();
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Checks whether there is an active session or signed-in provider.
|
|
156
|
+
*/
|
|
157
|
+
async isAuthenticated() {
|
|
158
|
+
const user = await this.getUser();
|
|
159
|
+
return user !== null;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Signs out of the active session and all configured providers.
|
|
163
|
+
*/
|
|
164
|
+
async signOut() {
|
|
165
|
+
if (this.sessionManager) {
|
|
166
|
+
await this.sessionManager.clearSession();
|
|
167
|
+
}
|
|
168
|
+
await this.google?.signOut().catch(() => { });
|
|
169
|
+
await this.github?.signOut().catch(() => { });
|
|
170
|
+
await this.microsoft?.signOut().catch(() => { });
|
|
171
|
+
await this.apple?.signOut().catch(() => { });
|
|
172
|
+
this.notify(null);
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Cleans up all event listeners.
|
|
176
|
+
*/
|
|
177
|
+
destroy() {
|
|
178
|
+
for (const unsub of this.unsubscribers) {
|
|
179
|
+
try {
|
|
180
|
+
unsub();
|
|
181
|
+
}
|
|
182
|
+
catch { }
|
|
183
|
+
}
|
|
184
|
+
this.unsubscribers = [];
|
|
185
|
+
this.listeners.clear();
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
exports.UnifiedAuthManager = UnifiedAuthManager;
|
package/package.json
CHANGED