From 63a653fcd3b5f06db017c4c120dfa1b3c780bfe5 Mon Sep 17 00:00:00 2001 From: Ken Perkins Date: Wed, 20 Mar 2013 08:36:31 -0700 Subject: [PATCH] Adding Date parsing to Identity Token expiration --- README.md | 4 +++- lib/cli/cli.js | 0 lib/client/identity.js | 38 ++++++++++++++++++++++++++++++++++++++ test/identity-tests.js | 30 ++++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 lib/cli/cli.js diff --git a/README.md b/README.md index 38b4e8c..0504146 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ # Rackspace -The working repository for the node.js package for Rackspace. \ No newline at end of file +The working repository for the node.js package for Rackspace. + +[![Build Status](https://travis-ci.org/kenperkins/rackspace.png)](https://travis-ci.org/kenperkins/rackspace) \ No newline at end of file diff --git a/lib/cli/cli.js b/lib/cli/cli.js new file mode 100644 index 0000000..e69de29 diff --git a/lib/client/identity.js b/lib/client/identity.js index 6f42cfa..1a14c75 100644 --- a/lib/client/identity.js +++ b/lib/client/identity.js @@ -1,4 +1,5 @@ var request = require('request'), + fs = require('fs'), _ = require('underscore'); exports.endpoints = { @@ -87,6 +88,41 @@ exports.authorize = (function() { }; })(); +exports.loadIdentity = function(token, region, callback) { + var inputFilename = process.cwd() + '/' + token + '.json'; + + var raw = require(inputFilename); + + var auth = null; + + try { + auth = new Identity(raw, { + region: region + }); + callback(null, auth); + } + catch (e) { + callback({ + message: 'Unable to parse identity', + error: e + }); + } +}; + +exports.saveIdentity = function(identity, callback) { + var outputFilename = process.cwd() + '/' + identity.token.id + '.json'; + + fs.writeFile(outputFilename, JSON.stringify(identity.raw, null, 4), function(err) { + if (err) { + console.log(err); + } else { + console.log("JSON saved "); + } + + callback(err); + }); +}; + var Identity = function(details, options) { var self = this; @@ -94,6 +130,7 @@ var Identity = function(details, options) { if (details.access.token) { self.token = details.access.token; + self.token.expires = new Date(self.token.expires); } if (details.access.serviceCatalog) { @@ -103,6 +140,7 @@ var Identity = function(details, options) { } self.user = details.access.user; + self.raw = details; }; // TODO rationalize how invalid inputs get bubbled up to callers, considering diff --git a/test/identity-tests.js b/test/identity-tests.js index 8e38ea9..be93940 100644 --- a/test/identity-tests.js +++ b/test/identity-tests.js @@ -128,6 +128,36 @@ describe('Authentication Tests', function() { }); }); + it('Token expires should be a date object', function(done) { + + var cfg = config ? config : { + apiKey: 'asdf1234', + username: 'thisismyusername', + region: 'ORD' + }; + + if (mock) { + nock('https://identity.api.rackspacecloud.com') + .post('/v2.0/tokens', { auth: { + 'RAX-KSKEY:apiKeyCredentials': { + username: cfg.username, + apiKey: cfg.apiKey + } + }}) + .replyWithFile(200, __dirname + '/mock/identity/200-USA-identity-response.json'); + } + + identity.authorize(cfg, function(err, identity) { + + should.not.exist(err); + should.exist(identity); + should.exist(identity.token); + identity.token.expires.should.be.instanceof(Date); + + done(); + }); + }); + it('Should fail to authenticate with bad apiKey & username', function(done) { var cfg = config ? config : {