Skip to content

Commit

Permalink
Rearrange code.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredhanson committed Jun 15, 2012
1 parent a31351d commit fc928cf
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 95 deletions.
72 changes: 36 additions & 36 deletions lib/passport-openid/strategy.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -196,6 +196,13 @@ Strategy.prototype.authenticate = function(req) {
} }
} }


Strategy.prototype.saveAssociation = function(fn) {
openid.saveAssociation = function(provider, type, handle, secret, expiry, callback) {
fn(handle, provider, type, secret, expiry, callback)
}
return this; // return this for chaining
}

Strategy.prototype.loadAssociation = function(fn) { Strategy.prototype.loadAssociation = function(fn) {
openid.loadAssociation = function(handle, callback) { openid.loadAssociation = function(handle, callback) {
fn(handle, function(err, provider, algorithm, secret) { fn(handle, function(err, provider, algorithm, secret) {
Expand All @@ -211,83 +218,76 @@ Strategy.prototype.loadAssociation = function(fn) {
return this; // return this for chaining return this; // return this for chaining
} }


Strategy.prototype.saveAssociation = function(fn) {
openid.saveAssociation = function(provider, type, handle, secret, expiry, callback) {
fn(handle, provider, type, secret, expiry, callback)
}
return this; // return this for chaining
}

/** /**
* Register a function used to load discovered info from cache. * Register a function used to cache discovered info.
* *
* Caching discovered information about a provider can significantly speed up * Caching discovered information about a provider can significantly speed up
* the verification of positive assertions. Registering a function allows an * the verification of positive assertions. Registering a function allows an
* application to implement laoding of this info as necessary. * application to implement storage of this info as necessary.
* *
* The function accepts two arguments: `identifier` (which serves as a key to * The function accepts three arguments: `identifier` (which serves as a key to
* the provider information), and `done` a callback to invoke when the * the provider information), `provider` (the provider information being
* information has been loaded. * cached), and `done` a callback to invoke when the information has been
* stored.
* *
* This function is used to retrieve data previously cached with the * After the data has been cached, the corresponding `loadDiscoveredInfo`
* corresponding `saveDiscoveredInfo` function. * function will be used to look it up when needed.
* *
* This corresponds directly to the `loadDiscoveredInformation` provided by the * This corresponds directly to the `saveDiscoveredInformation` provided by the
* underlying node-openid module. Refer to that for more information. * underlying node-openid module. Refer to that for more information.
* *
* Examples: * Examples:
* *
* strategy.loadDiscoveredInfo(function(identifier, done) { * strategy.saveDiscoveredInfo(function(identifier, provider, done) {
* loadInfo(identifier, function(err, provider) { * saveInfo(identifier, provider, function(err) {
* if (err) { return done(err) } * if (err) { return done(err) }
* return done(); * return done();
* }); * });
* }); * };
* *
* @param {Function} fn * @param {Function} fn
* @return {Strategy} for chaining * @return {Strategy} for chaining
* @api public * @api public
*/ */
Strategy.prototype.loadDiscoveredInfo = Strategy.prototype.saveDiscoveredInfo =
Strategy.prototype.loadDiscoveredInformation = function(fn) { Strategy.prototype.saveDiscoveredInformation = function(fn) {
openid.loadDiscoveredInformation = fn; openid.saveDiscoveredInformation = fn;
return this; // return this for chaining return this; // return this for chaining
} }


/** /**
* Register a function used to cache discovered info. * Register a function used to load discovered info from cache.
* *
* Caching discovered information about a provider can significantly speed up * Caching discovered information about a provider can significantly speed up
* the verification of positive assertions. Registering a function allows an * the verification of positive assertions. Registering a function allows an
* application to implement storage of this info as necessary. * application to implement laoding of this info as necessary.
* *
* The function accepts three arguments: `identifier` (which serves as a key to * The function accepts two arguments: `identifier` (which serves as a key to
* the provider information), `provider` (the provider information being * the provider information), and `done` a callback to invoke when the
* cached), and `done` a callback to invoke when the information has been * information has been loaded.
* stored.
* *
* After the data has been cached, the corresponding `loadDiscoveredInfo` * This function is used to retrieve data previously cached with the
* function will be used to look it up when needed. * corresponding `saveDiscoveredInfo` function.
* *
* This corresponds directly to the `saveDiscoveredInformation` provided by the * This corresponds directly to the `loadDiscoveredInformation` provided by the
* underlying node-openid module. Refer to that for more information. * underlying node-openid module. Refer to that for more information.
* *
* Examples: * Examples:
* *
* strategy.saveDiscoveredInfo(function(identifier, provider, done) { * strategy.loadDiscoveredInfo(function(identifier, done) {
* saveInfo(identifier, provider, function(err) { * loadInfo(identifier, function(err, provider) {
* if (err) { return done(err) } * if (err) { return done(err) }
* return done(); * return done();
* }); * });
* }; * });
* *
* @param {Function} fn * @param {Function} fn
* @return {Strategy} for chaining * @return {Strategy} for chaining
* @api public * @api public
*/ */
Strategy.prototype.saveDiscoveredInfo = Strategy.prototype.loadDiscoveredInfo =
Strategy.prototype.saveDiscoveredInformation = function(fn) { Strategy.prototype.loadDiscoveredInformation = function(fn) {
openid.saveDiscoveredInformation = fn; openid.loadDiscoveredInformation = fn;
return this; // return this for chaining return this; // return this for chaining
} }


Expand Down
118 changes: 59 additions & 59 deletions test/strategy-test.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1102,50 +1102,6 @@ vows.describe('OpenIDStrategy').addBatch({
}, },
}, },


'strategy with loadAssociation function': {
topic: function() {
var strategy = new OpenIDStrategy({
returnURL: 'https://www.example.com/auth/openid/return',
},
function(identifier, done) {
done(null, { identifier: identifier });
}
);

strategy.loadAssociation(function(handle, done) {
var provider = { endpoint: 'https://www.google.com/accounts/o8/ud',
version: 'http://specs.openid.net/auth/2.0',
localIdentifier: 'http://www.google.com/profiles/jaredhanson',
claimedIdentifier: 'http://jaredhanson.net' };

strategy._args = {};
strategy._args.handle = handle;
done(null, provider, 'sha256', 'shh-its-secret');
});
return strategy;
},

'after calling openid.loadAssociation': {
topic: function(strategy) {
var self = this;
var handle = 'foo-xyz-123';

openid.loadAssociation(handle, function(err, association) {
self.callback(null, strategy, association);
});
},

'should call registered function' : function(err, strategy) {
assert.equal(strategy._args.handle, 'foo-xyz-123');
},
'should supply provider' : function(err, strategy, association) {
assert.equal(association.provider.endpoint, 'https://www.google.com/accounts/o8/ud');
assert.equal(association.type, 'sha256');
assert.equal(association.secret, 'shh-its-secret');
},
},
},

'strategy with saveAssociation function': { 'strategy with saveAssociation function': {
topic: function() { topic: function() {
var strategy = new OpenIDStrategy({ var strategy = new OpenIDStrategy({
Expand Down Expand Up @@ -1195,7 +1151,7 @@ vows.describe('OpenIDStrategy').addBatch({
}, },
}, },


'strategy with loadDiscoveredInfo function': { 'strategy with loadAssociation function': {
topic: function() { topic: function() {
var strategy = new OpenIDStrategy({ var strategy = new OpenIDStrategy({
returnURL: 'https://www.example.com/auth/openid/return', returnURL: 'https://www.example.com/auth/openid/return',
Expand All @@ -1205,38 +1161,36 @@ vows.describe('OpenIDStrategy').addBatch({
} }
); );


strategy.loadDiscoveredInfo(function(identifier, done) { strategy.loadAssociation(function(handle, done) {
var provider = { endpoint: 'https://www.google.com/accounts/o8/ud', var provider = { endpoint: 'https://www.google.com/accounts/o8/ud',
version: 'http://specs.openid.net/auth/2.0', version: 'http://specs.openid.net/auth/2.0',
localIdentifier: 'http://www.google.com/profiles/jaredhanson', localIdentifier: 'http://www.google.com/profiles/jaredhanson',
claimedIdentifier: 'http://jaredhanson.net' }; claimedIdentifier: 'http://jaredhanson.net' };


strategy._args = {}; strategy._args = {};
strategy._args.identifier = identifier; strategy._args.handle = handle;
done(null, provider); done(null, provider, 'sha256', 'shh-its-secret');
}); });
return strategy; return strategy;
}, },


'should alias function' : function(strategy) { 'after calling openid.loadAssociation': {
assert.strictEqual(strategy.loadDiscoveredInfo, strategy.loadDiscoveredInformation);
},

'after calling openid.loadDiscoveredInformation': {
topic: function(strategy) { topic: function(strategy) {
var self = this; var self = this;
var key = 'http://jaredhanson.net'; var handle = 'foo-xyz-123';


openid.loadDiscoveredInformation(key, function(err, provider) { openid.loadAssociation(handle, function(err, association) {
self.callback(null, strategy, provider); self.callback(null, strategy, association);
}); });
}, },


'should call registered function' : function(err, strategy) { 'should call registered function' : function(err, strategy) {
assert.equal(strategy._args.identifier, 'http://jaredhanson.net'); assert.equal(strategy._args.handle, 'foo-xyz-123');
}, },
'should supply provider' : function(err, strategy, provider) { 'should supply provider' : function(err, strategy, association) {
assert.equal(provider.endpoint, 'https://www.google.com/accounts/o8/ud'); assert.equal(association.provider.endpoint, 'https://www.google.com/accounts/o8/ud');
assert.equal(association.type, 'sha256');
assert.equal(association.secret, 'shh-its-secret');
}, },
}, },
}, },
Expand Down Expand Up @@ -1285,6 +1239,52 @@ vows.describe('OpenIDStrategy').addBatch({
}, },
}, },


'strategy with loadDiscoveredInfo function': {
topic: function() {
var strategy = new OpenIDStrategy({
returnURL: 'https://www.example.com/auth/openid/return',
},
function(identifier, done) {
done(null, { identifier: identifier });
}
);

strategy.loadDiscoveredInfo(function(identifier, done) {
var provider = { endpoint: 'https://www.google.com/accounts/o8/ud',
version: 'http://specs.openid.net/auth/2.0',
localIdentifier: 'http://www.google.com/profiles/jaredhanson',
claimedIdentifier: 'http://jaredhanson.net' };

strategy._args = {};
strategy._args.identifier = identifier;
done(null, provider);
});
return strategy;
},

'should alias function' : function(strategy) {
assert.strictEqual(strategy.loadDiscoveredInfo, strategy.loadDiscoveredInformation);
},

'after calling openid.loadDiscoveredInformation': {
topic: function(strategy) {
var self = this;
var key = 'http://jaredhanson.net';

openid.loadDiscoveredInformation(key, function(err, provider) {
self.callback(null, strategy, provider);
});
},

'should call registered function' : function(err, strategy) {
assert.equal(strategy._args.identifier, 'http://jaredhanson.net');
},
'should supply provider' : function(err, strategy, provider) {
assert.equal(provider.endpoint, 'https://www.google.com/accounts/o8/ud');
},
},
},

'strategy constructed without a validate callback': { 'strategy constructed without a validate callback': {
'should throw an error': function (strategy) { 'should throw an error': function (strategy) {
assert.throws(function() { assert.throws(function() {
Expand Down

0 comments on commit fc928cf

Please sign in to comment.