@hemia/auth-sdk 0.0.10 → 0.0.12

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.
@@ -1,4 +1,4 @@
1
- import { BadRequestError, CustomHttpError, InternalServerError, Get, Req, Res, Post, Controller, HttpError, METADATA_KEYS, ControllerRegistry } from '@hemia/common';
1
+ import { BadRequestError, CustomHttpError, InternalServerError, Get, Req, Res, Post, Controller, ManualRegister, HttpError, METADATA_KEYS, ControllerRegistry } from '@hemia/common';
2
2
  import { HMNetworkServices } from '@hemia/network-services';
3
3
  import { JwtManager } from '@hemia/jwt-manager';
4
4
  import { randomBytes, createHash } from 'crypto';
@@ -185,11 +185,11 @@ let AuthService = class AuthService {
185
185
  const sessionId = randomBytes(16).toString('hex');
186
186
  const sessionData = {
187
187
  accessToken: access_token,
188
- refreshToken: refresh_token,
189
- idToken: id_token,
188
+ refreshToken: refresh_token || '',
189
+ idToken: id_token || '',
190
190
  expiresAt: Date.now() + (expires_in * 1000),
191
191
  createdAt: new Date().toISOString(),
192
- ssoSessionId: session_id
192
+ sessionId: session_id
193
193
  };
194
194
  await this.storage.set(`x-session:${sessionId}`, sessionData, expires_in);
195
195
  return {
@@ -306,7 +306,7 @@ let AuthService = class AuthService {
306
306
  if (session) {
307
307
  try {
308
308
  await this.networkServices.post(this.config.ssoLogoutEndpoint, {
309
- ssoSessionId: session.sessionId
309
+ sessionId: session.sessionId
310
310
  });
311
311
  }
312
312
  catch (e) { /* Silent error */ }
@@ -350,7 +350,7 @@ let AuthService = class AuthService {
350
350
  clientId: this.config.clientId,
351
351
  clientSecret: this.config.clientSecret,
352
352
  refreshToken: session.refreshToken,
353
- sessionId: session.ssoSessionId
353
+ sessionId: session.sessionId
354
354
  });
355
355
  if (response.status !== 200) {
356
356
  throw new CustomHttpError('Token refresh failed', response.status, 'token_refresh_failed');
@@ -380,12 +380,13 @@ AuthService = __decorate([
380
380
  const AUTH_SERVICE_ID = Symbol.for('HemiaAuthService');
381
381
 
382
382
  /**
383
- * Controller AuthController
383
+ * Controller AuthSDKController
384
384
  * Gestiona automáticamente Login, Callback, Me y Logout.
385
385
  */
386
- let AuthController = class AuthController {
386
+ let AuthSDKController = class AuthSDKController {
387
387
  constructor(authService) {
388
388
  this.authService = authService;
389
+ this.COOKIE_NAME = process.env.AUTH_COOKIE_NAME || 'x-session';
389
390
  }
390
391
  async login(req, res) {
391
392
  try {
@@ -413,12 +414,12 @@ let AuthController = class AuthController {
413
414
  }
414
415
  const storedState = JSON.parse(authFlowCookie);
415
416
  const result = await this.authService.handleCallback(code, state, storedState);
416
- res.cookie('x-session', result.sessionId, {
417
+ res.cookie(this.COOKIE_NAME, result.sessionId, {
417
418
  httpOnly: true,
418
419
  secure: process.env.NODE_ENV === 'production',
419
420
  sameSite: 'lax',
420
421
  maxAge: result.expiresIn * 1000,
421
- path: '/'
422
+ path: '/',
422
423
  });
423
424
  res.clearCookie('auth_flow');
424
425
  res.redirect(result.redirectUrl);
@@ -441,7 +442,11 @@ let AuthController = class AuthController {
441
442
  }
442
443
  }
443
444
  async me(req, res) {
444
- const sessionId = req.cookies['x-session'];
445
+ const sessionId = req.cookies[this.COOKIE_NAME];
446
+ console.log(`🔍 [App Cookie Name]: ${this.COOKIE_NAME}`);
447
+ console.log('🔍 [App Port]:', req.headers.origin || 'unknown');
448
+ console.log('🔍 [SessionId]:', sessionId);
449
+ console.log('🔍 [All Cookies]:', req.cookies);
445
450
  if (!sessionId) {
446
451
  return res.status(401).json({
447
452
  success: false,
@@ -463,7 +468,7 @@ let AuthController = class AuthController {
463
468
  });
464
469
  }
465
470
  catch (error) {
466
- res.clearCookie('x-session', {
471
+ res.clearCookie(this.COOKIE_NAME, {
467
472
  httpOnly: true,
468
473
  secure: process.env.NODE_ENV === 'production',
469
474
  sameSite: 'lax',
@@ -491,11 +496,11 @@ let AuthController = class AuthController {
491
496
  }
492
497
  }
493
498
  async logout(req, res) {
494
- const sessionId = req.cookies['x-session'];
499
+ const sessionId = req.cookies[this.COOKIE_NAME];
495
500
  if (sessionId) {
496
501
  await this.authService.logout(sessionId);
497
502
  }
498
- res.clearCookie('x-session', {
503
+ res.clearCookie(this.COOKIE_NAME, {
499
504
  httpOnly: true,
500
505
  secure: process.env.NODE_ENV === 'production',
501
506
  sameSite: 'lax',
@@ -508,13 +513,13 @@ __decorate([
508
513
  __metadata("design:type", Function),
509
514
  __metadata("design:paramtypes", [Object, Object]),
510
515
  __metadata("design:returntype", Promise)
511
- ], AuthController.prototype, "login", null);
516
+ ], AuthSDKController.prototype, "login", null);
512
517
  __decorate([
513
518
  Get('/callback'),
514
519
  __metadata("design:type", Function),
515
520
  __metadata("design:paramtypes", [Object, Object]),
516
521
  __metadata("design:returntype", Promise)
517
- ], AuthController.prototype, "callback", null);
522
+ ], AuthSDKController.prototype, "callback", null);
518
523
  __decorate([
519
524
  Get('/me'),
520
525
  __param(0, Req()),
@@ -522,7 +527,7 @@ __decorate([
522
527
  __metadata("design:type", Function),
523
528
  __metadata("design:paramtypes", [Object, Object]),
524
529
  __metadata("design:returntype", Promise)
525
- ], AuthController.prototype, "me", null);
530
+ ], AuthSDKController.prototype, "me", null);
526
531
  __decorate([
527
532
  Post('/logout'),
528
533
  __param(0, Req()),
@@ -530,12 +535,13 @@ __decorate([
530
535
  __metadata("design:type", Function),
531
536
  __metadata("design:paramtypes", [Object, Object]),
532
537
  __metadata("design:returntype", Promise)
533
- ], AuthController.prototype, "logout", null);
534
- AuthController = __decorate([
538
+ ], AuthSDKController.prototype, "logout", null);
539
+ AuthSDKController = __decorate([
535
540
  Controller('/'),
541
+ ManualRegister(),
536
542
  __param(0, inject(AUTH_SERVICE_ID)),
537
543
  __metadata("design:paramtypes", [AuthService])
538
- ], AuthController);
544
+ ], AuthSDKController);
539
545
 
540
546
  class AuthCacheAdapter {
541
547
  constructor(externalCache) {
@@ -561,12 +567,12 @@ const authPlugin = (config, cacheFactory, options) => {
561
567
  const jwt = new JwtManager();
562
568
  return new AuthService(config, storageAdapter, network, jwt);
563
569
  }).inSingletonScope();
564
- Reflect.defineMetadata(METADATA_KEYS.BASE_PATH, options.basePath, AuthController);
565
- ControllerRegistry.register(AuthController);
566
- if (!container.isBound(AuthController)) {
567
- container.bind(AuthController).toSelf().inSingletonScope();
570
+ Reflect.defineMetadata(METADATA_KEYS.BASE_PATH, options.basePath, AuthSDKController);
571
+ ControllerRegistry.register(AuthSDKController);
572
+ if (!container.isBound(AuthSDKController)) {
573
+ container.bind(AuthSDKController).toSelf().inSingletonScope();
568
574
  }
569
575
  };
570
576
  };
571
577
 
572
- export { AUTH_SERVICE_ID, AuthController, AuthService, InvalidTokenFormatError, SessionError, SessionExpiredError, SessionInvalidError, SessionNotFoundError, TokenRefreshFailedError, authPlugin };
578
+ export { AUTH_SERVICE_ID, AuthSDKController, AuthService, InvalidTokenFormatError, SessionError, SessionExpiredError, SessionInvalidError, SessionNotFoundError, TokenRefreshFailedError, authPlugin };
@@ -187,11 +187,11 @@ exports.AuthService = class AuthService {
187
187
  const sessionId = crypto.randomBytes(16).toString('hex');
188
188
  const sessionData = {
189
189
  accessToken: access_token,
190
- refreshToken: refresh_token,
191
- idToken: id_token,
190
+ refreshToken: refresh_token || '',
191
+ idToken: id_token || '',
192
192
  expiresAt: Date.now() + (expires_in * 1000),
193
193
  createdAt: new Date().toISOString(),
194
- ssoSessionId: session_id
194
+ sessionId: session_id
195
195
  };
196
196
  await this.storage.set(`x-session:${sessionId}`, sessionData, expires_in);
197
197
  return {
@@ -308,7 +308,7 @@ exports.AuthService = class AuthService {
308
308
  if (session) {
309
309
  try {
310
310
  await this.networkServices.post(this.config.ssoLogoutEndpoint, {
311
- ssoSessionId: session.sessionId
311
+ sessionId: session.sessionId
312
312
  });
313
313
  }
314
314
  catch (e) { /* Silent error */ }
@@ -352,7 +352,7 @@ exports.AuthService = class AuthService {
352
352
  clientId: this.config.clientId,
353
353
  clientSecret: this.config.clientSecret,
354
354
  refreshToken: session.refreshToken,
355
- sessionId: session.ssoSessionId
355
+ sessionId: session.sessionId
356
356
  });
357
357
  if (response.status !== 200) {
358
358
  throw new common.CustomHttpError('Token refresh failed', response.status, 'token_refresh_failed');
@@ -382,12 +382,13 @@ exports.AuthService = __decorate([
382
382
  const AUTH_SERVICE_ID = Symbol.for('HemiaAuthService');
383
383
 
384
384
  /**
385
- * Controller AuthController
385
+ * Controller AuthSDKController
386
386
  * Gestiona automáticamente Login, Callback, Me y Logout.
387
387
  */
388
- exports.AuthController = class AuthController {
388
+ exports.AuthSDKController = class AuthSDKController {
389
389
  constructor(authService) {
390
390
  this.authService = authService;
391
+ this.COOKIE_NAME = process.env.AUTH_COOKIE_NAME || 'x-session';
391
392
  }
392
393
  async login(req, res) {
393
394
  try {
@@ -415,12 +416,12 @@ exports.AuthController = class AuthController {
415
416
  }
416
417
  const storedState = JSON.parse(authFlowCookie);
417
418
  const result = await this.authService.handleCallback(code, state, storedState);
418
- res.cookie('x-session', result.sessionId, {
419
+ res.cookie(this.COOKIE_NAME, result.sessionId, {
419
420
  httpOnly: true,
420
421
  secure: process.env.NODE_ENV === 'production',
421
422
  sameSite: 'lax',
422
423
  maxAge: result.expiresIn * 1000,
423
- path: '/'
424
+ path: '/',
424
425
  });
425
426
  res.clearCookie('auth_flow');
426
427
  res.redirect(result.redirectUrl);
@@ -443,7 +444,11 @@ exports.AuthController = class AuthController {
443
444
  }
444
445
  }
445
446
  async me(req, res) {
446
- const sessionId = req.cookies['x-session'];
447
+ const sessionId = req.cookies[this.COOKIE_NAME];
448
+ console.log(`🔍 [App Cookie Name]: ${this.COOKIE_NAME}`);
449
+ console.log('🔍 [App Port]:', req.headers.origin || 'unknown');
450
+ console.log('🔍 [SessionId]:', sessionId);
451
+ console.log('🔍 [All Cookies]:', req.cookies);
447
452
  if (!sessionId) {
448
453
  return res.status(401).json({
449
454
  success: false,
@@ -465,7 +470,7 @@ exports.AuthController = class AuthController {
465
470
  });
466
471
  }
467
472
  catch (error) {
468
- res.clearCookie('x-session', {
473
+ res.clearCookie(this.COOKIE_NAME, {
469
474
  httpOnly: true,
470
475
  secure: process.env.NODE_ENV === 'production',
471
476
  sameSite: 'lax',
@@ -493,11 +498,11 @@ exports.AuthController = class AuthController {
493
498
  }
494
499
  }
495
500
  async logout(req, res) {
496
- const sessionId = req.cookies['x-session'];
501
+ const sessionId = req.cookies[this.COOKIE_NAME];
497
502
  if (sessionId) {
498
503
  await this.authService.logout(sessionId);
499
504
  }
500
- res.clearCookie('x-session', {
505
+ res.clearCookie(this.COOKIE_NAME, {
501
506
  httpOnly: true,
502
507
  secure: process.env.NODE_ENV === 'production',
503
508
  sameSite: 'lax',
@@ -510,13 +515,13 @@ __decorate([
510
515
  __metadata("design:type", Function),
511
516
  __metadata("design:paramtypes", [Object, Object]),
512
517
  __metadata("design:returntype", Promise)
513
- ], exports.AuthController.prototype, "login", null);
518
+ ], exports.AuthSDKController.prototype, "login", null);
514
519
  __decorate([
515
520
  common.Get('/callback'),
516
521
  __metadata("design:type", Function),
517
522
  __metadata("design:paramtypes", [Object, Object]),
518
523
  __metadata("design:returntype", Promise)
519
- ], exports.AuthController.prototype, "callback", null);
524
+ ], exports.AuthSDKController.prototype, "callback", null);
520
525
  __decorate([
521
526
  common.Get('/me'),
522
527
  __param(0, common.Req()),
@@ -524,7 +529,7 @@ __decorate([
524
529
  __metadata("design:type", Function),
525
530
  __metadata("design:paramtypes", [Object, Object]),
526
531
  __metadata("design:returntype", Promise)
527
- ], exports.AuthController.prototype, "me", null);
532
+ ], exports.AuthSDKController.prototype, "me", null);
528
533
  __decorate([
529
534
  common.Post('/logout'),
530
535
  __param(0, common.Req()),
@@ -532,12 +537,13 @@ __decorate([
532
537
  __metadata("design:type", Function),
533
538
  __metadata("design:paramtypes", [Object, Object]),
534
539
  __metadata("design:returntype", Promise)
535
- ], exports.AuthController.prototype, "logout", null);
536
- exports.AuthController = __decorate([
540
+ ], exports.AuthSDKController.prototype, "logout", null);
541
+ exports.AuthSDKController = __decorate([
537
542
  common.Controller('/'),
543
+ common.ManualRegister(),
538
544
  __param(0, inversify.inject(AUTH_SERVICE_ID)),
539
545
  __metadata("design:paramtypes", [exports.AuthService])
540
- ], exports.AuthController);
546
+ ], exports.AuthSDKController);
541
547
 
542
548
  class AuthCacheAdapter {
543
549
  constructor(externalCache) {
@@ -563,10 +569,10 @@ const authPlugin = (config, cacheFactory, options) => {
563
569
  const jwt = new jwtManager.JwtManager();
564
570
  return new exports.AuthService(config, storageAdapter, network, jwt);
565
571
  }).inSingletonScope();
566
- Reflect.defineMetadata(common.METADATA_KEYS.BASE_PATH, options.basePath, exports.AuthController);
567
- common.ControllerRegistry.register(exports.AuthController);
568
- if (!container.isBound(exports.AuthController)) {
569
- container.bind(exports.AuthController).toSelf().inSingletonScope();
572
+ Reflect.defineMetadata(common.METADATA_KEYS.BASE_PATH, options.basePath, exports.AuthSDKController);
573
+ common.ControllerRegistry.register(exports.AuthSDKController);
574
+ if (!container.isBound(exports.AuthSDKController)) {
575
+ container.bind(exports.AuthSDKController).toSelf().inSingletonScope();
570
576
  }
571
577
  };
572
578
  };
@@ -1,11 +1,12 @@
1
1
  import { Request, Response } from "express";
2
2
  import { AuthService } from "../services/auth.service";
3
3
  /**
4
- * Controller AuthController
4
+ * Controller AuthSDKController
5
5
  * Gestiona automáticamente Login, Callback, Me y Logout.
6
6
  */
7
- export declare class AuthController {
7
+ export declare class AuthSDKController {
8
8
  private readonly authService;
9
+ private readonly COOKIE_NAME;
9
10
  constructor(authService: AuthService);
10
11
  login(req: Request, res: Response): Promise<void>;
11
12
  callback(req: Request, res: Response): Promise<void>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hemia/auth-sdk",
3
- "version": "0.0.10",
3
+ "version": "0.0.12",
4
4
  "description": "Hemia SDK for authentication",
5
5
  "main": "dist/hemia-auth-sdk.js",
6
6
  "module": "dist/hemia-auth-sdk.esm.js",
@@ -11,11 +11,12 @@
11
11
  "build": "npm run clean && npm run tscBuild",
12
12
  "test": "jest --detectOpenHandles",
13
13
  "test:coverage": "jest --coverage",
14
- "test:watch": "jest --watch"
14
+ "test:watch": "jest --watch",
15
+ "prepublish": "npm run build"
15
16
  },
16
17
  "devDependencies": {
17
18
  "@hemia/cache-manager": "^0.0.5",
18
- "@hemia/common": "^0.0.9",
19
+ "@hemia/common": "^0.0.12",
19
20
  "@hemia/jwt-manager": "^0.0.4",
20
21
  "@hemia/network-services": "^0.0.3",
21
22
  "@rollup/plugin-commonjs": "^26.0.1",
@@ -43,8 +44,8 @@
43
44
  ],
44
45
  "peerDependencies": {
45
46
  "@hemia/cache-manager": "^0.0.5",
46
- "@hemia/common": "^0.0.9",
47
- "@hemia/jwt-manager": "^0.0.4",
47
+ "@hemia/common": "^0.0.12",
48
+ "@hemia/jwt-manager": "^0.0.5",
48
49
  "@hemia/network-services": "^0.0.3",
49
50
  "inversify": "^7.11.0",
50
51
  "reflect-metadata": "^0.2.2"