@hemia/auth-sdk 0.0.9 → 0.0.11
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, 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';
|
|
@@ -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 AuthSDKController
|
|
378
384
|
* Gestiona automáticamente Login, Callback, Me y Logout.
|
|
379
385
|
*/
|
|
380
|
-
let
|
|
381
|
-
constructor() {
|
|
386
|
+
let AuthSDKController = class AuthSDKController {
|
|
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
|
+
], AuthSDKController.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
|
+
], AuthSDKController.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
|
+
], AuthSDKController.prototype, "me", null);
|
|
522
526
|
__decorate([
|
|
523
527
|
Post('/logout'),
|
|
524
528
|
__param(0, Req()),
|
|
@@ -526,11 +530,13 @@ __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
|
+
], AuthSDKController.prototype, "logout", null);
|
|
534
|
+
AuthSDKController = __decorate([
|
|
535
|
+
Controller('/'),
|
|
536
|
+
ManualRegister(),
|
|
537
|
+
__param(0, inject(AUTH_SERVICE_ID)),
|
|
538
|
+
__metadata("design:paramtypes", [AuthService])
|
|
539
|
+
], AuthSDKController);
|
|
534
540
|
|
|
535
541
|
class AuthCacheAdapter {
|
|
536
542
|
constructor(externalCache) {
|
|
@@ -547,14 +553,21 @@ class AuthCacheAdapter {
|
|
|
547
553
|
}
|
|
548
554
|
}
|
|
549
555
|
|
|
550
|
-
const
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
556
|
+
const authPlugin = (config, cacheFactory, options) => {
|
|
557
|
+
return async (container) => {
|
|
558
|
+
container.bind(AUTH_SERVICE_ID).toDynamicValue(async (context) => {
|
|
559
|
+
const rawCache = await Promise.resolve(cacheFactory(context));
|
|
560
|
+
const storageAdapter = new AuthCacheAdapter(rawCache);
|
|
561
|
+
const network = new HMNetworkServices(config.ssoBaseUrl);
|
|
562
|
+
const jwt = new JwtManager();
|
|
563
|
+
return new AuthService(config, storageAdapter, network, jwt);
|
|
564
|
+
}).inSingletonScope();
|
|
565
|
+
Reflect.defineMetadata(METADATA_KEYS.BASE_PATH, options.basePath, AuthSDKController);
|
|
566
|
+
ControllerRegistry.register(AuthSDKController);
|
|
567
|
+
if (!container.isBound(AuthSDKController)) {
|
|
568
|
+
container.bind(AuthSDKController).toSelf().inSingletonScope();
|
|
569
|
+
}
|
|
570
|
+
};
|
|
558
571
|
};
|
|
559
572
|
|
|
560
|
-
export { AUTH_SERVICE_ID,
|
|
573
|
+
export { AUTH_SERVICE_ID, AuthSDKController, 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 AuthSDKController
|
|
380
386
|
* Gestiona automáticamente Login, Callback, Me y Logout.
|
|
381
387
|
*/
|
|
382
|
-
exports.
|
|
383
|
-
constructor() {
|
|
388
|
+
exports.AuthSDKController = class AuthSDKController {
|
|
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.AuthSDKController.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.AuthSDKController.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.AuthSDKController.prototype, "me", null);
|
|
524
528
|
__decorate([
|
|
525
529
|
common.Post('/logout'),
|
|
526
530
|
__param(0, common.Req()),
|
|
@@ -528,11 +532,13 @@ __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.AuthSDKController.prototype, "logout", null);
|
|
536
|
+
exports.AuthSDKController = __decorate([
|
|
537
|
+
common.Controller('/'),
|
|
538
|
+
common.ManualRegister(),
|
|
539
|
+
__param(0, inversify.inject(AUTH_SERVICE_ID)),
|
|
540
|
+
__metadata("design:paramtypes", [exports.AuthService])
|
|
541
|
+
], exports.AuthSDKController);
|
|
536
542
|
|
|
537
543
|
class AuthCacheAdapter {
|
|
538
544
|
constructor(externalCache) {
|
|
@@ -549,14 +555,21 @@ class AuthCacheAdapter {
|
|
|
549
555
|
}
|
|
550
556
|
}
|
|
551
557
|
|
|
552
|
-
const
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
558
|
+
const authPlugin = (config, cacheFactory, options) => {
|
|
559
|
+
return async (container) => {
|
|
560
|
+
container.bind(AUTH_SERVICE_ID).toDynamicValue(async (context) => {
|
|
561
|
+
const rawCache = await Promise.resolve(cacheFactory(context));
|
|
562
|
+
const storageAdapter = new AuthCacheAdapter(rawCache);
|
|
563
|
+
const network = new networkServices.HMNetworkServices(config.ssoBaseUrl);
|
|
564
|
+
const jwt = new jwtManager.JwtManager();
|
|
565
|
+
return new exports.AuthService(config, storageAdapter, network, jwt);
|
|
566
|
+
}).inSingletonScope();
|
|
567
|
+
Reflect.defineMetadata(common.METADATA_KEYS.BASE_PATH, options.basePath, exports.AuthSDKController);
|
|
568
|
+
common.ControllerRegistry.register(exports.AuthSDKController);
|
|
569
|
+
if (!container.isBound(exports.AuthSDKController)) {
|
|
570
|
+
container.bind(exports.AuthSDKController).toSelf().inSingletonScope();
|
|
571
|
+
}
|
|
572
|
+
};
|
|
560
573
|
};
|
|
561
574
|
|
|
562
575
|
exports.AUTH_SERVICE_ID = AUTH_SERVICE_ID;
|
|
@@ -566,4 +579,4 @@ exports.SessionExpiredError = SessionExpiredError;
|
|
|
566
579
|
exports.SessionInvalidError = SessionInvalidError;
|
|
567
580
|
exports.SessionNotFoundError = SessionNotFoundError;
|
|
568
581
|
exports.TokenRefreshFailedError = TokenRefreshFailedError;
|
|
569
|
-
exports.
|
|
582
|
+
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 AuthSDKController
|
|
5
5
|
* Gestiona automáticamente Login, Callback, Me y Logout.
|
|
6
6
|
*/
|
|
7
|
-
export declare
|
|
8
|
-
|
|
9
|
-
constructor();
|
|
7
|
+
export declare class AuthSDKController {
|
|
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.11",
|
|
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.
|
|
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,7 +44,7 @@
|
|
|
43
44
|
],
|
|
44
45
|
"peerDependencies": {
|
|
45
46
|
"@hemia/cache-manager": "^0.0.5",
|
|
46
|
-
"@hemia/common": "^0.0.
|
|
47
|
+
"@hemia/common": "^0.0.10",
|
|
47
48
|
"@hemia/jwt-manager": "^0.0.4",
|
|
48
49
|
"@hemia/network-services": "^0.0.3",
|
|
49
50
|
"inversify": "^7.11.0",
|