call-credentials.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. import { Metadata } from './metadata';
  18. export interface CallMetadataOptions {
  19. method_name: string;
  20. service_url: string;
  21. }
  22. export type CallMetadataGenerator = (
  23. options: CallMetadataOptions,
  24. cb: (err: Error | null, metadata?: Metadata) => void
  25. ) => void;
  26. // google-auth-library pre-v2.0.0 does not have getRequestHeaders
  27. // but has getRequestMetadata, which is deprecated in v2.0.0
  28. export interface OldOAuth2Client {
  29. getRequestMetadata: (
  30. url: string,
  31. callback: (
  32. err: Error | null,
  33. headers?: {
  34. [index: string]: string;
  35. }
  36. ) => void
  37. ) => void;
  38. }
  39. export interface CurrentOAuth2Client {
  40. getRequestHeaders: (url?: string) => Promise<{ [index: string]: string }>;
  41. }
  42. export type OAuth2Client = OldOAuth2Client | CurrentOAuth2Client;
  43. function isCurrentOauth2Client(
  44. client: OAuth2Client
  45. ): client is CurrentOAuth2Client {
  46. return (
  47. 'getRequestHeaders' in client &&
  48. typeof client.getRequestHeaders === 'function'
  49. );
  50. }
  51. /**
  52. * A class that represents a generic method of adding authentication-related
  53. * metadata on a per-request basis.
  54. */
  55. export abstract class CallCredentials {
  56. /**
  57. * Asynchronously generates a new Metadata object.
  58. * @param options Options used in generating the Metadata object.
  59. */
  60. abstract generateMetadata(options: CallMetadataOptions): Promise<Metadata>;
  61. /**
  62. * Creates a new CallCredentials object from properties of both this and
  63. * another CallCredentials object. This object's metadata generator will be
  64. * called first.
  65. * @param callCredentials The other CallCredentials object.
  66. */
  67. abstract compose(callCredentials: CallCredentials): CallCredentials;
  68. /**
  69. * Check whether two call credentials objects are equal. Separate
  70. * SingleCallCredentials with identical metadata generator functions are
  71. * equal.
  72. * @param other The other CallCredentials object to compare with.
  73. */
  74. abstract _equals(other: CallCredentials): boolean;
  75. /**
  76. * Creates a new CallCredentials object from a given function that generates
  77. * Metadata objects.
  78. * @param metadataGenerator A function that accepts a set of options, and
  79. * generates a Metadata object based on these options, which is passed back
  80. * to the caller via a supplied (err, metadata) callback.
  81. */
  82. static createFromMetadataGenerator(
  83. metadataGenerator: CallMetadataGenerator
  84. ): CallCredentials {
  85. return new SingleCallCredentials(metadataGenerator);
  86. }
  87. /**
  88. * Create a gRPC credential from a Google credential object.
  89. * @param googleCredentials The authentication client to use.
  90. * @return The resulting CallCredentials object.
  91. */
  92. static createFromGoogleCredential(
  93. googleCredentials: OAuth2Client
  94. ): CallCredentials {
  95. return CallCredentials.createFromMetadataGenerator((options, callback) => {
  96. let getHeaders: Promise<{ [index: string]: string }>;
  97. if (isCurrentOauth2Client(googleCredentials)) {
  98. getHeaders = googleCredentials.getRequestHeaders(options.service_url);
  99. } else {
  100. getHeaders = new Promise((resolve, reject) => {
  101. googleCredentials.getRequestMetadata(
  102. options.service_url,
  103. (err, headers) => {
  104. if (err) {
  105. reject(err);
  106. return;
  107. }
  108. if (!headers) {
  109. reject(new Error('Headers not set by metadata plugin'));
  110. return;
  111. }
  112. resolve(headers);
  113. }
  114. );
  115. });
  116. }
  117. getHeaders.then(
  118. headers => {
  119. const metadata = new Metadata();
  120. for (const key of Object.keys(headers)) {
  121. metadata.add(key, headers[key]);
  122. }
  123. callback(null, metadata);
  124. },
  125. err => {
  126. callback(err);
  127. }
  128. );
  129. });
  130. }
  131. static createEmpty(): CallCredentials {
  132. return new EmptyCallCredentials();
  133. }
  134. }
  135. class ComposedCallCredentials extends CallCredentials {
  136. constructor(private creds: CallCredentials[]) {
  137. super();
  138. }
  139. async generateMetadata(options: CallMetadataOptions): Promise<Metadata> {
  140. const base: Metadata = new Metadata();
  141. const generated: Metadata[] = await Promise.all(
  142. this.creds.map(cred => cred.generateMetadata(options))
  143. );
  144. for (const gen of generated) {
  145. base.merge(gen);
  146. }
  147. return base;
  148. }
  149. compose(other: CallCredentials): CallCredentials {
  150. return new ComposedCallCredentials(this.creds.concat([other]));
  151. }
  152. _equals(other: CallCredentials): boolean {
  153. if (this === other) {
  154. return true;
  155. }
  156. if (other instanceof ComposedCallCredentials) {
  157. return this.creds.every((value, index) =>
  158. value._equals(other.creds[index])
  159. );
  160. } else {
  161. return false;
  162. }
  163. }
  164. }
  165. class SingleCallCredentials extends CallCredentials {
  166. constructor(private metadataGenerator: CallMetadataGenerator) {
  167. super();
  168. }
  169. generateMetadata(options: CallMetadataOptions): Promise<Metadata> {
  170. return new Promise<Metadata>((resolve, reject) => {
  171. this.metadataGenerator(options, (err, metadata) => {
  172. if (metadata !== undefined) {
  173. resolve(metadata);
  174. } else {
  175. reject(err);
  176. }
  177. });
  178. });
  179. }
  180. compose(other: CallCredentials): CallCredentials {
  181. return new ComposedCallCredentials([this, other]);
  182. }
  183. _equals(other: CallCredentials): boolean {
  184. if (this === other) {
  185. return true;
  186. }
  187. if (other instanceof SingleCallCredentials) {
  188. return this.metadataGenerator === other.metadataGenerator;
  189. } else {
  190. return false;
  191. }
  192. }
  193. }
  194. class EmptyCallCredentials extends CallCredentials {
  195. generateMetadata(options: CallMetadataOptions): Promise<Metadata> {
  196. return Promise.resolve(new Metadata());
  197. }
  198. compose(other: CallCredentials): CallCredentials {
  199. return other;
  200. }
  201. _equals(other: CallCredentials): boolean {
  202. return other instanceof EmptyCallCredentials;
  203. }
  204. }