UNPKG

6.55 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 */
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 * Return a new ChannelCredentials instance with no credentials.
74 */
75 static createInsecure() {
76 return new InsecureChannelCredentialsImpl();
77 }
78}
79exports.ChannelCredentials = ChannelCredentials;
80class 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}
97class 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 // Copy to prevent callers from mutating this.connectionOptions
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}
150class 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//# sourceMappingURL=channel-credentials.js.map
\No newline at end of file