@futdevpro/nts-dynamo 1.10.23 → 1.10.24

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 (71) hide show
  1. package/build/_modules/oauth2/_routes/oauth2.controller.d.ts +17 -0
  2. package/build/_modules/oauth2/_routes/oauth2.controller.d.ts.map +1 -0
  3. package/build/_modules/oauth2/_routes/oauth2.controller.js +97 -0
  4. package/build/_modules/oauth2/_routes/oauth2.controller.js.map +1 -0
  5. package/build/_modules/oauth2/_services/oauth2.auth-service.d.ts +31 -0
  6. package/build/_modules/oauth2/_services/oauth2.auth-service.d.ts.map +1 -0
  7. package/build/_modules/oauth2/_services/oauth2.auth-service.js +216 -0
  8. package/build/_modules/oauth2/_services/oauth2.auth-service.js.map +1 -0
  9. package/build/_modules/oauth2/_services/oauth2.control-service.d.ts +133 -0
  10. package/build/_modules/oauth2/_services/oauth2.control-service.d.ts.map +1 -0
  11. package/build/_modules/oauth2/_services/oauth2.control-service.js +574 -0
  12. package/build/_modules/oauth2/_services/oauth2.control-service.js.map +1 -0
  13. package/build/_modules/oauth2/index.d.ts +4 -0
  14. package/build/_modules/oauth2/index.d.ts.map +1 -0
  15. package/build/_modules/oauth2/index.js +7 -0
  16. package/build/_modules/oauth2/index.js.map +1 -0
  17. package/build/_modules/open-ai/_enums/open-ai-model.enum.d.ts +11 -0
  18. package/build/_modules/open-ai/_enums/open-ai-model.enum.d.ts.map +1 -0
  19. package/build/_modules/open-ai/_enums/open-ai-model.enum.js +110 -0
  20. package/build/_modules/open-ai/_enums/open-ai-model.enum.js.map +1 -0
  21. package/build/_modules/open-ai/_models/gpt-call-settings.interface.d.ts +54 -0
  22. package/build/_modules/open-ai/_models/gpt-call-settings.interface.d.ts.map +1 -0
  23. package/build/_modules/open-ai/_models/gpt-call-settings.interface.js +65 -0
  24. package/build/_modules/open-ai/_models/gpt-call-settings.interface.js.map +1 -0
  25. package/build/_modules/open-ai/_models/open-ai-api-env-settings.interface.d.ts +7 -0
  26. package/build/_modules/open-ai/_models/open-ai-api-env-settings.interface.d.ts.map +1 -0
  27. package/build/_modules/open-ai/_models/open-ai-api-env-settings.interface.js +3 -0
  28. package/build/_modules/open-ai/_models/open-ai-api-env-settings.interface.js.map +1 -0
  29. package/build/_modules/open-ai/_models/open-ai-settings.interface.d.ts +17 -0
  30. package/build/_modules/open-ai/_models/open-ai-settings.interface.d.ts.map +1 -0
  31. package/build/_modules/open-ai/_models/open-ai-settings.interface.js +25 -0
  32. package/build/_modules/open-ai/_models/open-ai-settings.interface.js.map +1 -0
  33. package/build/_modules/open-ai/_services/embedding.control-service.d.ts +26 -0
  34. package/build/_modules/open-ai/_services/embedding.control-service.d.ts.map +1 -0
  35. package/build/_modules/open-ai/_services/embedding.control-service.js +54 -0
  36. package/build/_modules/open-ai/_services/embedding.control-service.js.map +1 -0
  37. package/build/_modules/open-ai/_services/gpt.service-base.d.ts +98 -0
  38. package/build/_modules/open-ai/_services/gpt.service-base.d.ts.map +1 -0
  39. package/build/_modules/open-ai/_services/gpt.service-base.js +284 -0
  40. package/build/_modules/open-ai/_services/gpt.service-base.js.map +1 -0
  41. package/build/_modules/open-ai/_services/open-ai.service-base.d.ts +23 -0
  42. package/build/_modules/open-ai/_services/open-ai.service-base.d.ts.map +1 -0
  43. package/build/_modules/open-ai/_services/open-ai.service-base.js +50 -0
  44. package/build/_modules/open-ai/_services/open-ai.service-base.js.map +1 -0
  45. package/build/_modules/open-ai/index.d.ts +8 -0
  46. package/build/_modules/open-ai/index.d.ts.map +1 -0
  47. package/build/_modules/open-ai/index.js +11 -0
  48. package/build/_modules/open-ai/index.js.map +1 -0
  49. package/build/_services/base/db.service.d.ts +38 -0
  50. package/build/_services/base/db.service.d.ts.map +1 -1
  51. package/build/_services/base/db.service.js +40 -0
  52. package/build/_services/base/db.service.js.map +1 -1
  53. package/build/_services/server/app.server.d.ts +17 -0
  54. package/build/_services/server/app.server.d.ts.map +1 -1
  55. package/build/_services/server/app.server.js +17 -0
  56. package/build/_services/server/app.server.js.map +1 -1
  57. package/package.json +47 -17
  58. package/src/_modules/oauth2/_routes/oauth2.controller.ts +99 -0
  59. package/src/_modules/oauth2/_services/oauth2.auth-service.ts +232 -0
  60. package/src/_modules/oauth2/_services/oauth2.control-service.ts +651 -0
  61. package/src/_modules/oauth2/index.ts +9 -0
  62. package/src/_modules/open-ai/_enums/open-ai-model.enum.ts +137 -0
  63. package/src/_modules/open-ai/_models/gpt-call-settings.interface.ts +69 -0
  64. package/src/_modules/open-ai/_models/open-ai-api-env-settings.interface.ts +12 -0
  65. package/src/_modules/open-ai/_models/open-ai-settings.interface.ts +31 -0
  66. package/src/_modules/open-ai/_services/embedding.control-service.ts +61 -0
  67. package/src/_modules/open-ai/_services/gpt.service-base.ts +440 -0
  68. package/src/_modules/open-ai/_services/open-ai.service-base.ts +73 -0
  69. package/src/_modules/open-ai/index.ts +13 -0
  70. package/src/_services/base/db.service.ts +41 -0
  71. package/src/_services/server/app.server.ts +18 -0
@@ -0,0 +1,232 @@
1
+ import { Request, Response } from 'express';
2
+ import { DyFM_Error, DyFM_Log } from '@futdevpro/fsm-dynamo';
3
+ import { DyNTS_AuthService } from '../../../_services/core/auth.service';
4
+ import { DyNTS_global_settings } from '../../../_collections/global-settings.const';
5
+ import { DyNTS_OAuth2_ControlService } from './oauth2.control-service';
6
+
7
+ /**
8
+ * OAuth2 Authentication Service implementation
9
+ *
10
+ * This service handles OAuth2 specific authentication flows and token management
11
+ *
12
+ * @example
13
+ * const authService = DyNTS_OAuth2_AuthService.getInstance();
14
+ * await authService.authenticate_token(req, res);
15
+ */
16
+ export class DyNTS_OAuth2_AuthService extends DyNTS_AuthService {
17
+ static getInstance(): DyNTS_OAuth2_AuthService {
18
+ return DyNTS_OAuth2_AuthService.getSingletonInstance();
19
+ }
20
+
21
+ private readonly controlService: DyNTS_OAuth2_ControlService = DyNTS_OAuth2_ControlService.getInstance();
22
+
23
+ readonly authenticate_token = async (req: Request, res: Response): Promise<void> => {
24
+ try {
25
+ const token = this.getTokenFromRequest(req);
26
+
27
+ // Validate token format
28
+ if (!token?.startsWith('Bearer ')) {
29
+ throw new DyFM_Error({
30
+ status: 401,
31
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-OA2-AT1`,
32
+ addECToUserMsg: true,
33
+ message: 'Invalid token format',
34
+ userMessage: this.defaultErrorUserMsg,
35
+ issuerService: this.serviceName,
36
+ });
37
+ }
38
+
39
+ const accessToken = token.substring(7); // Remove 'Bearer ' prefix
40
+
41
+ // Validate token against stored tokens
42
+ const tokenData = this.controlService.getAccessTokenData(accessToken);
43
+ if (!tokenData || tokenData.expiresAt < Date.now()) {
44
+ throw new DyFM_Error({
45
+ status: 401,
46
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-OA2-AT2`,
47
+ addECToUserMsg: true,
48
+ message: 'Invalid or expired token',
49
+ userMessage: this.defaultErrorUserMsg,
50
+ issuerService: this.serviceName,
51
+ });
52
+ }
53
+
54
+ // Set token in response header
55
+ res.setHeader('authorization', `Bearer ${accessToken}`);
56
+ } catch (error) {
57
+ DyFM_Log.error('OAuth2 token authentication failed', error);
58
+ throw new DyFM_Error({
59
+ status: 401,
60
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-OA2-AT0`,
61
+ addECToUserMsg: true,
62
+ message: 'OAuth2 token authentication failed',
63
+ userMessage: this.defaultErrorUserMsg,
64
+ issuerService: this.serviceName,
65
+ error
66
+ });
67
+ }
68
+ };
69
+
70
+ readonly authenticate_tokenSelf = async (req: Request, res: Response): Promise<void> => {
71
+ try {
72
+ const token = this.getTokenFromRequest(req);
73
+
74
+ // Validate token format
75
+ if (!token || !token.startsWith('Bearer ')) {
76
+ throw new DyFM_Error({
77
+ status: 401,
78
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-OA2-ATS1`,
79
+ addECToUserMsg: true,
80
+ message: 'Invalid token format',
81
+ userMessage: this.defaultErrorUserMsg,
82
+ issuerService: this.serviceName,
83
+ });
84
+ }
85
+
86
+ const accessToken = token.substring(7); // Remove 'Bearer ' prefix
87
+
88
+ // Validate token against stored tokens
89
+ const tokenData = this.controlService.getAccessTokenData(accessToken);
90
+ if (!tokenData || tokenData.expiresAt < Date.now()) {
91
+ throw new DyFM_Error({
92
+ status: 401,
93
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-OA2-ATS2`,
94
+ addECToUserMsg: true,
95
+ message: 'Invalid or expired token',
96
+ userMessage: this.defaultErrorUserMsg,
97
+ issuerService: this.serviceName,
98
+ });
99
+ }
100
+
101
+ // For self-token validation, ensure the token is associated with the requesting user
102
+ const issuer = this.getIssuerFromRequest(req);
103
+ if (!issuer || issuer !== tokenData.clientId) {
104
+ throw new DyFM_Error({
105
+ status: 403,
106
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-OA2-ATS3`,
107
+ addECToUserMsg: true,
108
+ message: 'Token not associated with requesting user',
109
+ userMessage: this.defaultErrorUserMsg,
110
+ issuerService: this.serviceName,
111
+ });
112
+ }
113
+
114
+ // Set token in response header
115
+ res.setHeader('authorization', `Bearer ${accessToken}`);
116
+ } catch (error) {
117
+ DyFM_Log.error('OAuth2 self-token authentication failed', error);
118
+ throw new DyFM_Error({
119
+ status: 401,
120
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-OA2-ATS0`,
121
+ addECToUserMsg: true,
122
+ message: 'OAuth2 self-token authentication failed',
123
+ userMessage: this.defaultErrorUserMsg,
124
+ issuerService: this.serviceName,
125
+ error
126
+ });
127
+ }
128
+ };
129
+
130
+ readonly authenticate_tokenPerm_accUsageData = async (req: Request, res: Response): Promise<void> => {
131
+ try {
132
+ const token = this.getTokenFromRequest(req);
133
+
134
+ // Validate token format
135
+ if (!token || !token.startsWith('Bearer ')) {
136
+ throw new DyFM_Error({
137
+ status: 401,
138
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-OA2-ATU1`,
139
+ addECToUserMsg: true,
140
+ message: 'Invalid token format',
141
+ userMessage: this.defaultErrorUserMsg,
142
+ issuerService: this.serviceName,
143
+ });
144
+ }
145
+
146
+ const accessToken = token.substring(7); // Remove 'Bearer ' prefix
147
+
148
+ // Validate token against stored tokens
149
+ const tokenData = this.controlService.getAccessTokenData(accessToken);
150
+ if (!tokenData || tokenData.expiresAt < Date.now()) {
151
+ throw new DyFM_Error({
152
+ status: 401,
153
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-OA2-ATU2`,
154
+ addECToUserMsg: true,
155
+ message: 'Invalid or expired token',
156
+ userMessage: this.defaultErrorUserMsg,
157
+ issuerService: this.serviceName,
158
+ });
159
+ }
160
+
161
+ // Check if token has usage data permission
162
+ if (!tokenData.scope.includes('usage_data')) {
163
+ throw new DyFM_Error({
164
+ status: 403,
165
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-OA2-ATU3`,
166
+ addECToUserMsg: true,
167
+ message: 'Token does not have usage data permission',
168
+ userMessage: this.defaultErrorUserMsg,
169
+ issuerService: this.serviceName,
170
+ });
171
+ }
172
+
173
+ // Set token in response header
174
+ res.setHeader('authorization', `Bearer ${accessToken}`);
175
+ } catch (error) {
176
+ DyFM_Log.error('OAuth2 usage data permission check failed', error);
177
+ throw new DyFM_Error({
178
+ status: 401,
179
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-OA2-ATU0`,
180
+ addECToUserMsg: true,
181
+ message: 'OAuth2 usage data permission check failed',
182
+ userMessage: this.defaultErrorUserMsg,
183
+ issuerService: this.serviceName,
184
+ error
185
+ });
186
+ }
187
+ };
188
+
189
+ /**
190
+ * Gets the issuer (user ID) from the OAuth2 token in the request
191
+ * @param req Express Request object
192
+ * @returns The issuer ID from the token
193
+ */
194
+ getIssuerFromRequest(req: Request): string {
195
+ try {
196
+ const token = this.getTokenFromRequest(req);
197
+ if (!token || !token.startsWith('Bearer ')) {
198
+ return undefined;
199
+ }
200
+
201
+ const accessToken = token.substring(7); // Remove 'Bearer ' prefix
202
+ const tokenData = this.controlService.getAccessTokenData(accessToken);
203
+
204
+ return tokenData?.clientId;
205
+ } catch {
206
+ return undefined;
207
+ }
208
+ }
209
+
210
+ /**
211
+ * Gets the username from the OAuth2 token in the request
212
+ * @param req Express Request object
213
+ * @returns The username from the token
214
+ */
215
+ getUsernameFromRequest(req: Request): string {
216
+ try {
217
+ const token = this.getTokenFromRequest(req);
218
+ if (!token || !token.startsWith('Bearer ')) {
219
+ return undefined;
220
+ }
221
+
222
+ const accessToken = token.substring(7); // Remove 'Bearer ' prefix
223
+ const tokenData = this.controlService.getAccessTokenData(accessToken);
224
+
225
+ // TODO: Implement user information retrieval from database/storage
226
+ // For now, return the client ID as username
227
+ return tokenData?.clientId;
228
+ } catch {
229
+ return undefined;
230
+ }
231
+ }
232
+ }