src_credentials.js.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: src/credentials.js</title>
  6. <script src="scripts/prettify/prettify.js"> </script>
  7. <script src="scripts/prettify/lang-css.js"> </script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
  13. </head>
  14. <body>
  15. <div id="main">
  16. <h1 class="page-title">Source: src/credentials.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>/**
  20. * @license
  21. * Copyright 2015 gRPC authors.
  22. *
  23. * Licensed under the Apache License, Version 2.0 (the "License");
  24. * you may not use this file except in compliance with the License.
  25. * You may obtain a copy of the License at
  26. *
  27. * http://www.apache.org/licenses/LICENSE-2.0
  28. *
  29. * Unless required by applicable law or agreed to in writing, software
  30. * distributed under the License is distributed on an "AS IS" BASIS,
  31. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  32. * See the License for the specific language governing permissions and
  33. * limitations under the License.
  34. *
  35. */
  36. /**
  37. * Credentials module
  38. *
  39. * This module contains factory methods for two different credential types:
  40. * CallCredentials and ChannelCredentials. ChannelCredentials are things like
  41. * SSL credentials that can be used to secure a connection, and are used to
  42. * construct a Client object. CallCredentials genrally modify metadata, so they
  43. * can be attached to an individual method call.
  44. *
  45. * CallCredentials can be composed with other CallCredentials to create
  46. * CallCredentials. ChannelCredentials can be composed with CallCredentials
  47. * to create ChannelCredentials. No combined credential can have more than
  48. * one ChannelCredentials.
  49. *
  50. * For example, to create a client secured with SSL that uses Google
  51. * default application credentials to authenticate:
  52. *
  53. * @example
  54. * var channel_creds = credentials.createSsl(root_certs);
  55. * (new GoogleAuth()).getApplicationDefault(function(err, credential) {
  56. * var call_creds = credentials.createFromGoogleCredential(credential);
  57. * var combined_creds = credentials.combineChannelCredentials(
  58. * channel_creds, call_creds);
  59. * var client = new Client(address, combined_creds);
  60. * });
  61. *
  62. * @namespace grpc.credentials
  63. */
  64. 'use strict';
  65. var grpc = require('./grpc_extension');
  66. /**
  67. * This cannot be constructed directly. Instead, instances of this class should
  68. * be created using the factory functions in {@link grpc.credentials}
  69. * @constructor grpc.credentials~CallCredentials
  70. */
  71. var CallCredentials = grpc.CallCredentials;
  72. /**
  73. * This cannot be constructed directly. Instead, instances of this class should
  74. * be created using the factory functions in {@link grpc.credentials}
  75. * @constructor grpc.credentials~ChannelCredentials
  76. */
  77. var ChannelCredentials = grpc.ChannelCredentials;
  78. var Metadata = require('./metadata.js');
  79. var common = require('./common.js');
  80. var constants = require('./constants');
  81. var _ = require('lodash');
  82. /**
  83. * @external GoogleCredential
  84. * @see https://github.com/google/google-auth-library-nodejs
  85. */
  86. /**
  87. * Create an SSL Credentials object. If using a client-side certificate, both
  88. * the second and third arguments must be passed.
  89. * @memberof grpc.credentials
  90. * @alias grpc.credentials.createSsl
  91. * @kind function
  92. * @param {Buffer=} root_certs The root certificate data
  93. * @param {Buffer=} private_key The client certificate private key, if
  94. * applicable
  95. * @param {Buffer=} cert_chain The client certificate cert chain, if applicable
  96. * @return {grpc.credentials.ChannelCredentials} The SSL Credentials object
  97. */
  98. exports.createSsl = ChannelCredentials.createSsl;
  99. /**
  100. * @callback grpc.credentials~metadataCallback
  101. * @param {Error} error The error, if getting metadata failed
  102. * @param {grpc.Metadata} metadata The metadata
  103. */
  104. /**
  105. * @callback grpc.credentials~generateMetadata
  106. * @param {Object} params Parameters that can modify metadata generation
  107. * @param {string} params.service_url The URL of the service that the call is
  108. * going to
  109. * @param {grpc.credentials~metadataCallback} callback
  110. */
  111. /**
  112. * Create a gRPC credentials object from a metadata generation function. This
  113. * function gets the service URL and a callback as parameters. The error
  114. * passed to the callback can optionally have a 'code' value attached to it,
  115. * which corresponds to a status code that this library uses.
  116. * @memberof grpc.credentials
  117. * @alias grpc.credentials.createFromMetadataGenerator
  118. * @param {grpc.credentials~generateMetadata} metadata_generator The function
  119. * that generates metadata
  120. * @return {grpc.credentials.CallCredentials} The credentials object
  121. */
  122. exports.createFromMetadataGenerator = function(metadata_generator) {
  123. return CallCredentials.createFromPlugin(function(service_url, cb_data,
  124. callback) {
  125. metadata_generator({service_url: service_url}, function(error, metadata) {
  126. var code = constants.status.OK;
  127. var message = '';
  128. if (error) {
  129. message = error.message;
  130. if (error.hasOwnProperty('code') &amp;&amp; _.isFinite(error.code)) {
  131. code = error.code;
  132. } else {
  133. code = constants.status.UNAUTHENTICATED;
  134. }
  135. if (!metadata) {
  136. metadata = new Metadata();
  137. }
  138. }
  139. callback(code, message, metadata._getCoreRepresentation(), cb_data);
  140. });
  141. });
  142. };
  143. /**
  144. * Create a gRPC credential from a Google credential object.
  145. * @memberof grpc.credentials
  146. * @alias grpc.credentials.createFromGoogleCredential
  147. * @param {external:GoogleCredential} google_credential The Google credential
  148. * object to use
  149. * @return {grpc.credentials.CallCredentials} The resulting credentials object
  150. */
  151. exports.createFromGoogleCredential = function(google_credential) {
  152. return exports.createFromMetadataGenerator(function(auth_context, callback) {
  153. var service_url = auth_context.service_url;
  154. google_credential.getRequestMetadata(service_url, function(err, header) {
  155. if (err) {
  156. common.log(constants.logVerbosity.INFO, 'Auth error:' + err);
  157. callback(err);
  158. return;
  159. }
  160. var metadata = new Metadata();
  161. metadata.add('authorization', header.Authorization);
  162. callback(null, metadata);
  163. });
  164. });
  165. };
  166. /**
  167. * Combine a ChannelCredentials with any number of CallCredentials into a single
  168. * ChannelCredentials object.
  169. * @memberof grpc.credentials
  170. * @alias grpc.credentials.combineChannelCredentials
  171. * @param {ChannelCredentials} channel_credential The ChannelCredentials to
  172. * start with
  173. * @param {...CallCredentials} credentials The CallCredentials to compose
  174. * @return ChannelCredentials A credentials object that combines all of the
  175. * input credentials
  176. */
  177. exports.combineChannelCredentials = function(channel_credential) {
  178. var current = channel_credential;
  179. for (var i = 1; i &lt; arguments.length; i++) {
  180. current = current.compose(arguments[i]);
  181. }
  182. return current;
  183. };
  184. /**
  185. * Combine any number of CallCredentials into a single CallCredentials object
  186. * @memberof grpc.credentials
  187. * @alias grpc.credentials.combineCallCredentials
  188. * @param {...CallCredentials} credentials the CallCredentials to compose
  189. * @return CallCredentials A credentials object that combines all of the input
  190. * credentials
  191. */
  192. exports.combineCallCredentials = function() {
  193. var current = arguments[0];
  194. for (var i = 1; i &lt; arguments.length; i++) {
  195. current = current.compose(arguments[i]);
  196. }
  197. return current;
  198. };
  199. /**
  200. * Create an insecure credentials object. This is used to create a channel that
  201. * does not use SSL. This cannot be composed with anything.
  202. * @memberof grpc.credentials
  203. * @alias grpc.credentials.createInsecure
  204. * @kind function
  205. * @return {ChannelCredentials} The insecure credentials object
  206. */
  207. exports.createInsecure = ChannelCredentials.createInsecure;
  208. </code></pre>
  209. </article>
  210. </section>
  211. </div>
  212. <nav>
  213. <h2><a href="index.html">Home</a></h2><h3>Externals</h3><ul><li><a href="external-Duplex.html">Duplex</a></li><li><a href="external-EventEmitter.html">EventEmitter</a></li><li><a href="external-GoogleCredential.html">GoogleCredential</a></li><li><a href="external-Readable.html">Readable</a></li><li><a href="external-Writable.html">Writable</a></li></ul><h3>Classes</h3><ul><li><a href="grpc.Client.html">Client</a></li><li><a href="grpc.credentials-CallCredentials.html">CallCredentials</a></li><li><a href="grpc.credentials-ChannelCredentials.html">ChannelCredentials</a></li><li><a href="grpc.Metadata.html">Metadata</a></li><li><a href="grpc.Server.html">Server</a></li><li><a href="grpc.ServerCredentials.html">ServerCredentials</a></li><li><a href="grpc-ClientDuplexStream.html">ClientDuplexStream</a></li><li><a href="grpc-ClientReadableStream.html">ClientReadableStream</a></li><li><a href="grpc-ClientUnaryCall.html">ClientUnaryCall</a></li><li><a href="grpc-ClientWritableStream.html">ClientWritableStream</a></li><li><a href="grpc-ServerDuplexStream.html">ServerDuplexStream</a></li><li><a href="grpc-ServerReadableStream.html">ServerReadableStream</a></li><li><a href="grpc-ServerUnaryCall.html">ServerUnaryCall</a></li><li><a href="grpc-ServerWritableStream.html">ServerWritableStream</a></li></ul><h3>Events</h3><ul><li><a href="grpc-ClientDuplexStream.html#event:metadata">metadata</a></li><li><a href="grpc-ClientDuplexStream.html#event:status">status</a></li><li><a href="grpc-ClientReadableStream.html#event:metadata">metadata</a></li><li><a href="grpc-ClientReadableStream.html#event:status">status</a></li><li><a href="grpc-ClientUnaryCall.html#event:metadata">metadata</a></li><li><a href="grpc-ClientUnaryCall.html#event:status">status</a></li><li><a href="grpc-ClientWritableStream.html#event:metadata">metadata</a></li><li><a href="grpc-ClientWritableStream.html#event:status">status</a></li><li><a href="grpc-ServerDuplexStream.html#~event:cancelled">cancelled</a></li><li><a href="grpc-ServerReadableStream.html#~event:cancelled">cancelled</a></li><li><a href="grpc-ServerUnaryCall.html#~event:cancelled">cancelled</a></li><li><a href="grpc-ServerWritableStream.html#~event:cancelled">cancelled</a></li></ul><h3>Namespaces</h3><ul><li><a href="grpc.html">grpc</a></li><li><a href="grpc.credentials.html">credentials</a></li></ul>
  214. </nav>
  215. <br class="clear">
  216. <footer>
  217. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.3</a> on Fri Sep 08 2017 11:10:31 GMT-0700 (PDT)
  218. </footer>
  219. <script> prettyPrint(); </script>
  220. <script src="scripts/linenumber.js"> </script>
  221. </body>
  222. </html>