UNPKG

6.41 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 resolve(headers);
119 }
120 );
121 });
122 }
123 getHeaders.then(
124 (headers) => {
125 const metadata = new Metadata();
126 for (const key of Object.keys(headers)) {
127 metadata.add(key, headers[key]);
128 }
129 callback(null, metadata);
130 },
131 (err) => {
132 callback(err);
133 }
134 );
135 });
136 }
137
138 static createEmpty(): CallCredentials {
139 return new EmptyCallCredentials();
140 }
141}
142
143class ComposedCallCredentials extends CallCredentials {
144 constructor(private creds: CallCredentials[]) {
145 super();
146 }
147
148 async generateMetadata(options: CallMetadataOptions): Promise<Metadata> {
149 const base: Metadata = new Metadata();
150 const generated: Metadata[] = await Promise.all(
151 this.creds.map((cred) => cred.generateMetadata(options))
152 );
153 for (const gen of generated) {
154 base.merge(gen);
155 }
156 return base;
157 }
158
159 compose(other: CallCredentials): CallCredentials {
160 return new ComposedCallCredentials(this.creds.concat([other]));
161 }
162
163 _equals(other: CallCredentials): boolean {
164 if (this === other) {
165 return true;
166 }
167 if (other instanceof ComposedCallCredentials) {
168 return this.creds.every((value, index) =>
169 value._equals(other.creds[index])
170 );
171 } else {
172 return false;
173 }
174 }
175}
176
177class SingleCallCredentials extends CallCredentials {
178 constructor(private metadataGenerator: CallMetadataGenerator) {
179 super();
180 }
181
182 generateMetadata(options: CallMetadataOptions): Promise<Metadata> {
183 return new Promise<Metadata>((resolve, reject) => {
184 this.metadataGenerator(options, (err, metadata) => {
185 if (metadata !== undefined) {
186 resolve(metadata);
187 } else {
188 reject(err);
189 }
190 });
191 });
192 }
193
194 compose(other: CallCredentials): CallCredentials {
195 return new ComposedCallCredentials([this, other]);
196 }
197
198 _equals(other: CallCredentials): boolean {
199 if (this === other) {
200 return true;
201 }
202 if (other instanceof SingleCallCredentials) {
203 return this.metadataGenerator === other.metadataGenerator;
204 } else {
205 return false;
206 }
207 }
208}
209
210class EmptyCallCredentials extends CallCredentials {
211 generateMetadata(options: CallMetadataOptions): Promise<Metadata> {
212 return Promise.resolve(new Metadata());
213 }
214
215 compose(other: CallCredentials): CallCredentials {
216 return other;
217 }
218
219 _equals(other: CallCredentials): boolean {
220 return other instanceof EmptyCallCredentials;
221 }
222}
223
\No newline at end of file