@oxyhq/services 5.7.5 → 5.8.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.
Files changed (239) hide show
  1. package/README.md +76 -76
  2. package/lib/commonjs/core/index.js +177 -102
  3. package/lib/commonjs/core/index.js.map +1 -1
  4. package/lib/commonjs/index.js +88 -29
  5. package/lib/commonjs/index.js.map +1 -1
  6. package/lib/commonjs/node/createAuth.js +585 -7
  7. package/lib/commonjs/node/createAuth.js.map +1 -1
  8. package/lib/commonjs/node/index.js +38 -1
  9. package/lib/commonjs/node/index.js.map +1 -1
  10. package/lib/commonjs/ui/components/Avatar.js +15 -6
  11. package/lib/commonjs/ui/components/Avatar.js.map +1 -1
  12. package/lib/commonjs/ui/components/GroupedItem.js +58 -13
  13. package/lib/commonjs/ui/components/GroupedItem.js.map +1 -1
  14. package/lib/commonjs/ui/components/GroupedSection.js +7 -1
  15. package/lib/commonjs/ui/components/GroupedSection.js.map +1 -1
  16. package/lib/commonjs/ui/components/Header.js +322 -0
  17. package/lib/commonjs/ui/components/Header.js.map +1 -0
  18. package/lib/commonjs/ui/components/OxyProvider.js +23 -7
  19. package/lib/commonjs/ui/components/OxyProvider.js.map +1 -1
  20. package/lib/commonjs/ui/components/index.js +7 -0
  21. package/lib/commonjs/ui/components/index.js.map +1 -1
  22. package/lib/commonjs/ui/components/internal/GroupedPillButtons.js +1 -1
  23. package/lib/commonjs/ui/components/internal/GroupedPillButtons.js.map +1 -1
  24. package/lib/commonjs/ui/components/internal/TextField.js +606 -546
  25. package/lib/commonjs/ui/components/internal/TextField.js.map +1 -1
  26. package/lib/commonjs/ui/components/internal/TextField.md +436 -0
  27. package/lib/commonjs/ui/context/OxyContext.js +122 -78
  28. package/lib/commonjs/ui/context/OxyContext.js.map +1 -1
  29. package/lib/commonjs/ui/hooks/useSessionSocket.js +5 -2
  30. package/lib/commonjs/ui/hooks/useSessionSocket.js.map +1 -1
  31. package/lib/commonjs/ui/navigation/OxyRouter.js +1 -1
  32. package/lib/commonjs/ui/navigation/OxyRouter.js.map +1 -1
  33. package/lib/commonjs/ui/screens/AccountCenterScreen.js +6 -6
  34. package/lib/commonjs/ui/screens/AccountCenterScreen.js.map +1 -1
  35. package/lib/commonjs/ui/screens/AccountManagementDemo.js +3 -3
  36. package/lib/commonjs/ui/screens/AccountManagementDemo.js.map +1 -1
  37. package/lib/commonjs/ui/screens/AccountOverviewScreen.js +241 -598
  38. package/lib/commonjs/ui/screens/AccountOverviewScreen.js.map +1 -1
  39. package/lib/commonjs/ui/screens/AccountSettingsScreen.js +1151 -406
  40. package/lib/commonjs/ui/screens/AccountSettingsScreen.js.map +1 -1
  41. package/lib/commonjs/ui/screens/AccountSwitcherScreen.js +135 -237
  42. package/lib/commonjs/ui/screens/AccountSwitcherScreen.js.map +1 -1
  43. package/lib/commonjs/ui/screens/AppInfoScreen.js +246 -463
  44. package/lib/commonjs/ui/screens/AppInfoScreen.js.map +1 -1
  45. package/lib/commonjs/ui/screens/FeedbackScreen.js +3 -3
  46. package/lib/commonjs/ui/screens/FeedbackScreen.js.map +1 -1
  47. package/lib/commonjs/ui/screens/PaymentGatewayScreen.js +808 -650
  48. package/lib/commonjs/ui/screens/PaymentGatewayScreen.js.map +1 -1
  49. package/lib/commonjs/ui/screens/RecoverAccountScreen.js +51 -72
  50. package/lib/commonjs/ui/screens/RecoverAccountScreen.js.map +1 -1
  51. package/lib/commonjs/ui/screens/SessionManagementScreen.js +11 -29
  52. package/lib/commonjs/ui/screens/SessionManagementScreen.js.map +1 -1
  53. package/lib/commonjs/ui/screens/SignInScreen.js +30 -303
  54. package/lib/commonjs/ui/screens/SignInScreen.js.map +1 -1
  55. package/lib/commonjs/ui/screens/SignUpScreen.js +4 -4
  56. package/lib/commonjs/ui/screens/SignUpScreen.js.map +1 -1
  57. package/lib/commonjs/ui/screens/internal/SignInPasswordStep.js +19 -31
  58. package/lib/commonjs/ui/screens/internal/SignInPasswordStep.js.map +1 -1
  59. package/lib/commonjs/ui/screens/internal/SignInUsernameStep.js +7 -10
  60. package/lib/commonjs/ui/screens/internal/SignInUsernameStep.js.map +1 -1
  61. package/lib/commonjs/ui/screens/internal/SignUpIdentityStep.js +11 -5
  62. package/lib/commonjs/ui/screens/internal/SignUpIdentityStep.js.map +1 -1
  63. package/lib/commonjs/ui/screens/internal/SignUpSecurityStep.js +11 -4
  64. package/lib/commonjs/ui/screens/internal/SignUpSecurityStep.js.map +1 -1
  65. package/lib/commonjs/ui/stores/authStore.js +12 -0
  66. package/lib/commonjs/ui/stores/authStore.js.map +1 -1
  67. package/lib/commonjs/ui/styles/authStyles.js +337 -0
  68. package/lib/commonjs/ui/styles/authStyles.js.map +1 -0
  69. package/lib/commonjs/ui/styles/index.js +11 -0
  70. package/lib/commonjs/ui/styles/index.js.map +1 -1
  71. package/lib/module/core/index.js +177 -41
  72. package/lib/module/core/index.js.map +1 -1
  73. package/lib/module/index.js +26 -4
  74. package/lib/module/index.js.map +1 -1
  75. package/lib/module/node/createAuth.js +584 -7
  76. package/lib/module/node/createAuth.js.map +1 -1
  77. package/lib/module/node/index.js +7 -1
  78. package/lib/module/node/index.js.map +1 -1
  79. package/lib/module/ui/components/Avatar.js +15 -6
  80. package/lib/module/ui/components/Avatar.js.map +1 -1
  81. package/lib/module/ui/components/GroupedItem.js +59 -14
  82. package/lib/module/ui/components/GroupedItem.js.map +1 -1
  83. package/lib/module/ui/components/GroupedSection.js +7 -1
  84. package/lib/module/ui/components/GroupedSection.js.map +1 -1
  85. package/lib/module/ui/components/Header.js +317 -0
  86. package/lib/module/ui/components/Header.js.map +1 -0
  87. package/lib/module/ui/components/OxyProvider.js +25 -9
  88. package/lib/module/ui/components/OxyProvider.js.map +1 -1
  89. package/lib/module/ui/components/index.js +1 -0
  90. package/lib/module/ui/components/index.js.map +1 -1
  91. package/lib/module/ui/components/internal/GroupedPillButtons.js +1 -1
  92. package/lib/module/ui/components/internal/GroupedPillButtons.js.map +1 -1
  93. package/lib/module/ui/components/internal/TextField.js +607 -547
  94. package/lib/module/ui/components/internal/TextField.js.map +1 -1
  95. package/lib/module/ui/components/internal/TextField.md +436 -0
  96. package/lib/module/ui/context/OxyContext.js +121 -77
  97. package/lib/module/ui/context/OxyContext.js.map +1 -1
  98. package/lib/module/ui/hooks/useSessionSocket.js +5 -2
  99. package/lib/module/ui/hooks/useSessionSocket.js.map +1 -1
  100. package/lib/module/ui/navigation/OxyRouter.js +1 -1
  101. package/lib/module/ui/navigation/OxyRouter.js.map +1 -1
  102. package/lib/module/ui/screens/AccountCenterScreen.js +6 -6
  103. package/lib/module/ui/screens/AccountCenterScreen.js.map +1 -1
  104. package/lib/module/ui/screens/AccountManagementDemo.js +3 -3
  105. package/lib/module/ui/screens/AccountManagementDemo.js.map +1 -1
  106. package/lib/module/ui/screens/AccountOverviewScreen.js +242 -597
  107. package/lib/module/ui/screens/AccountOverviewScreen.js.map +1 -1
  108. package/lib/module/ui/screens/AccountSettingsScreen.js +1152 -407
  109. package/lib/module/ui/screens/AccountSettingsScreen.js.map +1 -1
  110. package/lib/module/ui/screens/AccountSwitcherScreen.js +135 -237
  111. package/lib/module/ui/screens/AccountSwitcherScreen.js.map +1 -1
  112. package/lib/module/ui/screens/AppInfoScreen.js +248 -465
  113. package/lib/module/ui/screens/AppInfoScreen.js.map +1 -1
  114. package/lib/module/ui/screens/FeedbackScreen.js +3 -3
  115. package/lib/module/ui/screens/FeedbackScreen.js.map +1 -1
  116. package/lib/module/ui/screens/PaymentGatewayScreen.js +809 -651
  117. package/lib/module/ui/screens/PaymentGatewayScreen.js.map +1 -1
  118. package/lib/module/ui/screens/RecoverAccountScreen.js +53 -74
  119. package/lib/module/ui/screens/RecoverAccountScreen.js.map +1 -1
  120. package/lib/module/ui/screens/SessionManagementScreen.js +11 -29
  121. package/lib/module/ui/screens/SessionManagementScreen.js.map +1 -1
  122. package/lib/module/ui/screens/SignInScreen.js +32 -305
  123. package/lib/module/ui/screens/SignInScreen.js.map +1 -1
  124. package/lib/module/ui/screens/SignUpScreen.js +5 -5
  125. package/lib/module/ui/screens/SignUpScreen.js.map +1 -1
  126. package/lib/module/ui/screens/internal/SignInPasswordStep.js +19 -31
  127. package/lib/module/ui/screens/internal/SignInPasswordStep.js.map +1 -1
  128. package/lib/module/ui/screens/internal/SignInUsernameStep.js +7 -10
  129. package/lib/module/ui/screens/internal/SignInUsernameStep.js.map +1 -1
  130. package/lib/module/ui/screens/internal/SignUpIdentityStep.js +11 -5
  131. package/lib/module/ui/screens/internal/SignUpIdentityStep.js.map +1 -1
  132. package/lib/module/ui/screens/internal/SignUpSecurityStep.js +11 -4
  133. package/lib/module/ui/screens/internal/SignUpSecurityStep.js.map +1 -1
  134. package/lib/module/ui/stores/authStore.js +12 -0
  135. package/lib/module/ui/stores/authStore.js.map +1 -1
  136. package/lib/module/ui/styles/authStyles.js +332 -0
  137. package/lib/module/ui/styles/authStyles.js.map +1 -0
  138. package/lib/module/ui/styles/index.js +1 -0
  139. package/lib/module/ui/styles/index.js.map +1 -1
  140. package/lib/typescript/core/index.d.ts +68 -24
  141. package/lib/typescript/core/index.d.ts.map +1 -1
  142. package/lib/typescript/index.d.ts +13 -3
  143. package/lib/typescript/index.d.ts.map +1 -1
  144. package/lib/typescript/node/createAuth.d.ts +112 -0
  145. package/lib/typescript/node/createAuth.d.ts.map +1 -1
  146. package/lib/typescript/node/index.d.ts +2 -0
  147. package/lib/typescript/node/index.d.ts.map +1 -1
  148. package/lib/typescript/ui/components/Avatar.d.ts.map +1 -1
  149. package/lib/typescript/ui/components/GroupedItem.d.ts +6 -0
  150. package/lib/typescript/ui/components/GroupedItem.d.ts.map +1 -1
  151. package/lib/typescript/ui/components/GroupedSection.d.ts +6 -0
  152. package/lib/typescript/ui/components/GroupedSection.d.ts.map +1 -1
  153. package/lib/typescript/ui/components/Header.d.ts +22 -0
  154. package/lib/typescript/ui/components/Header.d.ts.map +1 -0
  155. package/lib/typescript/ui/components/OxyProvider.d.ts.map +1 -1
  156. package/lib/typescript/ui/components/index.d.ts +1 -0
  157. package/lib/typescript/ui/components/index.d.ts.map +1 -1
  158. package/lib/typescript/ui/components/internal/TextField.d.ts +31 -16
  159. package/lib/typescript/ui/components/internal/TextField.d.ts.map +1 -1
  160. package/lib/typescript/ui/context/OxyContext.d.ts +5 -2
  161. package/lib/typescript/ui/context/OxyContext.d.ts.map +1 -1
  162. package/lib/typescript/ui/hooks/useSessionSocket.d.ts.map +1 -1
  163. package/lib/typescript/ui/navigation/types.d.ts +9 -2
  164. package/lib/typescript/ui/navigation/types.d.ts.map +1 -1
  165. package/lib/typescript/ui/screens/AccountOverviewScreen.d.ts.map +1 -1
  166. package/lib/typescript/ui/screens/AccountSettingsScreen.d.ts.map +1 -1
  167. package/lib/typescript/ui/screens/AccountSwitcherScreen.d.ts.map +1 -1
  168. package/lib/typescript/ui/screens/AppInfoScreen.d.ts.map +1 -1
  169. package/lib/typescript/ui/screens/PaymentGatewayScreen.d.ts.map +1 -1
  170. package/lib/typescript/ui/screens/RecoverAccountScreen.d.ts +5 -1
  171. package/lib/typescript/ui/screens/RecoverAccountScreen.d.ts.map +1 -1
  172. package/lib/typescript/ui/screens/SessionManagementScreen.d.ts.map +1 -1
  173. package/lib/typescript/ui/screens/SignInScreen.d.ts.map +1 -1
  174. package/lib/typescript/ui/screens/internal/SignInPasswordStep.d.ts +1 -1
  175. package/lib/typescript/ui/screens/internal/SignInPasswordStep.d.ts.map +1 -1
  176. package/lib/typescript/ui/screens/internal/SignInUsernameStep.d.ts +0 -1
  177. package/lib/typescript/ui/screens/internal/SignInUsernameStep.d.ts.map +1 -1
  178. package/lib/typescript/ui/screens/internal/SignUpIdentityStep.d.ts.map +1 -1
  179. package/lib/typescript/ui/screens/internal/SignUpSecurityStep.d.ts.map +1 -1
  180. package/lib/typescript/ui/stores/authStore.d.ts.map +1 -1
  181. package/lib/typescript/ui/styles/authStyles.d.ts +326 -0
  182. package/lib/typescript/ui/styles/authStyles.d.ts.map +1 -0
  183. package/lib/typescript/ui/styles/index.d.ts +1 -0
  184. package/lib/typescript/ui/styles/index.d.ts.map +1 -1
  185. package/package.json +1 -4
  186. package/src/core/index.ts +195 -41
  187. package/src/index.ts +72 -4
  188. package/src/node/createAuth.ts +623 -7
  189. package/src/node/index.ts +19 -1
  190. package/src/ui/components/Avatar.tsx +11 -5
  191. package/src/ui/components/GroupedItem.tsx +57 -9
  192. package/src/ui/components/GroupedSection.tsx +12 -0
  193. package/src/ui/components/Header.tsx +364 -0
  194. package/src/ui/components/OxyProvider.tsx +31 -15
  195. package/src/ui/components/index.ts +1 -0
  196. package/src/ui/components/internal/GroupedPillButtons.tsx +1 -1
  197. package/src/ui/components/internal/TextField.md +436 -0
  198. package/src/ui/components/internal/TextField.tsx +720 -620
  199. package/src/ui/context/OxyContext.tsx +150 -63
  200. package/src/ui/hooks/useSessionSocket.ts +5 -2
  201. package/src/ui/navigation/OxyRouter.tsx +1 -1
  202. package/src/ui/navigation/types.ts +10 -2
  203. package/src/ui/screens/AccountCenterScreen.tsx +5 -5
  204. package/src/ui/screens/AccountManagementDemo.tsx +9 -9
  205. package/src/ui/screens/AccountOverviewScreen.tsx +265 -414
  206. package/src/ui/screens/AccountSettingsScreen.tsx +1165 -403
  207. package/src/ui/screens/AccountSwitcherScreen.tsx +158 -202
  208. package/src/ui/screens/AppInfoScreen.tsx +270 -497
  209. package/src/ui/screens/FeedbackScreen.tsx +3 -3
  210. package/src/ui/screens/PaymentGatewayScreen.tsx +668 -365
  211. package/src/ui/screens/ProfileScreen.tsx +5 -5
  212. package/src/ui/screens/RecoverAccountScreen.tsx +46 -74
  213. package/src/ui/screens/SessionManagementScreen.tsx +14 -22
  214. package/src/ui/screens/SignInScreen.tsx +27 -294
  215. package/src/ui/screens/SignUpScreen.tsx +5 -5
  216. package/src/ui/screens/internal/SignInPasswordStep.tsx +11 -22
  217. package/src/ui/screens/internal/SignInUsernameStep.tsx +3 -10
  218. package/src/ui/screens/internal/SignUpIdentityStep.tsx +2 -5
  219. package/src/ui/screens/internal/SignUpSecurityStep.tsx +3 -4
  220. package/src/ui/stores/authStore.ts +12 -0
  221. package/src/ui/styles/authStyles.ts +352 -0
  222. package/src/ui/styles/index.ts +1 -0
  223. package/lib/commonjs/core/auth-manager.js +0 -440
  224. package/lib/commonjs/core/auth-manager.js.map +0 -1
  225. package/lib/commonjs/core/use-auth.js +0 -244
  226. package/lib/commonjs/core/use-auth.js.map +0 -1
  227. package/lib/module/core/auth-manager.js +0 -432
  228. package/lib/module/core/auth-manager.js.map +0 -1
  229. package/lib/module/core/use-auth.js +0 -235
  230. package/lib/module/core/use-auth.js.map +0 -1
  231. package/lib/typescript/core/auth-manager.d.ts +0 -136
  232. package/lib/typescript/core/auth-manager.d.ts.map +0 -1
  233. package/lib/typescript/core/use-auth.d.ts +0 -79
  234. package/lib/typescript/core/use-auth.d.ts.map +0 -1
  235. package/src/__tests__/middleware.test.ts +0 -105
  236. package/src/__tests__/setup.ts +0 -10
  237. package/src/__tests__/zero-config-auth.test.ts +0 -607
  238. package/src/core/auth-manager.ts +0 -500
  239. package/src/core/use-auth.tsx +0 -245
@@ -3,14 +3,428 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ exports.OxyAuth = void 0;
6
7
  exports.createAuth = createAuth;
7
8
  var _express = _interopRequireDefault(require("express"));
8
9
  var _core = require("../core");
10
+ var _jwtDecode = require("jwt-decode");
9
11
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
12
+ // Types for enhanced authentication
13
+
14
+ // User cache for performance
15
+ class UserCache {
16
+ cache = new Map();
17
+ constructor(ttl = 300) {
18
+ // 5 minutes default
19
+ this.ttl = ttl * 1000;
20
+ }
21
+ set(userId, user) {
22
+ this.cache.set(userId, {
23
+ user,
24
+ expiresAt: Date.now() + this.ttl
25
+ });
26
+ }
27
+ get(userId) {
28
+ const item = this.cache.get(userId);
29
+ if (!item || Date.now() > item.expiresAt) {
30
+ this.cache.delete(userId);
31
+ return null;
32
+ }
33
+ return item.user;
34
+ }
35
+ clear() {
36
+ this.cache.clear();
37
+ }
38
+ }
39
+
40
+ /**
41
+ * Enhanced OxyAuth class for backend authentication
42
+ */
43
+ class OxyAuth {
44
+ userCache = null;
45
+ constructor(options) {
46
+ this.options = {
47
+ loadFullUser: true,
48
+ enableSessionAuth: true,
49
+ enableDeviceAuth: true,
50
+ cacheUserData: true,
51
+ userCacheTTL: 300,
52
+ ...options
53
+ };
54
+ this.oxy = new _core.OxyServices({
55
+ baseURL: options.baseURL
56
+ });
57
+ if (this.options.cacheUserData) {
58
+ this.userCache = new UserCache(this.options.userCacheTTL);
59
+ }
60
+ }
61
+
62
+ /**
63
+ * Create authentication middleware
64
+ */
65
+ createAuthMiddleware(options = {}) {
66
+ return async (req, res, next) => {
67
+ try {
68
+ const result = await this.authenticateRequest(req);
69
+ if (!result.valid && options.required !== false) {
70
+ const error = {
71
+ message: 'Authentication required',
72
+ code: 'AUTH_REQUIRED'
73
+ };
74
+ if (options.onError) {
75
+ options.onError(error, req, res);
76
+ } else {
77
+ res.status(401).json(error);
78
+ }
79
+ return;
80
+ }
81
+
82
+ // Check roles if specified
83
+ if (result.valid && options.roles && result.user) {
84
+ const hasRole = options.roles.some(role => result.user.roles?.includes(role) || result.user.role === role);
85
+ if (!hasRole) {
86
+ const error = {
87
+ message: 'Insufficient permissions',
88
+ code: 'INSUFFICIENT_ROLES'
89
+ };
90
+ if (options.onError) {
91
+ options.onError(error, req, res);
92
+ } else {
93
+ res.status(403).json(error);
94
+ }
95
+ return;
96
+ }
97
+ }
98
+
99
+ // Check permissions if specified
100
+ if (result.valid && options.permissions && result.userId) {
101
+ for (const permission of options.permissions) {
102
+ const hasPermission = await this.hasPermission(result.userId, permission);
103
+ if (!hasPermission) {
104
+ const error = {
105
+ message: 'Insufficient permissions',
106
+ code: 'INSUFFICIENT_PERMISSIONS'
107
+ };
108
+ if (options.onError) {
109
+ options.onError(error, req, res);
110
+ } else {
111
+ res.status(403).json(error);
112
+ }
113
+ return;
114
+ }
115
+ }
116
+ }
117
+ next();
118
+ } catch (error) {
119
+ if (options.onError) {
120
+ options.onError(error, req, res);
121
+ } else {
122
+ res.status(500).json({
123
+ message: 'Authentication error'
124
+ });
125
+ }
126
+ }
127
+ };
128
+ }
129
+
130
+ /**
131
+ * Authenticate request and populate user data
132
+ */
133
+ async authenticateRequest(req) {
134
+ // Try JWT token first
135
+ const authHeader = req.headers.authorization;
136
+ if (authHeader && authHeader.startsWith('Bearer ')) {
137
+ const token = authHeader.substring(7);
138
+ const result = await this.validateToken(token);
139
+ if (result.valid) {
140
+ req.user = result.user;
141
+ req.userId = result.userId;
142
+ req.accessToken = token;
143
+ return {
144
+ ...result,
145
+ accessToken: token
146
+ };
147
+ }
148
+ }
149
+
150
+ // Try session-based auth
151
+ if (this.options.enableSessionAuth) {
152
+ const sessionId = req.headers['x-session-id'];
153
+ if (sessionId) {
154
+ const result = await this.validateSession(sessionId);
155
+ if (result.valid) {
156
+ req.user = result.user;
157
+ req.userId = result.userId;
158
+ req.sessionId = sessionId;
159
+ return result;
160
+ }
161
+ }
162
+ }
163
+
164
+ // Try device-based auth
165
+ if (this.options.enableDeviceAuth) {
166
+ const deviceFingerprint = req.headers['x-device-fingerprint'];
167
+ const userId = req.headers['x-user-id'];
168
+ if (deviceFingerprint && userId) {
169
+ const result = await this.validateDevice(userId, deviceFingerprint);
170
+ if (result.valid) {
171
+ req.user = result.user;
172
+ req.userId = result.userId;
173
+ req.deviceFingerprint = deviceFingerprint;
174
+ return result;
175
+ }
176
+ }
177
+ }
178
+ return {
179
+ valid: false,
180
+ error: 'No valid authentication found'
181
+ };
182
+ }
183
+
184
+ /**
185
+ * Validate JWT token
186
+ */
187
+ async validateToken(token) {
188
+ try {
189
+ // Local JWT validation if secret is provided
190
+ if (this.options.jwtSecret) {
191
+ const decoded = (0, _jwtDecode.jwtDecode)(token);
192
+ const currentTime = Math.floor(Date.now() / 1000);
193
+ if (decoded.exp && decoded.exp < currentTime) {
194
+ return {
195
+ valid: false,
196
+ error: 'Token expired',
197
+ code: 'TOKEN_EXPIRED',
198
+ expiresAt: decoded.exp
199
+ };
200
+ }
201
+ const userId = decoded.userId || decoded.id;
202
+ if (!userId) {
203
+ return {
204
+ valid: false,
205
+ error: 'Invalid token payload',
206
+ code: 'INVALID_PAYLOAD'
207
+ };
208
+ }
209
+
210
+ // Get user data from cache or API
211
+ let user = this.userCache?.get(userId);
212
+ const cached = !!user;
213
+ if (!user && this.options.loadFullUser) {
214
+ try {
215
+ user = await this.oxy.getUserById(userId);
216
+ this.userCache?.set(userId, user);
217
+ } catch (error) {
218
+ user = {
219
+ id: userId
220
+ };
221
+ }
222
+ } else if (!user) {
223
+ user = {
224
+ id: userId
225
+ };
226
+ }
227
+ return {
228
+ valid: true,
229
+ userId,
230
+ user,
231
+ expiresAt: decoded.exp,
232
+ cached
233
+ };
234
+ }
235
+
236
+ // Remote validation using OxyServices
237
+ const tempOxy = new _core.OxyServices({
238
+ baseURL: this.oxy.getBaseURL()
239
+ });
240
+ tempOxy.setTokens(token, '');
241
+ const isValid = await tempOxy.validate();
242
+ if (!isValid) {
243
+ return {
244
+ valid: false,
245
+ error: 'Invalid token',
246
+ code: 'INVALID_TOKEN'
247
+ };
248
+ }
249
+ const userId = tempOxy.getCurrentUserId();
250
+ if (!userId) {
251
+ return {
252
+ valid: false,
253
+ error: 'Invalid token payload',
254
+ code: 'INVALID_PAYLOAD'
255
+ };
256
+ }
257
+
258
+ // Get user data
259
+ let user = this.userCache?.get(userId);
260
+ const cached = !!user;
261
+ if (!user && this.options.loadFullUser) {
262
+ try {
263
+ user = await tempOxy.getUserById(userId);
264
+ this.userCache?.set(userId, user);
265
+ } catch (error) {
266
+ user = {
267
+ id: userId
268
+ };
269
+ }
270
+ } else if (!user) {
271
+ user = {
272
+ id: userId
273
+ };
274
+ }
275
+ return {
276
+ valid: true,
277
+ userId,
278
+ user,
279
+ cached
280
+ };
281
+ } catch (error) {
282
+ return {
283
+ valid: false,
284
+ error: 'Token validation failed',
285
+ code: 'VALIDATION_ERROR'
286
+ };
287
+ }
288
+ }
289
+
290
+ /**
291
+ * Validate session-based authentication
292
+ */
293
+ async validateSession(sessionId, deviceFingerprint) {
294
+ try {
295
+ // This would integrate with your session management system
296
+ // For now, it's a placeholder implementation
297
+ return {
298
+ valid: false,
299
+ error: 'Session validation not implemented',
300
+ code: 'NOT_IMPLEMENTED'
301
+ };
302
+ } catch (error) {
303
+ return {
304
+ valid: false,
305
+ error: 'Session validation failed',
306
+ code: 'VALIDATION_ERROR'
307
+ };
308
+ }
309
+ }
310
+
311
+ /**
312
+ * Validate device-based authentication
313
+ */
314
+ async validateDevice(userId, deviceFingerprint) {
315
+ try {
316
+ // This would validate device fingerprint against stored data
317
+ // For now, it's a placeholder implementation
318
+ return {
319
+ valid: false,
320
+ error: 'Device validation not implemented',
321
+ code: 'NOT_IMPLEMENTED'
322
+ };
323
+ } catch (error) {
324
+ return {
325
+ valid: false,
326
+ error: 'Device validation failed',
327
+ code: 'VALIDATION_ERROR'
328
+ };
329
+ }
330
+ }
331
+
332
+ /**
333
+ * Create role-based middleware
334
+ */
335
+ requireRole(roles) {
336
+ const roleArray = Array.isArray(roles) ? roles : [roles];
337
+ return this.createAuthMiddleware({
338
+ required: true,
339
+ roles: roleArray
340
+ });
341
+ }
342
+
343
+ /**
344
+ * Create permission-based middleware
345
+ */
346
+ requirePermission(permissions) {
347
+ const permissionArray = Array.isArray(permissions) ? permissions : [permissions];
348
+ return this.createAuthMiddleware({
349
+ required: true,
350
+ permissions: permissionArray
351
+ });
352
+ }
353
+
354
+ /**
355
+ * Create optional authentication middleware
356
+ */
357
+ optionalAuth() {
358
+ return this.createAuthMiddleware({
359
+ required: false,
360
+ onError: () => {} // No error thrown for optional auth
361
+ });
362
+ }
363
+
364
+ /**
365
+ * Clear user cache
366
+ */
367
+ clearCache() {
368
+ this.userCache?.clear();
369
+ }
370
+
371
+ /**
372
+ * Check if user data is cached for a given token
373
+ */
374
+ isUserCached(token) {
375
+ try {
376
+ const decoded = (0, _jwtDecode.jwtDecode)(token);
377
+ const userId = decoded.userId || decoded.id;
378
+ return userId ? this.userCache?.get(userId) !== null : false;
379
+ } catch {
380
+ return false;
381
+ }
382
+ }
383
+
384
+ /**
385
+ * Check if user has a specific permission
386
+ */
387
+ async hasPermission(userId, permission) {
388
+ try {
389
+ // This is a placeholder implementation
390
+ // In a real implementation, you would check against user roles/permissions
391
+ const user = this.userCache?.get(userId) || (await this.oxy.getUserById(userId));
392
+ return user?.permissions?.includes(permission) || user?.role === 'admin' || false;
393
+ } catch {
394
+ return false;
395
+ }
396
+ }
397
+
398
+ /**
399
+ * Get OxyServices instance
400
+ */
401
+ getOxyServices() {
402
+ return this.oxy;
403
+ }
404
+ }
405
+ exports.OxyAuth = OxyAuth;
406
+ /**
407
+ * Enhanced createAuth function that provides both router and middleware capabilities
408
+ *
409
+ * This is a unified authentication system that:
410
+ * 1. Maintains backward compatibility with the old router-based approach
411
+ * 2. Adds powerful new middleware capabilities
412
+ * 3. Includes caching, role-based access, and performance optimizations
413
+ * 4. Supports multiple authentication strategies
414
+ */
10
415
  function createAuth(options) {
11
- const oxy = new _core.OxyServices({
12
- baseURL: options.baseURL
13
- });
416
+ // Create the enhanced OxyAuth instance
417
+ const authOptions = {
418
+ baseURL: options.baseURL,
419
+ jwtSecret: options.jwtSecret,
420
+ loadFullUser: options.loadFullUser ?? true,
421
+ enableSessionAuth: options.enableSessionAuth ?? true,
422
+ enableDeviceAuth: options.enableDeviceAuth ?? true,
423
+ cacheUserData: options.cacheUserData ?? true,
424
+ userCacheTTL: options.userCacheTTL ?? 300
425
+ };
426
+ const oxyAuth = new OxyAuth(authOptions);
427
+ const oxy = oxyAuth.getOxyServices();
14
428
  const router = _express.default.Router();
15
429
 
16
430
  // Helper to handle async route functions
@@ -23,73 +437,237 @@ function createAuth(options) {
23
437
  });
24
438
  }
25
439
  };
440
+
441
+ // Enhanced signup with validation
26
442
  router.post('/signup', wrap(async (req, res) => {
27
443
  const {
28
444
  username,
29
445
  email,
30
446
  password
31
447
  } = req.body;
448
+
449
+ // Enhanced validation
450
+ if (!username || !email || !password) {
451
+ return res.status(400).json({
452
+ message: 'Username, email, and password are required'
453
+ });
454
+ }
32
455
  const result = await oxy.signUp(username, email, password);
33
456
  res.json(result);
34
457
  }));
458
+
459
+ // Enhanced login with device fingerprinting
35
460
  router.post('/login', wrap(async (req, res) => {
36
461
  const {
37
462
  username,
38
- password
463
+ password,
464
+ deviceFingerprint
39
465
  } = req.body;
466
+ if (!username || !password) {
467
+ return res.status(400).json({
468
+ message: 'Username and password are required'
469
+ });
470
+ }
40
471
  const result = await oxy.login(username, password);
472
+
473
+ // Store device fingerprint if provided
474
+ if (deviceFingerprint && result.user?.id) {
475
+ // This could be stored in a database for device tracking
476
+ console.log(`Device login: ${deviceFingerprint} for user ${result.user.id}`);
477
+ }
41
478
  res.json(result);
42
479
  }));
480
+
481
+ // Enhanced logout with session management
43
482
  router.post('/logout', wrap(async (req, res) => {
44
483
  const token = req.headers.authorization?.split(' ')[1];
45
484
  const refreshToken = req.body.refreshToken;
485
+ const sessionId = req.body.sessionId;
46
486
  if (token) oxy.setTokens(token, refreshToken);
47
- await oxy.logout();
487
+
488
+ // Enhanced logout with session tracking
489
+ if (sessionId) {
490
+ await oxy.logoutSession(sessionId);
491
+ } else {
492
+ await oxy.logout();
493
+ }
48
494
  res.json({
49
495
  success: true
50
496
  });
51
497
  }));
498
+
499
+ // Enhanced token refresh
52
500
  router.post('/refresh', wrap(async (req, res) => {
53
501
  const refreshToken = req.body.refreshToken;
54
502
  const accessToken = req.headers.authorization?.split(' ')[1] || '';
503
+ if (!refreshToken) {
504
+ return res.status(400).json({
505
+ message: 'Refresh token is required'
506
+ });
507
+ }
55
508
  oxy.setTokens(accessToken, refreshToken);
56
509
  const tokens = await oxy.refreshTokens();
57
510
  res.json(tokens);
58
511
  }));
512
+
513
+ // Enhanced token validation with caching
59
514
  router.get('/validate', wrap(async (req, res) => {
60
515
  const token = req.headers.authorization?.split(' ')[1] || '';
516
+ if (!token) {
517
+ return res.status(401).json({
518
+ valid: false,
519
+ message: 'No token provided'
520
+ });
521
+ }
61
522
  oxy.setTokens(token, '');
62
523
  const valid = await oxy.validate();
524
+
525
+ // Enhanced response with more details
63
526
  res.json({
64
- valid
527
+ valid,
528
+ timestamp: new Date().toISOString(),
529
+ cached: oxyAuth.isUserCached(token) // Check if user data is cached
65
530
  });
66
531
  }));
532
+
533
+ // Enhanced sessions management
67
534
  router.get('/sessions', wrap(async (req, res) => {
68
535
  const token = req.headers.authorization?.split(' ')[1] || '';
536
+ if (!token) {
537
+ return res.status(401).json({
538
+ message: 'Authentication required'
539
+ });
540
+ }
69
541
  oxy.setTokens(token, '');
70
542
  const sessions = await oxy.getUserSessions();
71
543
  res.json(sessions);
72
544
  }));
545
+
546
+ // Enhanced session deletion
73
547
  router.delete('/sessions/:id', wrap(async (req, res) => {
74
548
  const token = req.headers.authorization?.split(' ')[1] || '';
549
+ if (!token) {
550
+ return res.status(401).json({
551
+ message: 'Authentication required'
552
+ });
553
+ }
75
554
  oxy.setTokens(token, '');
76
555
  const result = await oxy.logoutSession(req.params.id);
556
+
557
+ // Clear cache for this user if logout was successful
558
+ if (result.success) {
559
+ oxyAuth.clearCache();
560
+ }
77
561
  res.json(result);
78
562
  }));
563
+
564
+ // Enhanced logout other sessions
79
565
  router.post('/sessions/logout-others', wrap(async (req, res) => {
80
566
  const token = req.headers.authorization?.split(' ')[1] || '';
567
+ if (!token) {
568
+ return res.status(401).json({
569
+ message: 'Authentication required'
570
+ });
571
+ }
81
572
  oxy.setTokens(token, '');
82
573
  const result = await oxy.logoutOtherSessions();
574
+
575
+ // Clear cache for this user
576
+ if (result.success) {
577
+ oxyAuth.clearCache();
578
+ }
83
579
  res.json(result);
84
580
  }));
581
+
582
+ // Enhanced logout all sessions
85
583
  router.post('/sessions/logout-all', wrap(async (req, res) => {
86
584
  const token = req.headers.authorization?.split(' ')[1] || '';
585
+ if (!token) {
586
+ return res.status(401).json({
587
+ message: 'Authentication required'
588
+ });
589
+ }
87
590
  oxy.setTokens(token, '');
88
591
  const result = await oxy.logoutAllSessions();
592
+
593
+ // Clear all cache
594
+ if (result.success) {
595
+ oxyAuth.clearCache();
596
+ }
89
597
  res.json(result);
90
598
  }));
599
+
600
+ // NEW: Get current user profile with caching
601
+ router.get('/profile', wrap(async (req, res) => {
602
+ const token = req.headers.authorization?.split(' ')[1] || '';
603
+ if (!token) {
604
+ return res.status(401).json({
605
+ message: 'Authentication required'
606
+ });
607
+ }
608
+
609
+ // Use the enhanced auth system for better performance
610
+ const validation = await oxyAuth.validateToken(token);
611
+ if (!validation.valid) {
612
+ return res.status(401).json({
613
+ message: 'Invalid token'
614
+ });
615
+ }
616
+ res.json({
617
+ user: validation.user,
618
+ cached: validation.cached,
619
+ expiresAt: validation.expiresAt
620
+ });
621
+ }));
622
+
623
+ // NEW: Check user permissions
624
+ router.post('/check-permissions', wrap(async (req, res) => {
625
+ const token = req.headers.authorization?.split(' ')[1] || '';
626
+ const {
627
+ permissions
628
+ } = req.body;
629
+ if (!token) {
630
+ return res.status(401).json({
631
+ message: 'Authentication required'
632
+ });
633
+ }
634
+ if (!permissions || !Array.isArray(permissions)) {
635
+ return res.status(400).json({
636
+ message: 'Permissions array is required'
637
+ });
638
+ }
639
+ const validation = await oxyAuth.validateToken(token);
640
+ if (!validation.valid) {
641
+ return res.status(401).json({
642
+ message: 'Invalid token'
643
+ });
644
+ }
645
+
646
+ // Check each permission
647
+ const results = await Promise.all(permissions.map(async permission => {
648
+ const hasPermission = await oxyAuth.hasPermission(validation.userId, permission);
649
+ return {
650
+ permission,
651
+ granted: hasPermission
652
+ };
653
+ }));
654
+ res.json({
655
+ permissions: results
656
+ });
657
+ }));
91
658
  return {
92
- middleware: router
659
+ middleware: router,
660
+ // NEW: Expose the enhanced auth system
661
+ auth: oxyAuth,
662
+ // NEW: Convenience methods for middleware
663
+ requireAuth: (roles, permissions) => oxyAuth.createAuthMiddleware({
664
+ required: true,
665
+ roles: Array.isArray(roles) ? roles : roles ? [roles] : undefined,
666
+ permissions: Array.isArray(permissions) ? permissions : permissions ? [permissions] : undefined
667
+ }),
668
+ optionalAuth: () => oxyAuth.optionalAuth(),
669
+ requireRole: roles => oxyAuth.requireRole(roles),
670
+ requirePermission: permissions => oxyAuth.requirePermission(permissions)
93
671
  };
94
672
  }
95
673
  //# sourceMappingURL=createAuth.js.map