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