@hemia/auth-sdk 0.0.7 → 0.0.9
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.
|
@@ -238,6 +238,64 @@ let AuthService = class AuthService {
|
|
|
238
238
|
throw new InvalidTokenFormatError();
|
|
239
239
|
}
|
|
240
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 claimsAccess = this.jwtManager.decode(session.accessToken, true);
|
|
270
|
+
if (!claimsAccess) {
|
|
271
|
+
throw new SessionInvalidError();
|
|
272
|
+
}
|
|
273
|
+
const claimsId = this.jwtManager.decode(session.idToken, true);
|
|
274
|
+
return {
|
|
275
|
+
aud: claimsAccess.aud,
|
|
276
|
+
iss: claimsAccess.iss || '',
|
|
277
|
+
exp: claimsAccess.exp || 0,
|
|
278
|
+
iat: claimsAccess.iat || 0,
|
|
279
|
+
sub: claimsAccess.sub || '',
|
|
280
|
+
user: {
|
|
281
|
+
id: claimsId?.sub || '',
|
|
282
|
+
name: claimsId?.name || '',
|
|
283
|
+
email: claimsId?.email || '',
|
|
284
|
+
given_name: claimsId?.given_name,
|
|
285
|
+
family_name: claimsId?.family_name,
|
|
286
|
+
picture: claimsId?.picture
|
|
287
|
+
},
|
|
288
|
+
permissions: claimsAccess.permissions,
|
|
289
|
+
context: {
|
|
290
|
+
...claimsAccess['https://hemia.mx/context'],
|
|
291
|
+
...claimsId?.['https://hemia.mx/context']
|
|
292
|
+
}
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
catch (e) {
|
|
296
|
+
throw new InvalidTokenFormatError();
|
|
297
|
+
}
|
|
298
|
+
}
|
|
241
299
|
/**
|
|
242
300
|
* Cierra la sesión del usuario tanto en el SSO como localmente.
|
|
243
301
|
* @param sessionId Identificador de la sesión
|
package/dist/hemia-auth-sdk.js
CHANGED
|
@@ -240,6 +240,64 @@ exports.AuthService = class AuthService {
|
|
|
240
240
|
throw new InvalidTokenFormatError();
|
|
241
241
|
}
|
|
242
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 claimsAccess = this.jwtManager.decode(session.accessToken, true);
|
|
272
|
+
if (!claimsAccess) {
|
|
273
|
+
throw new SessionInvalidError();
|
|
274
|
+
}
|
|
275
|
+
const claimsId = this.jwtManager.decode(session.idToken, true);
|
|
276
|
+
return {
|
|
277
|
+
aud: claimsAccess.aud,
|
|
278
|
+
iss: claimsAccess.iss || '',
|
|
279
|
+
exp: claimsAccess.exp || 0,
|
|
280
|
+
iat: claimsAccess.iat || 0,
|
|
281
|
+
sub: claimsAccess.sub || '',
|
|
282
|
+
user: {
|
|
283
|
+
id: claimsId?.sub || '',
|
|
284
|
+
name: claimsId?.name || '',
|
|
285
|
+
email: claimsId?.email || '',
|
|
286
|
+
given_name: claimsId?.given_name,
|
|
287
|
+
family_name: claimsId?.family_name,
|
|
288
|
+
picture: claimsId?.picture
|
|
289
|
+
},
|
|
290
|
+
permissions: claimsAccess.permissions,
|
|
291
|
+
context: {
|
|
292
|
+
...claimsAccess['https://hemia.mx/context'],
|
|
293
|
+
...claimsId?.['https://hemia.mx/context']
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
catch (e) {
|
|
298
|
+
throw new InvalidTokenFormatError();
|
|
299
|
+
}
|
|
300
|
+
}
|
|
243
301
|
/**
|
|
244
302
|
* Cierra la sesión del usuario tanto en el SSO como localmente.
|
|
245
303
|
* @param sessionId Identificador de la sesión
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { HMNetworkServices } from '@hemia/network-services';
|
|
2
2
|
import { JwtManager } from '@hemia/jwt-manager';
|
|
3
|
-
import { IAuthConfig, ICallbackResponse, ILoginParams, ISessionStorage, ISessionUser, IStoredState } from '../types';
|
|
3
|
+
import { IAuthConfig, ICallbackResponse, ILoginParams, ISessionStorage, ISessionUser, IStoredState, SessionAccess } from '../types';
|
|
4
4
|
export declare class AuthService {
|
|
5
5
|
private readonly config;
|
|
6
6
|
private readonly storage;
|
|
@@ -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<SessionAccess>;
|
|
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
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface SessionAccess {
|
|
2
|
+
sub: string;
|
|
3
|
+
exp: number;
|
|
4
|
+
iat: number;
|
|
5
|
+
iss: string;
|
|
6
|
+
permissions?: string[];
|
|
7
|
+
context?: any;
|
|
8
|
+
user?: AccessUser;
|
|
9
|
+
[key: string]: any;
|
|
10
|
+
}
|
|
11
|
+
interface AccessUser {
|
|
12
|
+
id: string;
|
|
13
|
+
name?: string;
|
|
14
|
+
email?: string;
|
|
15
|
+
given_name?: string;
|
|
16
|
+
family_name?: string;
|
|
17
|
+
picture?: string;
|
|
18
|
+
[key: string]: any;
|
|
19
|
+
}
|
|
20
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hemia/auth-sdk",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.9",
|
|
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.5",
|
|
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.5",
|
|
47
47
|
"@hemia/jwt-manager": "^0.0.4",
|
|
48
48
|
"@hemia/network-services": "^0.0.3",
|
|
49
49
|
"inversify": "^7.11.0",
|