src_credentials.js.html 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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('./grpc_extension');
  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. } else {
  112. code = grpc.status.UNAUTHENTICATED;
  113. }
  114. if (!metadata) {
  115. metadata = new Metadata();
  116. }
  117. }
  118. callback(code, message, metadata._getCoreRepresentation());
  119. });
  120. });
  121. };
  122. /**
  123. * Create a gRPC credential from a Google credential object.
  124. * @param {Object} google_credential The Google credential object to use
  125. * @return {CallCredentials} The resulting credentials object
  126. */
  127. exports.createFromGoogleCredential = function(google_credential) {
  128. return exports.createFromMetadataGenerator(function(auth_context, callback) {
  129. var service_url = auth_context.service_url;
  130. google_credential.getRequestMetadata(service_url, function(err, header) {
  131. if (err) {
  132. console.log('Auth error:', err);
  133. callback(err);
  134. return;
  135. }
  136. var metadata = new Metadata();
  137. metadata.add('authorization', header.Authorization);
  138. callback(null, metadata);
  139. });
  140. });
  141. };
  142. /**
  143. * Combine a ChannelCredentials with any number of CallCredentials into a single
  144. * ChannelCredentials object.
  145. * @param {ChannelCredentials} channel_credential The ChannelCredentials to
  146. * start with
  147. * @param {...CallCredentials} credentials The CallCredentials to compose
  148. * @return ChannelCredentials A credentials object that combines all of the
  149. * input credentials
  150. */
  151. exports.combineChannelCredentials = function(channel_credential) {
  152. var current = channel_credential;
  153. for (var i = 1; i &lt; arguments.length; i++) {
  154. current = current.compose(arguments[i]);
  155. }
  156. return current;
  157. };
  158. /**
  159. * Combine any number of CallCredentials into a single CallCredentials object
  160. * @param {...CallCredentials} credentials the CallCredentials to compose
  161. * @return CallCredentials A credentials object that combines all of the input
  162. * credentials
  163. */
  164. exports.combineCallCredentials = function() {
  165. var current = arguments[0];
  166. for (var i = 1; i &lt; arguments.length; i++) {
  167. current = current.compose(arguments[i]);
  168. }
  169. return current;
  170. };
  171. /**
  172. * Create an insecure credentials object. This is used to create a channel that
  173. * does not use SSL. This cannot be composed with anything.
  174. * @return {ChannelCredentials} The insecure credentials object
  175. */
  176. exports.createInsecure = ChannelCredentials.createInsecure;
  177. </code></pre>
  178. </article>
  179. </section>
  180. </div>
  181. <nav>
  182. <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>
  183. </nav>
  184. <br class="clear">
  185. <footer>
  186. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Wed May 11 2016 18:17:25 GMT-0700 (PDT)
  187. </footer>
  188. <script> prettyPrint(); </script>
  189. <script src="scripts/linenumber.js"> </script>
  190. </body>
  191. </html>