@pelican-identity/auth-core 1.2.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/dist/index.mjs ADDED
@@ -0,0 +1,460 @@
1
+ import nacl from 'tweetnacl';
2
+ import { encodeBase64, decodeBase64 } from 'tweetnacl-util';
3
+ import QRCode from 'qrcode';
4
+
5
+ // src/utilities/crypto.ts
6
+ var CryptoService = class {
7
+ generateSymmetricKey() {
8
+ const key = nacl.randomBytes(32);
9
+ return encodeBase64(key);
10
+ }
11
+ /**
12
+ * Encrypt with symmetric key (secret-key encryption)
13
+ * Use this when both parties share the same secret key
14
+ * @param plaintext - Message to encrypt
15
+ * @param keyString - Symmetric key (base64)
16
+ * @returns Encrypted message with nonce
17
+ */
18
+ encryptSymmetric({
19
+ plaintext,
20
+ keyString
21
+ }) {
22
+ const key = decodeBase64(keyString);
23
+ const nonce = nacl.randomBytes(24);
24
+ const messageBytes = new TextEncoder().encode(plaintext);
25
+ const ciphertext = nacl.secretbox(messageBytes, nonce, key);
26
+ return {
27
+ cipher: encodeBase64(ciphertext),
28
+ nonce: encodeBase64(nonce)
29
+ };
30
+ }
31
+ /**
32
+ * Decrypt with symmetric key
33
+ * @param encrypted - Encrypted message with nonce
34
+ * @param keyString - Symmetric key (base64)
35
+ * @returns Decrypted plaintext
36
+ */
37
+ decryptSymmetric({
38
+ encrypted,
39
+ keyString
40
+ }) {
41
+ try {
42
+ const key = decodeBase64(keyString);
43
+ const ciphertextBytes = decodeBase64(encrypted.cipher);
44
+ const nonceBytes = decodeBase64(encrypted.nonce);
45
+ const decrypted = nacl.secretbox.open(ciphertextBytes, nonceBytes, key);
46
+ if (!decrypted) {
47
+ throw new Error("Decryption failed - invalid key or corrupted data");
48
+ }
49
+ const decoded = new TextDecoder().decode(decrypted);
50
+ return decoded;
51
+ } catch (error) {
52
+ console.error("Decryption failed", error);
53
+ return null;
54
+ }
55
+ }
56
+ };
57
+ var crypto_default = CryptoService;
58
+
59
+ // src/utilities/storage.ts
60
+ var AuthStorage = class {
61
+ constructor() {
62
+ this.prefix = "pelican_auth_";
63
+ this.defaultTTL = 5 * 60 * 1e3;
64
+ // 5 minutes
65
+ this.memoryCache = /* @__PURE__ */ new Map();
66
+ }
67
+ /**
68
+ * Store auth session with automatic cleanup
69
+ */
70
+ set(key, value, options = {}) {
71
+ const { ttlMs = this.defaultTTL, useSessionStorage = true } = options;
72
+ const expiresAt = Date.now() + ttlMs;
73
+ const data = { value, expiresAt };
74
+ if (useSessionStorage) {
75
+ try {
76
+ sessionStorage.setItem(`${this.prefix}${key}`, JSON.stringify(data));
77
+ } catch (error) {
78
+ console.warn("SessionStorage unavailable, using memory:", error);
79
+ this.setMemory(key, data);
80
+ }
81
+ } else {
82
+ this.setMemory(key, data);
83
+ }
84
+ }
85
+ /**
86
+ * Get stored value if not expired
87
+ */
88
+ get(key) {
89
+ try {
90
+ const stored = sessionStorage.getItem(`${this.prefix}${key}`);
91
+ if (stored) {
92
+ const data = JSON.parse(stored);
93
+ if (Date.now() > data.expiresAt) {
94
+ this.remove(key);
95
+ return null;
96
+ }
97
+ return data.value;
98
+ }
99
+ } catch (error) {
100
+ }
101
+ return this.getMemory(key);
102
+ }
103
+ /**
104
+ * Remove specific key
105
+ */
106
+ remove(key) {
107
+ try {
108
+ sessionStorage.removeItem(`${this.prefix}${key}`);
109
+ } catch (error) {
110
+ }
111
+ this.memoryCache.delete(key);
112
+ }
113
+ /**
114
+ * Clear all auth data
115
+ */
116
+ clear() {
117
+ try {
118
+ Object.keys(sessionStorage).forEach((key) => {
119
+ if (key.startsWith(this.prefix)) {
120
+ sessionStorage.removeItem(key);
121
+ }
122
+ });
123
+ } catch (error) {
124
+ }
125
+ this.memoryCache.clear();
126
+ }
127
+ // ========================================
128
+ // Memory cache helpers
129
+ // ========================================
130
+ setMemory(key, data) {
131
+ this.memoryCache.set(key, data);
132
+ const ttl = data.expiresAt - Date.now();
133
+ setTimeout(() => {
134
+ this.memoryCache.delete(key);
135
+ }, ttl);
136
+ }
137
+ getMemory(key) {
138
+ const data = this.memoryCache.get(key);
139
+ if (!data) return null;
140
+ if (Date.now() > data.expiresAt) {
141
+ this.memoryCache.delete(key);
142
+ return null;
143
+ }
144
+ return data.value;
145
+ }
146
+ };
147
+ var storage = new AuthStorage();
148
+ var storeAuthSession = (sessionId, sessionKey, ttlMs) => {
149
+ storage.set("session", { sessionId, sessionKey }, { ttlMs });
150
+ };
151
+ var getAuthSession = () => {
152
+ return storage.get("session");
153
+ };
154
+ var clearAuthSession = () => {
155
+ storage.remove("session");
156
+ };
157
+ var clearAllAuthData = () => {
158
+ storage.clear();
159
+ };
160
+
161
+ // src/utilities/stateMachine.ts
162
+ var StateMachine = class {
163
+ constructor() {
164
+ this.state = "idle";
165
+ this.listeners = /* @__PURE__ */ new Set();
166
+ }
167
+ get current() {
168
+ return this.state;
169
+ }
170
+ transition(next) {
171
+ this.state = next;
172
+ this.listeners.forEach((l) => l(next));
173
+ }
174
+ subscribe(fn) {
175
+ this.listeners.add(fn);
176
+ return () => this.listeners.delete(fn);
177
+ }
178
+ };
179
+
180
+ // src/utilities/transport.ts
181
+ var Transport = class {
182
+ constructor(handlers) {
183
+ this.handlers = handlers;
184
+ }
185
+ connect(url) {
186
+ this.socket = new WebSocket(url);
187
+ this.socket.onopen = () => this.handlers.onOpen?.();
188
+ this.socket.onmessage = (e) => this.handlers.onMessage?.(e.data);
189
+ this.socket.onerror = (e) => this.handlers.onError?.(e);
190
+ this.socket.onclose = (e) => this.handlers.onClose?.(e);
191
+ }
192
+ send(payload) {
193
+ if (this.socket?.readyState === WebSocket.OPEN) {
194
+ this.socket.send(JSON.stringify(payload));
195
+ }
196
+ }
197
+ close() {
198
+ this.socket?.close();
199
+ }
200
+ };
201
+
202
+ // src/constants.ts
203
+ var BASEURL = "http://localhost:8080";
204
+
205
+ // src/engine/engine.ts
206
+ var PelicanAuthentication = class {
207
+ constructor(config) {
208
+ this.crypto = new crypto_default();
209
+ this.stateMachine = new StateMachine();
210
+ this.sessionId = "";
211
+ this.sessionKey = null;
212
+ this.listeners = {};
213
+ if (!config.publicKey) throw new Error("Missing publicKey");
214
+ if (!config.projectId) throw new Error("Missing projectId");
215
+ if (!config.authType) throw new Error("Missing authType");
216
+ this.config = {
217
+ continuousMode: false,
218
+ forceQRCode: false,
219
+ ...config
220
+ };
221
+ this.stateMachine.subscribe((s) => this.emit("state", s));
222
+ this.attachVisibilityRecovery();
223
+ }
224
+ /* -------------------- public API -------------------- */
225
+ /**
226
+ * Subscribe to SDK events (qr, deeplink, success, error, state)
227
+ * @returns Unsubscribe function
228
+ */
229
+ on(event, cb) {
230
+ var _a;
231
+ (_a = this.listeners)[event] ?? (_a[event] = /* @__PURE__ */ new Set());
232
+ this.listeners[event].add(cb);
233
+ return () => this.listeners[event].delete(cb);
234
+ }
235
+ /**
236
+ * Initializes the authentication flow.
237
+ * Fetches a relay, establishes a WebSocket, and generates the E2EE session key.
238
+ */
239
+ async start() {
240
+ if (this.stateMachine.current !== "idle") return;
241
+ this.resetSession();
242
+ this.stateMachine.transition("initializing");
243
+ try {
244
+ const relay = await this.fetchRelayUrl();
245
+ this.sessionKey = this.crypto.generateSymmetricKey();
246
+ this.sessionId = crypto.randomUUID() + crypto.randomUUID();
247
+ this.transport = new Transport({
248
+ onOpen: () => {
249
+ this.transport.send({
250
+ type: "register",
251
+ sessionID: this.sessionId,
252
+ ...this.config
253
+ });
254
+ this.stateMachine.transition("awaiting-pair");
255
+ },
256
+ onMessage: (msg) => this.handleMessage(msg),
257
+ onError: () => this.fail(new Error("WebSocket connection failed"))
258
+ });
259
+ this.transport.connect(relay);
260
+ await this.emitEntryPoint();
261
+ } catch (err) {
262
+ this.fail(err instanceof Error ? err : new Error("Start failed"));
263
+ }
264
+ }
265
+ /**
266
+ * Manually stops the authentication process and closes connections.
267
+ */
268
+ stop() {
269
+ this.terminate(false);
270
+ }
271
+ /* -------------------- internals -------------------- */
272
+ /** Fetches the WebSocket Relay URL from the backend */
273
+ async fetchRelayUrl() {
274
+ const { publicKey, projectId, authType } = this.config;
275
+ const res = await fetch(
276
+ `${BASEURL}/relay?public_key=${publicKey}&auth_type=${authType}&project_id=${projectId}`
277
+ );
278
+ if (!res.ok) {
279
+ throw new Error("Failed to fetch relay URL");
280
+ }
281
+ const json = await res.json();
282
+ return json.relay_url;
283
+ }
284
+ /**
285
+ * Decides whether to show a QR code (Desktop) or a Deep Link (Mobile).
286
+ */
287
+ async emitEntryPoint() {
288
+ const payload = {
289
+ sessionID: this.sessionId,
290
+ sessionKey: this.sessionKey,
291
+ publicKey: this.config.publicKey,
292
+ authType: this.config.authType,
293
+ projectId: this.config.projectId,
294
+ url: window.location.href
295
+ };
296
+ const shouldUseQR = this.config.forceQRCode || !/Android|iPhone|iPad/i.test(navigator.userAgent);
297
+ if (!shouldUseQR && this.sessionKey) {
298
+ storeAuthSession(this.sessionId, this.sessionKey, 5 * 6e4);
299
+ this.emit(
300
+ "deeplink",
301
+ `pelicanvault://auth/deep-link?data=${encodeURIComponent(
302
+ JSON.stringify(payload)
303
+ )}`
304
+ );
305
+ } else {
306
+ const qr = await QRCode.toDataURL(JSON.stringify(payload));
307
+ this.emit("qr", qr);
308
+ }
309
+ }
310
+ /** Main WebSocket message router */
311
+ handleMessage(msg) {
312
+ switch (msg.type) {
313
+ case "paired":
314
+ this.stateMachine.transition("paired");
315
+ this.transport.send({
316
+ type: "authenticate",
317
+ sessionID: this.sessionId,
318
+ ...this.config,
319
+ url: window.location.href
320
+ });
321
+ return;
322
+ case "phone-auth-success":
323
+ this.handleAuthSuccess(msg);
324
+ return;
325
+ case "phone-terminated":
326
+ this.fail(new Error("Authenticating device terminated the connection"));
327
+ this.restartIfContinuous();
328
+ return;
329
+ case "confirmed":
330
+ this.terminate(true);
331
+ this.restartIfContinuous();
332
+ return;
333
+ }
334
+ }
335
+ /**
336
+ * Decrypts the identity payload received from the phone using the session key.
337
+ */
338
+ handleAuthSuccess(msg) {
339
+ if (!this.sessionKey || !msg.cipher || !msg.nonce) {
340
+ this.fail(new Error("Invalid authentication payload"));
341
+ this.restartIfContinuous();
342
+ return;
343
+ }
344
+ try {
345
+ const decrypted = this.crypto.decryptSymmetric({
346
+ encrypted: { cipher: msg.cipher, nonce: msg.nonce },
347
+ keyString: this.sessionKey
348
+ });
349
+ if (!decrypted) {
350
+ this.fail(new Error("Invalid authentication data"));
351
+ this.restartIfContinuous();
352
+ return;
353
+ }
354
+ const result = JSON.parse(decrypted);
355
+ this.emit("success", result);
356
+ this.stateMachine.transition("authenticated");
357
+ this.transport?.send({
358
+ type: "confirm",
359
+ sessionID: this.sessionId
360
+ });
361
+ } catch {
362
+ this.fail(new Error("Failed to decrypt authentication data"));
363
+ this.restartIfContinuous();
364
+ }
365
+ }
366
+ /**
367
+ * Logic to handle users returning to the browser tab after using the mobile app.
368
+ * Checks the server for a completed session that might have finished while in background.
369
+ */
370
+ async getCachedEntry(cached) {
371
+ try {
372
+ const res = await fetch(
373
+ `${BASEURL}/session?session_id=${cached.sessionId}`
374
+ );
375
+ if (!res.ok) throw new Error("Invalid session");
376
+ const data = await res.json();
377
+ const decrypted = this.crypto.decryptSymmetric({
378
+ encrypted: { cipher: data.cipher, nonce: data.nonce },
379
+ keyString: cached.sessionKey
380
+ });
381
+ if (!decrypted) {
382
+ this.fail(new Error("Invalid session data"));
383
+ this.restartIfContinuous();
384
+ return;
385
+ }
386
+ const result = JSON.parse(decrypted);
387
+ this.emit("success", result);
388
+ clearAuthSession();
389
+ } catch {
390
+ this.fail(new Error("Session recovery failed"));
391
+ this.restartIfContinuous();
392
+ }
393
+ }
394
+ attachVisibilityRecovery() {
395
+ this.visibilityHandler = async () => {
396
+ if (document.visibilityState !== "visible") return;
397
+ const cached = getAuthSession();
398
+ if (!cached) return;
399
+ await this.getCachedEntry(cached);
400
+ };
401
+ document.addEventListener("visibilitychange", this.visibilityHandler);
402
+ }
403
+ detachVisibilityRecovery() {
404
+ if (this.visibilityHandler) {
405
+ document.removeEventListener("visibilitychange", this.visibilityHandler);
406
+ this.visibilityHandler = void 0;
407
+ }
408
+ }
409
+ /** Cleans up the current session state */
410
+ terminate(success) {
411
+ if (!this.transport) return;
412
+ if (!success) {
413
+ this.transport?.send({
414
+ type: "client-terminated",
415
+ sessionID: this.sessionId,
416
+ projectId: this.config.projectId,
417
+ publicKey: this.config.publicKey,
418
+ authType: this.config.authType
419
+ });
420
+ }
421
+ setTimeout(() => {
422
+ this.transport?.close();
423
+ }, 150);
424
+ clearAuthSession();
425
+ this.resetSession();
426
+ if (!this.config.continuousMode) {
427
+ this.detachVisibilityRecovery();
428
+ }
429
+ this.stateMachine.transition(success ? "confirmed" : "terminated");
430
+ }
431
+ restartIfContinuous() {
432
+ if (!this.config.continuousMode) return;
433
+ this.terminate(false);
434
+ this.stateMachine.transition("idle");
435
+ setTimeout(() => {
436
+ this.start();
437
+ }, 150);
438
+ }
439
+ resetSession() {
440
+ this.sessionId = "";
441
+ this.sessionKey = null;
442
+ }
443
+ /** Completely destroys the instance and removes all listeners */
444
+ destroy() {
445
+ this.detachVisibilityRecovery();
446
+ this.transport?.close();
447
+ this.listeners = {};
448
+ }
449
+ emit(event, payload) {
450
+ this.listeners[event]?.forEach((cb) => cb(payload));
451
+ }
452
+ fail(err) {
453
+ this.emit("error", err);
454
+ this.stateMachine.transition("error");
455
+ }
456
+ };
457
+
458
+ export { BASEURL, CryptoService, PelicanAuthentication, StateMachine, Transport, clearAllAuthData, clearAuthSession, getAuthSession, storeAuthSession };
459
+ //# sourceMappingURL=index.mjs.map
460
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/utilities/crypto.ts","../src/utilities/storage.ts","../src/utilities/stateMachine.ts","../src/utilities/transport.ts","../src/constants.ts","../src/engine/engine.ts"],"names":[],"mappings":";;;;;AAQO,IAAM,gBAAN,MAAoB;AAAA,EACzB,oBAAA,GAA+B;AAC7B,IAAA,MAAM,GAAA,GAAM,IAAA,CAAK,WAAA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ACjB,IAAA,IAAA,CAAK,UAAA,GAAa,IAAA;AAAA,EACpB;AAAA;AAAA,EAGA,OAAA,GAAU;AACR,IAAA,IAAA,CAAK,wBAAA,EAAyB;AAC9B,IAAA,IAAA,CAAK,WAAW,KAAA,EAAM;AACtB,IAAA,IAAA,CAAK,YAAY,EAAC;AAAA,EACpB;AAAA,EAEQ,IAAA,CACN,OACA,OAAA,EACA;AACA,IAAA,IAAA,CAAK,SAAA,CAAU,KAAK,CAAA,EAAG,OAAA,CAAQ,CAAC,EAAA,KAAO,EAAA,CAAG,OAAO,CAAC,CAAA;AAAA,EACpD;AAAA,EAEQ,KAAK,GAAA,EAAY;AACvB,IAAA,IAAA,CAAK,IAAA,CAAK,SAAS,GAAG,CAAA;AACtB,IAAA,IAAA,CAAK,YAAA,CAAa,WAAW,OAAO,CAAA;AAAA,EACtC;AACF","file":"index.mjs","sourcesContent":["import nacl from \"tweetnacl\";\nimport { decodeBase64, encodeBase64 } from \"tweetnacl-util\";\n\ninterface EncryptedMessage {\n cipher: string; // Base64 encoded\n nonce: string; // Base64 encoded\n}\n\nexport class CryptoService {\n generateSymmetricKey(): string {\n const key = nacl.randomBytes(32);\n return encodeBase64(key);\n }\n\n /**\n * Encrypt with symmetric key (secret-key encryption)\n * Use this when both parties share the same secret key\n * @param plaintext - Message to encrypt\n * @param keyString - Symmetric key (base64)\n * @returns Encrypted message with nonce\n */\n encryptSymmetric({\n plaintext,\n keyString,\n }: {\n plaintext: string;\n keyString: string;\n }): EncryptedMessage {\n const key = decodeBase64(keyString);\n const nonce = nacl.randomBytes(24);\n\n const messageBytes = new TextEncoder().encode(plaintext);\n\n const ciphertext = nacl.secretbox(messageBytes, nonce, key);\n\n return {\n cipher: encodeBase64(ciphertext),\n nonce: encodeBase64(nonce),\n };\n }\n\n /**\n * Decrypt with symmetric key\n * @param encrypted - Encrypted message with nonce\n * @param keyString - Symmetric key (base64)\n * @returns Decrypted plaintext\n */\n decryptSymmetric({\n encrypted,\n keyString,\n }: {\n encrypted: EncryptedMessage;\n keyString: string;\n }): string | null {\n try {\n const key = decodeBase64(keyString);\n const ciphertextBytes = decodeBase64(encrypted.cipher);\n const nonceBytes = decodeBase64(encrypted.nonce);\n\n const decrypted = nacl.secretbox.open(ciphertextBytes, nonceBytes, key);\n\n if (!decrypted) {\n throw new Error(\"Decryption failed - invalid key or corrupted data\");\n }\n\n const decoded = new TextDecoder().decode(decrypted);\n return decoded;\n } catch (error) {\n console.error(\"Decryption failed\", error);\n return null;\n }\n }\n}\n\nexport default CryptoService;\nexport type { EncryptedMessage };\n","// hybridStorage.ts\n/**\n * Hybrid storage: Try sessionStorage first, fallback to memory\n * Best of both worlds - survives page refresh but auto-cleans\n */\n\ninterface StorageOptions {\n ttlMs?: number;\n useSessionStorage?: boolean;\n}\n\nclass AuthStorage {\n private readonly prefix = \"pelican_auth_\";\n private readonly defaultTTL = 5 * 60 * 1000; // 5 minutes\n private memoryCache: Map<string, { value: any; expiresAt: number }> =\n new Map();\n\n /**\n * Store auth session with automatic cleanup\n */\n set(key: string, value: any, options: StorageOptions = {}): void {\n const { ttlMs = this.defaultTTL, useSessionStorage = true } = options;\n\n const expiresAt = Date.now() + ttlMs;\n const data = { value, expiresAt };\n\n // Try sessionStorage first\n if (useSessionStorage) {\n try {\n sessionStorage.setItem(`${this.prefix}${key}`, JSON.stringify(data));\n } catch (error) {\n console.warn(\"SessionStorage unavailable, using memory:\", error);\n this.setMemory(key, data);\n }\n } else {\n this.setMemory(key, data);\n }\n }\n\n /**\n * Get stored value if not expired\n */\n get(key: string): any | null {\n // Try sessionStorage first\n try {\n const stored = sessionStorage.getItem(`${this.prefix}${key}`);\n if (stored) {\n const data = JSON.parse(stored);\n\n // Check expiration\n if (Date.now() > data.expiresAt) {\n this.remove(key);\n return null;\n }\n\n return data.value;\n }\n } catch (error) {\n // Fallback to memory\n }\n\n // Try memory cache\n return this.getMemory(key);\n }\n\n /**\n * Remove specific key\n */\n remove(key: string): void {\n try {\n sessionStorage.removeItem(`${this.prefix}${key}`);\n } catch (error) {\n // Ignore\n }\n this.memoryCache.delete(key);\n }\n\n /**\n * Clear all auth data\n */\n clear(): void {\n // Clear sessionStorage\n try {\n Object.keys(sessionStorage).forEach((key) => {\n if (key.startsWith(this.prefix)) {\n sessionStorage.removeItem(key);\n }\n });\n } catch (error) {\n // Ignore\n }\n\n // Clear memory\n this.memoryCache.clear();\n }\n\n // ========================================\n // Memory cache helpers\n // ========================================\n\n private setMemory(\n key: string,\n data: { value: any; expiresAt: number }\n ): void {\n this.memoryCache.set(key, data);\n\n // Schedule cleanup\n const ttl = data.expiresAt - Date.now();\n setTimeout(() => {\n this.memoryCache.delete(key);\n }, ttl);\n }\n\n private getMemory(key: string): any | null {\n const data = this.memoryCache.get(key);\n if (!data) return null;\n\n if (Date.now() > data.expiresAt) {\n this.memoryCache.delete(key);\n return null;\n }\n\n return data.value;\n }\n}\n\n// Singleton instance\nconst storage = new AuthStorage();\n\n// ========================================\n// Convenience functions for auth flow\n// ========================================\n\nexport interface AuthSession {\n sessionId: string;\n sessionKey: string;\n}\n\nexport const storeAuthSession = (\n sessionId: string,\n sessionKey: string,\n ttlMs?: number\n): void => {\n storage.set(\"session\", { sessionId, sessionKey }, { ttlMs });\n};\n\nexport const getAuthSession = (): AuthSession | null => {\n return storage.get(\"session\");\n};\n\nexport const clearAuthSession = (): void => {\n storage.remove(\"session\");\n};\n\nexport const clearAllAuthData = (): void => {\n storage.clear();\n};\n\nexport default storage;\n","import type { PelicanAuthState } from \"../types/types\";\n\nexport class StateMachine {\n private state: PelicanAuthState = \"idle\";\n private listeners = new Set<(s: PelicanAuthState) => void>();\n\n get current() {\n return this.state;\n }\n\n transition(next: PelicanAuthState) {\n this.state = next;\n this.listeners.forEach((l) => l(next));\n }\n\n subscribe(fn: (s: PelicanAuthState) => void) {\n this.listeners.add(fn);\n return () => this.listeners.delete(fn);\n }\n}\n","import { ISocketMessage } from \"../types/types\";\n\ntype TransportHandlers = {\n onOpen?: () => void;\n onMessage?: (msg: ISocketMessage) => void;\n onError?: (err: Event) => void;\n onClose?: (ev: CloseEvent) => void;\n};\n\ninterface PelicanMessageEvent extends MessageEvent {\n data: ISocketMessage;\n}\nexport class Transport {\n private socket?: WebSocket;\n private handlers: TransportHandlers;\n\n constructor(handlers: TransportHandlers) {\n this.handlers = handlers;\n }\n\n connect(url: string) {\n this.socket = new WebSocket(url);\n\n this.socket.onopen = () => this.handlers.onOpen?.();\n this.socket.onmessage = (e: PelicanMessageEvent) =>\n this.handlers.onMessage?.(e.data);\n this.socket.onerror = (e: Event) => this.handlers.onError?.(e);\n this.socket.onclose = (e: CloseEvent) => this.handlers.onClose?.(e);\n }\n\n send(payload: ISocketMessage) {\n if (this.socket?.readyState === WebSocket.OPEN) {\n this.socket.send(JSON.stringify(payload));\n }\n }\n\n close() {\n this.socket?.close();\n }\n}\n","export const BASEURL = \"http://localhost:8080\";\n","import CryptoService from \"../utilities/crypto\";\nimport QRCode from \"qrcode\";\nimport {\n PelicanAuthConfig,\n PelicanAuthEventMap,\n ISocketMessage,\n IdentityResult,\n} from \"../types/types\";\nimport {\n storeAuthSession,\n getAuthSession,\n clearAuthSession,\n AuthSession,\n} from \"../utilities/storage\";\nimport { StateMachine } from \"../utilities/stateMachine\";\nimport { Transport } from \"../utilities/transport\";\n\nimport { BASEURL } from \"../constants\";\n\ntype Listener<T> = (payload: T) => void;\n\n/**\n * PelicanAuth SDK\n * Handles cross-device authentication via WebSockets and E2EE (End-to-End Encryption).\n */\nexport class PelicanAuthentication {\n private readonly crypto = new CryptoService();\n private readonly stateMachine = new StateMachine();\n\n private transport?: Transport;\n private sessionId = \"\";\n private sessionKey: string | null = null;\n\n private visibilityHandler?: () => void;\n\n private listeners: {\n [K in keyof PelicanAuthEventMap]?: Set<Listener<any>>;\n } = {};\n\n private readonly config: Required<PelicanAuthConfig>;\n\n constructor(config: PelicanAuthConfig) {\n if (!config.publicKey) throw new Error(\"Missing publicKey\");\n if (!config.projectId) throw new Error(\"Missing projectId\");\n if (!config.authType) throw new Error(\"Missing authType\");\n\n this.config = {\n continuousMode: false,\n forceQRCode: false,\n ...config,\n };\n\n // Sync internal state machine changes with the 'state' event\n this.stateMachine.subscribe((s) => this.emit(\"state\", s));\n this.attachVisibilityRecovery();\n }\n\n /* -------------------- public API -------------------- */\n\n /**\n * Subscribe to SDK events (qr, deeplink, success, error, state)\n * @returns Unsubscribe function\n */\n on<K extends keyof PelicanAuthEventMap>(\n event: K,\n cb: Listener<PelicanAuthEventMap[K]>\n ) {\n this.listeners[event] ??= new Set();\n this.listeners[event]!.add(cb);\n return () => this.listeners[event]!.delete(cb);\n }\n\n /**\n * Initializes the authentication flow.\n * Fetches a relay, establishes a WebSocket, and generates the E2EE session key.\n */\n async start() {\n if (this.stateMachine.current !== \"idle\") return;\n\n this.resetSession();\n this.stateMachine.transition(\"initializing\");\n\n try {\n const relay = await this.fetchRelayUrl();\n\n // Generate a fresh symmetric key for this specific session\n this.sessionKey = this.crypto.generateSymmetricKey();\n this.sessionId = crypto.randomUUID() + crypto.randomUUID();\n\n this.transport = new Transport({\n onOpen: () => {\n // Register this browser session with the relay\n this.transport!.send({\n type: \"register\",\n sessionID: this.sessionId,\n ...this.config,\n });\n this.stateMachine.transition(\"awaiting-pair\");\n },\n onMessage: (msg: ISocketMessage) => this.handleMessage(msg),\n onError: () => this.fail(new Error(\"WebSocket connection failed\")),\n });\n\n this.transport.connect(relay);\n await this.emitEntryPoint();\n } catch (err) {\n this.fail(err instanceof Error ? err : new Error(\"Start failed\"));\n }\n }\n\n /**\n * Manually stops the authentication process and closes connections.\n */\n stop() {\n this.terminate(false);\n }\n\n /* -------------------- internals -------------------- */\n\n /** Fetches the WebSocket Relay URL from the backend */\n private async fetchRelayUrl(): Promise<string> {\n const { publicKey, projectId, authType } = this.config;\n const res = await fetch(\n `${BASEURL}/relay?public_key=${publicKey}&auth_type=${authType}&project_id=${projectId}`\n );\n\n if (!res.ok) {\n throw new Error(\"Failed to fetch relay URL\");\n }\n\n const json = await res.json();\n return json.relay_url;\n }\n\n /**\n * Decides whether to show a QR code (Desktop) or a Deep Link (Mobile).\n */\n private async emitEntryPoint() {\n const payload = {\n sessionID: this.sessionId,\n sessionKey: this.sessionKey,\n publicKey: this.config.publicKey,\n authType: this.config.authType,\n projectId: this.config.projectId,\n url: window.location.href,\n };\n\n const shouldUseQR =\n this.config.forceQRCode ||\n !/Android|iPhone|iPad/i.test(navigator.userAgent);\n\n if (!shouldUseQR && this.sessionKey) {\n // For mobile: Store session locally so we can recover when the user returns from the app\n storeAuthSession(this.sessionId, this.sessionKey, 5 * 60_000);\n this.emit(\n \"deeplink\",\n `pelicanvault://auth/deep-link?data=${encodeURIComponent(\n JSON.stringify(payload)\n )}`\n );\n } else {\n // For desktop: Generate a QR code for the mobile app to scan\n const qr = await QRCode.toDataURL(JSON.stringify(payload));\n this.emit(\"qr\", qr);\n }\n }\n\n /** Main WebSocket message router */\n private handleMessage(msg: ISocketMessage) {\n switch (msg.type) {\n case \"paired\":\n // The mobile device has scanned the QR/opened the link\n this.stateMachine.transition(\"paired\");\n this.transport!.send({\n type: \"authenticate\",\n sessionID: this.sessionId,\n ...this.config,\n url: window.location.href,\n });\n return;\n\n case \"phone-auth-success\":\n // Mobile device successfully authenticated and sent encrypted credentials\n this.handleAuthSuccess(msg);\n return;\n\n case \"phone-terminated\":\n this.fail(new Error(\"Authenticating device terminated the connection\"));\n this.restartIfContinuous();\n return;\n\n case \"confirmed\":\n // Handshake complete\n this.terminate(true);\n this.restartIfContinuous();\n return;\n }\n }\n\n /**\n * Decrypts the identity payload received from the phone using the session key.\n */\n private handleAuthSuccess(msg: ISocketMessage) {\n if (!this.sessionKey || !msg.cipher || !msg.nonce) {\n this.fail(new Error(\"Invalid authentication payload\"));\n this.restartIfContinuous();\n return;\n }\n\n try {\n const decrypted = this.crypto.decryptSymmetric({\n encrypted: { cipher: msg.cipher, nonce: msg.nonce },\n keyString: this.sessionKey,\n });\n\n if (!decrypted) {\n this.fail(new Error(\"Invalid authentication data\"));\n this.restartIfContinuous();\n return;\n }\n\n const result: IdentityResult = JSON.parse(decrypted);\n\n this.emit(\"success\", result);\n this.stateMachine.transition(\"authenticated\");\n\n // Signal to the relay/phone that we have received and decrypted the data\n this.transport?.send({\n type: \"confirm\",\n sessionID: this.sessionId,\n });\n } catch {\n this.fail(new Error(\"Failed to decrypt authentication data\"));\n this.restartIfContinuous();\n }\n }\n\n /**\n * Logic to handle users returning to the browser tab after using the mobile app.\n * Checks the server for a completed session that might have finished while in background.\n */\n\n private async getCachedEntry(cached: AuthSession) {\n try {\n const res = await fetch(\n `${BASEURL}/session?session_id=${cached.sessionId}`\n );\n\n if (!res.ok) throw new Error(\"Invalid session\");\n\n const data = await res.json();\n const decrypted = this.crypto.decryptSymmetric({\n encrypted: { cipher: data.cipher, nonce: data.nonce },\n keyString: cached.sessionKey,\n });\n\n if (!decrypted) {\n this.fail(new Error(\"Invalid session data\"));\n this.restartIfContinuous();\n return;\n }\n const result: IdentityResult = JSON.parse(decrypted);\n this.emit(\"success\", result);\n clearAuthSession();\n } catch {\n this.fail(new Error(\"Session recovery failed\"));\n this.restartIfContinuous();\n }\n }\n\n private attachVisibilityRecovery() {\n this.visibilityHandler = async () => {\n if (document.visibilityState !== \"visible\") return;\n\n const cached = getAuthSession();\n if (!cached) return;\n await this.getCachedEntry(cached);\n };\n\n document.addEventListener(\"visibilitychange\", this.visibilityHandler);\n }\n\n private detachVisibilityRecovery() {\n if (this.visibilityHandler) {\n document.removeEventListener(\"visibilitychange\", this.visibilityHandler);\n this.visibilityHandler = undefined;\n }\n }\n\n /** Cleans up the current session state */\n private terminate(success: boolean) {\n if (!this.transport) return;\n if (!success) {\n this.transport?.send({\n type: \"client-terminated\",\n sessionID: this.sessionId,\n projectId: this.config.projectId,\n publicKey: this.config.publicKey,\n authType: this.config.authType,\n });\n }\n\n setTimeout(() => {\n this.transport?.close();\n }, 150);\n\n clearAuthSession();\n this.resetSession();\n\n if (!this.config.continuousMode) {\n this.detachVisibilityRecovery();\n }\n this.stateMachine.transition(success ? \"confirmed\" : \"terminated\");\n }\n\n private restartIfContinuous() {\n if (!this.config.continuousMode) return;\n this.terminate(false);\n this.stateMachine.transition(\"idle\");\n\n setTimeout(() => {\n this.start();\n }, 150);\n }\n\n private resetSession() {\n this.sessionId = \"\";\n this.sessionKey = null;\n }\n\n /** Completely destroys the instance and removes all listeners */\n destroy() {\n this.detachVisibilityRecovery();\n this.transport?.close();\n this.listeners = {};\n }\n\n private emit<K extends keyof PelicanAuthEventMap>(\n event: K,\n payload: PelicanAuthEventMap[K]\n ) {\n this.listeners[event]?.forEach((cb) => cb(payload));\n }\n\n private fail(err: Error) {\n this.emit(\"error\", err);\n this.stateMachine.transition(\"error\");\n }\n}\n"]}
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@pelican-identity/auth-core",
3
+ "version": "1.2.0",
4
+ "description": "Framework-agnostic authentication SDK for Pelican Identity",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./dist/index.mjs",
11
+ "require": "./dist/index.js"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist",
16
+ "README.md",
17
+ "package.json"
18
+ ],
19
+ "dependencies": {
20
+ "tweetnacl": "^1.0.3",
21
+ "tweetnacl-util": "^0.15.1",
22
+ "qrcode": "^1.5.3"
23
+ },
24
+ "devDependencies": {
25
+ "@types/qrcode": "^1.5.5",
26
+ "tsup": "^8.0.1",
27
+ "typescript": "^5.3.3"
28
+ },
29
+ "repository": {
30
+ "type": "git",
31
+ "url": "https://github.com/Pelican-Identity/pelican-sdk.git",
32
+ "directory": "packages/core"
33
+ },
34
+ "publishConfig": {
35
+ "access": "public"
36
+ },
37
+ "scripts": {
38
+ "build": "tsup",
39
+ "dev": "tsup --watch",
40
+ "typecheck": "tsc --noEmit"
41
+ }
42
+ }