1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | Object.defineProperty(exports, "__esModule", { value: true });
|
19 | exports.CallCredentialsFilterFactory = exports.CallCredentialsFilter = void 0;
|
20 | const filter_1 = require("./filter");
|
21 | const constants_1 = require("./constants");
|
22 | const uri_parser_1 = require("./uri-parser");
|
23 | class 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 | |
34 |
|
35 |
|
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 | |
41 |
|
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 | }
|
64 | exports.CallCredentialsFilter = CallCredentialsFilter;
|
65 | class 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 | }
|
74 | exports.CallCredentialsFilterFactory = CallCredentialsFilterFactory;
|
75 |
|
\ | No newline at end of file |