@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.
- package/dist/index.d.ts +12 -0
- package/dist/index.js +8 -19
- package/dist/interfaces/auth.d.ts +9 -0
- package/dist/interfaces/crypto.d.ts +10 -0
- package/dist/interfaces/deps.d.ts +13 -0
- package/dist/interfaces/identifier.d.ts +10 -0
- package/dist/interfaces/index.d.ts +5 -0
- package/dist/interfaces/jwt.d.ts +25 -0
- package/dist/services/auth.d.ts +41 -0
- package/dist/services/auth.js +90 -113
- package/dist/services/crypto.d.ts +15 -0
- package/dist/services/crypto.js +23 -24
- package/dist/services/hash.d.ts +9 -0
- package/dist/services/hash.js +16 -18
- package/dist/services/identifier.d.ts +12 -0
- package/dist/services/identifier.js +22 -29
- package/dist/services/index.d.ts +6 -0
- package/dist/services/index.js +7 -23
- package/dist/services/jwe.d.ts +17 -0
- package/dist/services/jwe.js +43 -59
- package/dist/services/jwt.d.ts +30 -0
- package/dist/services/jwt.js +40 -81
- package/package.json +33 -22
- package/dist/index.js.map +0 -1
- package/dist/interfaces/auth.js +0 -3
- package/dist/interfaces/auth.js.map +0 -1
- package/dist/interfaces/crypto.js +0 -3
- package/dist/interfaces/crypto.js.map +0 -1
- package/dist/interfaces/deps.js +0 -3
- package/dist/interfaces/deps.js.map +0 -1
- package/dist/interfaces/identifier.js +0 -3
- package/dist/interfaces/identifier.js.map +0 -1
- package/dist/interfaces/index.js +0 -22
- package/dist/interfaces/index.js.map +0 -1
- package/dist/interfaces/jwt.js +0 -3
- package/dist/interfaces/jwt.js.map +0 -1
- package/dist/services/auth.js.map +0 -1
- package/dist/services/crypto.js.map +0 -1
- package/dist/services/hash.js.map +0 -1
- package/dist/services/identifier.js.map +0 -1
- package/dist/services/index.js.map +0 -1
- package/dist/services/jwe.js.map +0 -1
- package/dist/services/jwt.js.map +0 -1
- package/dist/types/index.d.ts +0 -2
- package/dist/types/interfaces/auth.d.ts +0 -5
- package/dist/types/interfaces/crypto.d.ts +0 -7
- package/dist/types/interfaces/deps.d.ts +0 -7
- package/dist/types/interfaces/identifier.d.ts +0 -7
- package/dist/types/interfaces/index.d.ts +0 -5
- package/dist/types/interfaces/jwt.d.ts +0 -21
- package/dist/types/services/auth.d.ts +0 -37
- package/dist/types/services/crypto.d.ts +0 -11
- package/dist/types/services/hash.d.ts +0 -5
- package/dist/types/services/identifier.d.ts +0 -8
- package/dist/types/services/index.d.ts +0 -6
- package/dist/types/services/jwe.d.ts +0 -13
- package/dist/types/services/jwt.d.ts +0 -26
package/dist/index.d.ts
ADDED
|
@@ -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
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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,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,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 };
|
package/dist/services/auth.js
CHANGED
|
@@ -1,113 +1,90 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
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 };
|
package/dist/services/crypto.js
CHANGED
|
@@ -1,24 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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 };
|
package/dist/services/hash.js
CHANGED
|
@@ -1,18 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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 };
|
package/dist/services/index.js
CHANGED
|
@@ -1,23 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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 };
|