1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | Object.defineProperty(exports, "__esModule", { value: true });
|
19 | exports.ChannelCredentials = void 0;
|
20 | const tls_1 = require("tls");
|
21 | const call_credentials_1 = require("./call-credentials");
|
22 | const tls_helpers_1 = require("./tls-helpers");
|
23 |
|
24 | function verifyIsBufferOrNull(obj, friendlyName) {
|
25 | if (obj && !(obj instanceof Buffer)) {
|
26 | throw new TypeError(`${friendlyName}, if provided, must be a Buffer.`);
|
27 | }
|
28 | }
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | class ChannelCredentials {
|
35 | constructor(callCredentials) {
|
36 | this.callCredentials = callCredentials || call_credentials_1.CallCredentials.createEmpty();
|
37 | }
|
38 | |
39 |
|
40 |
|
41 | _getCallCredentials() {
|
42 | return this.callCredentials;
|
43 | }
|
44 | |
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | static createSsl(rootCerts, privateKey, certChain, verifyOptions) {
|
54 | var _a;
|
55 | verifyIsBufferOrNull(rootCerts, 'Root certificate');
|
56 | verifyIsBufferOrNull(privateKey, 'Private key');
|
57 | verifyIsBufferOrNull(certChain, 'Certificate chain');
|
58 | if (privateKey && !certChain) {
|
59 | throw new Error('Private key must be given with accompanying certificate chain');
|
60 | }
|
61 | if (!privateKey && certChain) {
|
62 | throw new Error('Certificate chain must be given with accompanying private key');
|
63 | }
|
64 | const secureContext = (0, tls_1.createSecureContext)({
|
65 | ca: (_a = rootCerts !== null && rootCerts !== void 0 ? rootCerts : (0, tls_helpers_1.getDefaultRootsData)()) !== null && _a !== void 0 ? _a : undefined,
|
66 | key: privateKey !== null && privateKey !== void 0 ? privateKey : undefined,
|
67 | cert: certChain !== null && certChain !== void 0 ? certChain : undefined,
|
68 | ciphers: tls_helpers_1.CIPHER_SUITES,
|
69 | });
|
70 | return new SecureChannelCredentialsImpl(secureContext, verifyOptions !== null && verifyOptions !== void 0 ? verifyOptions : {});
|
71 | }
|
72 | |
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | static createFromSecureContext(secureContext, verifyOptions) {
|
83 | return new SecureChannelCredentialsImpl(secureContext, verifyOptions !== null && verifyOptions !== void 0 ? verifyOptions : {});
|
84 | }
|
85 | |
86 |
|
87 |
|
88 | static createInsecure() {
|
89 | return new InsecureChannelCredentialsImpl();
|
90 | }
|
91 | }
|
92 | exports.ChannelCredentials = ChannelCredentials;
|
93 | class InsecureChannelCredentialsImpl extends ChannelCredentials {
|
94 | constructor(callCredentials) {
|
95 | super(callCredentials);
|
96 | }
|
97 | compose(callCredentials) {
|
98 | throw new Error('Cannot compose insecure credentials');
|
99 | }
|
100 | _getConnectionOptions() {
|
101 | return null;
|
102 | }
|
103 | _isSecure() {
|
104 | return false;
|
105 | }
|
106 | _equals(other) {
|
107 | return other instanceof InsecureChannelCredentialsImpl;
|
108 | }
|
109 | }
|
110 | class SecureChannelCredentialsImpl extends ChannelCredentials {
|
111 | constructor(secureContext, verifyOptions) {
|
112 | super();
|
113 | this.secureContext = secureContext;
|
114 | this.verifyOptions = verifyOptions;
|
115 | this.connectionOptions = {
|
116 | secureContext,
|
117 | };
|
118 |
|
119 | if (verifyOptions === null || verifyOptions === void 0 ? void 0 : verifyOptions.checkServerIdentity) {
|
120 | this.connectionOptions.checkServerIdentity =
|
121 | verifyOptions.checkServerIdentity;
|
122 | }
|
123 | }
|
124 | compose(callCredentials) {
|
125 | const combinedCallCredentials = this.callCredentials.compose(callCredentials);
|
126 | return new ComposedChannelCredentialsImpl(this, combinedCallCredentials);
|
127 | }
|
128 | _getConnectionOptions() {
|
129 |
|
130 | return Object.assign({}, this.connectionOptions);
|
131 | }
|
132 | _isSecure() {
|
133 | return true;
|
134 | }
|
135 | _equals(other) {
|
136 | if (this === other) {
|
137 | return true;
|
138 | }
|
139 | if (other instanceof SecureChannelCredentialsImpl) {
|
140 | return (this.secureContext === other.secureContext &&
|
141 | this.verifyOptions.checkServerIdentity ===
|
142 | other.verifyOptions.checkServerIdentity);
|
143 | }
|
144 | else {
|
145 | return false;
|
146 | }
|
147 | }
|
148 | }
|
149 | class ComposedChannelCredentialsImpl extends ChannelCredentials {
|
150 | constructor(channelCredentials, callCreds) {
|
151 | super(callCreds);
|
152 | this.channelCredentials = channelCredentials;
|
153 | }
|
154 | compose(callCredentials) {
|
155 | const combinedCallCredentials = this.callCredentials.compose(callCredentials);
|
156 | return new ComposedChannelCredentialsImpl(this.channelCredentials, combinedCallCredentials);
|
157 | }
|
158 | _getConnectionOptions() {
|
159 | return this.channelCredentials._getConnectionOptions();
|
160 | }
|
161 | _isSecure() {
|
162 | return true;
|
163 | }
|
164 | _equals(other) {
|
165 | if (this === other) {
|
166 | return true;
|
167 | }
|
168 | if (other instanceof ComposedChannelCredentialsImpl) {
|
169 | return (this.channelCredentials._equals(other.channelCredentials) &&
|
170 | this.callCredentials._equals(other.callCredentials));
|
171 | }
|
172 | else {
|
173 | return false;
|
174 | }
|
175 | }
|
176 | }
|
177 |
|
\ | No newline at end of file |