call-credentials.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. */
  18. Object.defineProperty(exports, "__esModule", { value: true });
  19. exports.CallCredentials = void 0;
  20. const metadata_1 = require("./metadata");
  21. function isCurrentOauth2Client(client) {
  22. return ('getRequestHeaders' in client &&
  23. typeof client.getRequestHeaders === 'function');
  24. }
  25. /**
  26. * A class that represents a generic method of adding authentication-related
  27. * metadata on a per-request basis.
  28. */
  29. class CallCredentials {
  30. /**
  31. * Creates a new CallCredentials object from a given function that generates
  32. * Metadata objects.
  33. * @param metadataGenerator A function that accepts a set of options, and
  34. * generates a Metadata object based on these options, which is passed back
  35. * to the caller via a supplied (err, metadata) callback.
  36. */
  37. static createFromMetadataGenerator(metadataGenerator) {
  38. return new SingleCallCredentials(metadataGenerator);
  39. }
  40. /**
  41. * Create a gRPC credential from a Google credential object.
  42. * @param googleCredentials The authentication client to use.
  43. * @return The resulting CallCredentials object.
  44. */
  45. static createFromGoogleCredential(googleCredentials) {
  46. return CallCredentials.createFromMetadataGenerator((options, callback) => {
  47. let getHeaders;
  48. if (isCurrentOauth2Client(googleCredentials)) {
  49. getHeaders = googleCredentials.getRequestHeaders(options.service_url);
  50. }
  51. else {
  52. getHeaders = new Promise((resolve, reject) => {
  53. googleCredentials.getRequestMetadata(options.service_url, (err, headers) => {
  54. if (err) {
  55. reject(err);
  56. return;
  57. }
  58. resolve(headers);
  59. });
  60. });
  61. }
  62. getHeaders.then((headers) => {
  63. const metadata = new metadata_1.Metadata();
  64. for (const key of Object.keys(headers)) {
  65. metadata.add(key, headers[key]);
  66. }
  67. callback(null, metadata);
  68. }, (err) => {
  69. callback(err);
  70. });
  71. });
  72. }
  73. static createEmpty() {
  74. return new EmptyCallCredentials();
  75. }
  76. }
  77. exports.CallCredentials = CallCredentials;
  78. class ComposedCallCredentials extends CallCredentials {
  79. constructor(creds) {
  80. super();
  81. this.creds = creds;
  82. }
  83. async generateMetadata(options) {
  84. const base = new metadata_1.Metadata();
  85. const generated = await Promise.all(this.creds.map((cred) => cred.generateMetadata(options)));
  86. for (const gen of generated) {
  87. base.merge(gen);
  88. }
  89. return base;
  90. }
  91. compose(other) {
  92. return new ComposedCallCredentials(this.creds.concat([other]));
  93. }
  94. _equals(other) {
  95. if (this === other) {
  96. return true;
  97. }
  98. if (other instanceof ComposedCallCredentials) {
  99. return this.creds.every((value, index) => value._equals(other.creds[index]));
  100. }
  101. else {
  102. return false;
  103. }
  104. }
  105. }
  106. class SingleCallCredentials extends CallCredentials {
  107. constructor(metadataGenerator) {
  108. super();
  109. this.metadataGenerator = metadataGenerator;
  110. }
  111. generateMetadata(options) {
  112. return new Promise((resolve, reject) => {
  113. this.metadataGenerator(options, (err, metadata) => {
  114. if (metadata !== undefined) {
  115. resolve(metadata);
  116. }
  117. else {
  118. reject(err);
  119. }
  120. });
  121. });
  122. }
  123. compose(other) {
  124. return new ComposedCallCredentials([this, other]);
  125. }
  126. _equals(other) {
  127. if (this === other) {
  128. return true;
  129. }
  130. if (other instanceof SingleCallCredentials) {
  131. return this.metadataGenerator === other.metadataGenerator;
  132. }
  133. else {
  134. return false;
  135. }
  136. }
  137. }
  138. class EmptyCallCredentials extends CallCredentials {
  139. generateMetadata(options) {
  140. return Promise.resolve(new metadata_1.Metadata());
  141. }
  142. compose(other) {
  143. return other;
  144. }
  145. _equals(other) {
  146. return other instanceof EmptyCallCredentials;
  147. }
  148. }
  149. //# sourceMappingURL=call-credentials.js.map