file_system_credentials.js (2134B)
1 var AWS = require('../core'); 2 3 /** 4 * Represents credentials from a JSON file on disk. 5 * If the credentials expire, the SDK can {refresh} the credentials 6 * from the file. 7 * 8 * The format of the file should be similar to the options passed to 9 * {AWS.Config}: 10 * 11 * ```javascript 12 * {accessKeyId: 'akid', secretAccessKey: 'secret', sessionToken: 'optional'} 13 * ``` 14 * 15 * @example Loading credentials from disk 16 * var creds = new AWS.FileSystemCredentials('./configuration.json'); 17 * creds.accessKeyId == 'AKID' 18 * 19 * @!attribute filename 20 * @readonly 21 * @return [String] the path to the JSON file on disk containing the 22 * credentials. 23 * @!macro nobrowser 24 */ 25 AWS.FileSystemCredentials = AWS.util.inherit(AWS.Credentials, { 26 27 /** 28 * @overload AWS.FileSystemCredentials(filename) 29 * Creates a new FileSystemCredentials object from a filename 30 * 31 * @param filename [String] the path on disk to the JSON file to load. 32 */ 33 constructor: function FileSystemCredentials(filename) { 34 AWS.Credentials.call(this); 35 this.filename = filename; 36 this.get(function() {}); 37 }, 38 39 /** 40 * Loads the credentials from the {filename} on disk. 41 * 42 * @callback callback function(err) 43 * Called after the JSON file on disk is read and parsed. When this callback 44 * is called with no error, it means that the credentials information 45 * has been loaded into the object (as the `accessKeyId`, `secretAccessKey`, 46 * and `sessionToken` properties). 47 * @param err [Error] if an error occurred, this value will be filled 48 * @see get 49 */ 50 refresh: function refresh(callback) { 51 if (!callback) callback = function(err) { if (err) throw err; }; 52 try { 53 var creds = JSON.parse(AWS.util.readFileSync(this.filename)); 54 AWS.Credentials.call(this, creds); 55 if (!this.accessKeyId || !this.secretAccessKey) { 56 throw AWS.util.error( 57 new Error('Credentials not set in ' + this.filename), 58 { code: 'FileSystemCredentialsProviderFailure' } 59 ); 60 } 61 this.expired = false; 62 callback(); 63 } catch (err) { 64 callback(err); 65 } 66 } 67 68 });