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