var webfinger = require('webfinger').webfinger
  , configuration = require('../configuration').configuration;

module.exports = function() {
  
  
  return function(identifier, done) {
    if (!identifier) { return done(); }
  
  
    console.log('WEBFINGER: ' + identifier);
    
    webfinger(identifier, 'http://openid.net/specs/connect/1.0/issuer', function(err, jrd) {
      if (err) {
        console.log('WebFinger error');
        console.log(err);
        return done(err);
      };
      
      console.log('JRD:');
      console.log(jrd);
      
      var issuer;
      for (var i = 0; i < jrd.links.length; i++) {
        var link = jrd.links[i];
        if (link.rel == 'http://openid.net/specs/connect/1.0/issuer') {
          issuer = link.href;
          break;
        }
      }
      
      if (!issuer) { return done(new Error('No OpenID Connect issuer found in resource descriptor')); }
      
      console.log('FOUND ISSUER: ' + issuer);
      
      configuration(issuer, function(err, config) {
        if (err) {
          console.log('configuration error');
          console.log(err);
          return done(err);
        };
        
        console.log('CONFIG:');
        console.log(config);
        
      });
    });
  }
}
@@ -1,12 +1,44 @@
+/**%0A * Module dependencies.%0A */%0A
 var webfinge
@@ -136,16 +136,706 @@
 ation;%0A%0A
+%0A/**%0A * Discover OpenID Connect provider configuration using WebFinger.%0A *%0A * This discovery mechanism uses WebFinger to discover the issuer for a%0A * user-supplied identifier.  Once the issuer is known, it's configuration is%0A * loaded.%0A *%0A * Note: Prior to draft 12, OpenID Connect Discovery used Simple Web Discovery%0A *       rather than WebFinger.  At the time of writing, many provider%0A *       implementations continue to implement SWD.%0A *%0A * References:%0A *   - %5BOpenID Connect Discovery 1.0 - draft 12%5D(http://openid.net/specs/openid-connect-discovery-1_0.html)%0A *   - %5BWebFinger%5D(http://tools.ietf.org/html/draft-ietf-appsawg-webfinger-10)%0A *%0A * @return %7BFunction%7D%0A * @api public%0A */%0A
 module.e
@@ -859,19 +859,16 @@
 () %7B%0A  %0A
-  %0A
   return
@@ -941,59 +941,8 @@
 ; %7D%0A
-  %0A  %0A    console.log('WEBFINGER: ' + identifier);%0A
     
@@ -1055,82 +1055,8 @@
 r) %7B
-%0A        console.log('WebFinger error');%0A        console.log(err);%0A       
  ret
@@ -1069,22 +1069,16 @@
 ne(err);
-%0A     
  %7D;%0A    

Add documentation to WebFinger discovery.