@oxyhq/services 5.9.3 → 5.9.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.
- package/lib/commonjs/core/OxyServices.js +163 -0
- package/lib/commonjs/core/OxyServices.js.map +1 -1
- package/lib/module/core/OxyServices.js +163 -0
- package/lib/module/core/OxyServices.js.map +1 -1
- package/lib/typescript/core/OxyServices.d.ts +35 -0
- package/lib/typescript/core/OxyServices.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/core/OxyServices.ts +184 -1
|
@@ -154,6 +154,169 @@ class OxyServices {
|
|
|
154
154
|
throw this.handleError(error);
|
|
155
155
|
}
|
|
156
156
|
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Simple Express.js authentication middleware
|
|
160
|
+
*
|
|
161
|
+
* Built-in authentication middleware that validates JWT tokens and adds user data to requests.
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* ```typescript
|
|
165
|
+
* // Basic usage - just add it to your routes
|
|
166
|
+
* app.use('/api/protected', oxyServices.auth());
|
|
167
|
+
*
|
|
168
|
+
* // With debug logging
|
|
169
|
+
* app.use('/api/protected', oxyServices.auth({ debug: true }));
|
|
170
|
+
*
|
|
171
|
+
* // With custom error handling
|
|
172
|
+
* app.use('/api/protected', oxyServices.auth({
|
|
173
|
+
* onError: (error) => console.error('Auth failed:', error)
|
|
174
|
+
* }));
|
|
175
|
+
*
|
|
176
|
+
* // Load full user data
|
|
177
|
+
* app.use('/api/protected', oxyServices.auth({ loadUser: true }));
|
|
178
|
+
* ```
|
|
179
|
+
*
|
|
180
|
+
* @param options Optional configuration
|
|
181
|
+
* @param options.debug Enable debug logging (default: false)
|
|
182
|
+
* @param options.onError Custom error handler
|
|
183
|
+
* @param options.loadUser Load full user data (default: false for performance)
|
|
184
|
+
* @param options.session Use session-based validation (default: false)
|
|
185
|
+
* @returns Express middleware function
|
|
186
|
+
*/
|
|
187
|
+
auth(options = {}) {
|
|
188
|
+
const {
|
|
189
|
+
debug = false,
|
|
190
|
+
onError,
|
|
191
|
+
loadUser = false,
|
|
192
|
+
session = false
|
|
193
|
+
} = options;
|
|
194
|
+
return async (req, res, next) => {
|
|
195
|
+
try {
|
|
196
|
+
// Extract token from Authorization header
|
|
197
|
+
const authHeader = req.headers['authorization'];
|
|
198
|
+
const token = authHeader?.startsWith('Bearer ') ? authHeader.substring(7) : null;
|
|
199
|
+
if (debug) {
|
|
200
|
+
console.log(`🔐 Auth: Processing ${req.method} ${req.path}`);
|
|
201
|
+
console.log(`🔐 Auth: Token present: ${!!token}`);
|
|
202
|
+
}
|
|
203
|
+
if (!token) {
|
|
204
|
+
const error = {
|
|
205
|
+
message: 'Access token required',
|
|
206
|
+
code: 'MISSING_TOKEN',
|
|
207
|
+
status: 401
|
|
208
|
+
};
|
|
209
|
+
if (debug) console.log(`❌ Auth: Missing token`);
|
|
210
|
+
if (onError) return onError(error);
|
|
211
|
+
return res.status(401).json(error);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// Decode and validate token
|
|
215
|
+
let decoded;
|
|
216
|
+
try {
|
|
217
|
+
decoded = (0, _jwtDecode.jwtDecode)(token);
|
|
218
|
+
if (debug) {
|
|
219
|
+
console.log(`🔐 Auth: Token decoded, User ID: ${decoded.userId || decoded.id}`);
|
|
220
|
+
}
|
|
221
|
+
} catch (decodeError) {
|
|
222
|
+
const error = {
|
|
223
|
+
message: 'Invalid token format',
|
|
224
|
+
code: 'INVALID_TOKEN_FORMAT',
|
|
225
|
+
status: 403
|
|
226
|
+
};
|
|
227
|
+
if (debug) console.log(`❌ Auth: Token decode failed`);
|
|
228
|
+
if (onError) return onError(error);
|
|
229
|
+
return res.status(403).json(error);
|
|
230
|
+
}
|
|
231
|
+
const userId = decoded.userId || decoded.id;
|
|
232
|
+
if (!userId) {
|
|
233
|
+
const error = {
|
|
234
|
+
message: 'Token missing user ID',
|
|
235
|
+
code: 'INVALID_TOKEN_PAYLOAD',
|
|
236
|
+
status: 403
|
|
237
|
+
};
|
|
238
|
+
if (debug) console.log(`❌ Auth: Token missing user ID`);
|
|
239
|
+
if (onError) return onError(error);
|
|
240
|
+
return res.status(403).json(error);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// Check token expiration
|
|
244
|
+
if (decoded.exp && decoded.exp < Math.floor(Date.now() / 1000)) {
|
|
245
|
+
const error = {
|
|
246
|
+
message: 'Token expired',
|
|
247
|
+
code: 'TOKEN_EXPIRED',
|
|
248
|
+
status: 403
|
|
249
|
+
};
|
|
250
|
+
if (debug) console.log(`❌ Auth: Token expired`);
|
|
251
|
+
if (onError) return onError(error);
|
|
252
|
+
return res.status(403).json(error);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// Session-based validation if requested
|
|
256
|
+
if (session && decoded.sessionId) {
|
|
257
|
+
try {
|
|
258
|
+
if (debug) console.log(`🔐 Auth: Validating session ${decoded.sessionId}`);
|
|
259
|
+
const sessionRes = await this.client.get(`/session/validate/${decoded.sessionId}`);
|
|
260
|
+
const sessionData = sessionRes.data;
|
|
261
|
+
if (!sessionData.valid) {
|
|
262
|
+
const error = {
|
|
263
|
+
message: 'Invalid session',
|
|
264
|
+
code: 'INVALID_SESSION',
|
|
265
|
+
status: 403
|
|
266
|
+
};
|
|
267
|
+
if (debug) console.log(`❌ Auth: Session validation failed`);
|
|
268
|
+
if (onError) return onError(error);
|
|
269
|
+
return res.status(403).json(error);
|
|
270
|
+
}
|
|
271
|
+
if (debug) console.log(`✅ Auth: Session validation successful`);
|
|
272
|
+
} catch (sessionError) {
|
|
273
|
+
const error = {
|
|
274
|
+
message: 'Session validation failed',
|
|
275
|
+
code: 'SESSION_VALIDATION_ERROR',
|
|
276
|
+
status: 403
|
|
277
|
+
};
|
|
278
|
+
if (debug) console.log(`❌ Auth: Session validation error`);
|
|
279
|
+
if (onError) return onError(error);
|
|
280
|
+
return res.status(403).json(error);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// Set request properties
|
|
285
|
+
req.userId = userId;
|
|
286
|
+
req.accessToken = token;
|
|
287
|
+
|
|
288
|
+
// Load user data if requested
|
|
289
|
+
if (loadUser) {
|
|
290
|
+
try {
|
|
291
|
+
const userRes = await this.client.get(`/users/${userId}`);
|
|
292
|
+
req.user = userRes.data;
|
|
293
|
+
} catch (userError) {
|
|
294
|
+
// If user loading fails, just use minimal user data
|
|
295
|
+
req.user = {
|
|
296
|
+
id: userId
|
|
297
|
+
};
|
|
298
|
+
if (debug) console.log(`⚠️ Auth: Failed to load user data, using minimal data`);
|
|
299
|
+
}
|
|
300
|
+
} else {
|
|
301
|
+
// Use minimal user data for performance
|
|
302
|
+
req.user = {
|
|
303
|
+
id: userId
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
if (debug) {
|
|
307
|
+
console.log(`✅ Auth: Authentication successful for user ${userId}`);
|
|
308
|
+
}
|
|
309
|
+
next();
|
|
310
|
+
} catch (error) {
|
|
311
|
+
const apiError = this.handleError(error);
|
|
312
|
+
if (debug) {
|
|
313
|
+
console.log(`❌ Auth: Unexpected error:`, apiError);
|
|
314
|
+
}
|
|
315
|
+
if (onError) return onError(apiError);
|
|
316
|
+
return res.status(apiError.status || 500).json(apiError);
|
|
317
|
+
}
|
|
318
|
+
};
|
|
319
|
+
}
|
|
157
320
|
}
|
|
158
321
|
exports.OxyServices = OxyServices;
|
|
159
322
|
//# sourceMappingURL=OxyServices.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_axios","_interopRequireDefault","require","_jwtDecode","_errorUtils","e","__esModule","default","OxyServices","accessToken","refreshToken","constructor","config","client","axios","create","baseURL","timeout","setupInterceptors","interceptors","request","use","req","decoded","jwtDecode","currentTime","Math","floor","Date","now","exp","sessionId","res","get","data","refreshError","clearTokens","headers","Authorization","error","getBaseURL","defaults","setTokens","getCurrentUserId","userId","id","hasAccessToken","validate","valid","getClient","handleError","handleHttpError","healthCheck","exports"],"sourceRo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E,MAAMsC,QAAQA,CAAA,EAAqB;IACjC,IAAI,CAAC,IAAI,CAACD,cAAc,CAAC,CAAC,EAAE;MAC1B,OAAO,KAAK;IACd;IAEA,IAAI;MACF,MAAMd,GAAG,GAAG,MAAM,IAAI,CAACnB,MAAM,CAACoB,GAAG,CAAC,gBAAgB,CAAC;MACnD,OAAOD,GAAG,CAACE,IAAI,CAACc,KAAK,KAAK,IAAI;IAChC,CAAC,CAAC,OAAOT,KAAK,EAAE;MACd,OAAO,KAAK;IACd;EACF;;EAEA;AACF;AACA;EACYU,SAASA,CAAA,EAAkB;IACnC,OAAO,IAAI,CAACpC,MAAM;EACpB;;EAEA;AACF;AACA;EACYqC,WAAWA,CAACX,KAAU,EAAY;IAC1C,OAAO,IAAAY,2BAAe,EAACZ,KAAK,CAAC;EAC/B;;EAEA;AACF;AACA;EACE,MAAMa,WAAWA,CAAA,EAKd;IACD,IAAI;MACF,MAAMpB,GAAG,GAAG,MAAM,IAAI,CAACnB,MAAM,CAACoB,GAAG,CAAC,SAAS,CAAC;MAC5C,OAAOD,GAAG,CAACE,IAAI;IACjB,CAAC,CAAC,OAAOK,KAAK,EAAE;MACd,MAAM,IAAI,CAACW,WAAW,CAACX,KAAK,CAAC;IAC/B;EACF;AACF;
|
|
1
|
+
{"version":3,"names":["_axios","_interopRequireDefault","require","_jwtDecode","_errorUtils","e","__esModule","default","OxyServices","accessToken","refreshToken","constructor","config","client","axios","create","baseURL","timeout","setupInterceptors","interceptors","request","use","req","decoded","jwtDecode","currentTime","Math","floor","Date","now","exp","sessionId","res","get","data","refreshError","clearTokens","headers","Authorization","error","getBaseURL","defaults","setTokens","getCurrentUserId","userId","id","hasAccessToken","validate","valid","getClient","handleError","handleHttpError","healthCheck","auth","options","debug","onError","loadUser","session","next","authHeader","token","startsWith","substring","console","log","method","path","message","code","status","json","decodeError","sessionRes","sessionData","sessionError","userRes","user","userError","apiError","exports"],"sourceRoot":"../../../src","sources":["core/OxyServices.ts"],"mappings":";;;;;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,CAAC;UACjF;QACF,CAAC,MAAM;UACL;UACA3C,GAAG,CAACuD,IAAI,GAAG;YAAEhC,EAAE,EAAED;UAAO,CAAS;QACnC;QAEA,IAAIW,KAAK,EAAE;UACTS,OAAO,CAACC,GAAG,CAAC,8CAA8CrB,MAAM,EAAE,CAAC;QACrE;QAEAe,IAAI,CAAC,CAAC;MACR,CAAC,CAAC,OAAOpB,KAAK,EAAE;QACd,MAAMwC,QAAQ,GAAG,IAAI,CAAC7B,WAAW,CAACX,KAAK,CAAC;QAExC,IAAIgB,KAAK,EAAE;UACTS,OAAO,CAACC,GAAG,CAAC,2BAA2B,EAAEc,QAAQ,CAAC;QACpD;QAEA,IAAIvB,OAAO,EAAE,OAAOA,OAAO,CAACuB,QAAQ,CAAC;QACrC,OAAO/C,GAAG,CAACsC,MAAM,CAACS,QAAQ,CAACT,MAAM,IAAI,GAAG,CAAC,CAACC,IAAI,CAACQ,QAAQ,CAAC;MAC1D;IACF,CAAC;EACH;AACF;AAACC,OAAA,CAAAxE,WAAA,GAAAA,WAAA","ignoreList":[]}
|
|
@@ -149,5 +149,168 @@ export class OxyServices {
|
|
|
149
149
|
throw this.handleError(error);
|
|
150
150
|
}
|
|
151
151
|
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Simple Express.js authentication middleware
|
|
155
|
+
*
|
|
156
|
+
* Built-in authentication middleware that validates JWT tokens and adds user data to requests.
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* ```typescript
|
|
160
|
+
* // Basic usage - just add it to your routes
|
|
161
|
+
* app.use('/api/protected', oxyServices.auth());
|
|
162
|
+
*
|
|
163
|
+
* // With debug logging
|
|
164
|
+
* app.use('/api/protected', oxyServices.auth({ debug: true }));
|
|
165
|
+
*
|
|
166
|
+
* // With custom error handling
|
|
167
|
+
* app.use('/api/protected', oxyServices.auth({
|
|
168
|
+
* onError: (error) => console.error('Auth failed:', error)
|
|
169
|
+
* }));
|
|
170
|
+
*
|
|
171
|
+
* // Load full user data
|
|
172
|
+
* app.use('/api/protected', oxyServices.auth({ loadUser: true }));
|
|
173
|
+
* ```
|
|
174
|
+
*
|
|
175
|
+
* @param options Optional configuration
|
|
176
|
+
* @param options.debug Enable debug logging (default: false)
|
|
177
|
+
* @param options.onError Custom error handler
|
|
178
|
+
* @param options.loadUser Load full user data (default: false for performance)
|
|
179
|
+
* @param options.session Use session-based validation (default: false)
|
|
180
|
+
* @returns Express middleware function
|
|
181
|
+
*/
|
|
182
|
+
auth(options = {}) {
|
|
183
|
+
const {
|
|
184
|
+
debug = false,
|
|
185
|
+
onError,
|
|
186
|
+
loadUser = false,
|
|
187
|
+
session = false
|
|
188
|
+
} = options;
|
|
189
|
+
return async (req, res, next) => {
|
|
190
|
+
try {
|
|
191
|
+
// Extract token from Authorization header
|
|
192
|
+
const authHeader = req.headers['authorization'];
|
|
193
|
+
const token = authHeader?.startsWith('Bearer ') ? authHeader.substring(7) : null;
|
|
194
|
+
if (debug) {
|
|
195
|
+
console.log(`🔐 Auth: Processing ${req.method} ${req.path}`);
|
|
196
|
+
console.log(`🔐 Auth: Token present: ${!!token}`);
|
|
197
|
+
}
|
|
198
|
+
if (!token) {
|
|
199
|
+
const error = {
|
|
200
|
+
message: 'Access token required',
|
|
201
|
+
code: 'MISSING_TOKEN',
|
|
202
|
+
status: 401
|
|
203
|
+
};
|
|
204
|
+
if (debug) console.log(`❌ Auth: Missing token`);
|
|
205
|
+
if (onError) return onError(error);
|
|
206
|
+
return res.status(401).json(error);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// Decode and validate token
|
|
210
|
+
let decoded;
|
|
211
|
+
try {
|
|
212
|
+
decoded = jwtDecode(token);
|
|
213
|
+
if (debug) {
|
|
214
|
+
console.log(`🔐 Auth: Token decoded, User ID: ${decoded.userId || decoded.id}`);
|
|
215
|
+
}
|
|
216
|
+
} catch (decodeError) {
|
|
217
|
+
const error = {
|
|
218
|
+
message: 'Invalid token format',
|
|
219
|
+
code: 'INVALID_TOKEN_FORMAT',
|
|
220
|
+
status: 403
|
|
221
|
+
};
|
|
222
|
+
if (debug) console.log(`❌ Auth: Token decode failed`);
|
|
223
|
+
if (onError) return onError(error);
|
|
224
|
+
return res.status(403).json(error);
|
|
225
|
+
}
|
|
226
|
+
const userId = decoded.userId || decoded.id;
|
|
227
|
+
if (!userId) {
|
|
228
|
+
const error = {
|
|
229
|
+
message: 'Token missing user ID',
|
|
230
|
+
code: 'INVALID_TOKEN_PAYLOAD',
|
|
231
|
+
status: 403
|
|
232
|
+
};
|
|
233
|
+
if (debug) console.log(`❌ Auth: Token missing user ID`);
|
|
234
|
+
if (onError) return onError(error);
|
|
235
|
+
return res.status(403).json(error);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// Check token expiration
|
|
239
|
+
if (decoded.exp && decoded.exp < Math.floor(Date.now() / 1000)) {
|
|
240
|
+
const error = {
|
|
241
|
+
message: 'Token expired',
|
|
242
|
+
code: 'TOKEN_EXPIRED',
|
|
243
|
+
status: 403
|
|
244
|
+
};
|
|
245
|
+
if (debug) console.log(`❌ Auth: Token expired`);
|
|
246
|
+
if (onError) return onError(error);
|
|
247
|
+
return res.status(403).json(error);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// Session-based validation if requested
|
|
251
|
+
if (session && decoded.sessionId) {
|
|
252
|
+
try {
|
|
253
|
+
if (debug) console.log(`🔐 Auth: Validating session ${decoded.sessionId}`);
|
|
254
|
+
const sessionRes = await this.client.get(`/session/validate/${decoded.sessionId}`);
|
|
255
|
+
const sessionData = sessionRes.data;
|
|
256
|
+
if (!sessionData.valid) {
|
|
257
|
+
const error = {
|
|
258
|
+
message: 'Invalid session',
|
|
259
|
+
code: 'INVALID_SESSION',
|
|
260
|
+
status: 403
|
|
261
|
+
};
|
|
262
|
+
if (debug) console.log(`❌ Auth: Session validation failed`);
|
|
263
|
+
if (onError) return onError(error);
|
|
264
|
+
return res.status(403).json(error);
|
|
265
|
+
}
|
|
266
|
+
if (debug) console.log(`✅ Auth: Session validation successful`);
|
|
267
|
+
} catch (sessionError) {
|
|
268
|
+
const error = {
|
|
269
|
+
message: 'Session validation failed',
|
|
270
|
+
code: 'SESSION_VALIDATION_ERROR',
|
|
271
|
+
status: 403
|
|
272
|
+
};
|
|
273
|
+
if (debug) console.log(`❌ Auth: Session validation error`);
|
|
274
|
+
if (onError) return onError(error);
|
|
275
|
+
return res.status(403).json(error);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// Set request properties
|
|
280
|
+
req.userId = userId;
|
|
281
|
+
req.accessToken = token;
|
|
282
|
+
|
|
283
|
+
// Load user data if requested
|
|
284
|
+
if (loadUser) {
|
|
285
|
+
try {
|
|
286
|
+
const userRes = await this.client.get(`/users/${userId}`);
|
|
287
|
+
req.user = userRes.data;
|
|
288
|
+
} catch (userError) {
|
|
289
|
+
// If user loading fails, just use minimal user data
|
|
290
|
+
req.user = {
|
|
291
|
+
id: userId
|
|
292
|
+
};
|
|
293
|
+
if (debug) console.log(`⚠️ Auth: Failed to load user data, using minimal data`);
|
|
294
|
+
}
|
|
295
|
+
} else {
|
|
296
|
+
// Use minimal user data for performance
|
|
297
|
+
req.user = {
|
|
298
|
+
id: userId
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
if (debug) {
|
|
302
|
+
console.log(`✅ Auth: Authentication successful for user ${userId}`);
|
|
303
|
+
}
|
|
304
|
+
next();
|
|
305
|
+
} catch (error) {
|
|
306
|
+
const apiError = this.handleError(error);
|
|
307
|
+
if (debug) {
|
|
308
|
+
console.log(`❌ Auth: Unexpected error:`, apiError);
|
|
309
|
+
}
|
|
310
|
+
if (onError) return onError(apiError);
|
|
311
|
+
return res.status(apiError.status || 500).json(apiError);
|
|
312
|
+
}
|
|
313
|
+
};
|
|
314
|
+
}
|
|
152
315
|
}
|
|
153
316
|
//# sourceMappingURL=OxyServices.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["axios","jwtDecode","handleHttpError","OxyServices","accessToken","refreshToken","constructor","config","client","create","baseURL","timeout","setupInterceptors","interceptors","request","use","req","decoded","currentTime","Math","floor","Date","now","exp","sessionId","res","get","data","refreshError","clearTokens","headers","Authorization","error","getBaseURL","defaults","setTokens","getCurrentUserId","userId","id","hasAccessToken","validate","valid","getClient","handleError","healthCheck"],"sourceRoot":"../../../src","sources":["core/OxyServices.ts"],"mappings":";;AAAA,OAAOA,KAAK,MAAqD,OAAO;AACxE,SAASC,SAAS,QAAQ,YAAY;AAEtC,SAASC,eAAe,QAAQ,qBAAqB;AAQrD;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMC,WAAW,CAAC;EAEfC,WAAW,GAAkB,IAAI;EACjCC,YAAY,GAAkB,IAAI;;EAE1C;AACF;AACA;AACA;EACEC,WAAWA,CAACC,MAAiB,EAAE;IAC7B,IAAI,CAACC,MAAM,GAAGR,KAAK,CAACS,MAAM,CAAC;MACzBC,OAAO,EAAEH,MAAM,CAACG,OAAO;MACvBC,OAAO,EAAE,KAAK,CAAC;IACjB,CAAC,CAAC;IAEF,IAAI,CAACC,iBAAiB,CAAC,CAAC;EAC1B;;EAEA;AACF;AACA;EACUA,iBAAiBA,CAAA,EAAS;IAChC;IACA,IAAI,CAACJ,MAAM,CAACK,YAAY,CAACC,OAAO,CAACC,GAAG,CAAC,MAAOC,GAA+B,IAAK;MAC9E,IAAI,CAAC,IAAI,CAACZ,WAAW,EAAE;QACrB,OAAOY,GAAG;MACZ;;MAEA;MACA,IAAI;QACF,MAAMC,OAAO,GAAGhB,SAAS,CAAa,IAAI,CAACG,WAAW,CAAC;QACvD,MAAMc,WAAW,GAAGC,IAAI,CAACC,KAAK,CAACC,IAAI,CAACC,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC;;QAEjD;QACA,IAAIL,OAAO,CAACM,GAAG,GAAGL,WAAW,GAAG,EAAE,EAAE;UAClC;UACA,IAAID,OAAO,CAACO,SAAS,EAAE;YACrB,IAAI;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O,cAAcA,CAAA,EAAY;IAChC,OAAO,CAAC,CAAC,IAAI,CAACnC,WAAW;EAC3B;;EAEA;AACF;AACA;EACE,MAAMoC,QAAQA,CAAA,EAAqB;IACjC,IAAI,CAAC,IAAI,CAACD,cAAc,CAAC,CAAC,EAAE;MAC1B,OAAO,KAAK;IACd;IAEA,IAAI;MACF,MAAMd,GAAG,GAAG,MAAM,IAAI,CAACjB,MAAM,CAACkB,GAAG,CAAC,gBAAgB,CAAC;MACnD,OAAOD,GAAG,CAACE,IAAI,CAACc,KAAK,KAAK,IAAI;IAChC,CAAC,CAAC,OAAOT,KAAK,EAAE;MACd,OAAO,KAAK;IACd;EACF;;EAEA;AACF;AACA;EACYU,SAASA,CAAA,EAAkB;IACnC,OAAO,IAAI,CAAClC,MAAM;EACpB;;EAEA;AACF;AACA;EACYmC,WAAWA,CAACX,KAAU,EAAY;IAC1C,OAAO9B,eAAe,CAAC8B,KAAK,CAAC;EAC/B;;EAEA;AACF;AACA;EACE,MAAMY,WAAWA,CAAA,EAKd;IACD,IAAI;MACF,MAAMnB,GAAG,GAAG,MAAM,IAAI,CAACjB,MAAM,CAACkB,GAAG,CAAC,SAAS,CAAC;MAC5C,OAAOD,GAAG,CAACE,IAAI;IACjB,CAAC,CAAC,OAAOK,KAAK,EAAE;MACd,MAAM,IAAI,CAACW,WAAW,CAACX,KAAK,CAAC;IAC/B;EACF;AACF","ignoreList":[]}
|
|
1
|
+
{"version":3,"names":["axios","jwtDecode","handleHttpError","OxyServices","accessToken","refreshToken","constructor","config","client","create","baseURL","timeout","setupInterceptors","interceptors","request","use","req","decoded","currentTime","Math","floor","Date","now","exp","sessionId","res","get","data","refreshError","clearTokens","headers","Authorization","error","getBaseURL","defaults","setTokens","getCurrentUserId","userId","id","hasAccessToken","validate","valid","getClient","handleError","healthCheck","auth","options","debug","onError","loadUser","session","next","authHeader","token","startsWith","substring","console","log","method","path","message","code","status","json","decodeError","sessionRes","sessionData","sessionError","userRes","user","userError","apiError"],"s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nC;QAEA,IAAIU,KAAK,EAAE;UACTS,OAAO,CAACC,GAAG,CAAC,8CAA8CpB,MAAM,EAAE,CAAC;QACrE;QAEAc,IAAI,CAAC,CAAC;MACR,CAAC,CAAC,OAAOnB,KAAK,EAAE;QACd,MAAMuC,QAAQ,GAAG,IAAI,CAAC5B,WAAW,CAACX,KAAK,CAAC;QAExC,IAAIe,KAAK,EAAE;UACTS,OAAO,CAACC,GAAG,CAAC,2BAA2B,EAAEc,QAAQ,CAAC;QACpD;QAEA,IAAIvB,OAAO,EAAE,OAAOA,OAAO,CAACuB,QAAQ,CAAC;QACrC,OAAO9C,GAAG,CAACqC,MAAM,CAACS,QAAQ,CAACT,MAAM,IAAI,GAAG,CAAC,CAACC,IAAI,CAACQ,QAAQ,CAAC;MAC1D;IACF,CAAC;EACH;AACF","ignoreList":[]}
|
|
@@ -60,5 +60,40 @@ export declare class OxyServices {
|
|
|
60
60
|
timestamp?: string;
|
|
61
61
|
[key: string]: any;
|
|
62
62
|
}>;
|
|
63
|
+
/**
|
|
64
|
+
* Simple Express.js authentication middleware
|
|
65
|
+
*
|
|
66
|
+
* Built-in authentication middleware that validates JWT tokens and adds user data to requests.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```typescript
|
|
70
|
+
* // Basic usage - just add it to your routes
|
|
71
|
+
* app.use('/api/protected', oxyServices.auth());
|
|
72
|
+
*
|
|
73
|
+
* // With debug logging
|
|
74
|
+
* app.use('/api/protected', oxyServices.auth({ debug: true }));
|
|
75
|
+
*
|
|
76
|
+
* // With custom error handling
|
|
77
|
+
* app.use('/api/protected', oxyServices.auth({
|
|
78
|
+
* onError: (error) => console.error('Auth failed:', error)
|
|
79
|
+
* }));
|
|
80
|
+
*
|
|
81
|
+
* // Load full user data
|
|
82
|
+
* app.use('/api/protected', oxyServices.auth({ loadUser: true }));
|
|
83
|
+
* ```
|
|
84
|
+
*
|
|
85
|
+
* @param options Optional configuration
|
|
86
|
+
* @param options.debug Enable debug logging (default: false)
|
|
87
|
+
* @param options.onError Custom error handler
|
|
88
|
+
* @param options.loadUser Load full user data (default: false for performance)
|
|
89
|
+
* @param options.session Use session-based validation (default: false)
|
|
90
|
+
* @returns Express middleware function
|
|
91
|
+
*/
|
|
92
|
+
auth(options?: {
|
|
93
|
+
debug?: boolean;
|
|
94
|
+
onError?: (error: ApiError) => any;
|
|
95
|
+
loadUser?: boolean;
|
|
96
|
+
session?: boolean;
|
|
97
|
+
}): (req: any, res: any, next: any) => Promise<any>;
|
|
63
98
|
}
|
|
64
99
|
//# sourceMappingURL=OxyServices.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"OxyServices.d.ts","sourceRoot":"","sources":["../../../src/core/OxyServices.ts"],"names":[],"mappings":"AAAA,OAAc,EAAE,aAAa,EAA8B,MAAM,OAAO,CAAC;AAEzE,OAAO,EAAE,SAAS,EAAE,QAAQ,
|
|
1
|
+
{"version":3,"file":"OxyServices.d.ts","sourceRoot":"","sources":["../../../src/core/OxyServices.ts"],"names":[],"mappings":"AAAA,OAAc,EAAE,aAAa,EAA8B,MAAM,OAAO,CAAC;AAEzE,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAQ,MAAM,sBAAsB,CAAC;AASjE;;;;;GAKG;AACH,qBAAa,WAAW;IACtB,SAAS,CAAC,MAAM,EAAE,aAAa,CAAC;IAChC,OAAO,CAAC,WAAW,CAAuB;IAC1C,OAAO,CAAC,YAAY,CAAuB;IAE3C;;;OAGG;gBACS,MAAM,EAAE,SAAS;IAS7B;;OAEG;IACH,OAAO,CAAC,iBAAiB;IAqCzB;;OAEG;IACI,UAAU,IAAI,MAAM;IAI3B;;OAEG;IACI,SAAS,CAAC,WAAW,EAAE,MAAM,EAAE,YAAY,GAAE,MAAW,GAAG,IAAI;IAKtE;;OAEG;IACI,WAAW,IAAI,IAAI;IAK1B;;OAEG;IACI,gBAAgB,IAAI,MAAM,GAAG,IAAI;IAaxC;;OAEG;IACH,OAAO,CAAC,cAAc;IAItB;;OAEG;IACG,QAAQ,IAAI,OAAO,CAAC,OAAO,CAAC;IAalC;;OAEG;IACH,SAAS,CAAC,SAAS,IAAI,aAAa;IAIpC;;OAEG;IACH,SAAS,CAAC,WAAW,CAAC,KAAK,EAAE,GAAG,GAAG,QAAQ;IAI3C;;OAEG;IACG,WAAW,IAAI,OAAO,CAAC;QAC3B,MAAM,EAAE,MAAM,CAAC;QACf,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,SAAS,CAAC,EAAE,MAAM,CAAC;QACnB,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;KACnB,CAAC;IASF;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA4BG;IACH,IAAI,CAAC,OAAO,GAAE;QACZ,KAAK,CAAC,EAAE,OAAO,CAAC;QAChB,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,QAAQ,KAAK,GAAG,CAAC;QACnC,QAAQ,CAAC,EAAE,OAAO,CAAC;QACnB,OAAO,CAAC,EAAE,OAAO,CAAC;KACd,IAGU,KAAK,GAAG,EAAE,KAAK,GAAG,EAAE,MAAM,GAAG;CAiJ9C"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oxyhq/services",
|
|
3
|
-
"version": "5.9.
|
|
3
|
+
"version": "5.9.4",
|
|
4
4
|
"description": "Reusable OxyHQ module to handle authentication, user management, karma system, device-based session management and more 🚀",
|
|
5
5
|
"main": "lib/commonjs/index.js",
|
|
6
6
|
"module": "lib/module/index.js",
|
package/src/core/OxyServices.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import axios, { AxiosInstance, InternalAxiosRequestConfig } from 'axios';
|
|
2
2
|
import { jwtDecode } from 'jwt-decode';
|
|
3
|
-
import { OxyConfig, ApiError } from '../models/interfaces';
|
|
3
|
+
import { OxyConfig, ApiError, User } from '../models/interfaces';
|
|
4
4
|
import { handleHttpError } from '../utils/errorUtils';
|
|
5
5
|
|
|
6
6
|
interface JwtPayload {
|
|
@@ -165,4 +165,187 @@ export class OxyServices {
|
|
|
165
165
|
throw this.handleError(error);
|
|
166
166
|
}
|
|
167
167
|
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Simple Express.js authentication middleware
|
|
171
|
+
*
|
|
172
|
+
* Built-in authentication middleware that validates JWT tokens and adds user data to requests.
|
|
173
|
+
*
|
|
174
|
+
* @example
|
|
175
|
+
* ```typescript
|
|
176
|
+
* // Basic usage - just add it to your routes
|
|
177
|
+
* app.use('/api/protected', oxyServices.auth());
|
|
178
|
+
*
|
|
179
|
+
* // With debug logging
|
|
180
|
+
* app.use('/api/protected', oxyServices.auth({ debug: true }));
|
|
181
|
+
*
|
|
182
|
+
* // With custom error handling
|
|
183
|
+
* app.use('/api/protected', oxyServices.auth({
|
|
184
|
+
* onError: (error) => console.error('Auth failed:', error)
|
|
185
|
+
* }));
|
|
186
|
+
*
|
|
187
|
+
* // Load full user data
|
|
188
|
+
* app.use('/api/protected', oxyServices.auth({ loadUser: true }));
|
|
189
|
+
* ```
|
|
190
|
+
*
|
|
191
|
+
* @param options Optional configuration
|
|
192
|
+
* @param options.debug Enable debug logging (default: false)
|
|
193
|
+
* @param options.onError Custom error handler
|
|
194
|
+
* @param options.loadUser Load full user data (default: false for performance)
|
|
195
|
+
* @param options.session Use session-based validation (default: false)
|
|
196
|
+
* @returns Express middleware function
|
|
197
|
+
*/
|
|
198
|
+
auth(options: {
|
|
199
|
+
debug?: boolean;
|
|
200
|
+
onError?: (error: ApiError) => any;
|
|
201
|
+
loadUser?: boolean;
|
|
202
|
+
session?: boolean;
|
|
203
|
+
} = {}) {
|
|
204
|
+
const { debug = false, onError, loadUser = false, session = false } = options;
|
|
205
|
+
|
|
206
|
+
return async (req: any, res: any, next: any) => {
|
|
207
|
+
try {
|
|
208
|
+
// Extract token from Authorization header
|
|
209
|
+
const authHeader = req.headers['authorization'];
|
|
210
|
+
const token = authHeader?.startsWith('Bearer ') ? authHeader.substring(7) : null;
|
|
211
|
+
|
|
212
|
+
if (debug) {
|
|
213
|
+
console.log(`🔐 Auth: Processing ${req.method} ${req.path}`);
|
|
214
|
+
console.log(`🔐 Auth: Token present: ${!!token}`);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
if (!token) {
|
|
218
|
+
const error = {
|
|
219
|
+
message: 'Access token required',
|
|
220
|
+
code: 'MISSING_TOKEN',
|
|
221
|
+
status: 401
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
if (debug) console.log(`❌ Auth: Missing token`);
|
|
225
|
+
|
|
226
|
+
if (onError) return onError(error);
|
|
227
|
+
return res.status(401).json(error);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// Decode and validate token
|
|
231
|
+
let decoded: JwtPayload;
|
|
232
|
+
try {
|
|
233
|
+
decoded = jwtDecode<JwtPayload>(token);
|
|
234
|
+
|
|
235
|
+
if (debug) {
|
|
236
|
+
console.log(`🔐 Auth: Token decoded, User ID: ${decoded.userId || decoded.id}`);
|
|
237
|
+
}
|
|
238
|
+
} catch (decodeError) {
|
|
239
|
+
const error = {
|
|
240
|
+
message: 'Invalid token format',
|
|
241
|
+
code: 'INVALID_TOKEN_FORMAT',
|
|
242
|
+
status: 403
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
if (debug) console.log(`❌ Auth: Token decode failed`);
|
|
246
|
+
|
|
247
|
+
if (onError) return onError(error);
|
|
248
|
+
return res.status(403).json(error);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
const userId = decoded.userId || decoded.id;
|
|
252
|
+
if (!userId) {
|
|
253
|
+
const error = {
|
|
254
|
+
message: 'Token missing user ID',
|
|
255
|
+
code: 'INVALID_TOKEN_PAYLOAD',
|
|
256
|
+
status: 403
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
if (debug) console.log(`❌ Auth: Token missing user ID`);
|
|
260
|
+
|
|
261
|
+
if (onError) return onError(error);
|
|
262
|
+
return res.status(403).json(error);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// Check token expiration
|
|
266
|
+
if (decoded.exp && decoded.exp < Math.floor(Date.now() / 1000)) {
|
|
267
|
+
const error = {
|
|
268
|
+
message: 'Token expired',
|
|
269
|
+
code: 'TOKEN_EXPIRED',
|
|
270
|
+
status: 403
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
if (debug) console.log(`❌ Auth: Token expired`);
|
|
274
|
+
|
|
275
|
+
if (onError) return onError(error);
|
|
276
|
+
return res.status(403).json(error);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// Session-based validation if requested
|
|
280
|
+
if (session && decoded.sessionId) {
|
|
281
|
+
try {
|
|
282
|
+
if (debug) console.log(`🔐 Auth: Validating session ${decoded.sessionId}`);
|
|
283
|
+
|
|
284
|
+
const sessionRes = await this.client.get(`/session/validate/${decoded.sessionId}`);
|
|
285
|
+
const sessionData = sessionRes.data;
|
|
286
|
+
|
|
287
|
+
if (!sessionData.valid) {
|
|
288
|
+
const error = {
|
|
289
|
+
message: 'Invalid session',
|
|
290
|
+
code: 'INVALID_SESSION',
|
|
291
|
+
status: 403
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
if (debug) console.log(`❌ Auth: Session validation failed`);
|
|
295
|
+
|
|
296
|
+
if (onError) return onError(error);
|
|
297
|
+
return res.status(403).json(error);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
if (debug) console.log(`✅ Auth: Session validation successful`);
|
|
301
|
+
} catch (sessionError) {
|
|
302
|
+
const error = {
|
|
303
|
+
message: 'Session validation failed',
|
|
304
|
+
code: 'SESSION_VALIDATION_ERROR',
|
|
305
|
+
status: 403
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
if (debug) console.log(`❌ Auth: Session validation error`);
|
|
309
|
+
|
|
310
|
+
if (onError) return onError(error);
|
|
311
|
+
return res.status(403).json(error);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// Set request properties
|
|
316
|
+
req.userId = userId;
|
|
317
|
+
req.accessToken = token;
|
|
318
|
+
|
|
319
|
+
// Load user data if requested
|
|
320
|
+
if (loadUser) {
|
|
321
|
+
try {
|
|
322
|
+
const userRes = await this.client.get(`/users/${userId}`);
|
|
323
|
+
req.user = userRes.data;
|
|
324
|
+
} catch (userError) {
|
|
325
|
+
// If user loading fails, just use minimal user data
|
|
326
|
+
req.user = { id: userId } as User;
|
|
327
|
+
if (debug) console.log(`⚠️ Auth: Failed to load user data, using minimal data`);
|
|
328
|
+
}
|
|
329
|
+
} else {
|
|
330
|
+
// Use minimal user data for performance
|
|
331
|
+
req.user = { id: userId } as User;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
if (debug) {
|
|
335
|
+
console.log(`✅ Auth: Authentication successful for user ${userId}`);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
next();
|
|
339
|
+
} catch (error) {
|
|
340
|
+
const apiError = this.handleError(error);
|
|
341
|
+
|
|
342
|
+
if (debug) {
|
|
343
|
+
console.log(`❌ Auth: Unexpected error:`, apiError);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
if (onError) return onError(apiError);
|
|
347
|
+
return res.status(apiError.status || 500).json(apiError);
|
|
348
|
+
}
|
|
349
|
+
};
|
|
350
|
+
}
|
|
168
351
|
}
|