Skip to content

Commit

Permalink
Adding Date parsing to Identity Token expiration
Browse files Browse the repository at this point in the history
  • Loading branch information
kenperkins committed Mar 20, 2013
1 parent d6c415b commit 63a653f
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
@@ -1,3 +1,5 @@
# Rackspace

The working repository for the node.js package for Rackspace.
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)
Empty file added lib/cli/cli.js
Empty file.
38 changes: 38 additions & 0 deletions lib/client/identity.js
@@ -1,4 +1,5 @@
var request = require('request'),
fs = require('fs'),
_ = require('underscore');

exports.endpoints = {
Expand Down Expand Up @@ -87,13 +88,49 @@ 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;

options = options || {};

if (details.access.token) {
self.token = details.access.token;
self.token.expires = new Date(self.token.expires);
}

if (details.access.serviceCatalog) {
Expand All @@ -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
Expand Down
30 changes: 30 additions & 0 deletions test/identity-tests.js
Expand Up @@ -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 : {
Expand Down

0 comments on commit 63a653f

Please sign in to comment.