src_credentials.js.html 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. *
  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. * var channel_creds = credentials.createSsl(root_certs);
  69. * (new GoogleAuth()).getApplicationDefault(function(err, credential) {
  70. * var call_creds = credentials.createFromGoogleCredential(credential);
  71. * var combined_creds = credentials.combineChannelCredentials(
  72. * channel_creds, call_creds);
  73. * var client = new Client(address, combined_creds);
  74. * });
  75. *
  76. * @module
  77. */
  78. 'use strict';
  79. var grpc = require('bindings')('grpc_node.node');
  80. var CallCredentials = grpc.CallCredentials;
  81. var ChannelCredentials = grpc.ChannelCredentials;
  82. var Metadata = require('./metadata.js');
  83. /**
  84. * Create an SSL Credentials object. If using a client-side certificate, both
  85. * the second and third arguments must be passed.
  86. * @param {Buffer} root_certs The root certificate data
  87. * @param {Buffer=} private_key The client certificate private key, if
  88. * applicable
  89. * @param {Buffer=} cert_chain The client certificate cert chain, if applicable
  90. * @return {ChannelCredentials} The SSL Credentials object
  91. */
  92. exports.createSsl = ChannelCredentials.createSsl;
  93. /**
  94. * Create a gRPC credentials object from a metadata generation function. This
  95. * function gets the service URL and a callback as parameters. The error
  96. * passed to the callback can optionally have a 'code' value attached to it,
  97. * which corresponds to a status code that this library uses.
  98. * @param {function(String, function(Error, Metadata))} metadata_generator The
  99. * function that generates metadata
  100. * @return {CallCredentials} The credentials object
  101. */
  102. exports.createFromMetadataGenerator = function(metadata_generator) {
  103. return CallCredentials.createFromPlugin(function(service_url, callback) {
  104. metadata_generator({service_url: service_url}, function(error, metadata) {
  105. var code = grpc.status.OK;
  106. var message = '';
  107. if (error) {
  108. message = error.message;
  109. if (error.hasOwnProperty('code')) {
  110. code = error.code;
  111. }
  112. if (!metadata) {
  113. metadata = new Metadata();
  114. }
  115. }
  116. callback(code, message, metadata._getCoreRepresentation());
  117. });
  118. });
  119. };
  120. /**
  121. * Create a gRPC credential from a Google credential object.
  122. * @param {Object} google_credential The Google credential object to use
  123. * @return {CallCredentials} The resulting credentials object
  124. */
  125. exports.createFromGoogleCredential = function(google_credential) {
  126. return exports.createFromMetadataGenerator(function(auth_context, callback) {
  127. var service_url = auth_context.service_url;
  128. google_credential.getRequestMetadata(service_url, function(err, header) {
  129. if (err) {
  130. callback(err);
  131. return;
  132. }
  133. var metadata = new Metadata();
  134. metadata.add('authorization', header.Authorization);
  135. callback(null, metadata);
  136. });
  137. });
  138. };
  139. /**
  140. * Combine a ChannelCredentials with any number of CallCredentials into a single
  141. * ChannelCredentials object.
  142. * @param {ChannelCredentials} channel_credential The ChannelCredentials to
  143. * start with
  144. * @param {...CallCredentials} credentials The CallCredentials to compose
  145. * @return ChannelCredentials A credentials object that combines all of the
  146. * input credentials
  147. */
  148. exports.combineChannelCredentials = function(channel_credential) {
  149. var current = channel_credential;
  150. for (var i = 1; i &lt; arguments.length; i++) {
  151. current = current.compose(arguments[i]);
  152. }
  153. return current;
  154. };
  155. /**
  156. * Combine any number of CallCredentials into a single CallCredentials object
  157. * @param {...CallCredentials} credentials the CallCredentials to compose
  158. * @return CallCredentials A credentials object that combines all of the input
  159. * credentials
  160. */
  161. exports.combineCallCredentials = function() {
  162. var current = arguments[0];
  163. for (var i = 1; i &lt; arguments.length; i++) {
  164. current = current.compose(arguments[i]);
  165. }
  166. return current;
  167. };
  168. /**
  169. * Create an insecure credentials object. This is used to create a channel that
  170. * does not use SSL. This cannot be composed with anything.
  171. * @return {ChannelCredentials} The insecure credentials object
  172. */
  173. exports.createInsecure = ChannelCredentials.createInsecure;
  174. </code></pre>
  175. </article>
  176. </section>
  177. </div>
  178. <nav>
  179. <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-src_client.html">src/client</a></li><li><a href="module-src_common.html">src/common</a></li><li><a href="module-src_credentials.html">src/credentials</a></li><li><a href="module-src_metadata.html">src/metadata</a></li><li><a href="module-src_server.html">src/server</a></li></ul><h3>Classes</h3><ul><li><a href="module-src_client.makeClientConstructor-Client.html">Client</a></li><li><a href="module-src_client-ClientDuplexStream.html">ClientDuplexStream</a></li><li><a href="module-src_client-ClientReadableStream.html">ClientReadableStream</a></li><li><a href="module-src_client-ClientWritableStream.html">ClientWritableStream</a></li><li><a href="module-src_metadata-Metadata.html">Metadata</a></li><li><a href="module-src_server-Server.html">Server</a></li><li><a href="module-src_server-ServerDuplexStream.html">ServerDuplexStream</a></li><li><a href="module-src_server-ServerReadableStream.html">ServerReadableStream</a></li><li><a href="module-src_server-ServerWritableStream.html">ServerWritableStream</a></li></ul><h3>Global</h3><ul><li><a href="global.html#callError">callError</a></li><li><a href="global.html#credentials">credentials</a></li><li><a href="global.html#getClientChannel">getClientChannel</a></li><li><a href="global.html#load">load</a></li><li><a href="global.html#loadObject">loadObject</a></li><li><a href="global.html#makeGenericClientConstructor">makeGenericClientConstructor</a></li><li><a href="global.html#Metadata">Metadata</a></li><li><a href="global.html#propagate">propagate</a></li><li><a href="global.html#Server">Server</a></li><li><a href="global.html#ServerCredentials">ServerCredentials</a></li><li><a href="global.html#status">status</a></li><li><a href="global.html#waitForClientReady">waitForClientReady</a></li><li><a href="global.html#writeFlags">writeFlags</a></li></ul>
  180. </nav>
  181. <br class="clear">
  182. <footer>
  183. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Thu Jan 14 2016 16:17:15 GMT-0800 (PST)
  184. </footer>
  185. <script> prettyPrint(); </script>
  186. <script src="scripts/linenumber.js"> </script>
  187. </body>
  188. </html>