@oxyhq/core 1.0.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.
Files changed (277) hide show
  1. package/README.md +50 -0
  2. package/dist/cjs/AuthManager.js +361 -0
  3. package/dist/cjs/CrossDomainAuth.js +258 -0
  4. package/dist/cjs/HttpService.js +618 -0
  5. package/dist/cjs/OxyServices.base.js +263 -0
  6. package/dist/cjs/OxyServices.errors.js +22 -0
  7. package/dist/cjs/OxyServices.js +63 -0
  8. package/dist/cjs/constants/version.js +16 -0
  9. package/dist/cjs/crypto/index.js +20 -0
  10. package/dist/cjs/crypto/keyManager.js +887 -0
  11. package/dist/cjs/crypto/polyfill.js +64 -0
  12. package/dist/cjs/crypto/recoveryPhrase.js +169 -0
  13. package/dist/cjs/crypto/signatureService.js +296 -0
  14. package/dist/cjs/i18n/index.js +73 -0
  15. package/dist/cjs/i18n/locales/ar-SA.json +120 -0
  16. package/dist/cjs/i18n/locales/ca-ES.json +120 -0
  17. package/dist/cjs/i18n/locales/de-DE.json +120 -0
  18. package/dist/cjs/i18n/locales/en-US.json +956 -0
  19. package/dist/cjs/i18n/locales/es-ES.json +944 -0
  20. package/dist/cjs/i18n/locales/fr-FR.json +120 -0
  21. package/dist/cjs/i18n/locales/it-IT.json +120 -0
  22. package/dist/cjs/i18n/locales/ja-JP.json +119 -0
  23. package/dist/cjs/i18n/locales/ko-KR.json +120 -0
  24. package/dist/cjs/i18n/locales/locales/ar-SA.json +120 -0
  25. package/dist/cjs/i18n/locales/locales/ca-ES.json +120 -0
  26. package/dist/cjs/i18n/locales/locales/de-DE.json +120 -0
  27. package/dist/cjs/i18n/locales/locales/en-US.json +956 -0
  28. package/dist/cjs/i18n/locales/locales/es-ES.json +944 -0
  29. package/dist/cjs/i18n/locales/locales/fr-FR.json +120 -0
  30. package/dist/cjs/i18n/locales/locales/it-IT.json +120 -0
  31. package/dist/cjs/i18n/locales/locales/ja-JP.json +119 -0
  32. package/dist/cjs/i18n/locales/locales/ko-KR.json +120 -0
  33. package/dist/cjs/i18n/locales/locales/pt-PT.json +120 -0
  34. package/dist/cjs/i18n/locales/locales/zh-CN.json +120 -0
  35. package/dist/cjs/i18n/locales/pt-PT.json +120 -0
  36. package/dist/cjs/i18n/locales/zh-CN.json +120 -0
  37. package/dist/cjs/index.js +153 -0
  38. package/dist/cjs/mixins/OxyServices.analytics.js +49 -0
  39. package/dist/cjs/mixins/OxyServices.assets.js +380 -0
  40. package/dist/cjs/mixins/OxyServices.auth.js +259 -0
  41. package/dist/cjs/mixins/OxyServices.developer.js +97 -0
  42. package/dist/cjs/mixins/OxyServices.devices.js +116 -0
  43. package/dist/cjs/mixins/OxyServices.features.js +309 -0
  44. package/dist/cjs/mixins/OxyServices.fedcm.js +435 -0
  45. package/dist/cjs/mixins/OxyServices.karma.js +108 -0
  46. package/dist/cjs/mixins/OxyServices.language.js +154 -0
  47. package/dist/cjs/mixins/OxyServices.location.js +43 -0
  48. package/dist/cjs/mixins/OxyServices.payment.js +158 -0
  49. package/dist/cjs/mixins/OxyServices.popup.js +371 -0
  50. package/dist/cjs/mixins/OxyServices.privacy.js +162 -0
  51. package/dist/cjs/mixins/OxyServices.redirect.js +345 -0
  52. package/dist/cjs/mixins/OxyServices.security.js +81 -0
  53. package/dist/cjs/mixins/OxyServices.user.js +355 -0
  54. package/dist/cjs/mixins/OxyServices.utility.js +156 -0
  55. package/dist/cjs/mixins/index.js +79 -0
  56. package/dist/cjs/mixins/mixinHelpers.js +53 -0
  57. package/dist/cjs/models/interfaces.js +20 -0
  58. package/dist/cjs/models/session.js +2 -0
  59. package/dist/cjs/shared/index.js +70 -0
  60. package/dist/cjs/shared/utils/colorUtils.js +153 -0
  61. package/dist/cjs/shared/utils/debugUtils.js +73 -0
  62. package/dist/cjs/shared/utils/errorUtils.js +183 -0
  63. package/dist/cjs/shared/utils/index.js +49 -0
  64. package/dist/cjs/shared/utils/networkUtils.js +183 -0
  65. package/dist/cjs/shared/utils/themeUtils.js +106 -0
  66. package/dist/cjs/utils/apiUtils.js +61 -0
  67. package/dist/cjs/utils/asyncUtils.js +194 -0
  68. package/dist/cjs/utils/cache.js +226 -0
  69. package/dist/cjs/utils/deviceManager.js +205 -0
  70. package/dist/cjs/utils/errorUtils.js +154 -0
  71. package/dist/cjs/utils/index.js +26 -0
  72. package/dist/cjs/utils/languageUtils.js +165 -0
  73. package/dist/cjs/utils/loggerUtils.js +126 -0
  74. package/dist/cjs/utils/platform.js +144 -0
  75. package/dist/cjs/utils/requestUtils.js +209 -0
  76. package/dist/cjs/utils/sessionUtils.js +181 -0
  77. package/dist/cjs/utils/validationUtils.js +173 -0
  78. package/dist/esm/AuthManager.js +356 -0
  79. package/dist/esm/CrossDomainAuth.js +253 -0
  80. package/dist/esm/HttpService.js +614 -0
  81. package/dist/esm/OxyServices.base.js +259 -0
  82. package/dist/esm/OxyServices.errors.js +17 -0
  83. package/dist/esm/OxyServices.js +59 -0
  84. package/dist/esm/constants/version.js +13 -0
  85. package/dist/esm/crypto/index.js +13 -0
  86. package/dist/esm/crypto/keyManager.js +850 -0
  87. package/dist/esm/crypto/polyfill.js +61 -0
  88. package/dist/esm/crypto/recoveryPhrase.js +132 -0
  89. package/dist/esm/crypto/signatureService.js +259 -0
  90. package/dist/esm/i18n/index.js +69 -0
  91. package/dist/esm/i18n/locales/ar-SA.json +120 -0
  92. package/dist/esm/i18n/locales/ca-ES.json +120 -0
  93. package/dist/esm/i18n/locales/de-DE.json +120 -0
  94. package/dist/esm/i18n/locales/en-US.json +956 -0
  95. package/dist/esm/i18n/locales/es-ES.json +944 -0
  96. package/dist/esm/i18n/locales/fr-FR.json +120 -0
  97. package/dist/esm/i18n/locales/it-IT.json +120 -0
  98. package/dist/esm/i18n/locales/ja-JP.json +119 -0
  99. package/dist/esm/i18n/locales/ko-KR.json +120 -0
  100. package/dist/esm/i18n/locales/locales/ar-SA.json +120 -0
  101. package/dist/esm/i18n/locales/locales/ca-ES.json +120 -0
  102. package/dist/esm/i18n/locales/locales/de-DE.json +120 -0
  103. package/dist/esm/i18n/locales/locales/en-US.json +956 -0
  104. package/dist/esm/i18n/locales/locales/es-ES.json +944 -0
  105. package/dist/esm/i18n/locales/locales/fr-FR.json +120 -0
  106. package/dist/esm/i18n/locales/locales/it-IT.json +120 -0
  107. package/dist/esm/i18n/locales/locales/ja-JP.json +119 -0
  108. package/dist/esm/i18n/locales/locales/ko-KR.json +120 -0
  109. package/dist/esm/i18n/locales/locales/pt-PT.json +120 -0
  110. package/dist/esm/i18n/locales/locales/zh-CN.json +120 -0
  111. package/dist/esm/i18n/locales/pt-PT.json +120 -0
  112. package/dist/esm/i18n/locales/zh-CN.json +120 -0
  113. package/dist/esm/index.js +55 -0
  114. package/dist/esm/mixins/OxyServices.analytics.js +46 -0
  115. package/dist/esm/mixins/OxyServices.assets.js +377 -0
  116. package/dist/esm/mixins/OxyServices.auth.js +256 -0
  117. package/dist/esm/mixins/OxyServices.developer.js +94 -0
  118. package/dist/esm/mixins/OxyServices.devices.js +113 -0
  119. package/dist/esm/mixins/OxyServices.features.js +306 -0
  120. package/dist/esm/mixins/OxyServices.fedcm.js +433 -0
  121. package/dist/esm/mixins/OxyServices.karma.js +105 -0
  122. package/dist/esm/mixins/OxyServices.language.js +118 -0
  123. package/dist/esm/mixins/OxyServices.location.js +40 -0
  124. package/dist/esm/mixins/OxyServices.payment.js +155 -0
  125. package/dist/esm/mixins/OxyServices.popup.js +369 -0
  126. package/dist/esm/mixins/OxyServices.privacy.js +159 -0
  127. package/dist/esm/mixins/OxyServices.redirect.js +343 -0
  128. package/dist/esm/mixins/OxyServices.security.js +78 -0
  129. package/dist/esm/mixins/OxyServices.user.js +352 -0
  130. package/dist/esm/mixins/OxyServices.utility.js +153 -0
  131. package/dist/esm/mixins/index.js +76 -0
  132. package/dist/esm/mixins/mixinHelpers.js +48 -0
  133. package/dist/esm/models/interfaces.js +17 -0
  134. package/dist/esm/models/session.js +1 -0
  135. package/dist/esm/shared/index.js +31 -0
  136. package/dist/esm/shared/utils/colorUtils.js +143 -0
  137. package/dist/esm/shared/utils/debugUtils.js +65 -0
  138. package/dist/esm/shared/utils/errorUtils.js +170 -0
  139. package/dist/esm/shared/utils/index.js +15 -0
  140. package/dist/esm/shared/utils/networkUtils.js +173 -0
  141. package/dist/esm/shared/utils/themeUtils.js +98 -0
  142. package/dist/esm/utils/apiUtils.js +55 -0
  143. package/dist/esm/utils/asyncUtils.js +179 -0
  144. package/dist/esm/utils/cache.js +218 -0
  145. package/dist/esm/utils/deviceManager.js +168 -0
  146. package/dist/esm/utils/errorUtils.js +146 -0
  147. package/dist/esm/utils/index.js +7 -0
  148. package/dist/esm/utils/languageUtils.js +158 -0
  149. package/dist/esm/utils/loggerUtils.js +115 -0
  150. package/dist/esm/utils/platform.js +102 -0
  151. package/dist/esm/utils/requestUtils.js +203 -0
  152. package/dist/esm/utils/sessionUtils.js +171 -0
  153. package/dist/esm/utils/validationUtils.js +153 -0
  154. package/dist/types/AuthManager.d.ts +143 -0
  155. package/dist/types/CrossDomainAuth.d.ts +160 -0
  156. package/dist/types/HttpService.d.ts +163 -0
  157. package/dist/types/OxyServices.base.d.ts +126 -0
  158. package/dist/types/OxyServices.d.ts +81 -0
  159. package/dist/types/OxyServices.errors.d.ts +11 -0
  160. package/dist/types/constants/version.d.ts +13 -0
  161. package/dist/types/crypto/index.d.ts +11 -0
  162. package/dist/types/crypto/keyManager.d.ts +189 -0
  163. package/dist/types/crypto/polyfill.d.ts +11 -0
  164. package/dist/types/crypto/recoveryPhrase.d.ts +58 -0
  165. package/dist/types/crypto/signatureService.d.ts +86 -0
  166. package/dist/types/i18n/index.d.ts +3 -0
  167. package/dist/types/index.d.ts +50 -0
  168. package/dist/types/mixins/OxyServices.analytics.d.ts +66 -0
  169. package/dist/types/mixins/OxyServices.assets.d.ts +135 -0
  170. package/dist/types/mixins/OxyServices.auth.d.ts +186 -0
  171. package/dist/types/mixins/OxyServices.developer.d.ts +99 -0
  172. package/dist/types/mixins/OxyServices.devices.d.ts +96 -0
  173. package/dist/types/mixins/OxyServices.features.d.ts +228 -0
  174. package/dist/types/mixins/OxyServices.fedcm.d.ts +200 -0
  175. package/dist/types/mixins/OxyServices.karma.d.ts +85 -0
  176. package/dist/types/mixins/OxyServices.language.d.ts +81 -0
  177. package/dist/types/mixins/OxyServices.location.d.ts +64 -0
  178. package/dist/types/mixins/OxyServices.payment.d.ts +111 -0
  179. package/dist/types/mixins/OxyServices.popup.d.ts +205 -0
  180. package/dist/types/mixins/OxyServices.privacy.d.ts +122 -0
  181. package/dist/types/mixins/OxyServices.redirect.d.ts +245 -0
  182. package/dist/types/mixins/OxyServices.security.d.ts +78 -0
  183. package/dist/types/mixins/OxyServices.user.d.ts +182 -0
  184. package/dist/types/mixins/OxyServices.utility.d.ts +93 -0
  185. package/dist/types/mixins/index.d.ts +30 -0
  186. package/dist/types/mixins/mixinHelpers.d.ts +31 -0
  187. package/dist/types/models/interfaces.d.ts +415 -0
  188. package/dist/types/models/session.d.ts +27 -0
  189. package/dist/types/shared/index.d.ts +28 -0
  190. package/dist/types/shared/utils/colorUtils.d.ts +104 -0
  191. package/dist/types/shared/utils/debugUtils.d.ts +48 -0
  192. package/dist/types/shared/utils/errorUtils.d.ts +97 -0
  193. package/dist/types/shared/utils/index.d.ts +13 -0
  194. package/dist/types/shared/utils/networkUtils.d.ts +139 -0
  195. package/dist/types/shared/utils/themeUtils.d.ts +90 -0
  196. package/dist/types/utils/apiUtils.d.ts +53 -0
  197. package/dist/types/utils/asyncUtils.d.ts +58 -0
  198. package/dist/types/utils/cache.d.ts +127 -0
  199. package/dist/types/utils/deviceManager.d.ts +65 -0
  200. package/dist/types/utils/errorUtils.d.ts +46 -0
  201. package/dist/types/utils/index.d.ts +6 -0
  202. package/dist/types/utils/languageUtils.d.ts +37 -0
  203. package/dist/types/utils/loggerUtils.d.ts +48 -0
  204. package/dist/types/utils/platform.d.ts +40 -0
  205. package/dist/types/utils/requestUtils.d.ts +123 -0
  206. package/dist/types/utils/sessionUtils.d.ts +54 -0
  207. package/dist/types/utils/validationUtils.d.ts +85 -0
  208. package/package.json +84 -0
  209. package/src/AuthManager.ts +436 -0
  210. package/src/CrossDomainAuth.ts +307 -0
  211. package/src/HttpService.ts +752 -0
  212. package/src/OxyServices.base.ts +334 -0
  213. package/src/OxyServices.errors.ts +26 -0
  214. package/src/OxyServices.ts +129 -0
  215. package/src/constants/version.ts +15 -0
  216. package/src/crypto/index.ts +25 -0
  217. package/src/crypto/keyManager.ts +962 -0
  218. package/src/crypto/polyfill.ts +70 -0
  219. package/src/crypto/recoveryPhrase.ts +166 -0
  220. package/src/crypto/signatureService.ts +323 -0
  221. package/src/i18n/index.ts +75 -0
  222. package/src/i18n/locales/ar-SA.json +120 -0
  223. package/src/i18n/locales/ca-ES.json +120 -0
  224. package/src/i18n/locales/de-DE.json +120 -0
  225. package/src/i18n/locales/en-US.json +956 -0
  226. package/src/i18n/locales/es-ES.json +944 -0
  227. package/src/i18n/locales/fr-FR.json +120 -0
  228. package/src/i18n/locales/it-IT.json +120 -0
  229. package/src/i18n/locales/ja-JP.json +119 -0
  230. package/src/i18n/locales/ko-KR.json +120 -0
  231. package/src/i18n/locales/pt-PT.json +120 -0
  232. package/src/i18n/locales/zh-CN.json +120 -0
  233. package/src/index.ts +153 -0
  234. package/src/mixins/OxyServices.analytics.ts +53 -0
  235. package/src/mixins/OxyServices.assets.ts +412 -0
  236. package/src/mixins/OxyServices.auth.ts +358 -0
  237. package/src/mixins/OxyServices.developer.ts +114 -0
  238. package/src/mixins/OxyServices.devices.ts +119 -0
  239. package/src/mixins/OxyServices.features.ts +428 -0
  240. package/src/mixins/OxyServices.fedcm.ts +494 -0
  241. package/src/mixins/OxyServices.karma.ts +111 -0
  242. package/src/mixins/OxyServices.language.ts +127 -0
  243. package/src/mixins/OxyServices.location.ts +46 -0
  244. package/src/mixins/OxyServices.payment.ts +163 -0
  245. package/src/mixins/OxyServices.popup.ts +443 -0
  246. package/src/mixins/OxyServices.privacy.ts +182 -0
  247. package/src/mixins/OxyServices.redirect.ts +397 -0
  248. package/src/mixins/OxyServices.security.ts +103 -0
  249. package/src/mixins/OxyServices.user.ts +392 -0
  250. package/src/mixins/OxyServices.utility.ts +191 -0
  251. package/src/mixins/index.ts +91 -0
  252. package/src/mixins/mixinHelpers.ts +69 -0
  253. package/src/models/interfaces.ts +511 -0
  254. package/src/models/session.ts +30 -0
  255. package/src/shared/index.ts +82 -0
  256. package/src/shared/utils/colorUtils.ts +155 -0
  257. package/src/shared/utils/debugUtils.ts +73 -0
  258. package/src/shared/utils/errorUtils.ts +181 -0
  259. package/src/shared/utils/index.ts +59 -0
  260. package/src/shared/utils/networkUtils.ts +248 -0
  261. package/src/shared/utils/themeUtils.ts +115 -0
  262. package/src/types/bip39.d.ts +32 -0
  263. package/src/types/buffer.d.ts +97 -0
  264. package/src/types/color.d.ts +20 -0
  265. package/src/types/elliptic.d.ts +62 -0
  266. package/src/utils/apiUtils.ts +88 -0
  267. package/src/utils/asyncUtils.ts +252 -0
  268. package/src/utils/cache.ts +264 -0
  269. package/src/utils/deviceManager.ts +198 -0
  270. package/src/utils/errorUtils.ts +216 -0
  271. package/src/utils/index.ts +21 -0
  272. package/src/utils/languageUtils.ts +174 -0
  273. package/src/utils/loggerUtils.ts +153 -0
  274. package/src/utils/platform.ts +117 -0
  275. package/src/utils/requestUtils.ts +237 -0
  276. package/src/utils/sessionUtils.ts +206 -0
  277. package/src/utils/validationUtils.ts +174 -0
package/README.md ADDED
@@ -0,0 +1,50 @@
1
+ # @oxyhq/core
2
+
3
+ OxyHQ SDK Foundation. Platform-agnostic core library that works in Node.js, browser, and React Native environments. No React dependency.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @oxyhq/core
9
+ ```
10
+
11
+ ## Contents
12
+
13
+ - **OxyServices API client** — all API methods for interacting with OxyHQ services
14
+ - **AuthManager, CrossDomainAuth** — authentication and cross-domain session handling
15
+ - **Crypto** — KeyManager, SignatureService, RecoveryPhraseService
16
+ - **Models and types** — User, ApiError, ClientSession, and more
17
+ - **i18n** — translate function and locale files
18
+ - **Shared utilities** — color, theme, error, network, debug helpers
19
+ - **Platform detection utilities**
20
+ - **Device management**
21
+
22
+ ## Exports
23
+
24
+ The package exposes three entry points:
25
+
26
+ - `@oxyhq/core` — main entry (API client, auth, models, i18n, platform, device)
27
+ - `@oxyhq/core/crypto` — cryptographic utilities (KeyManager, SignatureService, RecoveryPhraseService)
28
+ - `@oxyhq/core/shared` — shared utilities (color, theme, error, network, debug)
29
+
30
+ ## Usage
31
+
32
+ ```ts
33
+ import { OxyServices, oxyClient } from '@oxyhq/core';
34
+ import type { User, ApiError } from '@oxyhq/core';
35
+
36
+ // Get user
37
+ const user = await oxyClient.getUserById('123');
38
+
39
+ // Crypto
40
+ import { KeyManager, SignatureService } from '@oxyhq/core/crypto';
41
+ const keyManager = new KeyManager();
42
+ ```
43
+
44
+ ## Build
45
+
46
+ ```bash
47
+ npm run build
48
+ ```
49
+
50
+ Compiles with TypeScript, producing CJS, ESM, and type declaration outputs.
@@ -0,0 +1,361 @@
1
+ "use strict";
2
+ /**
3
+ * AuthManager - Centralized Authentication Manager
4
+ *
5
+ * Provides a unified authentication interface for all platforms.
6
+ * Handles token storage, session management, and auth state changes.
7
+ *
8
+ * @module core/AuthManager
9
+ */
10
+ Object.defineProperty(exports, "__esModule", { value: true });
11
+ exports.AuthManager = void 0;
12
+ exports.createAuthManager = createAuthManager;
13
+ /**
14
+ * Storage keys used by AuthManager.
15
+ */
16
+ const STORAGE_KEYS = {
17
+ ACCESS_TOKEN: 'oxy_access_token',
18
+ REFRESH_TOKEN: 'oxy_refresh_token',
19
+ SESSION: 'oxy_session',
20
+ USER: 'oxy_user',
21
+ AUTH_METHOD: 'oxy_auth_method',
22
+ };
23
+ /**
24
+ * Default in-memory storage for non-browser environments.
25
+ */
26
+ class MemoryStorage {
27
+ constructor() {
28
+ this.store = new Map();
29
+ }
30
+ getItem(key) {
31
+ return this.store.get(key) ?? null;
32
+ }
33
+ setItem(key, value) {
34
+ this.store.set(key, value);
35
+ }
36
+ removeItem(key) {
37
+ this.store.delete(key);
38
+ }
39
+ }
40
+ /**
41
+ * Browser localStorage adapter.
42
+ */
43
+ class LocalStorageAdapter {
44
+ getItem(key) {
45
+ if (typeof window === 'undefined')
46
+ return null;
47
+ try {
48
+ return localStorage.getItem(key);
49
+ }
50
+ catch {
51
+ return null;
52
+ }
53
+ }
54
+ setItem(key, value) {
55
+ if (typeof window === 'undefined')
56
+ return;
57
+ try {
58
+ localStorage.setItem(key, value);
59
+ }
60
+ catch {
61
+ // Storage full or blocked
62
+ }
63
+ }
64
+ removeItem(key) {
65
+ if (typeof window === 'undefined')
66
+ return;
67
+ try {
68
+ localStorage.removeItem(key);
69
+ }
70
+ catch {
71
+ // Ignore errors
72
+ }
73
+ }
74
+ }
75
+ /**
76
+ * AuthManager - Centralized authentication management.
77
+ *
78
+ * Provides a single point of control for:
79
+ * - Token storage and retrieval
80
+ * - Session management
81
+ * - Auth state change notifications
82
+ * - Multiple auth method support
83
+ *
84
+ * @example
85
+ * ```typescript
86
+ * const authManager = new AuthManager(oxyServices);
87
+ *
88
+ * // Listen for auth changes
89
+ * authManager.onAuthStateChange((user) => {
90
+ * console.log('Auth state changed:', user);
91
+ * });
92
+ *
93
+ * // Handle successful auth
94
+ * await authManager.handleAuthSuccess(session);
95
+ *
96
+ * // Sign out
97
+ * await authManager.signOut();
98
+ * ```
99
+ */
100
+ class AuthManager {
101
+ constructor(oxyServices, config = {}) {
102
+ this.listeners = new Set();
103
+ this.currentUser = null;
104
+ this.refreshTimer = null;
105
+ this.oxyServices = oxyServices;
106
+ this.config = {
107
+ storage: config.storage ?? this.getDefaultStorage(),
108
+ autoRefresh: config.autoRefresh ?? true,
109
+ refreshBuffer: config.refreshBuffer ?? 5 * 60 * 1000, // 5 minutes
110
+ };
111
+ this.storage = this.config.storage;
112
+ }
113
+ /**
114
+ * Get default storage based on environment.
115
+ */
116
+ getDefaultStorage() {
117
+ if (typeof window !== 'undefined' && window.localStorage) {
118
+ return new LocalStorageAdapter();
119
+ }
120
+ return new MemoryStorage();
121
+ }
122
+ /**
123
+ * Subscribe to auth state changes.
124
+ *
125
+ * @param callback - Function called when auth state changes
126
+ * @returns Unsubscribe function
127
+ */
128
+ onAuthStateChange(callback) {
129
+ this.listeners.add(callback);
130
+ // Call immediately with current state
131
+ callback(this.currentUser);
132
+ return () => {
133
+ this.listeners.delete(callback);
134
+ };
135
+ }
136
+ /**
137
+ * Notify all listeners of auth state change.
138
+ */
139
+ notifyListeners() {
140
+ for (const listener of this.listeners) {
141
+ try {
142
+ listener(this.currentUser);
143
+ }
144
+ catch {
145
+ // Ignore listener errors
146
+ }
147
+ }
148
+ }
149
+ /**
150
+ * Handle successful authentication.
151
+ *
152
+ * @param session - Session response from auth
153
+ * @param method - Auth method used
154
+ */
155
+ async handleAuthSuccess(session, method = 'credentials') {
156
+ // Store tokens
157
+ if (session.accessToken) {
158
+ await this.storage.setItem(STORAGE_KEYS.ACCESS_TOKEN, session.accessToken);
159
+ this.oxyServices.httpService.setTokens(session.accessToken);
160
+ }
161
+ // Store refresh token if available
162
+ if (session.refreshToken) {
163
+ await this.storage.setItem(STORAGE_KEYS.REFRESH_TOKEN, session.refreshToken);
164
+ }
165
+ // Store session info
166
+ await this.storage.setItem(STORAGE_KEYS.SESSION, JSON.stringify({
167
+ sessionId: session.sessionId,
168
+ deviceId: session.deviceId,
169
+ expiresAt: session.expiresAt,
170
+ }));
171
+ // Store user only if it has valid required fields (not an empty placeholder)
172
+ if (session.user && typeof session.user.id === 'string' && session.user.id.length > 0) {
173
+ await this.storage.setItem(STORAGE_KEYS.USER, JSON.stringify(session.user));
174
+ this.currentUser = session.user;
175
+ }
176
+ // Store auth method
177
+ await this.storage.setItem(STORAGE_KEYS.AUTH_METHOD, method);
178
+ // Setup auto-refresh if enabled
179
+ if (this.config.autoRefresh && session.expiresAt) {
180
+ this.setupTokenRefresh(session.expiresAt);
181
+ }
182
+ // Notify listeners
183
+ this.notifyListeners();
184
+ }
185
+ /**
186
+ * Setup automatic token refresh.
187
+ */
188
+ setupTokenRefresh(expiresAt) {
189
+ if (this.refreshTimer) {
190
+ clearTimeout(this.refreshTimer);
191
+ }
192
+ const expiresAtMs = new Date(expiresAt).getTime();
193
+ const now = Date.now();
194
+ const refreshAt = expiresAtMs - this.config.refreshBuffer;
195
+ const delay = Math.max(0, refreshAt - now);
196
+ if (delay > 0) {
197
+ this.refreshTimer = setTimeout(() => {
198
+ this.refreshToken().catch(() => {
199
+ // Refresh failed, user will need to re-auth
200
+ });
201
+ }, delay);
202
+ }
203
+ }
204
+ /**
205
+ * Refresh the access token.
206
+ */
207
+ async refreshToken() {
208
+ const refreshToken = await this.storage.getItem(STORAGE_KEYS.REFRESH_TOKEN);
209
+ if (!refreshToken) {
210
+ return false;
211
+ }
212
+ try {
213
+ // Cast httpService to proper type (needed due to mixin composition)
214
+ const httpService = this.oxyServices.httpService;
215
+ const response = await httpService.request({
216
+ method: 'POST',
217
+ url: '/api/auth/refresh',
218
+ data: { refreshToken },
219
+ cache: false,
220
+ });
221
+ await this.handleAuthSuccess(response, 'credentials');
222
+ return true;
223
+ }
224
+ catch {
225
+ // Refresh failed, clear session and update state
226
+ await this.clearSession();
227
+ this.currentUser = null;
228
+ this.notifyListeners();
229
+ return false;
230
+ }
231
+ }
232
+ /**
233
+ * Sign out and clear all auth data.
234
+ */
235
+ async signOut() {
236
+ // Clear refresh timer
237
+ if (this.refreshTimer) {
238
+ clearTimeout(this.refreshTimer);
239
+ this.refreshTimer = null;
240
+ }
241
+ // Revoke FedCM credential if supported
242
+ try {
243
+ const services = this.oxyServices;
244
+ if (services.revokeFedCMCredential) {
245
+ await services.revokeFedCMCredential();
246
+ }
247
+ }
248
+ catch {
249
+ // Ignore FedCM errors
250
+ }
251
+ // Clear HTTP client tokens
252
+ this.oxyServices.httpService.setTokens('');
253
+ // Clear storage
254
+ await this.clearSession();
255
+ // Update state and notify
256
+ this.currentUser = null;
257
+ this.notifyListeners();
258
+ }
259
+ /**
260
+ * Clear session data from storage.
261
+ */
262
+ async clearSession() {
263
+ await this.storage.removeItem(STORAGE_KEYS.ACCESS_TOKEN);
264
+ await this.storage.removeItem(STORAGE_KEYS.REFRESH_TOKEN);
265
+ await this.storage.removeItem(STORAGE_KEYS.SESSION);
266
+ await this.storage.removeItem(STORAGE_KEYS.USER);
267
+ await this.storage.removeItem(STORAGE_KEYS.AUTH_METHOD);
268
+ }
269
+ /**
270
+ * Get current user.
271
+ */
272
+ getCurrentUser() {
273
+ return this.currentUser;
274
+ }
275
+ /**
276
+ * Check if user is authenticated.
277
+ */
278
+ isAuthenticated() {
279
+ return this.currentUser !== null;
280
+ }
281
+ /**
282
+ * Get stored access token.
283
+ */
284
+ async getAccessToken() {
285
+ return this.storage.getItem(STORAGE_KEYS.ACCESS_TOKEN);
286
+ }
287
+ /**
288
+ * Get the auth method used for current session.
289
+ */
290
+ async getAuthMethod() {
291
+ const method = await this.storage.getItem(STORAGE_KEYS.AUTH_METHOD);
292
+ return method;
293
+ }
294
+ /**
295
+ * Initialize auth state from storage.
296
+ *
297
+ * Call this on app startup to restore previous session.
298
+ */
299
+ async initialize() {
300
+ try {
301
+ // Try to restore user from storage
302
+ const userJson = await this.storage.getItem(STORAGE_KEYS.USER);
303
+ if (userJson) {
304
+ this.currentUser = JSON.parse(userJson);
305
+ }
306
+ // Restore token to HTTP client
307
+ const token = await this.storage.getItem(STORAGE_KEYS.ACCESS_TOKEN);
308
+ if (token) {
309
+ this.oxyServices.httpService.setTokens(token);
310
+ }
311
+ // Check session expiry
312
+ const sessionJson = await this.storage.getItem(STORAGE_KEYS.SESSION);
313
+ if (sessionJson) {
314
+ const session = JSON.parse(sessionJson);
315
+ if (session.expiresAt) {
316
+ const expiresAt = new Date(session.expiresAt).getTime();
317
+ if (expiresAt <= Date.now()) {
318
+ // Session expired, try refresh
319
+ const refreshed = await this.refreshToken();
320
+ if (!refreshed) {
321
+ await this.clearSession();
322
+ this.currentUser = null;
323
+ }
324
+ }
325
+ else if (this.config.autoRefresh) {
326
+ // Setup refresh timer
327
+ this.setupTokenRefresh(session.expiresAt);
328
+ }
329
+ }
330
+ }
331
+ return this.currentUser;
332
+ }
333
+ catch {
334
+ // Failed to restore, start fresh
335
+ await this.clearSession();
336
+ this.currentUser = null;
337
+ return null;
338
+ }
339
+ }
340
+ /**
341
+ * Destroy the auth manager and clean up resources.
342
+ */
343
+ destroy() {
344
+ if (this.refreshTimer) {
345
+ clearTimeout(this.refreshTimer);
346
+ this.refreshTimer = null;
347
+ }
348
+ this.listeners.clear();
349
+ }
350
+ }
351
+ exports.AuthManager = AuthManager;
352
+ /**
353
+ * Create an AuthManager instance.
354
+ *
355
+ * @param oxyServices - OxyServices instance
356
+ * @param config - Optional configuration
357
+ * @returns AuthManager instance
358
+ */
359
+ function createAuthManager(oxyServices, config) {
360
+ return new AuthManager(oxyServices, config);
361
+ }
@@ -0,0 +1,258 @@
1
+ "use strict";
2
+ /**
3
+ * Cross-Domain Authentication Helper
4
+ *
5
+ * Provides a simplified API for cross-domain SSO authentication that automatically
6
+ * selects the best authentication method based on browser capabilities:
7
+ *
8
+ * 1. FedCM (if supported) - Modern, Google-style browser-native auth
9
+ * 2. Popup (fallback) - OAuth2-style popup window
10
+ * 3. Redirect (final fallback) - Traditional full-page redirect
11
+ *
12
+ * Usage:
13
+ * ```typescript
14
+ * import { CrossDomainAuth } from '@oxyhq/services';
15
+ *
16
+ * const auth = new CrossDomainAuth(oxyServices);
17
+ *
18
+ * // Automatic method selection
19
+ * const session = await auth.signIn();
20
+ *
21
+ * // Or use specific method
22
+ * const session = await auth.signInWithPopup();
23
+ * ```
24
+ */
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.CrossDomainAuth = void 0;
27
+ exports.createCrossDomainAuth = createCrossDomainAuth;
28
+ class CrossDomainAuth {
29
+ constructor(oxyServices) {
30
+ this.oxyServices = oxyServices;
31
+ }
32
+ /**
33
+ * Sign in with automatic method selection
34
+ *
35
+ * Tries methods in this order:
36
+ * 1. FedCM (if supported and not in private browsing)
37
+ * 2. Popup (if not blocked)
38
+ * 3. Redirect (always works)
39
+ *
40
+ * @param options - Authentication options
41
+ * @returns Session with user data and access token
42
+ */
43
+ async signIn(options = {}) {
44
+ const method = options.method || 'auto';
45
+ // If specific method requested, use it directly
46
+ if (method === 'fedcm') {
47
+ return this.signInWithFedCM(options);
48
+ }
49
+ if (method === 'popup') {
50
+ return this.signInWithPopup(options);
51
+ }
52
+ if (method === 'redirect') {
53
+ this.signInWithRedirect(options);
54
+ return null; // Redirect doesn't return immediately
55
+ }
56
+ // Auto mode: Try methods in order of preference
57
+ return this.autoSignIn(options);
58
+ }
59
+ /**
60
+ * Automatic sign-in with progressive enhancement
61
+ *
62
+ * @private
63
+ */
64
+ async autoSignIn(options) {
65
+ // 1. Try FedCM first (best UX, most modern)
66
+ if (this.isFedCMSupported()) {
67
+ try {
68
+ options.onMethodSelected?.('fedcm');
69
+ return await this.signInWithFedCM(options);
70
+ }
71
+ catch (error) {
72
+ console.warn('[CrossDomainAuth] FedCM failed, trying popup...', error);
73
+ }
74
+ }
75
+ // 2. Try popup (good UX, widely supported)
76
+ try {
77
+ options.onMethodSelected?.('popup');
78
+ return await this.signInWithPopup(options);
79
+ }
80
+ catch (error) {
81
+ console.warn('[CrossDomainAuth] Popup failed, falling back to redirect...', error);
82
+ }
83
+ // 3. Fallback to redirect (always works)
84
+ options.onMethodSelected?.('redirect');
85
+ this.signInWithRedirect(options);
86
+ return null;
87
+ }
88
+ /**
89
+ * Sign in using FedCM (Federated Credential Management)
90
+ *
91
+ * Best method - browser-native, no popups, Google-like experience
92
+ */
93
+ async signInWithFedCM(options = {}) {
94
+ return this.oxyServices.signInWithFedCM({
95
+ context: options.isSignup ? 'signup' : 'signin',
96
+ });
97
+ }
98
+ /**
99
+ * Sign in using popup window
100
+ *
101
+ * Good method - preserves app state, no full page reload
102
+ */
103
+ async signInWithPopup(options = {}) {
104
+ return this.oxyServices.signInWithPopup({
105
+ mode: options.isSignup ? 'signup' : 'login',
106
+ width: options.popupDimensions?.width,
107
+ height: options.popupDimensions?.height,
108
+ });
109
+ }
110
+ /**
111
+ * Sign in using full-page redirect
112
+ *
113
+ * Fallback method - works everywhere but loses app state
114
+ */
115
+ signInWithRedirect(options = {}) {
116
+ this.oxyServices.signInWithRedirect({
117
+ redirectUri: options.redirectUri,
118
+ mode: options.isSignup ? 'signup' : 'login',
119
+ });
120
+ }
121
+ /**
122
+ * Handle redirect callback
123
+ *
124
+ * Call this on app startup to check if we're returning from auth redirect
125
+ */
126
+ handleRedirectCallback() {
127
+ return this.oxyServices.handleAuthCallback();
128
+ }
129
+ /**
130
+ * Silent sign-in (check for existing session)
131
+ *
132
+ * Tries to automatically sign in without user interaction.
133
+ * Works with both FedCM and popup/iframe methods.
134
+ *
135
+ * @returns Session if user is already signed in, null otherwise
136
+ */
137
+ async silentSignIn() {
138
+ // Try FedCM silent sign-in first (if supported)
139
+ if (this.isFedCMSupported()) {
140
+ try {
141
+ const session = await this.oxyServices.silentSignInWithFedCM();
142
+ if (session) {
143
+ return session;
144
+ }
145
+ }
146
+ catch (error) {
147
+ console.warn('[CrossDomainAuth] FedCM silent sign-in failed:', error);
148
+ }
149
+ }
150
+ // Fallback to iframe-based silent auth
151
+ try {
152
+ return await this.oxyServices.silentSignIn();
153
+ }
154
+ catch (error) {
155
+ console.warn('[CrossDomainAuth] Silent sign-in failed:', error);
156
+ return null;
157
+ }
158
+ }
159
+ /**
160
+ * Restore session from storage
161
+ *
162
+ * For redirect method - restores previously authenticated session from localStorage
163
+ */
164
+ restoreSession() {
165
+ return this.oxyServices.restoreSession?.() || false;
166
+ }
167
+ /**
168
+ * Check if FedCM is supported in current browser
169
+ */
170
+ isFedCMSupported() {
171
+ return this.oxyServices.constructor.isFedCMSupported?.() || false;
172
+ }
173
+ /**
174
+ * Get recommended authentication method for current environment
175
+ *
176
+ * @returns Recommended method name and reason
177
+ */
178
+ getRecommendedMethod() {
179
+ if (this.isFedCMSupported()) {
180
+ return {
181
+ method: 'fedcm',
182
+ reason: 'FedCM is supported - provides best UX with browser-native auth',
183
+ };
184
+ }
185
+ if (typeof window !== 'undefined') {
186
+ return {
187
+ method: 'popup',
188
+ reason: 'Browser environment - popup preserves app state',
189
+ };
190
+ }
191
+ return {
192
+ method: 'redirect',
193
+ reason: 'Fallback method - works in all environments',
194
+ };
195
+ }
196
+ /**
197
+ * Initialize cross-domain auth on app startup
198
+ *
199
+ * This handles:
200
+ * 1. Redirect callback (if returning from auth.oxy.so)
201
+ * 2. Session restoration (from localStorage)
202
+ * 3. Silent sign-in (check for existing SSO session)
203
+ *
204
+ * @returns Session if user is authenticated, null otherwise
205
+ */
206
+ async initialize() {
207
+ // 1. Check if this is a redirect callback
208
+ const callbackSession = this.handleRedirectCallback();
209
+ if (callbackSession) {
210
+ return callbackSession;
211
+ }
212
+ // 2. Try to restore existing session from storage
213
+ const restored = this.restoreSession();
214
+ if (restored) {
215
+ // Verify session is still valid by fetching user
216
+ try {
217
+ const user = await this.oxyServices.getCurrentUser();
218
+ if (user) {
219
+ return {
220
+ sessionId: this.oxyServices.getStoredSessionId?.() || '',
221
+ deviceId: '',
222
+ expiresAt: new Date(Date.now() + 24 * 60 * 60 * 1000).toISOString(),
223
+ user,
224
+ };
225
+ }
226
+ }
227
+ catch (error) {
228
+ console.warn('[CrossDomainAuth] Stored session invalid:', error);
229
+ }
230
+ }
231
+ // 3. Try silent sign-in (check for SSO session at auth.oxy.so)
232
+ return await this.silentSignIn();
233
+ }
234
+ }
235
+ exports.CrossDomainAuth = CrossDomainAuth;
236
+ /**
237
+ * Helper function to create CrossDomainAuth instance
238
+ *
239
+ * @example
240
+ * ```typescript
241
+ * import { createCrossDomainAuth } from '@oxyhq/services';
242
+ *
243
+ * const oxyServices = new OxyServices({ baseURL: 'https://api.oxy.so' });
244
+ * const auth = createCrossDomainAuth(oxyServices);
245
+ *
246
+ * // On app startup
247
+ * const session = await auth.initialize();
248
+ * if (session) {
249
+ * console.log('User is signed in:', session.user);
250
+ * }
251
+ *
252
+ * // Sign in button click
253
+ * const session = await auth.signIn();
254
+ * ```
255
+ */
256
+ function createCrossDomainAuth(oxyServices) {
257
+ return new CrossDomainAuth(oxyServices);
258
+ }