@diia-inhouse/crypto 1.20.20 → 2.0.5

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.
Files changed (57) hide show
  1. package/dist/index.d.ts +12 -0
  2. package/dist/index.js +8 -19
  3. package/dist/interfaces/auth.d.ts +9 -0
  4. package/dist/interfaces/crypto.d.ts +10 -0
  5. package/dist/interfaces/deps.d.ts +13 -0
  6. package/dist/interfaces/identifier.d.ts +10 -0
  7. package/dist/interfaces/index.d.ts +5 -0
  8. package/dist/interfaces/jwt.d.ts +25 -0
  9. package/dist/services/auth.d.ts +41 -0
  10. package/dist/services/auth.js +90 -113
  11. package/dist/services/crypto.d.ts +15 -0
  12. package/dist/services/crypto.js +23 -24
  13. package/dist/services/hash.d.ts +9 -0
  14. package/dist/services/hash.js +16 -18
  15. package/dist/services/identifier.d.ts +12 -0
  16. package/dist/services/identifier.js +22 -29
  17. package/dist/services/index.d.ts +6 -0
  18. package/dist/services/index.js +7 -23
  19. package/dist/services/jwe.d.ts +17 -0
  20. package/dist/services/jwe.js +43 -59
  21. package/dist/services/jwt.d.ts +30 -0
  22. package/dist/services/jwt.js +40 -81
  23. package/package.json +33 -22
  24. package/dist/index.js.map +0 -1
  25. package/dist/interfaces/auth.js +0 -3
  26. package/dist/interfaces/auth.js.map +0 -1
  27. package/dist/interfaces/crypto.js +0 -3
  28. package/dist/interfaces/crypto.js.map +0 -1
  29. package/dist/interfaces/deps.js +0 -3
  30. package/dist/interfaces/deps.js.map +0 -1
  31. package/dist/interfaces/identifier.js +0 -3
  32. package/dist/interfaces/identifier.js.map +0 -1
  33. package/dist/interfaces/index.js +0 -22
  34. package/dist/interfaces/index.js.map +0 -1
  35. package/dist/interfaces/jwt.js +0 -3
  36. package/dist/interfaces/jwt.js.map +0 -1
  37. package/dist/services/auth.js.map +0 -1
  38. package/dist/services/crypto.js.map +0 -1
  39. package/dist/services/hash.js.map +0 -1
  40. package/dist/services/identifier.js.map +0 -1
  41. package/dist/services/index.js.map +0 -1
  42. package/dist/services/jwe.js.map +0 -1
  43. package/dist/services/jwt.js.map +0 -1
  44. package/dist/types/index.d.ts +0 -2
  45. package/dist/types/interfaces/auth.d.ts +0 -5
  46. package/dist/types/interfaces/crypto.d.ts +0 -7
  47. package/dist/types/interfaces/deps.d.ts +0 -7
  48. package/dist/types/interfaces/identifier.d.ts +0 -7
  49. package/dist/types/interfaces/index.d.ts +0 -5
  50. package/dist/types/interfaces/jwt.d.ts +0 -21
  51. package/dist/types/services/auth.d.ts +0 -37
  52. package/dist/types/services/crypto.d.ts +0 -11
  53. package/dist/types/services/hash.d.ts +0 -5
  54. package/dist/types/services/identifier.d.ts +0 -8
  55. package/dist/types/services/index.d.ts +0 -6
  56. package/dist/types/services/jwe.d.ts +0 -13
  57. package/dist/types/services/jwt.d.ts +0 -26
@@ -0,0 +1,12 @@
1
+ import { Algorithm, Jwt, JwtHeader, JwtPayload, JwtServiceParams, JwtToken, TokenSignOptions, TokenVerifyOptions } from "./interfaces/jwt.js";
2
+ import { AuthConfig } from "./interfaces/auth.js";
3
+ import { AuthService } from "./services/auth.js";
4
+ import { DocumentDecryptedData, DocumentEncryptedDataResult } from "./interfaces/crypto.js";
5
+ import { HashService } from "./services/hash.js";
6
+ import { CryptoService } from "./services/crypto.js";
7
+ import { JweService } from "./services/jwe.js";
8
+ import { JwtService } from "./services/jwt.js";
9
+ import { IdentifierConfig, IdentifierOps } from "./interfaces/identifier.js";
10
+ import { IdentifierService } from "./services/identifier.js";
11
+ import { CryptoDeps } from "./interfaces/deps.js";
12
+ export { Algorithm, AuthConfig, AuthService, CryptoDeps, CryptoService, DocumentDecryptedData, DocumentEncryptedDataResult, HashService, IdentifierConfig, IdentifierOps, IdentifierService, JweService, Jwt, JwtHeader, JwtPayload, JwtService, JwtServiceParams, JwtToken, TokenSignOptions, TokenVerifyOptions };
package/dist/index.js CHANGED
@@ -1,19 +1,8 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./interfaces"), exports);
18
- __exportStar(require("./services"), exports);
19
- //# sourceMappingURL=index.js.map
1
+ import { JweService } from "./services/jwe.js";
2
+ import { JwtService } from "./services/jwt.js";
3
+ import { AuthService } from "./services/auth.js";
4
+ import { CryptoService } from "./services/crypto.js";
5
+ import { IdentifierService } from "./services/identifier.js";
6
+ import { HashService } from "./services/hash.js";
7
+ import "./services/index.js";
8
+ export { AuthService, CryptoService, HashService, IdentifierService, JweService, JwtService };
@@ -0,0 +1,9 @@
1
+ import { JwtServiceParams } from "./jwt.js";
2
+
3
+ //#region src/interfaces/auth.d.ts
4
+ interface AuthConfig {
5
+ jwk?: string;
6
+ jwt?: JwtServiceParams;
7
+ }
8
+ //#endregion
9
+ export { AuthConfig };
@@ -0,0 +1,10 @@
1
+ //#region src/interfaces/crypto.d.ts
2
+ type DocumentDecryptedData = Record<string, unknown> & {
3
+ id: string;
4
+ };
5
+ interface DocumentEncryptedDataResult {
6
+ hashData?: string;
7
+ encryptedData: string;
8
+ }
9
+ //#endregion
10
+ export { DocumentDecryptedData, DocumentEncryptedDataResult };
@@ -0,0 +1,13 @@
1
+ import { AuthService } from "../services/auth.js";
2
+ import { HashService } from "../services/hash.js";
3
+ import { CryptoService } from "../services/crypto.js";
4
+ import { IdentifierService } from "../services/identifier.js";
5
+ //#region src/interfaces/deps.d.ts
6
+ type CryptoDeps = {
7
+ auth?: AuthService;
8
+ crypto?: CryptoService;
9
+ hash?: HashService;
10
+ identifier?: IdentifierService;
11
+ };
12
+ //#endregion
13
+ export { CryptoDeps };
@@ -0,0 +1,10 @@
1
+ //#region src/interfaces/identifier.d.ts
2
+ interface IdentifierConfig {
3
+ salt: string;
4
+ }
5
+ interface IdentifierOps {
6
+ customSalt?: string;
7
+ prefix?: string;
8
+ }
9
+ //#endregion
10
+ export { IdentifierConfig, IdentifierOps };
@@ -0,0 +1,5 @@
1
+ import { Algorithm, Jwt, JwtHeader, JwtPayload, JwtServiceParams, JwtToken, TokenSignOptions, TokenVerifyOptions } from "./jwt.js";
2
+ import { AuthConfig } from "./auth.js";
3
+ import { DocumentDecryptedData, DocumentEncryptedDataResult } from "./crypto.js";
4
+ import { IdentifierConfig, IdentifierOps } from "./identifier.js";
5
+ import { CryptoDeps } from "./deps.js";
@@ -0,0 +1,25 @@
1
+ import { Jwt as Jwt$1, JwtHeader, JwtPayload as JwtPayload$1 } from "jsonwebtoken";
2
+
3
+ //#region src/interfaces/jwt.d.ts
4
+ interface JwtServiceParams {
5
+ tokenVerifyOptions: TokenVerifyOptions;
6
+ tokenSignOptions?: TokenSignOptions;
7
+ privateKey?: string;
8
+ publicKey?: string;
9
+ }
10
+ interface TokenSignOptions {
11
+ algorithm: string;
12
+ expiresIn: string;
13
+ }
14
+ type Algorithm = "HS256" | "HS384" | "HS512" | "RS256" | "RS384" | "RS512" | "ES256" | "ES384" | "ES512" | "PS256" | "PS384" | "PS512" | "none";
15
+ interface TokenVerifyOptions {
16
+ algorithms: Algorithm[];
17
+ ignoreExpiration: boolean;
18
+ }
19
+ interface JwtToken {
20
+ data: string;
21
+ exp?: number;
22
+ iat?: number;
23
+ }
24
+ //#endregion
25
+ export { Algorithm, type Jwt$1 as Jwt, type JwtHeader, type JwtPayload$1 as JwtPayload, JwtServiceParams, JwtToken, TokenSignOptions, TokenVerifyOptions };
@@ -0,0 +1,41 @@
1
+ import { AuthConfig } from "../interfaces/auth.js";
2
+ import { Jwt, VerifyOptions } from "jsonwebtoken";
3
+ import { KeyObject } from "node:crypto";
4
+ import { AcquirerTokenData, EResidentApplicantTokenData, EResidentTokenData, Logger, OnInit, PartnerTokenData, PortalUserTokenData, ServiceEntranceTokenData, ServiceUserTokenData, SessionType, TemporaryTokenData, TokenData, UserTokenData, VerifiedBaseTokenData } from "@diia-inhouse/types";
5
+
6
+ //#region src/services/auth.d.ts
7
+ declare class AuthService implements OnInit {
8
+ private readonly authConfig;
9
+ private readonly logger;
10
+ private jwe;
11
+ private jwt;
12
+ constructor(authConfig: AuthConfig, logger: Logger);
13
+ onInit(): Promise<void>;
14
+ newInstance(authConfig: AuthConfig, logger: Logger): AuthService;
15
+ /** Decodes JWT token and decrypts JWE payload */
16
+ decodeToken<T = TokenData>(token: string, shouldDecrypt?: boolean): Promise<T | null>;
17
+ /** Only decodes JWT token returning complete token data (header, payload, signature) */
18
+ decodeTokenComplete(token: string): Jwt | null;
19
+ verifyToken<T>(token: string, publicKey: KeyObject, options?: VerifyOptions & {
20
+ complete?: false;
21
+ }): T;
22
+ getJweInJwt(data: unknown, expiresIn?: string): Promise<string>;
23
+ getJWT(enc: string, expiresIn?: string): Promise<string>;
24
+ getJWTPayload(payload: string | Buffer | object, expiresIn?: string): Promise<string>;
25
+ encryptJWE(data: unknown): Promise<string>;
26
+ decryptJWE<T = TokenData>(data: string): Promise<T>;
27
+ validate(authToken: string | null, tokenSessionType: SessionType.Acquirer, mobileUid?: string, skipJwtVerification?: boolean): Promise<VerifiedBaseTokenData<AcquirerTokenData>>;
28
+ validate(authToken: string | null, tokenSessionType: SessionType.ServiceUser, mobileUid?: string, skipJwtVerification?: boolean): Promise<VerifiedBaseTokenData<ServiceUserTokenData>>;
29
+ validate(authToken: string | null, tokenSessionType: SessionType.ServiceEntrance, mobileUid?: string, skipJwtVerification?: boolean): Promise<VerifiedBaseTokenData<ServiceEntranceTokenData>>;
30
+ validate(authToken: string | null, tokenSessionType: SessionType.Temporary, mobileUid?: string, skipJwtVerification?: boolean): Promise<VerifiedBaseTokenData<TemporaryTokenData>>;
31
+ validate(authToken: string | null, tokenSessionType: SessionType.EResident, mobileUid?: string, skipJwtVerification?: boolean): Promise<VerifiedBaseTokenData<EResidentTokenData>>;
32
+ validate(authToken: string | null, tokenSessionType: SessionType.EResidentApplicant, mobileUid?: string, skipJwtVerification?: boolean): Promise<VerifiedBaseTokenData<EResidentApplicantTokenData>>;
33
+ validate(authToken: string | null, tokenSessionType: SessionType.PortalUser, mobileUid?: string, skipJwtVerification?: boolean): Promise<VerifiedBaseTokenData<PortalUserTokenData>>;
34
+ validate(authToken: string | null, tokenSessionType: SessionType.Partner, mobileUid?: string, skipJwtVerification?: boolean): Promise<VerifiedBaseTokenData<PartnerTokenData>>;
35
+ validate(authToken: string | null, tokenSessionType: SessionType.User, mobileUid?: string, skipJwtVerification?: boolean): Promise<VerifiedBaseTokenData<UserTokenData>>;
36
+ validate<T extends TokenData = TokenData>(authToken: string | null, allowedSessionTypes: SessionType | SessionType[], mobileUid?: string, skipJwtVerification?: boolean): Promise<VerifiedBaseTokenData<T>>;
37
+ private getJwtService;
38
+ private getJweService;
39
+ }
40
+ //#endregion
41
+ export { AuthService };
@@ -1,113 +1,90 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AuthService = void 0;
4
- const errors_1 = require("@diia-inhouse/errors");
5
- const utils_1 = require("@diia-inhouse/utils");
6
- const jwe_1 = require("./jwe");
7
- const jwt_1 = require("./jwt");
8
- class AuthService {
9
- authConfig;
10
- logger;
11
- jwe = null;
12
- jwt = null;
13
- constructor(authConfig, logger) {
14
- this.authConfig = authConfig;
15
- this.logger = logger;
16
- const { jwk, jwt } = this.authConfig;
17
- if (jwk) {
18
- this.jwe = new jwe_1.JweService(jwk, logger);
19
- }
20
- if (jwt) {
21
- this.jwt = new jwt_1.JwtService(jwt);
22
- }
23
- }
24
- async onInit() {
25
- if (this.jwe) {
26
- await this.jwe.onInit();
27
- }
28
- }
29
- newInstance(authConfig, logger) {
30
- return new AuthService(authConfig, logger);
31
- }
32
- /** Decodes JWT token and decrypts JWE payload */
33
- async decodeToken(token, shouldDecrypt = true) {
34
- const payload = this.getJwtService().decode(token);
35
- if (!shouldDecrypt) {
36
- return payload;
37
- }
38
- if (payload) {
39
- return await this.decryptJWE(payload.data);
40
- }
41
- return null;
42
- }
43
- /** Only decodes JWT token returning complete token data (header, payload, signature) */
44
- decodeTokenComplete(token) {
45
- return this.getJwtService().decodeWithOptions(token, { complete: true });
46
- }
47
- verifyToken(token, publicKey, options) {
48
- const service = this.getJwtService();
49
- const tokenData = service.verifyWithOptions(token, publicKey, options);
50
- return tokenData;
51
- }
52
- async getJweInJwt(data, expiresIn) {
53
- const enc = await this.encryptJWE(data);
54
- return await this.getJWT(enc, expiresIn);
55
- }
56
- async getJWT(enc, expiresIn) {
57
- return this.getJwtService().sign(enc, expiresIn);
58
- }
59
- async getJWTPayload(payload, expiresIn) {
60
- return this.getJwtService().signPayload(payload, expiresIn);
61
- }
62
- async encryptJWE(data) {
63
- return await this.getJweService().encryptJWE(data);
64
- }
65
- async decryptJWE(data) {
66
- return await this.getJweService().decryptJWE(data);
67
- }
68
- async validate(authToken, allowedSessionTypes, mobileUid, skipJwtVerification) {
69
- try {
70
- if (!authToken) {
71
- throw new errors_1.UnauthorizedError();
72
- }
73
- const { data, exp, iat } = skipJwtVerification ? this.getJwtService().decode(authToken) : this.getJwtService().verify(authToken);
74
- if (!data) {
75
- throw new errors_1.UnauthorizedError();
76
- }
77
- const tokenData = await this.decryptJWE(data);
78
- const { sessionType: tokenSessionType } = tokenData;
79
- const sessionTypes = Array.isArray(allowedSessionTypes) ? allowedSessionTypes : [allowedSessionTypes];
80
- if (!sessionTypes.includes(tokenSessionType)) {
81
- this.logger.log(`Invalid session type: actual - ${tokenSessionType}, expected - ${JSON.stringify(sessionTypes)}`);
82
- throw new errors_1.UnauthorizedError(`Invalid session type`);
83
- }
84
- if (mobileUid && 'mobileUid' in tokenData && tokenData.mobileUid !== mobileUid) {
85
- throw new errors_1.UnauthorizedError(`Mobile uid does not match: actual - ${tokenData.mobileUid}, expected - ${mobileUid}`);
86
- }
87
- utils_1.asserts.isRefreshTokenExists(tokenData);
88
- return { ...tokenData, exp, iat };
89
- }
90
- catch (err) {
91
- this.logger.error('Failed to validate verified JWT', { err });
92
- let errMessage = '';
93
- if (err instanceof Error && err.message === 'jwt malformed') {
94
- errMessage = 'Invalid token';
95
- }
96
- throw new errors_1.UnauthorizedError(errMessage);
97
- }
98
- }
99
- getJwtService() {
100
- if (!this.jwt) {
101
- throw new Error('Jwt config is not provided');
102
- }
103
- return this.jwt;
104
- }
105
- getJweService() {
106
- if (!this.jwe) {
107
- throw new Error('JWK is not provided');
108
- }
109
- return this.jwe;
110
- }
111
- }
112
- exports.AuthService = AuthService;
113
- //# sourceMappingURL=auth.js.map
1
+ import { JweService } from "./jwe.js";
2
+ import { JwtService } from "./jwt.js";
3
+ import { UnauthorizedError } from "@diia-inhouse/errors";
4
+ import { asserts } from "@diia-inhouse/utils";
5
+ //#region src/services/auth.ts
6
+ var AuthService = class AuthService {
7
+ authConfig;
8
+ logger;
9
+ jwe = null;
10
+ jwt = null;
11
+ constructor(authConfig, logger) {
12
+ this.authConfig = authConfig;
13
+ this.logger = logger;
14
+ const { jwk, jwt } = this.authConfig;
15
+ if (jwk) this.jwe = new JweService(jwk, logger);
16
+ if (jwt) this.jwt = new JwtService(jwt);
17
+ }
18
+ async onInit() {
19
+ if (this.jwe) await this.jwe.onInit();
20
+ }
21
+ newInstance(authConfig, logger) {
22
+ return new AuthService(authConfig, logger);
23
+ }
24
+ /** Decodes JWT token and decrypts JWE payload */
25
+ async decodeToken(token, shouldDecrypt = true) {
26
+ const payload = this.getJwtService().decode(token);
27
+ if (!shouldDecrypt) return payload;
28
+ if (payload) return await this.decryptJWE(payload.data);
29
+ return null;
30
+ }
31
+ /** Only decodes JWT token returning complete token data (header, payload, signature) */
32
+ decodeTokenComplete(token) {
33
+ return this.getJwtService().decodeWithOptions(token, { complete: true });
34
+ }
35
+ verifyToken(token, publicKey, options) {
36
+ return this.getJwtService().verifyWithOptions(token, publicKey, options);
37
+ }
38
+ async getJweInJwt(data, expiresIn) {
39
+ const enc = await this.encryptJWE(data);
40
+ return await this.getJWT(enc, expiresIn);
41
+ }
42
+ async getJWT(enc, expiresIn) {
43
+ return this.getJwtService().sign(enc, expiresIn);
44
+ }
45
+ async getJWTPayload(payload, expiresIn) {
46
+ return this.getJwtService().signPayload(payload, expiresIn);
47
+ }
48
+ async encryptJWE(data) {
49
+ return await this.getJweService().encryptJWE(data);
50
+ }
51
+ async decryptJWE(data) {
52
+ return await this.getJweService().decryptJWE(data);
53
+ }
54
+ async validate(authToken, allowedSessionTypes, mobileUid, skipJwtVerification) {
55
+ try {
56
+ if (!authToken) throw new UnauthorizedError();
57
+ const { data, exp, iat } = skipJwtVerification ? this.getJwtService().decode(authToken) : this.getJwtService().verify(authToken);
58
+ if (!data) throw new UnauthorizedError();
59
+ const tokenData = await this.decryptJWE(data);
60
+ const { sessionType: tokenSessionType } = tokenData;
61
+ const sessionTypes = Array.isArray(allowedSessionTypes) ? allowedSessionTypes : [allowedSessionTypes];
62
+ if (!sessionTypes.includes(tokenSessionType)) {
63
+ this.logger.log(`Invalid session type: actual - ${tokenSessionType}, expected - ${JSON.stringify(sessionTypes)}`);
64
+ throw new UnauthorizedError(`Invalid session type`);
65
+ }
66
+ if (mobileUid && "mobileUid" in tokenData && tokenData.mobileUid !== mobileUid) throw new UnauthorizedError(`Mobile uid does not match: actual - ${tokenData.mobileUid}, expected - ${mobileUid}`);
67
+ asserts.isRefreshTokenExists(tokenData);
68
+ return {
69
+ ...tokenData,
70
+ exp,
71
+ iat
72
+ };
73
+ } catch (err) {
74
+ this.logger.error("Failed to validate verified JWT", { err });
75
+ let errMessage = "";
76
+ if (err instanceof Error && err.message === "jwt malformed") errMessage = "Invalid token";
77
+ throw new UnauthorizedError(errMessage);
78
+ }
79
+ }
80
+ getJwtService() {
81
+ if (!this.jwt) throw new Error("Jwt config is not provided");
82
+ return this.jwt;
83
+ }
84
+ getJweService() {
85
+ if (!this.jwe) throw new Error("JWK is not provided");
86
+ return this.jwe;
87
+ }
88
+ };
89
+ //#endregion
90
+ export { AuthService };
@@ -0,0 +1,15 @@
1
+ import { AuthService } from "./auth.js";
2
+ import { DocumentDecryptedData, DocumentEncryptedDataResult } from "../interfaces/crypto.js";
3
+ import { HashService } from "./hash.js";
4
+
5
+ //#region src/services/crypto.d.ts
6
+ declare class CryptoService {
7
+ private readonly auth;
8
+ private readonly hash;
9
+ constructor(auth: AuthService, hash: HashService);
10
+ encryptData(dataToEncrypt: DocumentDecryptedData | string): Promise<DocumentEncryptedDataResult>;
11
+ generateHashData(dataToEncrypt: DocumentDecryptedData): string;
12
+ decryptData<T>(encryptedData: string): Promise<T>;
13
+ }
14
+ //#endregion
15
+ export { CryptoService };
@@ -1,24 +1,23 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CryptoService = void 0;
4
- class CryptoService {
5
- auth;
6
- hash;
7
- constructor(auth, hash) {
8
- this.auth = auth;
9
- this.hash = hash;
10
- }
11
- async encryptData(dataToEncrypt) {
12
- const hashData = typeof dataToEncrypt === 'object' ? this.generateHashData(dataToEncrypt) : undefined;
13
- const encryptedData = await this.auth.encryptJWE(dataToEncrypt);
14
- return { hashData, encryptedData };
15
- }
16
- generateHashData(dataToEncrypt) {
17
- return this.hash.hmac(JSON.stringify(dataToEncrypt), dataToEncrypt.id);
18
- }
19
- async decryptData(encryptedData) {
20
- return this.auth.decryptJWE(encryptedData);
21
- }
22
- }
23
- exports.CryptoService = CryptoService;
24
- //# sourceMappingURL=crypto.js.map
1
+ //#region src/services/crypto.ts
2
+ var CryptoService = class {
3
+ auth;
4
+ hash;
5
+ constructor(auth, hash) {
6
+ this.auth = auth;
7
+ this.hash = hash;
8
+ }
9
+ async encryptData(dataToEncrypt) {
10
+ return {
11
+ hashData: typeof dataToEncrypt === "object" ? this.generateHashData(dataToEncrypt) : void 0,
12
+ encryptedData: await this.auth.encryptJWE(dataToEncrypt)
13
+ };
14
+ }
15
+ generateHashData(dataToEncrypt) {
16
+ return this.hash.hmac(JSON.stringify(dataToEncrypt), dataToEncrypt.id);
17
+ }
18
+ async decryptData(encryptedData) {
19
+ return this.auth.decryptJWE(encryptedData);
20
+ }
21
+ };
22
+ //#endregion
23
+ export { CryptoService };
@@ -0,0 +1,9 @@
1
+ import { BinaryToTextEncoding } from "node:crypto";
2
+
3
+ //#region src/services/hash.d.ts
4
+ declare class HashService {
5
+ hmac(data: string, secret: string, algorithm?: string, encodeTo?: BinaryToTextEncoding): string;
6
+ hash(data: string, algorithm?: string, encodeTo?: BinaryToTextEncoding): string;
7
+ }
8
+ //#endregion
9
+ export { HashService };
@@ -1,18 +1,16 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.HashService = void 0;
4
- const node_crypto_1 = require("node:crypto");
5
- class HashService {
6
- hmac(data, secret, algorithm = 'sha512', encodeTo = 'hex') {
7
- const hash = (0, node_crypto_1.createHmac)(algorithm, secret);
8
- hash.update(data);
9
- return hash.digest(encodeTo);
10
- }
11
- hash(data, algorithm = 'sha512', encodeTo = 'hex') {
12
- const hash = (0, node_crypto_1.createHash)(algorithm);
13
- hash.update(data);
14
- return hash.digest(encodeTo);
15
- }
16
- }
17
- exports.HashService = HashService;
18
- //# sourceMappingURL=hash.js.map
1
+ import { createHash, createHmac } from "node:crypto";
2
+ //#region src/services/hash.ts
3
+ var HashService = class {
4
+ hmac(data, secret, algorithm = "sha512", encodeTo = "hex") {
5
+ const hash = createHmac(algorithm, secret);
6
+ hash.update(data);
7
+ return hash.digest(encodeTo);
8
+ }
9
+ hash(data, algorithm = "sha512", encodeTo = "hex") {
10
+ const hash = createHash(algorithm);
11
+ hash.update(data);
12
+ return hash.digest(encodeTo);
13
+ }
14
+ };
15
+ //#endregion
16
+ export { HashService };
@@ -0,0 +1,12 @@
1
+ import { IdentifierConfig, IdentifierOps } from "../interfaces/identifier.js";
2
+ import { SessionType } from "@diia-inhouse/types";
3
+
4
+ //#region src/services/identifier.d.ts
5
+ declare class IdentifierService {
6
+ private readonly salt;
7
+ constructor(identifierConfig: IdentifierConfig);
8
+ createIdentifier(itn: string, ops?: IdentifierOps): string;
9
+ getSessionTypeFromIdentifier(userIdentifier: string): SessionType;
10
+ }
11
+ //#endregion
12
+ export { IdentifierService };
@@ -1,29 +1,22 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.IdentifierService = void 0;
4
- const node_crypto_1 = require("node:crypto");
5
- const types_1 = require("@diia-inhouse/types");
6
- class IdentifierService {
7
- salt;
8
- constructor(identifierConfig) {
9
- this.salt = identifierConfig.salt;
10
- }
11
- createIdentifier(itn, ops = {}) {
12
- const { customSalt, prefix = '' } = ops;
13
- const hmac = (0, node_crypto_1.createHmac)('sha512', customSalt || this.salt);
14
- hmac.update(itn);
15
- const hash = hmac.digest('hex');
16
- return `${prefix}${hash}`;
17
- }
18
- getSessionTypeFromIdentifier(userIdentifier) {
19
- if (userIdentifier.startsWith(types_1.IdentifierPrefix.EResidentApplicant)) {
20
- return types_1.SessionType.EResidentApplicant;
21
- }
22
- if (userIdentifier.startsWith(types_1.IdentifierPrefix.EResident)) {
23
- return types_1.SessionType.EResident;
24
- }
25
- return types_1.SessionType.User;
26
- }
27
- }
28
- exports.IdentifierService = IdentifierService;
29
- //# sourceMappingURL=identifier.js.map
1
+ import { createHmac } from "node:crypto";
2
+ import { IdentifierPrefix, SessionType } from "@diia-inhouse/types";
3
+ //#region src/services/identifier.ts
4
+ var IdentifierService = class {
5
+ salt;
6
+ constructor(identifierConfig) {
7
+ this.salt = identifierConfig.salt;
8
+ }
9
+ createIdentifier(itn, ops = {}) {
10
+ const { customSalt, prefix = "" } = ops;
11
+ const hmac = createHmac("sha512", customSalt || this.salt);
12
+ hmac.update(itn);
13
+ return `${prefix}${hmac.digest("hex")}`;
14
+ }
15
+ getSessionTypeFromIdentifier(userIdentifier) {
16
+ if (userIdentifier.startsWith(IdentifierPrefix.EResidentApplicant)) return SessionType.EResidentApplicant;
17
+ if (userIdentifier.startsWith(IdentifierPrefix.EResident)) return SessionType.EResident;
18
+ return SessionType.User;
19
+ }
20
+ };
21
+ //#endregion
22
+ export { IdentifierService };
@@ -0,0 +1,6 @@
1
+ import { AuthService } from "./auth.js";
2
+ import { HashService } from "./hash.js";
3
+ import { CryptoService } from "./crypto.js";
4
+ import { JweService } from "./jwe.js";
5
+ import { JwtService } from "./jwt.js";
6
+ import { IdentifierService } from "./identifier.js";
@@ -1,23 +1,7 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./auth"), exports);
18
- __exportStar(require("./crypto"), exports);
19
- __exportStar(require("./jwe"), exports);
20
- __exportStar(require("./jwt"), exports);
21
- __exportStar(require("./identifier"), exports);
22
- __exportStar(require("./hash"), exports);
23
- //# sourceMappingURL=index.js.map
1
+ import "./jwe.js";
2
+ import "./jwt.js";
3
+ import "./auth.js";
4
+ import "./crypto.js";
5
+ import "./identifier.js";
6
+ import "./hash.js";
7
+ export {};
@@ -0,0 +1,17 @@
1
+ import { JwtToken } from "../interfaces/jwt.js";
2
+ import { Logger, OnInit, TokenData } from "@diia-inhouse/types";
3
+
4
+ //#region src/services/jwe.d.ts
5
+ declare class JweService implements OnInit {
6
+ private jwkSecretData;
7
+ private logger;
8
+ private key?;
9
+ constructor(jwkSecretData: string, logger: Logger);
10
+ onInit(): Promise<void>;
11
+ encryptJWE(tokenData: unknown): Promise<string>;
12
+ decryptJWE<T = TokenData>(data: JwtToken["data"]): Promise<T>;
13
+ private handleUriData;
14
+ private getKey;
15
+ }
16
+ //#endregion
17
+ export { JweService };