@hemia/auth-sdk 0.0.9 → 0.0.10
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, HttpError } from '@hemia/common';
|
|
1
|
+
import { BadRequestError, CustomHttpError, InternalServerError, Get, Req, Res, Post, Controller, 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';
|
|
@@ -178,10 +178,10 @@ let AuthService = class AuthService {
|
|
|
178
178
|
if (response.status !== 200) {
|
|
179
179
|
throw new CustomHttpError('Token exchange failed', response.status, 'token_exchange_failed');
|
|
180
180
|
}
|
|
181
|
-
if (!response.data.access_token) {
|
|
181
|
+
if (!response.data.data?.access_token) {
|
|
182
182
|
throw new InternalServerError('No access token received from SSO', 'invalid_token_response');
|
|
183
183
|
}
|
|
184
|
-
const { access_token, refresh_token, id_token, expires_in, session_id } = response.data;
|
|
184
|
+
const { access_token, refresh_token, id_token, expires_in, session_id } = response.data.data;
|
|
185
185
|
const sessionId = randomBytes(16).toString('hex');
|
|
186
186
|
const sessionData = {
|
|
187
187
|
accessToken: access_token,
|
|
@@ -352,13 +352,19 @@ let AuthService = class AuthService {
|
|
|
352
352
|
refreshToken: session.refreshToken,
|
|
353
353
|
sessionId: session.ssoSessionId
|
|
354
354
|
});
|
|
355
|
-
|
|
355
|
+
if (response.status !== 200) {
|
|
356
|
+
throw new CustomHttpError('Token refresh failed', response.status, 'token_refresh_failed');
|
|
357
|
+
}
|
|
358
|
+
if (!response.data.data) {
|
|
359
|
+
throw new InternalServerError('No token data received from SSO during refresh', 'invalid_token_response');
|
|
360
|
+
}
|
|
361
|
+
const { access_token, refresh_token, id_token, expires_in, session_id } = response.data.data;
|
|
356
362
|
const updatedSession = {
|
|
357
363
|
accessToken: access_token,
|
|
358
364
|
refreshToken: refresh_token || session.refreshToken,
|
|
359
365
|
idToken: id_token || session.idToken,
|
|
360
366
|
expiresAt: Date.now() + (expires_in * 1000),
|
|
361
|
-
sessionId:
|
|
367
|
+
sessionId: session_id || '',
|
|
362
368
|
createdAt: Date.now().toString()
|
|
363
369
|
};
|
|
364
370
|
await this.storage.set(`x-session:${sessionId}`, updatedSession, expires_in);
|
|
@@ -374,11 +380,13 @@ AuthService = __decorate([
|
|
|
374
380
|
const AUTH_SERVICE_ID = Symbol.for('HemiaAuthService');
|
|
375
381
|
|
|
376
382
|
/**
|
|
377
|
-
* Controller
|
|
383
|
+
* Controller AuthController
|
|
378
384
|
* Gestiona automáticamente Login, Callback, Me y Logout.
|
|
379
385
|
*/
|
|
380
|
-
let
|
|
381
|
-
constructor() {
|
|
386
|
+
let AuthController = class AuthController {
|
|
387
|
+
constructor(authService) {
|
|
388
|
+
this.authService = authService;
|
|
389
|
+
}
|
|
382
390
|
async login(req, res) {
|
|
383
391
|
try {
|
|
384
392
|
const autoParam = typeof req.query.auto === 'string' ? req.query.auto : 'false';
|
|
@@ -495,22 +503,18 @@ let AbstractAuthController = class AbstractAuthController {
|
|
|
495
503
|
return res.status(200).json({ success: true });
|
|
496
504
|
}
|
|
497
505
|
};
|
|
498
|
-
__decorate([
|
|
499
|
-
inject(AUTH_SERVICE_ID),
|
|
500
|
-
__metadata("design:type", AuthService)
|
|
501
|
-
], AbstractAuthController.prototype, "authService", void 0);
|
|
502
506
|
__decorate([
|
|
503
507
|
Get('/login'),
|
|
504
508
|
__metadata("design:type", Function),
|
|
505
509
|
__metadata("design:paramtypes", [Object, Object]),
|
|
506
510
|
__metadata("design:returntype", Promise)
|
|
507
|
-
],
|
|
511
|
+
], AuthController.prototype, "login", null);
|
|
508
512
|
__decorate([
|
|
509
513
|
Get('/callback'),
|
|
510
514
|
__metadata("design:type", Function),
|
|
511
515
|
__metadata("design:paramtypes", [Object, Object]),
|
|
512
516
|
__metadata("design:returntype", Promise)
|
|
513
|
-
],
|
|
517
|
+
], AuthController.prototype, "callback", null);
|
|
514
518
|
__decorate([
|
|
515
519
|
Get('/me'),
|
|
516
520
|
__param(0, Req()),
|
|
@@ -518,7 +522,7 @@ __decorate([
|
|
|
518
522
|
__metadata("design:type", Function),
|
|
519
523
|
__metadata("design:paramtypes", [Object, Object]),
|
|
520
524
|
__metadata("design:returntype", Promise)
|
|
521
|
-
],
|
|
525
|
+
], AuthController.prototype, "me", null);
|
|
522
526
|
__decorate([
|
|
523
527
|
Post('/logout'),
|
|
524
528
|
__param(0, Req()),
|
|
@@ -526,11 +530,12 @@ __decorate([
|
|
|
526
530
|
__metadata("design:type", Function),
|
|
527
531
|
__metadata("design:paramtypes", [Object, Object]),
|
|
528
532
|
__metadata("design:returntype", Promise)
|
|
529
|
-
],
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
533
|
+
], AuthController.prototype, "logout", null);
|
|
534
|
+
AuthController = __decorate([
|
|
535
|
+
Controller('/'),
|
|
536
|
+
__param(0, inject(AUTH_SERVICE_ID)),
|
|
537
|
+
__metadata("design:paramtypes", [AuthService])
|
|
538
|
+
], AuthController);
|
|
534
539
|
|
|
535
540
|
class AuthCacheAdapter {
|
|
536
541
|
constructor(externalCache) {
|
|
@@ -547,14 +552,21 @@ class AuthCacheAdapter {
|
|
|
547
552
|
}
|
|
548
553
|
}
|
|
549
554
|
|
|
550
|
-
const
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
555
|
+
const authPlugin = (config, cacheFactory, options) => {
|
|
556
|
+
return async (container) => {
|
|
557
|
+
container.bind(AUTH_SERVICE_ID).toDynamicValue(async (context) => {
|
|
558
|
+
const rawCache = await Promise.resolve(cacheFactory(context));
|
|
559
|
+
const storageAdapter = new AuthCacheAdapter(rawCache);
|
|
560
|
+
const network = new HMNetworkServices(config.ssoBaseUrl);
|
|
561
|
+
const jwt = new JwtManager();
|
|
562
|
+
return new AuthService(config, storageAdapter, network, jwt);
|
|
563
|
+
}).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();
|
|
568
|
+
}
|
|
569
|
+
};
|
|
558
570
|
};
|
|
559
571
|
|
|
560
|
-
export { AUTH_SERVICE_ID,
|
|
572
|
+
export { AUTH_SERVICE_ID, AuthController, AuthService, InvalidTokenFormatError, SessionError, SessionExpiredError, SessionInvalidError, SessionNotFoundError, TokenRefreshFailedError, authPlugin };
|
package/dist/hemia-auth-sdk.js
CHANGED
|
@@ -180,10 +180,10 @@ exports.AuthService = class AuthService {
|
|
|
180
180
|
if (response.status !== 200) {
|
|
181
181
|
throw new common.CustomHttpError('Token exchange failed', response.status, 'token_exchange_failed');
|
|
182
182
|
}
|
|
183
|
-
if (!response.data.access_token) {
|
|
183
|
+
if (!response.data.data?.access_token) {
|
|
184
184
|
throw new common.InternalServerError('No access token received from SSO', 'invalid_token_response');
|
|
185
185
|
}
|
|
186
|
-
const { access_token, refresh_token, id_token, expires_in, session_id } = response.data;
|
|
186
|
+
const { access_token, refresh_token, id_token, expires_in, session_id } = response.data.data;
|
|
187
187
|
const sessionId = crypto.randomBytes(16).toString('hex');
|
|
188
188
|
const sessionData = {
|
|
189
189
|
accessToken: access_token,
|
|
@@ -354,13 +354,19 @@ exports.AuthService = class AuthService {
|
|
|
354
354
|
refreshToken: session.refreshToken,
|
|
355
355
|
sessionId: session.ssoSessionId
|
|
356
356
|
});
|
|
357
|
-
|
|
357
|
+
if (response.status !== 200) {
|
|
358
|
+
throw new common.CustomHttpError('Token refresh failed', response.status, 'token_refresh_failed');
|
|
359
|
+
}
|
|
360
|
+
if (!response.data.data) {
|
|
361
|
+
throw new common.InternalServerError('No token data received from SSO during refresh', 'invalid_token_response');
|
|
362
|
+
}
|
|
363
|
+
const { access_token, refresh_token, id_token, expires_in, session_id } = response.data.data;
|
|
358
364
|
const updatedSession = {
|
|
359
365
|
accessToken: access_token,
|
|
360
366
|
refreshToken: refresh_token || session.refreshToken,
|
|
361
367
|
idToken: id_token || session.idToken,
|
|
362
368
|
expiresAt: Date.now() + (expires_in * 1000),
|
|
363
|
-
sessionId:
|
|
369
|
+
sessionId: session_id || '',
|
|
364
370
|
createdAt: Date.now().toString()
|
|
365
371
|
};
|
|
366
372
|
await this.storage.set(`x-session:${sessionId}`, updatedSession, expires_in);
|
|
@@ -376,11 +382,13 @@ exports.AuthService = __decorate([
|
|
|
376
382
|
const AUTH_SERVICE_ID = Symbol.for('HemiaAuthService');
|
|
377
383
|
|
|
378
384
|
/**
|
|
379
|
-
* Controller
|
|
385
|
+
* Controller AuthController
|
|
380
386
|
* Gestiona automáticamente Login, Callback, Me y Logout.
|
|
381
387
|
*/
|
|
382
|
-
exports.
|
|
383
|
-
constructor() {
|
|
388
|
+
exports.AuthController = class AuthController {
|
|
389
|
+
constructor(authService) {
|
|
390
|
+
this.authService = authService;
|
|
391
|
+
}
|
|
384
392
|
async login(req, res) {
|
|
385
393
|
try {
|
|
386
394
|
const autoParam = typeof req.query.auto === 'string' ? req.query.auto : 'false';
|
|
@@ -497,22 +505,18 @@ exports.AbstractAuthController = class AbstractAuthController {
|
|
|
497
505
|
return res.status(200).json({ success: true });
|
|
498
506
|
}
|
|
499
507
|
};
|
|
500
|
-
__decorate([
|
|
501
|
-
inversify.inject(AUTH_SERVICE_ID),
|
|
502
|
-
__metadata("design:type", exports.AuthService)
|
|
503
|
-
], exports.AbstractAuthController.prototype, "authService", void 0);
|
|
504
508
|
__decorate([
|
|
505
509
|
common.Get('/login'),
|
|
506
510
|
__metadata("design:type", Function),
|
|
507
511
|
__metadata("design:paramtypes", [Object, Object]),
|
|
508
512
|
__metadata("design:returntype", Promise)
|
|
509
|
-
], exports.
|
|
513
|
+
], exports.AuthController.prototype, "login", null);
|
|
510
514
|
__decorate([
|
|
511
515
|
common.Get('/callback'),
|
|
512
516
|
__metadata("design:type", Function),
|
|
513
517
|
__metadata("design:paramtypes", [Object, Object]),
|
|
514
518
|
__metadata("design:returntype", Promise)
|
|
515
|
-
], exports.
|
|
519
|
+
], exports.AuthController.prototype, "callback", null);
|
|
516
520
|
__decorate([
|
|
517
521
|
common.Get('/me'),
|
|
518
522
|
__param(0, common.Req()),
|
|
@@ -520,7 +524,7 @@ __decorate([
|
|
|
520
524
|
__metadata("design:type", Function),
|
|
521
525
|
__metadata("design:paramtypes", [Object, Object]),
|
|
522
526
|
__metadata("design:returntype", Promise)
|
|
523
|
-
], exports.
|
|
527
|
+
], exports.AuthController.prototype, "me", null);
|
|
524
528
|
__decorate([
|
|
525
529
|
common.Post('/logout'),
|
|
526
530
|
__param(0, common.Req()),
|
|
@@ -528,11 +532,12 @@ __decorate([
|
|
|
528
532
|
__metadata("design:type", Function),
|
|
529
533
|
__metadata("design:paramtypes", [Object, Object]),
|
|
530
534
|
__metadata("design:returntype", Promise)
|
|
531
|
-
], exports.
|
|
532
|
-
exports.
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
535
|
+
], exports.AuthController.prototype, "logout", null);
|
|
536
|
+
exports.AuthController = __decorate([
|
|
537
|
+
common.Controller('/'),
|
|
538
|
+
__param(0, inversify.inject(AUTH_SERVICE_ID)),
|
|
539
|
+
__metadata("design:paramtypes", [exports.AuthService])
|
|
540
|
+
], exports.AuthController);
|
|
536
541
|
|
|
537
542
|
class AuthCacheAdapter {
|
|
538
543
|
constructor(externalCache) {
|
|
@@ -549,14 +554,21 @@ class AuthCacheAdapter {
|
|
|
549
554
|
}
|
|
550
555
|
}
|
|
551
556
|
|
|
552
|
-
const
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
557
|
+
const authPlugin = (config, cacheFactory, options) => {
|
|
558
|
+
return async (container) => {
|
|
559
|
+
container.bind(AUTH_SERVICE_ID).toDynamicValue(async (context) => {
|
|
560
|
+
const rawCache = await Promise.resolve(cacheFactory(context));
|
|
561
|
+
const storageAdapter = new AuthCacheAdapter(rawCache);
|
|
562
|
+
const network = new networkServices.HMNetworkServices(config.ssoBaseUrl);
|
|
563
|
+
const jwt = new jwtManager.JwtManager();
|
|
564
|
+
return new exports.AuthService(config, storageAdapter, network, jwt);
|
|
565
|
+
}).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();
|
|
570
|
+
}
|
|
571
|
+
};
|
|
560
572
|
};
|
|
561
573
|
|
|
562
574
|
exports.AUTH_SERVICE_ID = AUTH_SERVICE_ID;
|
|
@@ -566,4 +578,4 @@ exports.SessionExpiredError = SessionExpiredError;
|
|
|
566
578
|
exports.SessionInvalidError = SessionInvalidError;
|
|
567
579
|
exports.SessionNotFoundError = SessionNotFoundError;
|
|
568
580
|
exports.TokenRefreshFailedError = TokenRefreshFailedError;
|
|
569
|
-
exports.
|
|
581
|
+
exports.authPlugin = authPlugin;
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { Request, Response } from "express";
|
|
2
2
|
import { AuthService } from "../services/auth.service";
|
|
3
3
|
/**
|
|
4
|
-
* Controller
|
|
4
|
+
* Controller AuthController
|
|
5
5
|
* Gestiona automáticamente Login, Callback, Me y Logout.
|
|
6
6
|
*/
|
|
7
|
-
export declare
|
|
8
|
-
|
|
9
|
-
constructor();
|
|
7
|
+
export declare class AuthController {
|
|
8
|
+
private readonly authService;
|
|
9
|
+
constructor(authService: AuthService);
|
|
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
package/dist/types/ioc.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ResolutionContext } from "inversify";
|
|
2
2
|
import { IAuthConfig } from "./types";
|
|
3
3
|
import { IHemiaCacheService } from "./adapters";
|
|
4
|
+
import { Plugin } from "@hemia/common";
|
|
4
5
|
export type CacheFactory = (context: ResolutionContext) => Promise<IHemiaCacheService> | IHemiaCacheService;
|
|
5
|
-
export declare const
|
|
6
|
+
export declare const authPlugin: (config: IAuthConfig, cacheFactory: CacheFactory, options: {
|
|
7
|
+
basePath: string;
|
|
8
|
+
}) => Plugin;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hemia/auth-sdk",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.10",
|
|
4
4
|
"description": "Hemia SDK for authentication",
|
|
5
5
|
"main": "dist/hemia-auth-sdk.js",
|
|
6
6
|
"module": "dist/hemia-auth-sdk.esm.js",
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
},
|
|
16
16
|
"devDependencies": {
|
|
17
17
|
"@hemia/cache-manager": "^0.0.5",
|
|
18
|
-
"@hemia/common": "^0.0.
|
|
18
|
+
"@hemia/common": "^0.0.9",
|
|
19
19
|
"@hemia/jwt-manager": "^0.0.4",
|
|
20
20
|
"@hemia/network-services": "^0.0.3",
|
|
21
21
|
"@rollup/plugin-commonjs": "^26.0.1",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
],
|
|
44
44
|
"peerDependencies": {
|
|
45
45
|
"@hemia/cache-manager": "^0.0.5",
|
|
46
|
-
"@hemia/common": "^0.0.
|
|
46
|
+
"@hemia/common": "^0.0.9",
|
|
47
47
|
"@hemia/jwt-manager": "^0.0.4",
|
|
48
48
|
"@hemia/network-services": "^0.0.3",
|
|
49
49
|
"inversify": "^7.11.0",
|