@hemia/auth-sdk 0.0.6 → 0.0.8
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,3 @@
|
|
|
1
|
-
import 'reflect-metadata';
|
|
2
1
|
import { BadRequestError, CustomHttpError, InternalServerError, Get, Req, Res, Post, HttpError } from '@hemia/common';
|
|
3
2
|
import { HMNetworkServices } from '@hemia/network-services';
|
|
4
3
|
import { JwtManager } from '@hemia/jwt-manager';
|
|
@@ -239,6 +238,46 @@ let AuthService = class AuthService {
|
|
|
239
238
|
throw new InvalidTokenFormatError();
|
|
240
239
|
}
|
|
241
240
|
}
|
|
241
|
+
/**
|
|
242
|
+
* Obtiene y valida los claims del access token de la sesión.
|
|
243
|
+
* @param sessionId Identificador de la sesión
|
|
244
|
+
* @returns Claims del access token o error si el token no es válido
|
|
245
|
+
*/
|
|
246
|
+
async getSessionAccess(sessionId) {
|
|
247
|
+
const key = `x-session:${sessionId}`;
|
|
248
|
+
let session = await this.storage.get(key);
|
|
249
|
+
if (!session) {
|
|
250
|
+
throw new SessionNotFoundError();
|
|
251
|
+
}
|
|
252
|
+
if (session.expiresAt < Date.now()) {
|
|
253
|
+
throw new SessionExpiredError();
|
|
254
|
+
}
|
|
255
|
+
const timeUntilExpiry = session.expiresAt - Date.now();
|
|
256
|
+
if (timeUntilExpiry < 2 * 60 * 1000) {
|
|
257
|
+
try {
|
|
258
|
+
session = await this.refreshTokens(session, sessionId);
|
|
259
|
+
}
|
|
260
|
+
catch (error) {
|
|
261
|
+
throw new TokenRefreshFailedError();
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
try {
|
|
265
|
+
const verify = this.jwtManager.verify(session.accessToken);
|
|
266
|
+
if (!verify) {
|
|
267
|
+
throw new SessionInvalidError();
|
|
268
|
+
}
|
|
269
|
+
const claims = this.jwtManager.decode(session.accessToken, true);
|
|
270
|
+
if (!claims) {
|
|
271
|
+
throw new SessionInvalidError();
|
|
272
|
+
}
|
|
273
|
+
return {
|
|
274
|
+
...claims
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
catch (e) {
|
|
278
|
+
throw new InvalidTokenFormatError();
|
|
279
|
+
}
|
|
280
|
+
}
|
|
242
281
|
/**
|
|
243
282
|
* Cierra la sesión del usuario tanto en el SSO como localmente.
|
|
244
283
|
* @param sessionId Identificador de la sesión
|
|
@@ -314,16 +353,14 @@ AuthService = __decorate([
|
|
|
314
353
|
JwtManager])
|
|
315
354
|
], AuthService);
|
|
316
355
|
|
|
317
|
-
const AUTH_SERVICE_ID = Symbol.for(
|
|
356
|
+
const AUTH_SERVICE_ID = Symbol.for('HemiaAuthService');
|
|
318
357
|
|
|
319
358
|
/**
|
|
320
359
|
* Controller Abstracto Reutilizable
|
|
321
360
|
* Gestiona automáticamente Login, Callback, Me y Logout.
|
|
322
361
|
*/
|
|
323
362
|
let AbstractAuthController = class AbstractAuthController {
|
|
324
|
-
constructor(
|
|
325
|
-
this.authService = authService;
|
|
326
|
-
}
|
|
363
|
+
constructor() { }
|
|
327
364
|
async login(req, res) {
|
|
328
365
|
try {
|
|
329
366
|
const autoParam = typeof req.query.auto === 'string' ? req.query.auto : 'false';
|
|
@@ -440,10 +477,12 @@ let AbstractAuthController = class AbstractAuthController {
|
|
|
440
477
|
return res.status(200).json({ success: true });
|
|
441
478
|
}
|
|
442
479
|
};
|
|
480
|
+
__decorate([
|
|
481
|
+
inject(AUTH_SERVICE_ID),
|
|
482
|
+
__metadata("design:type", AuthService)
|
|
483
|
+
], AbstractAuthController.prototype, "authService", void 0);
|
|
443
484
|
__decorate([
|
|
444
485
|
Get('/login'),
|
|
445
|
-
__param(0, Req()),
|
|
446
|
-
__param(1, Res()),
|
|
447
486
|
__metadata("design:type", Function),
|
|
448
487
|
__metadata("design:paramtypes", [Object, Object]),
|
|
449
488
|
__metadata("design:returntype", Promise)
|
|
@@ -472,8 +511,7 @@ __decorate([
|
|
|
472
511
|
], AbstractAuthController.prototype, "logout", null);
|
|
473
512
|
AbstractAuthController = __decorate([
|
|
474
513
|
injectable(),
|
|
475
|
-
|
|
476
|
-
__metadata("design:paramtypes", [AuthService])
|
|
514
|
+
__metadata("design:paramtypes", [])
|
|
477
515
|
], AbstractAuthController);
|
|
478
516
|
|
|
479
517
|
class AuthCacheAdapter {
|
package/dist/hemia-auth-sdk.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
require('reflect-metadata');
|
|
4
3
|
var common = require('@hemia/common');
|
|
5
4
|
var networkServices = require('@hemia/network-services');
|
|
6
5
|
var jwtManager = require('@hemia/jwt-manager');
|
|
@@ -241,6 +240,46 @@ exports.AuthService = class AuthService {
|
|
|
241
240
|
throw new InvalidTokenFormatError();
|
|
242
241
|
}
|
|
243
242
|
}
|
|
243
|
+
/**
|
|
244
|
+
* Obtiene y valida los claims del access token de la sesión.
|
|
245
|
+
* @param sessionId Identificador de la sesión
|
|
246
|
+
* @returns Claims del access token o error si el token no es válido
|
|
247
|
+
*/
|
|
248
|
+
async getSessionAccess(sessionId) {
|
|
249
|
+
const key = `x-session:${sessionId}`;
|
|
250
|
+
let session = await this.storage.get(key);
|
|
251
|
+
if (!session) {
|
|
252
|
+
throw new SessionNotFoundError();
|
|
253
|
+
}
|
|
254
|
+
if (session.expiresAt < Date.now()) {
|
|
255
|
+
throw new SessionExpiredError();
|
|
256
|
+
}
|
|
257
|
+
const timeUntilExpiry = session.expiresAt - Date.now();
|
|
258
|
+
if (timeUntilExpiry < 2 * 60 * 1000) {
|
|
259
|
+
try {
|
|
260
|
+
session = await this.refreshTokens(session, sessionId);
|
|
261
|
+
}
|
|
262
|
+
catch (error) {
|
|
263
|
+
throw new TokenRefreshFailedError();
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
try {
|
|
267
|
+
const verify = this.jwtManager.verify(session.accessToken);
|
|
268
|
+
if (!verify) {
|
|
269
|
+
throw new SessionInvalidError();
|
|
270
|
+
}
|
|
271
|
+
const claims = this.jwtManager.decode(session.accessToken, true);
|
|
272
|
+
if (!claims) {
|
|
273
|
+
throw new SessionInvalidError();
|
|
274
|
+
}
|
|
275
|
+
return {
|
|
276
|
+
...claims
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
catch (e) {
|
|
280
|
+
throw new InvalidTokenFormatError();
|
|
281
|
+
}
|
|
282
|
+
}
|
|
244
283
|
/**
|
|
245
284
|
* Cierra la sesión del usuario tanto en el SSO como localmente.
|
|
246
285
|
* @param sessionId Identificador de la sesión
|
|
@@ -316,16 +355,14 @@ exports.AuthService = __decorate([
|
|
|
316
355
|
jwtManager.JwtManager])
|
|
317
356
|
], exports.AuthService);
|
|
318
357
|
|
|
319
|
-
const AUTH_SERVICE_ID = Symbol.for(
|
|
358
|
+
const AUTH_SERVICE_ID = Symbol.for('HemiaAuthService');
|
|
320
359
|
|
|
321
360
|
/**
|
|
322
361
|
* Controller Abstracto Reutilizable
|
|
323
362
|
* Gestiona automáticamente Login, Callback, Me y Logout.
|
|
324
363
|
*/
|
|
325
364
|
exports.AbstractAuthController = class AbstractAuthController {
|
|
326
|
-
constructor(
|
|
327
|
-
this.authService = authService;
|
|
328
|
-
}
|
|
365
|
+
constructor() { }
|
|
329
366
|
async login(req, res) {
|
|
330
367
|
try {
|
|
331
368
|
const autoParam = typeof req.query.auto === 'string' ? req.query.auto : 'false';
|
|
@@ -442,10 +479,12 @@ exports.AbstractAuthController = class AbstractAuthController {
|
|
|
442
479
|
return res.status(200).json({ success: true });
|
|
443
480
|
}
|
|
444
481
|
};
|
|
482
|
+
__decorate([
|
|
483
|
+
inversify.inject(AUTH_SERVICE_ID),
|
|
484
|
+
__metadata("design:type", exports.AuthService)
|
|
485
|
+
], exports.AbstractAuthController.prototype, "authService", void 0);
|
|
445
486
|
__decorate([
|
|
446
487
|
common.Get('/login'),
|
|
447
|
-
__param(0, common.Req()),
|
|
448
|
-
__param(1, common.Res()),
|
|
449
488
|
__metadata("design:type", Function),
|
|
450
489
|
__metadata("design:paramtypes", [Object, Object]),
|
|
451
490
|
__metadata("design:returntype", Promise)
|
|
@@ -474,8 +513,7 @@ __decorate([
|
|
|
474
513
|
], exports.AbstractAuthController.prototype, "logout", null);
|
|
475
514
|
exports.AbstractAuthController = __decorate([
|
|
476
515
|
inversify.injectable(),
|
|
477
|
-
|
|
478
|
-
__metadata("design:paramtypes", [exports.AuthService])
|
|
516
|
+
__metadata("design:paramtypes", [])
|
|
479
517
|
], exports.AbstractAuthController);
|
|
480
518
|
|
|
481
519
|
class AuthCacheAdapter {
|
|
@@ -6,7 +6,7 @@ import { AuthService } from "../services/auth.service";
|
|
|
6
6
|
*/
|
|
7
7
|
export declare abstract class AbstractAuthController {
|
|
8
8
|
protected readonly authService: AuthService;
|
|
9
|
-
constructor(
|
|
9
|
+
constructor();
|
|
10
10
|
login(req: Request, res: Response): Promise<void>;
|
|
11
11
|
callback(req: Request, res: Response): Promise<void>;
|
|
12
12
|
me(req: Request, res: Response): Promise<Response>;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -28,6 +28,12 @@ export declare class AuthService {
|
|
|
28
28
|
* @returns Información del usuario o error si la sesión no es válida
|
|
29
29
|
*/
|
|
30
30
|
getSessionUser(sessionId: string): Promise<ISessionUser>;
|
|
31
|
+
/**
|
|
32
|
+
* Obtiene y valida los claims del access token de la sesión.
|
|
33
|
+
* @param sessionId Identificador de la sesión
|
|
34
|
+
* @returns Claims del access token o error si el token no es válido
|
|
35
|
+
*/
|
|
36
|
+
getSessionAccess(sessionId: string): Promise<any>;
|
|
31
37
|
/**
|
|
32
38
|
* Cierra la sesión del usuario tanto en el SSO como localmente.
|
|
33
39
|
* @param sessionId Identificador de la sesión
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hemia/auth-sdk",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.8",
|
|
4
4
|
"description": "Hemia SDK for authentication",
|
|
5
5
|
"main": "dist/hemia-auth-sdk.js",
|
|
6
6
|
"module": "dist/hemia-auth-sdk.esm.js",
|
|
@@ -14,19 +14,19 @@
|
|
|
14
14
|
"test:watch": "jest --watch"
|
|
15
15
|
},
|
|
16
16
|
"devDependencies": {
|
|
17
|
+
"@hemia/cache-manager": "^0.0.5",
|
|
18
|
+
"@hemia/common": "^0.0.2",
|
|
19
|
+
"@hemia/jwt-manager": "^0.0.4",
|
|
20
|
+
"@hemia/network-services": "^0.0.3",
|
|
17
21
|
"@rollup/plugin-commonjs": "^26.0.1",
|
|
18
22
|
"@rollup/plugin-json": "^6.1.0",
|
|
19
23
|
"@rollup/plugin-node-resolve": "^15.2.3",
|
|
24
|
+
"@types/express": "^4.17.21",
|
|
20
25
|
"@types/jest": "^29.5.14",
|
|
21
26
|
"@types/node": "^22.3.0",
|
|
22
27
|
"@typescript-eslint/eslint-plugin": "^8.5.0",
|
|
23
|
-
"@hemia/network-services": "^0.0.3",
|
|
24
|
-
"@hemia/common": "^0.0.2",
|
|
25
|
-
"@hemia/cache-manager": "^0.0.5",
|
|
26
|
-
"@hemia/jwt-manager": "^0.0.4",
|
|
27
|
-
"@types/express": "^4.17.21",
|
|
28
|
-
"express": "^5.2.1",
|
|
29
28
|
"events": "^3.3.0",
|
|
29
|
+
"express": "^5.2.1",
|
|
30
30
|
"inversify": "^7.11.0",
|
|
31
31
|
"jest": "^29.7.0",
|
|
32
32
|
"rimraf": "^6.0.1",
|
|
@@ -42,14 +42,11 @@
|
|
|
42
42
|
"dist"
|
|
43
43
|
],
|
|
44
44
|
"peerDependencies": {
|
|
45
|
-
"
|
|
46
|
-
"inversify": "^7.11.0",
|
|
45
|
+
"@hemia/cache-manager": "^0.0.5",
|
|
47
46
|
"@hemia/common": "^0.0.2",
|
|
47
|
+
"@hemia/jwt-manager": "^0.0.4",
|
|
48
48
|
"@hemia/network-services": "^0.0.3",
|
|
49
|
-
"
|
|
50
|
-
"
|
|
51
|
-
},
|
|
52
|
-
"dependencies": {
|
|
53
|
-
|
|
49
|
+
"inversify": "^7.11.0",
|
|
50
|
+
"reflect-metadata": "^0.2.2"
|
|
54
51
|
}
|
|
55
52
|
}
|