UNPKG

6.55 kBPlain TextView Raw
1/*
2 * Copyright 2019 gRPC authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18import { Metadata } from './metadata';
19
20export interface CallMetadataOptions {
21 service_url: string;
22}
23
24export type CallMetadataGenerator = (
25 options: CallMetadataOptions,
26 cb: (err: Error | null, metadata?: Metadata) => void
27) => void;
28
29// google-auth-library pre-v2.0.0 does not have getRequestHeaders
30// but has getRequestMetadata, which is deprecated in v2.0.0
31export interface OldOAuth2Client {
32 getRequestMetadata: (
33 url: string,
34 callback: (
35 err: Error | null,
36 headers?: {
37 [index: string]: string;
38 }
39 ) => void
40 ) => void;
41}
42
43export interface CurrentOAuth2Client {
44 getRequestHeaders: (url?: string) => Promise<{ [index: string]: string }>;
45}
46
47export type OAuth2Client = OldOAuth2Client | CurrentOAuth2Client;
48
49function isCurrentOauth2Client(
50 client: OAuth2Client
51): client is CurrentOAuth2Client {
52 return (
53 'getRequestHeaders' in client &&
54 typeof client.getRequestHeaders === 'function'
55 );
56}
57
58/**
59 * A class that represents a generic method of adding authentication-related
60 * metadata on a per-request basis.
61 */
62export abstract class CallCredentials {
63 /**
64 * Asynchronously generates a new Metadata object.
65 * @param options Options used in generating the Metadata object.
66 */
67 abstract generateMetadata(options: CallMetadataOptions): Promise<Metadata>;
68 /**
69 * Creates a new CallCredentials object from properties of both this and
70 * another CallCredentials object. This object's metadata generator will be
71 * called first.
72 * @param callCredentials The other CallCredentials object.
73 */
74 abstract compose(callCredentials: CallCredentials): CallCredentials;
75
76 /**
77 * Check whether two call credentials objects are equal. Separate
78 * SingleCallCredentials with identical metadata generator functions are
79 * equal.
80 * @param other The other CallCredentials object to compare with.
81 */
82 abstract _equals(other: CallCredentials): boolean;
83
84 /**
85 * Creates a new CallCredentials object from a given function that generates
86 * Metadata objects.
87 * @param metadataGenerator A function that accepts a set of options, and
88 * generates a Metadata object based on these options, which is passed back
89 * to the caller via a supplied (err, metadata) callback.
90 */
91 static createFromMetadataGenerator(
92 metadataGenerator: CallMetadataGenerator
93 ): CallCredentials {
94 return new SingleCallCredentials(metadataGenerator);
95 }
96
97 /**
98 * Create a gRPC credential from a Google credential object.
99 * @param googleCredentials The authentication client to use.
100 * @return The resulting CallCredentials object.
101 */
102 static createFromGoogleCredential(
103 googleCredentials: OAuth2Client
104 ): CallCredentials {
105 return CallCredentials.createFromMetadataGenerator((options, callback) => {
106 let getHeaders: Promise<{ [index: string]: string }>;
107 if (isCurrentOauth2Client(googleCredentials)) {
108 getHeaders = googleCredentials.getRequestHeaders(options.service_url);
109 } else {
110 getHeaders = new Promise((resolve, reject) => {
111 googleCredentials.getRequestMetadata(
112 options.service_url,
113 (err, headers) => {
114 if (err) {
115 reject(err);
116 return;
117 }
118 if (!headers) {
119 reject(new Error('Headers not set by metadata plugin'));
120 return;
121 }
122 resolve(headers);
123 }
124 );
125 });
126 }
127 getHeaders.then(
128 (headers) => {
129 const metadata = new Metadata();
130 for (const key of Object.keys(headers)) {
131 metadata.add(key, headers[key]);
132 }
133 callback(null, metadata);
134 },
135 (err) => {
136 callback(err);
137 }
138 );
139 });
140 }
141
142 static createEmpty(): CallCredentials {
143 return new EmptyCallCredentials();
144 }
145}
146
147class ComposedCallCredentials extends CallCredentials {
148 constructor(private creds: CallCredentials[]) {
149 super();
150 }
151
152 async generateMetadata(options: CallMetadataOptions): Promise<Metadata> {
153 const base: Metadata = new Metadata();
154 const generated: Metadata[] = await Promise.all(
155 this.creds.map((cred) => cred.generateMetadata(options))
156 );
157 for (const gen of generated) {
158 base.merge(gen);
159 }
160 return base;
161 }
162
163 compose(other: CallCredentials): CallCredentials {
164 return new ComposedCallCredentials(this.creds.concat([other]));
165 }
166
167 _equals(other: CallCredentials): boolean {
168 if (this === other) {
169 return true;
170 }
171 if (other instanceof ComposedCallCredentials) {
172 return this.creds.every((value, index) =>
173 value._equals(other.creds[index])
174 );
175 } else {
176 return false;
177 }
178 }
179}
180
181class SingleCallCredentials extends CallCredentials {
182 constructor(private metadataGenerator: CallMetadataGenerator) {
183 super();
184 }
185
186 generateMetadata(options: CallMetadataOptions): Promise<Metadata> {
187 return new Promise<Metadata>((resolve, reject) => {
188 this.metadataGenerator(options, (err, metadata) => {
189 if (metadata !== undefined) {
190 resolve(metadata);
191 } else {
192 reject(err);
193 }
194 });
195 });
196 }
197
198 compose(other: CallCredentials): CallCredentials {
199 return new ComposedCallCredentials([this, other]);
200 }
201
202 _equals(other: CallCredentials): boolean {
203 if (this === other) {
204 return true;
205 }
206 if (other instanceof SingleCallCredentials) {
207 return this.metadataGenerator === other.metadataGenerator;
208 } else {
209 return false;
210 }
211 }
212}
213
214class EmptyCallCredentials extends CallCredentials {
215 generateMetadata(options: CallMetadataOptions): Promise<Metadata> {
216 return Promise.resolve(new Metadata());
217 }
218
219 compose(other: CallCredentials): CallCredentials {
220 return other;
221 }
222
223 _equals(other: CallCredentials): boolean {
224 return other instanceof EmptyCallCredentials;
225 }
226}
227
\No newline at end of file