UNPKG

3.21 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.CallCredentialsFilterFactory = exports.CallCredentialsFilter = void 0;
20const filter_1 = require("./filter");
21const constants_1 = require("./constants");
22const uri_parser_1 = require("./uri-parser");
23class CallCredentialsFilter extends filter_1.BaseFilter {
24 constructor(channel, stream) {
25 var _a, _b;
26 super();
27 this.channel = channel;
28 this.stream = stream;
29 this.channel = channel;
30 this.stream = stream;
31 const splitPath = stream.getMethod().split('/');
32 let serviceName = '';
33 /* The standard path format is "/{serviceName}/{methodName}", so if we split
34 * by '/', the first item should be empty and the second should be the
35 * service name */
36 if (splitPath.length >= 2) {
37 serviceName = splitPath[1];
38 }
39 const hostname = (_b = (_a = uri_parser_1.splitHostPort(stream.getHost())) === null || _a === void 0 ? void 0 : _a.host) !== null && _b !== void 0 ? _b : 'localhost';
40 /* Currently, call credentials are only allowed on HTTPS connections, so we
41 * can assume that the scheme is "https" */
42 this.serviceUrl = `https://${hostname}/${serviceName}`;
43 }
44 async sendMetadata(metadata) {
45 const credentials = this.stream.getCredentials();
46 const credsMetadata = credentials.generateMetadata({
47 service_url: this.serviceUrl,
48 });
49 const resultMetadata = await metadata;
50 try {
51 resultMetadata.merge(await credsMetadata);
52 }
53 catch (error) {
54 this.stream.cancelWithStatus(constants_1.Status.UNAUTHENTICATED, `Failed to retrieve auth metadata with error: ${error.message}`);
55 return Promise.reject('Failed to retrieve auth metadata');
56 }
57 if (resultMetadata.get('authorization').length > 1) {
58 this.stream.cancelWithStatus(constants_1.Status.INTERNAL, '"authorization" metadata cannot have multiple values');
59 return Promise.reject('"authorization" metadata cannot have multiple values');
60 }
61 return resultMetadata;
62 }
63}
64exports.CallCredentialsFilter = CallCredentialsFilter;
65class CallCredentialsFilterFactory {
66 constructor(channel) {
67 this.channel = channel;
68 this.channel = channel;
69 }
70 createFilter(callStream) {
71 return new CallCredentialsFilter(this.channel, callStream);
72 }
73}
74exports.CallCredentialsFilterFactory = CallCredentialsFilterFactory;
75//# sourceMappingURL=call-credentials-filter.js.map
\No newline at end of file