UNPKG

2.83 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.ServerCredentials = void 0;
20const tls_helpers_1 = require("./tls-helpers");
21class ServerCredentials {
22 static createInsecure() {
23 return new InsecureServerCredentials();
24 }
25 static createSsl(rootCerts, keyCertPairs, checkClientCertificate = false) {
26 if (rootCerts !== null && !Buffer.isBuffer(rootCerts)) {
27 throw new TypeError('rootCerts must be null or a Buffer');
28 }
29 if (!Array.isArray(keyCertPairs)) {
30 throw new TypeError('keyCertPairs must be an array');
31 }
32 if (typeof checkClientCertificate !== 'boolean') {
33 throw new TypeError('checkClientCertificate must be a boolean');
34 }
35 const cert = [];
36 const key = [];
37 for (let i = 0; i < keyCertPairs.length; i++) {
38 const pair = keyCertPairs[i];
39 if (pair === null || typeof pair !== 'object') {
40 throw new TypeError(`keyCertPair[${i}] must be an object`);
41 }
42 if (!Buffer.isBuffer(pair.private_key)) {
43 throw new TypeError(`keyCertPair[${i}].private_key must be a Buffer`);
44 }
45 if (!Buffer.isBuffer(pair.cert_chain)) {
46 throw new TypeError(`keyCertPair[${i}].cert_chain must be a Buffer`);
47 }
48 cert.push(pair.cert_chain);
49 key.push(pair.private_key);
50 }
51 return new SecureServerCredentials({
52 ca: rootCerts || tls_helpers_1.getDefaultRootsData() || undefined,
53 cert,
54 key,
55 requestCert: checkClientCertificate,
56 ciphers: tls_helpers_1.CIPHER_SUITES,
57 });
58 }
59}
60exports.ServerCredentials = ServerCredentials;
61class InsecureServerCredentials extends ServerCredentials {
62 _isSecure() {
63 return false;
64 }
65 _getSettings() {
66 return null;
67 }
68}
69class SecureServerCredentials extends ServerCredentials {
70 constructor(options) {
71 super();
72 this.options = options;
73 }
74 _isSecure() {
75 return true;
76 }
77 _getSettings() {
78 return this.options;
79 }
80}
81//# sourceMappingURL=server-credentials.js.map
\No newline at end of file