Permalink
Browse files

added ability to make hostMetaProxy optional and the ability to confi…

…gure the proxy
  • Loading branch information...
1 parent 4f36a21 commit 749c4d640260373e59860258fce8671ba4e284c0 @snoble committed Jan 16, 2012
Showing with 49 additions and 28 deletions.
  1. +49 −28 openid.js
View
@@ -39,24 +39,25 @@ var _discoveries = {};
var openid = exports;
-openid.RelyingParty = function(returnUrl, realm, stateless, strict, extensions)
+openid.RelyingParty = function(returnUrl, realm, stateless, strict, extensions, hostMetaProxy)
{
this.returnUrl = returnUrl;
this.realm = realm || null;
this.stateless = stateless;
this.strict = strict;
+ this.hostMetaProxy = hostMetaProxy;
this.extensions = extensions;
}
openid.RelyingParty.prototype.authenticate = function(identifier, immediate, callback)
-{
+{
openid.authenticate(identifier, this.returnUrl, this.realm,
- immediate, this.stateless, callback, this.extensions, this.strict);
+ immediate, this.stateless, callback, this.extensions, this.strict, this.hostMetaProxy);
}
openid.RelyingParty.prototype.verifyAssertion = function(requestOrUrl, callback)
{
- openid.verifyAssertion(requestOrUrl, callback, this.stateless, this.extensions);
+ openid.verifyAssertion(requestOrUrl, callback, this.stateless, this.extensions, this.hostMetaProxy);
}
var _isDef = function(e)
@@ -524,30 +525,50 @@ var _resolveHtml = function(identifier, callback, hops, data)
}
-var _resolveHostMeta = function(identifier, callback, fallBackToProxy)
+var _resolveHostMeta = function(identifier, hostMetaProxy, callback, fallBackToProxy)
{
var host = url.parse(identifier);
- hostMetaUrl = fallBackToProxy ? 'https://www.google.com/accounts/o8/.well-known/host-meta?hd=' + host.host : host.protocol + '://' + host.host + '/.well-known/host-meta';
- _get(hostMetaUrl, null, function(data, headers, statusCode)
+ var hostMetaUrl;
+ if(fallBackToProxy && hostMetaProxy)
{
- if(statusCode != 200 || data == null)
+ hostMetaUrl = (typeof hostMetaProxy == 'function') ? hostMetaProxy(identifier, host.host) : hostMetaProxy + host.host
+ }
+ else
+ {
+ hostMetaUrl = host.protocol + '://' + host.host + '/.well-known/host-meta';
+ }
+ if(!hostMetaUrl)
+ {
+ callback(null);
+ }
+ else
+ {
+ _get(hostMetaUrl, null, function(data, headers, statusCode)
{
- if(!fallBackToProxy){
- _resolveHostMeta(identifier, callback, true);
+ if(statusCode != 200 || data == null)
+ {
+ if(!fallBackToProxy){
+ _resolveHostMeta(identifier, hostMetaProxy, callback, true);
+ }
+ else{
+ callback(null);
+ }
}
- else{
- callback(null);
+ else
+ {
+ _parseHostMeta(data, callback);
}
- }
- else
- {
- _parseHostMeta(data, callback);
- }
- });
+ });
+ }
}
openid.discover = function(identifier, callback)
{
+ openid.discoverWithHostMetaProxy(identifier, null, callback);
+}
+
+openid.discoverWithHostMetaProxy = function(identifier, hostMetaProxy, callback)
+{
identifier = _normalizeIdentifier(identifier);
if(!identifier)
{
@@ -568,7 +589,7 @@ openid.discover = function(identifier, callback)
_resolveHtml(identifier, function(providers)
{
if(providers == null || providers.length == 0){
- _resolveHostMeta(identifier, function(providers){
+ _resolveHostMeta(identifier, hostMetaProxy, function(providers){
callback(null, providers);
});
}
@@ -765,9 +786,9 @@ var _generateAssociationRequestParameters = function(version, algorithm)
return params;
}
-openid.authenticate = function(identifier, returnUrl, realm, immediate, stateless, callback, extensions, strict)
+openid.authenticate = function(identifier, returnUrl, realm, immediate, stateless, callback, extensions, strict, hostMetaProxy)
{
- openid.discover(identifier, function(error, providers)
+ openid.discoverWithHostMetaProxy(identifier, hostMetaProxy, function(error, providers)
{
if(error)
{
@@ -916,7 +937,7 @@ var _requestAuthentication = function(provider, assoc_handle, returnUrl, realm,
callback(null, _buildUrl(provider.endpoint, params));
}
-openid.verifyAssertion = function(requestOrUrl, callback, stateless, extensions)
+openid.verifyAssertion = function(requestOrUrl, callback, stateless, extensions, hostMetaProxy)
{
extensions = extensions || {};
var assertionUrl = requestOrUrl;
@@ -933,7 +954,7 @@ openid.verifyAssertion = function(requestOrUrl, callback, stateless, extensions)
requestOrUrl.on('end', function() {
var params = querystring.parse(data);
- return _verifyAssertionData(params, callback, stateless, extensions);
+ return _verifyAssertionData(params, callback, stateless, extensions, hostMetaProxy);
});
}
else {
@@ -951,10 +972,10 @@ openid.verifyAssertion = function(requestOrUrl, callback, stateless, extensions)
assertionUrl = url.parse(assertionUrl, true);
var params = assertionUrl.query;
- return _verifyAssertionData(params, callback, stateless, extensions);
+ return _verifyAssertionData(params, callback, stateless, extensions, hostMetaProxy);
}
-var _verifyAssertionData = function(params, callback, stateless, extensions) {
+var _verifyAssertionData = function(params, callback, stateless, extensions, hostMetaProxy) {
var assertionError = _getAssertionError(params);
if(assertionError)
{
@@ -967,7 +988,7 @@ var _verifyAssertionData = function(params, callback, stateless, extensions) {
// TODO: Check nonce if OpenID 2.0
- _verifyDiscoveredInformation(params, stateless, extensions, function(error, result)
+ _verifyDiscoveredInformation(params, stateless, extensions, hostMetaProxy, function(error, result)
{
return callback(error, result);
});
@@ -996,7 +1017,7 @@ var _checkValidHandle = function(params)
return !_isDef(params['openid.invalidate_handle']);
}
-var _verifyDiscoveredInformation = function(params, stateless, extensions, callback)
+var _verifyDiscoveredInformation = function(params, stateless, extensions, hostMetaProxy, callback)
{
var claimedIdentifier = params['openid.claimed_id'];
var useLocalIdentifierAsKey = false;
@@ -1035,7 +1056,7 @@ var _verifyDiscoveredInformation = function(params, stateless, extensions, callb
return callback({ message: 'OpenID 1.0/1.1 response received, but no information has been discovered about the provider. It is likely that this is a fraudulent authentication response.' });
}
- openid.discover(claimedIdentifier, function(error, providers)
+ openid.discoverWithHostMetaProxy(claimedIdentifier, hostMetaProxy, function(error, providers)
{
if(error)
{

0 comments on commit 749c4d6

Please sign in to comment.