src_credentials.js.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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, Google Inc.
  22. * All rights reserved.
  23. *
  24. * Redistribution and use in source and binary forms, with or without
  25. * modification, are permitted provided that the following conditions are
  26. * met:
  27. *
  28. * * Redistributions of source code must retain the above copyright
  29. * notice, this list of conditions and the following disclaimer.
  30. * * Redistributions in binary form must reproduce the above
  31. * copyright notice, this list of conditions and the following disclaimer
  32. * in the documentation and/or other materials provided with the
  33. * distribution.
  34. * * Neither the name of Google Inc. nor the names of its
  35. * contributors may be used to endorse or promote products derived from
  36. * this software without specific prior written permission.
  37. *
  38. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  39. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  40. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  41. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  42. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  44. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  45. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  46. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  47. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  48. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  49. *
  50. */
  51. /**
  52. * Credentials module
  53. *
  54. * This module contains factory methods for two different credential types:
  55. * CallCredentials and ChannelCredentials. ChannelCredentials are things like
  56. * SSL credentials that can be used to secure a connection, and are used to
  57. * construct a Client object. CallCredentials genrally modify metadata, so they
  58. * can be attached to an individual method call.
  59. *
  60. * CallCredentials can be composed with other CallCredentials to create
  61. * CallCredentials. ChannelCredentials can be composed with CallCredentials
  62. * to create ChannelCredentials. No combined credential can have more than
  63. * one ChannelCredentials.
  64. *
  65. * For example, to create a client secured with SSL that uses Google
  66. * default application credentials to authenticate:
  67. *
  68. * @example
  69. * var channel_creds = credentials.createSsl(root_certs);
  70. * (new GoogleAuth()).getApplicationDefault(function(err, credential) {
  71. * var call_creds = credentials.createFromGoogleCredential(credential);
  72. * var combined_creds = credentials.combineChannelCredentials(
  73. * channel_creds, call_creds);
  74. * var client = new Client(address, combined_creds);
  75. * });
  76. *
  77. * @namespace grpc.credentials
  78. */
  79. 'use strict';
  80. var grpc = require('./grpc_extension');
  81. /**
  82. * This cannot be constructed directly. Instead, instances of this class should
  83. * be created using the factory functions in {@link grpc.credentials}
  84. * @constructor grpc.credentials~CallCredentials
  85. */
  86. var CallCredentials = grpc.CallCredentials;
  87. /**
  88. * This cannot be constructed directly. Instead, instances of this class should
  89. * be created using the factory functions in {@link grpc.credentials}
  90. * @constructor grpc.credentials~ChannelCredentials
  91. */
  92. var ChannelCredentials = grpc.ChannelCredentials;
  93. var Metadata = require('./metadata.js');
  94. var common = require('./common.js');
  95. var constants = require('./constants');
  96. var _ = require('lodash');
  97. /**
  98. * @external GoogleCredential
  99. * @see https://github.com/google/google-auth-library-nodejs
  100. */
  101. /**
  102. * Create an SSL Credentials object. If using a client-side certificate, both
  103. * the second and third arguments must be passed.
  104. * @memberof grpc.credentials
  105. * @alias grpc.credentials.createSsl
  106. * @kind function
  107. * @param {Buffer} root_certs The root certificate data
  108. * @param {Buffer=} private_key The client certificate private key, if
  109. * applicable
  110. * @param {Buffer=} cert_chain The client certificate cert chain, if applicable
  111. * @return {grpc.credentials.ChannelCredentials} The SSL Credentials object
  112. */
  113. exports.createSsl = ChannelCredentials.createSsl;
  114. /**
  115. * @callback grpc.credentials~metadataCallback
  116. * @param {Error} error The error, if getting metadata failed
  117. * @param {grpc.Metadata} metadata The metadata
  118. */
  119. /**
  120. * @callback grpc.credentials~generateMetadata
  121. * @param {Object} params Parameters that can modify metadata generation
  122. * @param {string} params.service_url The URL of the service that the call is
  123. * going to
  124. * @param {grpc.credentials~metadataCallback} callback
  125. */
  126. /**
  127. * Create a gRPC credentials object from a metadata generation function. This
  128. * function gets the service URL and a callback as parameters. The error
  129. * passed to the callback can optionally have a 'code' value attached to it,
  130. * which corresponds to a status code that this library uses.
  131. * @memberof grpc.credentials
  132. * @alias grpc.credentials.createFromMetadataGenerator
  133. * @param {grpc.credentials~generateMetadata} metadata_generator The function
  134. * that generates metadata
  135. * @return {grpc.credentials.CallCredentials} The credentials object
  136. */
  137. exports.createFromMetadataGenerator = function(metadata_generator) {
  138. return CallCredentials.createFromPlugin(function(service_url, cb_data,
  139. callback) {
  140. metadata_generator({service_url: service_url}, function(error, metadata) {
  141. var code = constants.status.OK;
  142. var message = '';
  143. if (error) {
  144. message = error.message;
  145. if (error.hasOwnProperty('code') &amp;&amp; _.isFinite(error.code)) {
  146. code = error.code;
  147. } else {
  148. code = constants.status.UNAUTHENTICATED;
  149. }
  150. if (!metadata) {
  151. metadata = new Metadata();
  152. }
  153. }
  154. callback(code, message, metadata._getCoreRepresentation(), cb_data);
  155. });
  156. });
  157. };
  158. /**
  159. * Create a gRPC credential from a Google credential object.
  160. * @memberof grpc.credentials
  161. * @alias grpc.credentials.createFromGoogleCredential
  162. * @param {external:GoogleCredential} google_credential The Google credential
  163. * object to use
  164. * @return {grpc.credentials.CallCredentials} The resulting credentials object
  165. */
  166. exports.createFromGoogleCredential = function(google_credential) {
  167. return exports.createFromMetadataGenerator(function(auth_context, callback) {
  168. var service_url = auth_context.service_url;
  169. google_credential.getRequestMetadata(service_url, function(err, header) {
  170. if (err) {
  171. common.log(constants.logVerbosity.INFO, 'Auth error:' + err);
  172. callback(err);
  173. return;
  174. }
  175. var metadata = new Metadata();
  176. metadata.add('authorization', header.Authorization);
  177. callback(null, metadata);
  178. });
  179. });
  180. };
  181. /**
  182. * Combine a ChannelCredentials with any number of CallCredentials into a single
  183. * ChannelCredentials object.
  184. * @memberof grpc.credentials
  185. * @alias grpc.credentials.combineChannelCredentials
  186. * @param {ChannelCredentials} channel_credential The ChannelCredentials to
  187. * start with
  188. * @param {...CallCredentials} credentials The CallCredentials to compose
  189. * @return ChannelCredentials A credentials object that combines all of the
  190. * input credentials
  191. */
  192. exports.combineChannelCredentials = function(channel_credential) {
  193. var current = channel_credential;
  194. for (var i = 1; i &lt; arguments.length; i++) {
  195. current = current.compose(arguments[i]);
  196. }
  197. return current;
  198. };
  199. /**
  200. * Combine any number of CallCredentials into a single CallCredentials object
  201. * @memberof grpc.credentials
  202. * @alias grpc.credentials.combineCallCredentials
  203. * @param {...CallCredentials} credentials the CallCredentials to compose
  204. * @return CallCredentials A credentials object that combines all of the input
  205. * credentials
  206. */
  207. exports.combineCallCredentials = function() {
  208. var current = arguments[0];
  209. for (var i = 1; i &lt; arguments.length; i++) {
  210. current = current.compose(arguments[i]);
  211. }
  212. return current;
  213. };
  214. /**
  215. * Create an insecure credentials object. This is used to create a channel that
  216. * does not use SSL. This cannot be composed with anything.
  217. * @memberof grpc.credentials
  218. * @alias grpc.credentials.createInsecure
  219. * @kind function
  220. * @return {ChannelCredentials} The insecure credentials object
  221. */
  222. exports.createInsecure = ChannelCredentials.createInsecure;
  223. </code></pre>
  224. </article>
  225. </section>
  226. </div>
  227. <nav>
  228. <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>
  229. </nav>
  230. <br class="clear">
  231. <footer>
  232. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Wed Jun 28 2017 09:44:06 GMT-0700 (PDT)
  233. </footer>
  234. <script> prettyPrint(); </script>
  235. <script src="scripts/linenumber.js"> </script>
  236. </body>
  237. </html>