UNPKG

7.31 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright 2019 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18Object.defineProperty(exports, "__esModule", { value: true });
19exports.ChannelCredentials = void 0;
20const tls_1 = require("tls");
21const call_credentials_1 = require("./call-credentials");
22const tls_helpers_1 = require("./tls-helpers");
23// eslint-disable-next-line @typescript-eslint/no-explicit-any
24function verifyIsBufferOrNull(obj, friendlyName) {
25 if (obj && !(obj instanceof Buffer)) {
26 throw new TypeError(`${friendlyName}, if provided, must be a Buffer.`);
27 }
28}
29function 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 * A class that contains credentials for communicating over a channel, as well
39 * as a set of per-call credentials, which are applied to every method call made
40 * over a channel initialized with an instance of this class.
41 */
42class ChannelCredentials {
43 constructor(callCredentials) {
44 this.callCredentials = callCredentials || call_credentials_1.CallCredentials.createEmpty();
45 }
46 /**
47 * Gets the set of per-call credentials associated with this instance.
48 */
49 _getCallCredentials() {
50 return this.callCredentials;
51 }
52 /**
53 * Return a new ChannelCredentials instance with a given set of credentials.
54 * The resulting instance can be used to construct a Channel that communicates
55 * over TLS.
56 * @param rootCerts The root certificate data.
57 * @param privateKey The client certificate private key, if available.
58 * @param certChain The client certificate key chain, if available.
59 * @param verifyOptions Additional options to modify certificate verification
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 * Return a new ChannelCredentials instance with credentials created using
82 * the provided secureContext. The resulting instances can be used to
83 * construct a Channel that communicates over TLS. gRPC will not override
84 * anything in the provided secureContext, so the environment variables
85 * GRPC_SSL_CIPHER_SUITES and GRPC_DEFAULT_SSL_ROOTS_FILE_PATH will
86 * not be applied.
87 * @param secureContext The return value of tls.createSecureContext()
88 * @param verifyOptions Additional options to modify certificate verification
89 */
90 static createFromSecureContext(secureContext, verifyOptions) {
91 return new SecureChannelCredentialsImpl(secureContext, verifyOptions !== null && verifyOptions !== void 0 ? verifyOptions : {});
92 }
93 /**
94 * Return a new ChannelCredentials instance with no credentials.
95 */
96 static createInsecure() {
97 return new InsecureChannelCredentialsImpl();
98 }
99}
100exports.ChannelCredentials = ChannelCredentials;
101class 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}
118class SecureChannelCredentialsImpl extends ChannelCredentials {
119 constructor(secureContext, verifyOptions) {
120 super();
121 this.secureContext = secureContext;
122 this.verifyOptions = verifyOptions;
123 this.connectionOptions = {
124 secureContext
125 };
126 // Node asserts that this option is a function, so we cannot pass undefined
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 // Copy to prevent callers from mutating this.connectionOptions
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}
155class 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//# sourceMappingURL=channel-credentials.js.map
\No newline at end of file