Skip to content

Commit

Permalink
Refactored {load,save}DiscoveredInformation to a cleaner interface, a…
Browse files Browse the repository at this point in the history
…nd moved validation logic to the OpenID library to avoid requiring implementers to validate OpenID relations.
  • Loading branch information
havard committed Sep 5, 2011
1 parent 5a852cd commit f94889f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 36 deletions.
17 changes: 6 additions & 11 deletions README.md
Expand Up @@ -4,7 +4,7 @@ OpenID for node.js is (yes, you guessed it) an OpenID implementation for node.js

Highlights and features include:

- Full OpenID 1.1/OpenID 2.0 compliant Relying Party (client) implementation
- Full OpenID 1.0/1.1/2.0 compliant Relying Party (client) implementation
- Very simple API
- Simple extension points for association state

Expand All @@ -16,10 +16,8 @@ The library can be [reviewed and retrieved from GitHub](http://github.com/havard

If you use [`npm`](http://npmjs.org), simply do `npm install openid`.

If you don't use npm, you should. Alternatively, you can download the library, and move the
`lib` folder and `openid.js` to where you want them, and then `require('openid')`.
(Remember to do `require.paths.unshift` on the directory you put the file in unless it's
already in your `require.paths`.)
Otherwise, you can grab the code from [GitHub](https://github.com/havard/node-openid)
and

## Examples

Expand Down Expand Up @@ -109,15 +107,12 @@ The `openid` module includes default implementations for these functions using a

The verification of a positive assertion (i.e. an authenticated user) can be sped up significantly by avoiding the need for additional provider discoveries when possible. In order to achieve, this speed-up, node-openid needs to cache its discovered providers. You can mix-in two functions to override the default cache, which is an in-memory cache utilizing a simple object store:

- `saveDiscoveredInformation(provider, useLocalIdentifierAsKey, callback)` is used when saving a discovered provider. The following behavior is required:
-
- If `useLocalIdentifierAsKey` is `true`, the `provider.localIdentifier` shall be used as the key for this object. Otherwise, the `provider.claimedIdentifier` attribute shall be used the key for this object. The keys will be used for lookup later, when attempting to reuse this discovered information through `loadDiscoveredInformation`.
- The function should validate that the provider has the appropriate attributes (i.e. that it has a `localIdentifier` if `useLocalIdentifierAsKey` is `true`, or a `claimedIdentifier` otherwise) before saving the provider.
- `saveDiscoveredInformation(key, provider, callback)` is used when saving a discovered provider. The following behavior is required:
- The `key` parameter should be uses as a key for storing the provider - it will be used as the lookup key when loading the provider. (Currently, the key is either a claimed identifier or an OP-local identifier, depending on the OpenID context.)
- When saving fails for some reason, `callback(error)` is called with `error` being an error object specifying what failed.
- When saving succeeds, `callback(null)` is called.

- `loadDiscoveredInformation(identifier, callback)` is used to load any previously discovered information about the provider for an identifier. The following behavior is required:

- `loadDiscoveredInformation(key, callback)` is used to load any previously discovered information about the provider for an identifier. The following behavior is required:
- When no provider is found for the identifier, `callback(null, null)` is called (i.e. it is not an error to not have any data to return).
- When loading fails for some reason, `callback(error, null)` is called with `error` being an error string specifying why loading failed.
- When loading succeeds, `callback(null, provider)` is called with the exact provider object that was previously stored using `saveDiscoveredInformation`.
Expand Down
44 changes: 19 additions & 25 deletions openid.js
Expand Up @@ -124,32 +124,20 @@ openid.removeAssociation = function(handle)
return true;
}

openid.saveDiscoveredInformation = function(provider, useLocalIdentifierAsKey, callback)
openid.saveDiscoveredInformation = function(key, provider, callback)
{
if((!useLocalIdentifierAsKey && !provider.claimedIdentifier) || (useLocalIdentifierAsKey && !provider.localIdentifier))
{
return callback({ message: 'The provider does not contain the expected key identifier' });
}

if (useLocalIdentifierAsKey)
{
_discoveries[provider.localIdentifier] = provider;
}
else
{
_discoveries[provider.claimedIdentifier] = provider;
}
_discoveries[key] = provider;
return callback(null);
}

openid.loadDiscoveredInformation = function(identifier, callback)
openid.loadDiscoveredInformation = function(key, callback)
{
if(!_isDef(_discoveries[identifier]))
if(!_isDef(_discoveries[key]))
{
return callback(null, null);
}

return callback(null, _discoveries[identifier]);
return callback(null, _discoveries[key]);
}

var _buildUrl = function(theUrl, params)
Expand Down Expand Up @@ -647,15 +635,15 @@ openid.associate = function(provider, callback, strict, algorithm)
}
/*else if(provider.version.indexOf('2.0') === -1)
{
// 2011-07-22: This is an OpenID 1.1 provider which means
// 2011-07-22: This is an OpenID 1.0/1.1 provider which means
// HMAC-SHA1 has already been attempted with a blank session
// type as per the OpenID 1.1 specification.
// type as per the OpenID 1.0/1.1 specification.
// (See http://openid.net/specs/openid-authentication-1_1.html#mode_associate)
// However, providers like wordpress.com don't follow the
// standard and reject these requests, but accept OpenID 2.0
// style requests without a session type, so we have to give
// those a shot as well.
callback({ message: 'Provider is OpenID 1.1 and does not support OpenID 1.1 association.' });
callback({ message: 'Provider is OpenID 1.0/1.1 and does not support OpenID 1.0/1.1 association.' });
}*/
else
{
Expand Down Expand Up @@ -727,7 +715,7 @@ var _generateAssociationRequestParameters = function(version, algorithm)
{
if(version.indexOf('2.0') === -1)
{
params['openid.session_type'] = ''; // OpenID 1.1 requires blank
params['openid.session_type'] = ''; // OpenID 1.0/1.1 requires blank
params['openid.assoc_type'] = 'HMAC-SHA1';
}
else
Expand Down Expand Up @@ -775,7 +763,14 @@ openid.authenticate = function(identifier, returnUrl, realm, immediate, stateles
var provider = providers[providerIndex];
if(provider.claimedIdentifier)
{
return openid.saveDiscoveredInformation(provider, provider.version.indexOf('2.0') === -1, function(error)
var useLocalIdentifierAsKey = provider.version.indexOf('2.0') === -1;
if((!useLocalIdentifierAsKey && !provider.claimedIdentifier) || (useLocalIdentifierAsKey && !provider.localIdentifier))
{
return callback({ message: 'Cannot retain discovered information; the provider does not contain the required attributes' });
}

return openid.saveDiscoveredInformation(useLocalIdentifierAsKey ? provider.localIdentifier : provider.claimedIdentifier,
provider, function(error)
{
if(error)
{
Expand All @@ -789,7 +784,7 @@ openid.authenticate = function(identifier, returnUrl, realm, immediate, stateles
return callback(null, authUrl);
}
else {
successOrNext({ message: 'OpenID 1.1 provider cannot be used without a claimed identifier' });
successOrNext({ message: 'OpenID 1.0/1.1 provider cannot be used without a claimed identifier' });
}
}
if(++providerIndex >= providers.length)
Expand Down Expand Up @@ -847,7 +842,6 @@ var _requestAuthentication = function(provider, assoc_handle, returnUrl, realm,
}
}

// TODO: 1.1 compatibility
if(provider.claimedIdentifier)
{
params['openid.claimed_id'] = provider.claimedIdentifier;
Expand All @@ -866,7 +860,7 @@ var _requestAuthentication = function(provider, assoc_handle, returnUrl, realm,
'http://specs.openid.net/auth/2.0/identifier_select';
}
else {
return callback({ message: 'OpenID 1.1 provider cannot be used without a claimed identifier' });
return callback({ message: 'OpenID 1.0/1.1 provider cannot be used without a claimed identifier' });
}

if(assoc_handle)
Expand Down

0 comments on commit f94889f

Please sign in to comment.