@hemia/auth-sdk 0.0.14 → 0.0.16

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.
@@ -182,7 +182,7 @@ let AuthService = class AuthService {
182
182
  if (!response.data.data?.access_token) {
183
183
  throw new InternalServerError('No access token received from SSO', 'invalid_token_response');
184
184
  }
185
- const { access_token, refresh_token, id_token, expires_in, session_id } = response.data.data;
185
+ const { access_token, refresh_token, id_token, expires_in, session_id, session_expires_in } = response.data.data;
186
186
  const sessionId = randomBytes(16).toString('hex');
187
187
  const sessionData = {
188
188
  accessToken: access_token,
@@ -192,7 +192,8 @@ let AuthService = class AuthService {
192
192
  createdAt: new Date().toISOString(),
193
193
  sessionId: session_id
194
194
  };
195
- await this.storage.set(`x-session:${sessionId}`, sessionData, expires_in);
195
+ const expiresSession = Date.now() + (session_expires_in ? session_expires_in * 1000 : 0);
196
+ await this.storage.set(`x-session:${sessionId}`, sessionData, expiresSession);
196
197
  return {
197
198
  sessionId,
198
199
  expiresIn: expires_in,
@@ -363,7 +364,7 @@ let AuthService = class AuthService {
363
364
  if (!response.data.data) {
364
365
  throw new InternalServerError('No token data received from SSO during refresh', 'invalid_token_response');
365
366
  }
366
- const { access_token, refresh_token, id_token, expires_in, session_id } = response.data.data;
367
+ const { access_token, refresh_token, id_token, expires_in, session_id, session_expires_in } = response.data.data;
367
368
  const updatedSession = {
368
369
  accessToken: access_token,
369
370
  refreshToken: refresh_token || session.refreshToken,
@@ -372,7 +373,8 @@ let AuthService = class AuthService {
372
373
  sessionId: session_id || '',
373
374
  createdAt: Date.now().toString()
374
375
  };
375
- await this.storage.set(`x-session:${sessionId}`, updatedSession, expires_in);
376
+ const expiresSession = Date.now() + (session_expires_in ? session_expires_in * 1000 : 0);
377
+ await this.storage.set(`x-session:${sessionId}`, updatedSession, expiresSession);
376
378
  return updatedSession;
377
379
  }
378
380
  };
@@ -544,6 +546,22 @@ AuthSDKController = __decorate([
544
546
  __metadata("design:paramtypes", [AuthService])
545
547
  ], AuthSDKController);
546
548
 
549
+ let OAuthService = class OAuthService {
550
+ constructor(oauthConfig, networkServices) {
551
+ this.oauthConfig = oauthConfig;
552
+ this.networkServices = networkServices;
553
+ }
554
+ async getByClientId(clientId) {
555
+ const url = `${this.oauthConfig.getByClientIdUrl}/${clientId}`;
556
+ const response = await this.networkServices.get(url);
557
+ return response.data.data;
558
+ }
559
+ };
560
+ OAuthService = __decorate([
561
+ injectable(),
562
+ __metadata("design:paramtypes", [Object, HMNetworkServices])
563
+ ], OAuthService);
564
+
547
565
  class AuthCacheAdapter {
548
566
  constructor(externalCache) {
549
567
  this.externalCache = externalCache;
@@ -575,5 +593,13 @@ const authPlugin = (config, cacheFactory, options) => {
575
593
  }
576
594
  };
577
595
  };
596
+ const oauthPlugin = (config) => {
597
+ return async (container) => {
598
+ container.bind(OAuthService).toDynamicValue((context) => {
599
+ const network = new HMNetworkServices(config.baseUrl);
600
+ return new OAuthService(config, network);
601
+ }).inSingletonScope();
602
+ };
603
+ };
578
604
 
579
- export { AUTH_SERVICE_ID, AuthSDKController, AuthService, InvalidTokenFormatError, SessionError, SessionExpiredError, SessionInvalidError, SessionNotFoundError, TokenRefreshFailedError, authPlugin };
605
+ export { AUTH_SERVICE_ID, AuthSDKController, AuthService, InvalidTokenFormatError, OAuthService, SessionError, SessionExpiredError, SessionInvalidError, SessionNotFoundError, TokenRefreshFailedError, authPlugin, oauthPlugin };
@@ -184,7 +184,7 @@ exports.AuthService = class AuthService {
184
184
  if (!response.data.data?.access_token) {
185
185
  throw new common.InternalServerError('No access token received from SSO', 'invalid_token_response');
186
186
  }
187
- const { access_token, refresh_token, id_token, expires_in, session_id } = response.data.data;
187
+ const { access_token, refresh_token, id_token, expires_in, session_id, session_expires_in } = response.data.data;
188
188
  const sessionId = crypto.randomBytes(16).toString('hex');
189
189
  const sessionData = {
190
190
  accessToken: access_token,
@@ -194,7 +194,8 @@ exports.AuthService = class AuthService {
194
194
  createdAt: new Date().toISOString(),
195
195
  sessionId: session_id
196
196
  };
197
- await this.storage.set(`x-session:${sessionId}`, sessionData, expires_in);
197
+ const expiresSession = Date.now() + (session_expires_in ? session_expires_in * 1000 : 0);
198
+ await this.storage.set(`x-session:${sessionId}`, sessionData, expiresSession);
198
199
  return {
199
200
  sessionId,
200
201
  expiresIn: expires_in,
@@ -365,7 +366,7 @@ exports.AuthService = class AuthService {
365
366
  if (!response.data.data) {
366
367
  throw new common.InternalServerError('No token data received from SSO during refresh', 'invalid_token_response');
367
368
  }
368
- const { access_token, refresh_token, id_token, expires_in, session_id } = response.data.data;
369
+ const { access_token, refresh_token, id_token, expires_in, session_id, session_expires_in } = response.data.data;
369
370
  const updatedSession = {
370
371
  accessToken: access_token,
371
372
  refreshToken: refresh_token || session.refreshToken,
@@ -374,7 +375,8 @@ exports.AuthService = class AuthService {
374
375
  sessionId: session_id || '',
375
376
  createdAt: Date.now().toString()
376
377
  };
377
- await this.storage.set(`x-session:${sessionId}`, updatedSession, expires_in);
378
+ const expiresSession = Date.now() + (session_expires_in ? session_expires_in * 1000 : 0);
379
+ await this.storage.set(`x-session:${sessionId}`, updatedSession, expiresSession);
378
380
  return updatedSession;
379
381
  }
380
382
  };
@@ -546,6 +548,22 @@ exports.AuthSDKController = __decorate([
546
548
  __metadata("design:paramtypes", [exports.AuthService])
547
549
  ], exports.AuthSDKController);
548
550
 
551
+ exports.OAuthService = class OAuthService {
552
+ constructor(oauthConfig, networkServices) {
553
+ this.oauthConfig = oauthConfig;
554
+ this.networkServices = networkServices;
555
+ }
556
+ async getByClientId(clientId) {
557
+ const url = `${this.oauthConfig.getByClientIdUrl}/${clientId}`;
558
+ const response = await this.networkServices.get(url);
559
+ return response.data.data;
560
+ }
561
+ };
562
+ exports.OAuthService = __decorate([
563
+ inversify.injectable(),
564
+ __metadata("design:paramtypes", [Object, networkServices.HMNetworkServices])
565
+ ], exports.OAuthService);
566
+
549
567
  class AuthCacheAdapter {
550
568
  constructor(externalCache) {
551
569
  this.externalCache = externalCache;
@@ -577,6 +595,14 @@ const authPlugin = (config, cacheFactory, options) => {
577
595
  }
578
596
  };
579
597
  };
598
+ const oauthPlugin = (config) => {
599
+ return async (container) => {
600
+ container.bind(exports.OAuthService).toDynamicValue((context) => {
601
+ const network = new networkServices.HMNetworkServices(config.baseUrl);
602
+ return new exports.OAuthService(config, network);
603
+ }).inSingletonScope();
604
+ };
605
+ };
580
606
 
581
607
  exports.AUTH_SERVICE_ID = AUTH_SERVICE_ID;
582
608
  exports.InvalidTokenFormatError = InvalidTokenFormatError;
@@ -586,3 +612,4 @@ exports.SessionInvalidError = SessionInvalidError;
586
612
  exports.SessionNotFoundError = SessionNotFoundError;
587
613
  exports.TokenRefreshFailedError = TokenRefreshFailedError;
588
614
  exports.authPlugin = authPlugin;
615
+ exports.oauthPlugin = oauthPlugin;
@@ -1,8 +1,9 @@
1
1
  import { ResolutionContext } from "inversify";
2
- import { IAuthConfig } from "./types";
2
+ import { IAuthConfig, IOAuthConfig } from "./types";
3
3
  import { IHemiaCacheService } from "./adapters";
4
4
  import { Plugin } from "@hemia/common";
5
5
  export type CacheFactory = (context: ResolutionContext) => Promise<IHemiaCacheService> | IHemiaCacheService;
6
6
  export declare const authPlugin: (config: IAuthConfig, cacheFactory: CacheFactory, options: {
7
7
  basePath: string;
8
8
  }) => Plugin;
9
+ export declare const oauthPlugin: (config: IOAuthConfig) => Plugin;
@@ -1 +1,2 @@
1
1
  export * from "./auth.service";
2
+ export * from "./oauth.service";
@@ -0,0 +1,8 @@
1
+ import { HMNetworkServices } from "@hemia/network-services";
2
+ import { IOAuthConfig, OAuthClientResponse } from "../types";
3
+ export declare class OAuthService {
4
+ private readonly oauthConfig;
5
+ private readonly networkServices;
6
+ constructor(oauthConfig: IOAuthConfig, networkServices: HMNetworkServices);
7
+ getByClientId(clientId: string): Promise<OAuthClientResponse | undefined>;
8
+ }
@@ -10,3 +10,5 @@ export * from "./standard-claims.interface";
10
10
  export * from "./session-data.interface";
11
11
  export * from "./session-storage.interface";
12
12
  export * from "./session-access.interface";
13
+ export * from "./oauth-config.interface";
14
+ export * from "./oauth-client-response.interface";
@@ -0,0 +1,15 @@
1
+ export interface OAuthClientResponse {
2
+ id: string;
3
+ clientId: string;
4
+ name: string;
5
+ type: string;
6
+ logoUri: string;
7
+ grantTypes: string[];
8
+ isActive: boolean;
9
+ redirectUris: string[];
10
+ allowedScopes: string[];
11
+ allowedAudiences: string[];
12
+ createdAt?: Date;
13
+ updatedAt?: Date;
14
+ productId?: string;
15
+ }
@@ -0,0 +1,4 @@
1
+ export interface IOAuthConfig {
2
+ baseUrl: string;
3
+ getByClientIdUrl: string;
4
+ }
@@ -6,4 +6,5 @@ export interface ITokenResponse {
6
6
  id_token?: string;
7
7
  scope?: string;
8
8
  session_id?: string;
9
+ session_expires_in?: number;
9
10
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hemia/auth-sdk",
3
- "version": "0.0.14",
3
+ "version": "0.0.16",
4
4
  "description": "Hemia SDK for authentication",
5
5
  "main": "dist/hemia-auth-sdk.js",
6
6
  "module": "dist/hemia-auth-sdk.esm.js",