UNPKG

7.16 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}
29/**
30 * A class that contains credentials for communicating over a channel, as well
31 * as a set of per-call credentials, which are applied to every method call made
32 * over a channel initialized with an instance of this class.
33 */
34class ChannelCredentials {
35 constructor(callCredentials) {
36 this.callCredentials = callCredentials || call_credentials_1.CallCredentials.createEmpty();
37 }
38 /**
39 * Gets the set of per-call credentials associated with this instance.
40 */
41 _getCallCredentials() {
42 return this.callCredentials;
43 }
44 /**
45 * Return a new ChannelCredentials instance with a given set of credentials.
46 * The resulting instance can be used to construct a Channel that communicates
47 * over TLS.
48 * @param rootCerts The root certificate data.
49 * @param privateKey The client certificate private key, if available.
50 * @param certChain The client certificate key chain, if available.
51 * @param verifyOptions Additional options to modify certificate verification
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 * Return a new ChannelCredentials instance with credentials created using
74 * the provided secureContext. The resulting instances can be used to
75 * construct a Channel that communicates over TLS. gRPC will not override
76 * anything in the provided secureContext, so the environment variables
77 * GRPC_SSL_CIPHER_SUITES and GRPC_DEFAULT_SSL_ROOTS_FILE_PATH will
78 * not be applied.
79 * @param secureContext The return value of tls.createSecureContext()
80 * @param verifyOptions Additional options to modify certificate verification
81 */
82 static createFromSecureContext(secureContext, verifyOptions) {
83 return new SecureChannelCredentialsImpl(secureContext, verifyOptions !== null && verifyOptions !== void 0 ? verifyOptions : {});
84 }
85 /**
86 * Return a new ChannelCredentials instance with no credentials.
87 */
88 static createInsecure() {
89 return new InsecureChannelCredentialsImpl();
90 }
91}
92exports.ChannelCredentials = ChannelCredentials;
93class 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}
110class SecureChannelCredentialsImpl extends ChannelCredentials {
111 constructor(secureContext, verifyOptions) {
112 super();
113 this.secureContext = secureContext;
114 this.verifyOptions = verifyOptions;
115 this.connectionOptions = {
116 secureContext,
117 };
118 // Node asserts that this option is a function, so we cannot pass undefined
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 // Copy to prevent callers from mutating this.connectionOptions
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}
149class 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//# sourceMappingURL=channel-credentials.js.map
\No newline at end of file