shogun-core 3.0.3 → 3.0.4

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.
Files changed (95) hide show
  1. package/package.json +1 -1
  2. package/dist/browser/shogun-core.js +0 -92128
  3. package/dist/browser/shogun-core.js.map +0 -1
  4. package/dist/config/simplified-config.js +0 -230
  5. package/dist/core.js +0 -338
  6. package/dist/gundb/crypto.js +0 -268
  7. package/dist/gundb/db.js +0 -1833
  8. package/dist/gundb/derive.js +0 -229
  9. package/dist/gundb/errors.js +0 -66
  10. package/dist/gundb/index.js +0 -6
  11. package/dist/gundb/restricted-put.js +0 -81
  12. package/dist/gundb/rxjs.js +0 -445
  13. package/dist/gundb/simple-api.js +0 -438
  14. package/dist/gundb/types.js +0 -4
  15. package/dist/index.js +0 -16
  16. package/dist/interfaces/common.js +0 -1
  17. package/dist/interfaces/events.js +0 -36
  18. package/dist/interfaces/plugin.js +0 -1
  19. package/dist/interfaces/shogun.js +0 -34
  20. package/dist/managers/AuthManager.js +0 -225
  21. package/dist/managers/CoreInitializer.js +0 -234
  22. package/dist/managers/EventManager.js +0 -67
  23. package/dist/managers/PluginManager.js +0 -296
  24. package/dist/migration-test.js +0 -91
  25. package/dist/plugins/base.js +0 -47
  26. package/dist/plugins/index.js +0 -15
  27. package/dist/plugins/nostr/index.js +0 -4
  28. package/dist/plugins/nostr/nostrConnector.js +0 -413
  29. package/dist/plugins/nostr/nostrConnectorPlugin.js +0 -446
  30. package/dist/plugins/nostr/nostrSigner.js +0 -313
  31. package/dist/plugins/nostr/types.js +0 -1
  32. package/dist/plugins/oauth/index.js +0 -3
  33. package/dist/plugins/oauth/oauthConnector.js +0 -753
  34. package/dist/plugins/oauth/oauthPlugin.js +0 -396
  35. package/dist/plugins/oauth/types.js +0 -1
  36. package/dist/plugins/web3/index.js +0 -4
  37. package/dist/plugins/web3/types.js +0 -1
  38. package/dist/plugins/web3/web3Connector.js +0 -528
  39. package/dist/plugins/web3/web3ConnectorPlugin.js +0 -448
  40. package/dist/plugins/web3/web3Signer.js +0 -308
  41. package/dist/plugins/webauthn/index.js +0 -3
  42. package/dist/plugins/webauthn/types.js +0 -11
  43. package/dist/plugins/webauthn/webauthn.js +0 -478
  44. package/dist/plugins/webauthn/webauthnPlugin.js +0 -398
  45. package/dist/plugins/webauthn/webauthnSigner.js +0 -304
  46. package/dist/storage/storage.js +0 -147
  47. package/dist/types/config/simplified-config.d.ts +0 -114
  48. package/dist/types/core.d.ts +0 -305
  49. package/dist/types/gundb/crypto.d.ts +0 -95
  50. package/dist/types/gundb/db.d.ts +0 -404
  51. package/dist/types/gundb/derive.d.ts +0 -21
  52. package/dist/types/gundb/errors.d.ts +0 -42
  53. package/dist/types/gundb/index.d.ts +0 -3
  54. package/dist/types/gundb/restricted-put.d.ts +0 -15
  55. package/dist/types/gundb/rxjs.d.ts +0 -110
  56. package/dist/types/gundb/simple-api.d.ts +0 -90
  57. package/dist/types/gundb/types.d.ts +0 -264
  58. package/dist/types/index.d.ts +0 -14
  59. package/dist/types/interfaces/common.d.ts +0 -85
  60. package/dist/types/interfaces/events.d.ts +0 -131
  61. package/dist/types/interfaces/plugin.d.ts +0 -162
  62. package/dist/types/interfaces/shogun.d.ts +0 -215
  63. package/dist/types/managers/AuthManager.d.ts +0 -72
  64. package/dist/types/managers/CoreInitializer.d.ts +0 -40
  65. package/dist/types/managers/EventManager.d.ts +0 -49
  66. package/dist/types/managers/PluginManager.d.ts +0 -145
  67. package/dist/types/migration-test.d.ts +0 -16
  68. package/dist/types/plugins/base.d.ts +0 -35
  69. package/dist/types/plugins/index.d.ts +0 -14
  70. package/dist/types/plugins/nostr/index.d.ts +0 -4
  71. package/dist/types/plugins/nostr/nostrConnector.d.ts +0 -119
  72. package/dist/types/plugins/nostr/nostrConnectorPlugin.d.ts +0 -163
  73. package/dist/types/plugins/nostr/nostrSigner.d.ts +0 -105
  74. package/dist/types/plugins/nostr/types.d.ts +0 -122
  75. package/dist/types/plugins/oauth/index.d.ts +0 -3
  76. package/dist/types/plugins/oauth/oauthConnector.d.ts +0 -110
  77. package/dist/types/plugins/oauth/oauthPlugin.d.ts +0 -91
  78. package/dist/types/plugins/oauth/types.d.ts +0 -114
  79. package/dist/types/plugins/web3/index.d.ts +0 -4
  80. package/dist/types/plugins/web3/types.d.ts +0 -107
  81. package/dist/types/plugins/web3/web3Connector.d.ts +0 -129
  82. package/dist/types/plugins/web3/web3ConnectorPlugin.d.ts +0 -160
  83. package/dist/types/plugins/web3/web3Signer.d.ts +0 -114
  84. package/dist/types/plugins/webauthn/index.d.ts +0 -3
  85. package/dist/types/plugins/webauthn/types.d.ts +0 -162
  86. package/dist/types/plugins/webauthn/webauthn.d.ts +0 -129
  87. package/dist/types/plugins/webauthn/webauthnPlugin.d.ts +0 -158
  88. package/dist/types/plugins/webauthn/webauthnSigner.d.ts +0 -91
  89. package/dist/types/storage/storage.d.ts +0 -50
  90. package/dist/types/utils/errorHandler.d.ts +0 -119
  91. package/dist/types/utils/eventEmitter.d.ts +0 -39
  92. package/dist/types/utils/validation.d.ts +0 -27
  93. package/dist/utils/errorHandler.js +0 -241
  94. package/dist/utils/eventEmitter.js +0 -76
  95. package/dist/utils/validation.js +0 -72
@@ -1,396 +0,0 @@
1
- import { BasePlugin } from "../base";
2
- import { OAuthConnector } from "./oauthConnector";
3
- import { ErrorHandler, ErrorType, createError } from "../../utils/errorHandler";
4
- import { ShogunStorage } from "../../storage/storage";
5
- /**
6
- * OAuth Plugin for ShogunCore
7
- * Provides authentication with external OAuth providers
8
- */
9
- export class OAuthPlugin extends BasePlugin {
10
- name = "oauth";
11
- version = "1.0.0";
12
- description = "Provides OAuth authentication with external providers for ShogunCore";
13
- oauthConnector = null;
14
- config = {};
15
- storage = null;
16
- /**
17
- * Constructor for OAuthPlugin
18
- * @param config - Initial configuration for OAuth
19
- */
20
- constructor(config) {
21
- super();
22
- if (config) {
23
- this.config = config;
24
- }
25
- }
26
- /**
27
- * @inheritdoc
28
- */
29
- initialize(core) {
30
- this.core = core;
31
- this.storage = new ShogunStorage();
32
- // Inizializziamo il connector OAuth con la configurazione già presente
33
- this.oauthConnector = new OAuthConnector(this.config);
34
- // Valida la configurazione di sicurezza dopo l'inizializzazione
35
- this.validateOAuthSecurity();
36
- }
37
- /**
38
- * Valida la configurazione di sicurezza OAuth
39
- */
40
- validateOAuthSecurity() {
41
- if (!this.oauthConnector)
42
- return;
43
- const providers = this.oauthConnector.getAvailableProviders();
44
- for (const provider of providers) {
45
- const providerConfig = this.config.providers?.[provider];
46
- if (!providerConfig)
47
- continue;
48
- // Verifica che PKCE sia abilitato per tutti i provider
49
- if (!providerConfig.usePKCE && typeof window !== "undefined") {
50
- console.warn(`[oauthPlugin] Provider ${provider} non ha PKCE abilitato - non sicuro per browser`);
51
- }
52
- // Verifica che non ci sia client_secret nel browser (eccetto Google con PKCE)
53
- if (providerConfig.clientSecret && typeof window !== "undefined") {
54
- if (provider === "google" && providerConfig.usePKCE) {
55
- // Non lanciare errore per Google con PKCE
56
- continue;
57
- }
58
- else {
59
- console.error(`[oauthPlugin] Provider ${provider} ha client_secret configurato nel browser - RIMUOVERE`);
60
- throw new Error(`Client secret non può essere usato nel browser per ${provider}`);
61
- }
62
- }
63
- }
64
- }
65
- /**
66
- * Configure the OAuth plugin with provider settings
67
- * @param config - Configuration options for OAuth
68
- */
69
- configure(config) {
70
- // Deep merge provider maps to preserve both existing and new providers
71
- const mergedProviders = {
72
- ...(this.config.providers || {}),
73
- ...(config?.providers || {}),
74
- };
75
- this.config = { ...this.config, ...config, providers: mergedProviders };
76
- // Inizializza il connector se non è già stato fatto
77
- if (!this.oauthConnector) {
78
- this.oauthConnector = new OAuthConnector(this.config);
79
- }
80
- else {
81
- // Update connector configuration se già inizializzato
82
- const conn = this.oauthConnector;
83
- if (typeof conn.updateConfig === "function") {
84
- conn.updateConfig(this.config);
85
- }
86
- else {
87
- // Fallback: recreate connector
88
- this.oauthConnector = new OAuthConnector(this.config);
89
- }
90
- }
91
- // Validate security settings
92
- this.validateOAuthSecurity();
93
- }
94
- /**
95
- * @inheritdoc
96
- */
97
- destroy() {
98
- if (this.oauthConnector) {
99
- const conn = this.oauthConnector;
100
- if (typeof conn.cleanup === "function") {
101
- conn.cleanup();
102
- }
103
- }
104
- this.oauthConnector = null;
105
- this.storage = null;
106
- super.destroy();
107
- }
108
- /**
109
- * Ensure that the OAuth connector is initialized
110
- * @private
111
- */
112
- assertOAuthConnector() {
113
- this.assertInitialized();
114
- if (!this.oauthConnector) {
115
- throw new Error("OAuth connector not initialized");
116
- }
117
- return this.oauthConnector;
118
- }
119
- /**
120
- * @inheritdoc
121
- */
122
- isSupported() {
123
- try {
124
- const conn = this.assertOAuthConnector();
125
- return typeof conn.isSupported === "function" ? conn.isSupported() : true;
126
- }
127
- catch {
128
- // If connector is not available, return false
129
- return false;
130
- }
131
- }
132
- /**
133
- * @inheritdoc
134
- */
135
- getAvailableProviders() {
136
- try {
137
- const conn = this.assertOAuthConnector();
138
- return typeof conn.getAvailableProviders === "function"
139
- ? conn.getAvailableProviders()
140
- : [];
141
- }
142
- catch {
143
- // If connector is not available, return empty array
144
- return [];
145
- }
146
- }
147
- /**
148
- * @inheritdoc
149
- */
150
- async initiateOAuth(provider) {
151
- const conn = this.assertOAuthConnector();
152
- return conn.initiateOAuth(provider);
153
- }
154
- /**
155
- * @inheritdoc
156
- */
157
- async completeOAuth(provider, authCode, state) {
158
- const conn = this.assertOAuthConnector();
159
- return conn.completeOAuth(provider, authCode, state);
160
- }
161
- /**
162
- * @inheritdoc
163
- */
164
- async generateCredentials(userInfo, provider) {
165
- const conn = this.assertOAuthConnector();
166
- return conn.generateCredentials(userInfo, provider);
167
- }
168
- /**
169
- * Login with OAuth
170
- * @param provider - OAuth provider to use
171
- * @returns {Promise<AuthResult>} Authentication result
172
- * @description Authenticates user using OAuth with external providers
173
- * NOTE: This method only initiates the OAuth flow. The actual authentication
174
- * happens in handleOAuthCallback when the provider redirects back.
175
- */
176
- async login(provider) {
177
- try {
178
- const core = this.assertInitialized();
179
- if (!provider) {
180
- throw createError(ErrorType.VALIDATION, "PROVIDER_REQUIRED", "OAuth provider required for OAuth login");
181
- }
182
- if (!this.isSupported()) {
183
- throw createError(ErrorType.ENVIRONMENT, "OAUTH_UNAVAILABLE", "OAuth is not supported in this environment");
184
- }
185
- // Check if provider is available
186
- const availableProviders = this.getAvailableProviders();
187
- if (!availableProviders.includes(provider)) {
188
- throw createError(ErrorType.VALIDATION, "PROVIDER_NOT_CONFIGURED", `Provider ${provider} is not configured or available`);
189
- }
190
- // Initiate OAuth flow with the provider
191
- const oauthResult = await this.initiateOAuth(provider);
192
- if (!oauthResult.success) {
193
- throw createError(ErrorType.AUTHENTICATION, "OAUTH_INITIATION_FAILED", oauthResult.error || "Failed to initiate OAuth flow");
194
- }
195
- // In a browser environment, this would redirect to the OAuth provider
196
- // The frontend should handle the redirect and then call handleOAuthCallback
197
- // with the received code and state when the provider redirects back
198
- // Return early with the auth URL that the frontend should use for redirection
199
- return {
200
- success: true,
201
- redirectUrl: oauthResult.authUrl,
202
- pendingAuth: true,
203
- message: "Redirect to OAuth provider required to complete authentication",
204
- provider,
205
- authMethod: "oauth",
206
- };
207
- }
208
- catch (error) {
209
- // Handle both ShogunError and generic errors
210
- const errorType = error?.type || ErrorType.AUTHENTICATION;
211
- const errorCode = error?.code || "OAUTH_LOGIN_ERROR";
212
- const errorMessage = error?.message || "Unknown error during OAuth login";
213
- ErrorHandler.handle(errorType, errorCode, errorMessage, error);
214
- return { success: false, error: errorMessage };
215
- }
216
- }
217
- /**
218
- * Register new user with OAuth provider
219
- * @param provider - OAuth provider
220
- * @returns {Promise<SignUpResult>} Registration result
221
- */
222
- async signUp(provider) {
223
- try {
224
- const core = this.assertInitialized();
225
- if (!provider) {
226
- throw createError(ErrorType.VALIDATION, "PROVIDER_REQUIRED", "OAuth provider required for OAuth signup");
227
- }
228
- if (!this.isSupported()) {
229
- throw createError(ErrorType.ENVIRONMENT, "OAUTH_UNAVAILABLE", "OAuth is not supported in this environment");
230
- }
231
- // Check if provider is available
232
- const availableProviders = this.getAvailableProviders();
233
- if (!availableProviders.includes(provider)) {
234
- throw createError(ErrorType.VALIDATION, "PROVIDER_NOT_CONFIGURED", `Provider ${provider} is not configured or available`);
235
- }
236
- // Initiate OAuth flow with the provider
237
- const oauthResult = await this.initiateOAuth(provider);
238
- if (!oauthResult.success) {
239
- throw createError(ErrorType.AUTHENTICATION, "OAUTH_INITIATION_FAILED", oauthResult.error || "Failed to initiate OAuth flow");
240
- }
241
- // In a browser environment, this would redirect to the OAuth provider
242
- // The frontend should handle the redirect and then call handleOAuthCallback
243
- // with the received code and state when the provider redirects back
244
- // Return early with the auth URL that the frontend should use for redirection
245
- return {
246
- success: true,
247
- redirectUrl: oauthResult.authUrl,
248
- pendingAuth: true,
249
- message: "Redirect to OAuth provider required to complete registration",
250
- provider,
251
- authMethod: "oauth",
252
- };
253
- }
254
- catch (error) {
255
- // Handle both ShogunError and generic errors
256
- const errorType = error?.type || ErrorType.AUTHENTICATION;
257
- const errorCode = error?.code || "OAUTH_SIGNUP_ERROR";
258
- const errorMessage = error?.message || "Unknown error during OAuth signup";
259
- ErrorHandler.handle(errorType, errorCode, errorMessage, error);
260
- return { success: false, error: errorMessage };
261
- }
262
- }
263
- /**
264
- * Handle OAuth callback (for frontend integration)
265
- * This method would be called when the OAuth provider redirects back
266
- */
267
- async handleOAuthCallback(provider, authCode, state) {
268
- try {
269
- const core = this.assertInitialized();
270
- // Validazione di sicurezza pre-callback
271
- if (!authCode || !state) {
272
- throw new Error("Authorization code and state parameter are required");
273
- }
274
- // Complete the OAuth flow
275
- const result = await this.completeOAuth(provider, authCode, state);
276
- if (!result.success || !result.userInfo) {
277
- throw new Error(result.error || "Failed to complete OAuth flow");
278
- }
279
- // Genera credenziali da user info
280
- const credentials = await this.generateCredentials(result.userInfo, provider);
281
- // Set authentication method
282
- core.setAuthMethod("oauth");
283
- // Login o signup usando la chiave derivata
284
- const authResult = await this._loginOrSignUp(credentials.username, credentials.key);
285
- if (authResult.success) {
286
- // Store user info in user metadata
287
- if (core.user) {
288
- await core.user.put({
289
- oauth: {
290
- provider,
291
- id: result.userInfo.id,
292
- email: result.userInfo.email,
293
- name: result.userInfo.name,
294
- picture: result.userInfo.picture,
295
- lastLogin: Date.now(),
296
- },
297
- });
298
- }
299
- // Emit appropriate event
300
- const eventType = authResult.isNewUser ? "auth:signup" : "auth:login";
301
- core.emit(eventType, {
302
- userPub: authResult.userPub || "",
303
- username: credentials.username,
304
- method: "oauth",
305
- provider,
306
- });
307
- // Pulisci i dati OAuth scaduti dopo un login riuscito
308
- this.cleanupExpiredOAuthData();
309
- // Return auth result with OAuth user data included
310
- return {
311
- ...authResult,
312
- sea: authResult.sea, // Include SEA pair from core
313
- user: {
314
- userPub: authResult.userPub,
315
- username: credentials.username,
316
- email: result.userInfo.email,
317
- name: result.userInfo.name ||
318
- result.userInfo.email ||
319
- `OAuth User (${provider})`,
320
- picture: result.userInfo.picture,
321
- oauth: {
322
- provider,
323
- id: result.userInfo.id,
324
- email: result.userInfo.email,
325
- name: result.userInfo.name,
326
- picture: result.userInfo.picture,
327
- lastLogin: Date.now(),
328
- },
329
- },
330
- };
331
- }
332
- return authResult;
333
- }
334
- catch (error) {
335
- // Pulisci i dati OAuth anche in caso di errore
336
- this.cleanupExpiredOAuthData();
337
- return {
338
- success: false,
339
- error: error.message || "Failed to handle OAuth callback",
340
- };
341
- }
342
- }
343
- /**
344
- * Pulisce i dati OAuth scaduti
345
- */
346
- cleanupExpiredOAuthData() {
347
- if (this.oauthConnector) {
348
- // Il metodo cleanupExpiredOAuthData è privato nel connector
349
- // quindi usiamo il metodo pubblico clearUserCache
350
- const conn = this.oauthConnector;
351
- if (typeof conn.clearUserCache === "function") {
352
- conn.clearUserCache();
353
- }
354
- }
355
- }
356
- /**
357
- * Private helper to login or sign up a user
358
- */
359
- async _loginOrSignUp(username, k) {
360
- if (!this.core) {
361
- return { success: false, error: "Shogun core not available" };
362
- }
363
- // Try login first
364
- const loginResult = await this.core.login(username, "", k);
365
- if (loginResult.success) {
366
- // Session is automatically saved by the login method
367
- loginResult.isNewUser = false;
368
- // Include SEA pair from core
369
- if (this.core.user && this.core.user._?.sea) {
370
- loginResult.sea = this.core.user._.sea;
371
- }
372
- return loginResult;
373
- }
374
- // If login fails, try signup
375
- const signupResult = await this.core.signUp(username, undefined, k);
376
- if (signupResult.success) {
377
- // Immediately login after signup
378
- const postSignupLogin = await this.core.login(username, "", k);
379
- if (postSignupLogin.success) {
380
- // Session is automatically saved by the login method
381
- postSignupLogin.isNewUser = true;
382
- // Include SEA pair from core
383
- if (this.core.user && this.core.user._?.sea) {
384
- postSignupLogin.sea = this.core.user._.sea;
385
- }
386
- return postSignupLogin;
387
- }
388
- return {
389
- success: false,
390
- error: postSignupLogin.error || "Login failed after successful signup.",
391
- };
392
- }
393
- // Return the original signup error for other failures
394
- return signupResult;
395
- }
396
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1,4 +0,0 @@
1
- export * from "./web3ConnectorPlugin";
2
- export * from "./types";
3
- export * from "./web3Connector";
4
- export * from "./web3Signer";
@@ -1 +0,0 @@
1
- export {};