UNPKG

aws-cognito-authorization

Version:
241 lines (200 loc) 7.37 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Source: AWSCredentials.js</title> <script src="scripts/prettify/prettify.js"> </script> <script src="scripts/prettify/lang-css.js"> </script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> </head> <body> <div id="main"> <h1 class="page-title">Source: AWSCredentials.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>'use strict'; const AWS = require( 'aws-sdk' ), https = require( 'https' ); /** * @class AWSCredentials */ class AWSCredentials { /** * @param options * @type {!object} * @desc &lt;code>options&lt;code> required for communication with &lt;a href="https://aws.amazon.com/cognito/">AWS Cognito&lt;/a> * @property {!string} region - AWS Region of operation * @property {!string} IdentityPoolId - "{region}:{IdentityPoolId UUID}" * @property {!string} DeveloperName - "com.developer.name" for Developer provider name * @property {number} TokenDuration - Amount of time the session token should last */ constructor( options ) { const required = [ 'IdentityPoolId', 'DeveloperName' ], missing = this.isMissingProperty( options, required ), isNull = this.isNullProperty( options, required ), creds = { params: {} }; if( missing || isNull ) throw `Argument Error - Missing or Empty Property: ${missing || isNull}`; if( options.hasOwnProperty( 'profile' ) ) creds.params.profile = options.profile; if( options.hasOwnProperty( 'region' ) ) creds.region = options.region; this.COG = new AWS.CognitoIdentity( creds ); this.IdentityPoolId = options.IdentityPoolId; this.DeveloperName = options.DeveloperName; this.TokenDuration = options.TokenDuration || 86400; this.Federation = options.Federation || 'cognito-identity.amazonaws.com'; this.init(); } init() { this.healthRequest() .then( () => this.isReady = true ) .catch( () => this.isReady = false ); } p( op ) { return new Promise( op ); } /** * healthRequest * @returns {HTTPResponse} * @desc Does a health check on AWSCognito via &lt;a href="https://cognito-identity.amazonaws.com/ping">GET /ping HTTP/1.1&lt;/a> */ healthRequest() { return this.p( ( res, rej ) => { https.get( `${this.COG.endpoint.href}/ping`, body => { body.on( 'data', d => res( d.toString() ) ); } ).on( 'error', rej ); } ); } isMissingProperty( obj, properties ) { let hasOwnProperty = false, i = 0; for( ; i &lt; properties.length; i++ ) if( !obj.hasOwnProperty( properties[ i ] ) ) hasOwnProperty = properties[ i ]; return hasOwnProperty; } isNullProperty( obj, properties ) { let isNullProperty = false, i = 0; for( ; i &lt; properties.length; i++ ) if( obj.hasOwnProperty( properties[ i ] ) ) if( !obj[ properties[ i ] ] ) isNullProperty = properties[ i ]; return isNullProperty; } removeProperties( obj, properties ) { let i = 0; for( ; i &lt; properties.length; i++ ) delete obj[ properties[ i ] ]; return obj; } /** * listIdentities * @param results - amount of Identities to list * @param token - &lt;code>NextToken&lt;/code> to continue listing identities * @desc ListIdentities call to list Identities in Identity Pool * @returns {array} */ listIdentities( results = 10, token = null ) { return this.p( ( res, rej ) => { const params = { IdentityPoolId: this.IdentityPoolId, MaxResults: results, HideDisabled: true }; if( token ) params.NextToken = token; this.COG.listIdentities( params, ( e, d ) => { if( e ) rej( e ); else res( d ); } ); } ); } /** * GetAuthorization * @param authoritativeProperty * @desc Function returns a &lt;code>AWSCognitoIdentity&lt;/code> and &lt;code>AWSCognitoCredentials&lt;/code> object * Both are needed to assume &lt;code>AWS.Credentials&lt;/code> * returns: {AWSCognitoIdentity, AWSCognitoCredentials} */ GetAuthorization( authoritativeProperty ) { return this.p( ( res, rej ) => { const result = {}; this.AWSIdentityProvider( authoritativeProperty ) .then( r => Object.assign( result, r ) ) .then( r => this.AWSCredentialsProvider( r ) ) .then( r => Object.assign( result, r ) ) .then( res ) .catch( rej ); } ); } /** * AWSIdentityProvider * @param authoritativeProperty * @desc AWSIdentityProvider call to get temporary Cognito IdentityId and Token * returns: {AWSCognitoIdentity &lt;IdentityId, Token>} */ AWSIdentityProvider( authoritativeProperty ) { if( !authoritativeProperty ) throw 'Argument Error - Must have Authoritative Property'; return this.p( ( res, rej ) => { this.COG.getOpenIdTokenForDeveloperIdentity( { IdentityPoolId: this.IdentityPoolId, Logins: { [ this.DeveloperName ]: authoritativeProperty }, TokenDuration: this.TokenDuration }, ( e, r ) => e ? rej( e ) : res( r ) ); } ); } /** * AWSCredentialsProvider * @param AWSCognitoIdentity * @desc AWSCredentialsProvider call to get temporary AccessKeyId, SecretKey, and SessionToken from AWSCognitoIdentity * returns: {AWSCognitoCredentials &lt;IdentityId, Credentials &lt;AccessKeyId, SecretKey, SessionToken, Expiration>>} */ AWSCredentialsProvider( AWSCognitoIdentity, CustomRoleArn ) { return this.p( ( res, rej ) => { this.COG.getCredentialsForIdentity( { IdentityId: AWSCognitoIdentity.IdentityId, CustomRoleArn: CustomRoleArn, Logins: { [ this.Federation ]: AWSCognitoIdentity.Token } }, ( e, r ) => e ? rej( e ) : res( r ) ); } ); } } module.exports = AWSCredentials; </code></pre> </article> </section> </div> <nav> <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="AWSCredentials.html">AWSCredentials</a></li></ul> </nav> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Thu May 04 2017 09:19:44 GMT-0400 (EDT) </footer> <script> prettyPrint(); </script> <script src="scripts/linenumber.js"> </script> </body> </html>