Skip to content
Browse files

Refactored and documented webfinger.js API.

  • Loading branch information...
1 parent 403012c commit b7e3d96fd126fd96769435f9b7a4917759094ed9 @eschnou committed Feb 27, 2011
View
10 bin/wfinger.js
@@ -24,6 +24,7 @@
*/
var Ostatus = require('ostatus'),
+ Util = require('util'),
Hcard = Ostatus.hcard,
Webfinger = Ostatus.webfinger;
@@ -57,14 +58,7 @@ var _result = function(error, result) {
if (error) {
_error(error);
} else {
- var links = result.documentElement.getElementsByTagName("Link");
- for (i=0;i<links.length;i++) {
- var attributes = links[i].attributes;
- var aHref = attributes.getNamedItem("href");
- var rel = attributes.getNamedItem("rel");
- if (aHref != undefined && rel != undefined)
- console.log(rel.nodeValue + ": " + aHref.nodeValue);
- }
+ console.log(Util.inspect(result));
}
};
View
0 lib/ostatus/templates/xrd.xml.mu → lib/ostatus/templates/hostXrd.xml.mu
File renamed without changes.
View
0 lib/ostatus/templates/lrdd.xml.mu → lib/ostatus/templates/userXrd.xml.mu
File renamed without changes.
View
135 lib/ostatus/webfinger.js
@@ -29,38 +29,89 @@ var Sys = require('sys')
,Mu = require('mu')
,Http = require('./http.js');
-function xrd(host, callback) {
+/*
+ * Generate the domain's host-meta XRD.
+ *
+ * This function simply process the hostXrd.xml.mu template and output the result. The template
+ * is a fairly basic one with just a LRDD link indicating how to fetch a user XRD.
+ *
+ * @param {String} host
+ * the domain name
+ * @param {Function} callback
+ * a callback function in the form function(err, result)
+ *
+ */
+function hostXrd(host, callback) {
var context = {host: host};
- Mu.compile('xrd.xml.mu', function (err, parsed) {
+ var buffer = '';
+
+ Mu.compile('hostXrd.xml.mu', function (err, parsed) {
if (err) return callback(err);
-
- var buffer = '';
Mu.render(parsed, context)
.on('data', function (c) { buffer += c.toString(); })
- .on('end', function () {
- callback(null, buffer);
- });
- });
+ .on('end', function () { callback(null, buffer); });
+ });
}
-function lrdd(subject, alias, links, callback) {
+
+/*
+ * Generate a user's XRD.
+ *
+ * This function simply process the userXrd.xml.mu template and fill-in the subject, alias and links.
+ *
+ * @param {String} subject
+ * the value of the subject item. This should be the acct uri that is being served.
+ * @param {String} alias
+ * an alias field. Usually a HTTP URI identifying the same user resource.
+ * @param {Array} links
+ * an array of links to be added in the XRD.
+ * @param {Function} callback
+ * a callback function in the form function(err, result)
+ *
+ * The links have the form:
+ * var link = {
+ * "href": "..",
+ * "rel" : "..",
+ * "type": "..",
+ * "ref" : "..
+ * }
+ *
+ * TODO Allow for more than one alias (array instead of single value)
+ *
+ */
+function userXrd(subject, alias, links, callback) {
+ var buffer = '';
var context = {
subject: subject
,alias: alias
,links: links
};
- Mu.compile('lrdd.xml.mu', function (err, parsed) {
+ Mu.compile('userXrd.xml.mu', function (err, parsed) {
if (err) return callback(err);
- var buffer = '';
Mu.render(parsed, context)
.on('data', function (c) { buffer += c.toString(); })
- .on('end', function () {
- callback(null, buffer);
- });
- });
+ .on('end', function () {callback(null, buffer);});
+ });
}
+/*
+ * Perform a webfinger lookup for the provided acct: uri, the result is a JSON representation
+ * of the user XRD.
+ *
+ * @param {String} identifier
+ * a string representation of a valid acct: uri
+ * @param {Function} callback
+ * a callback function of the form function(err, result)
+ *
+ * The XRD object has the form:
+ * var xrd = {
+ * "subject": the subject of the xrd lookup
+ * "alias" : an array of strings (a xrd can have more than one alias)
+ * "links" : an array of links
+ * }
+ *
+ */
function lookup(identifier, callback) {
var url = Url.parse(identifier);
if (url.protocol != "acct:") {
@@ -70,11 +121,25 @@ function lookup(identifier, callback) {
_fetchHostMeta(url.hostname, function (err, template) {
if (err) return callback(err);
_fetchUserMeta(template, identifier, function (err, meta) {
- callback(err,meta);
+ if (err) return callback(err);
+ return callback(null, _parseUserMeta(meta));
});
});
}
+/*
+ * Parse a string representation of an acct uri and return the username and
+ * hostname parts. The input can ommit the acct: prefix.
+ *
+ * @param {String} pAcct
+ * a string representing an acct: uri (prefix can be omitted)
+ *
+ * @return
+ * an object with a username and hostname property
+ *
+ * Example: parseAcct("user@example.com") will return:
+ * { "username": "user", "hostname": "example.com"}
+ */
function parseAcct(pAcct) {
// Add the acct prefix if required
if (pAcct<5 || pAcct.substring(0,5) != "acct:") {
@@ -94,6 +159,7 @@ function parseAcct(pAcct) {
return result;
}
+/////// Private helper functions
function _fetchHostMeta(host, callback) {
var url = "http://" + host + "/.well-known/host-meta";
console.log("Requesting host meta at " + url);
@@ -124,7 +190,40 @@ function _fetchUserMeta(template, identifier, callback) {
});
}
+function _parseUserMeta(xml) {
+ var result = {
+ "alias": [],
+ "links": []
+ };
+
+ // parse the subject
+ var subjects = xml.documentElement.getElementsByTagName("Subject");
+ if (subjects.length > 0) {
+ result.subject = subjects[0].nodeValue;
+ }
+
+ // parse the links
+ var links = xml.documentElement.getElementsByTagName("Link");
+ for (i=0;i<links.length;i++) {
+ var link = {};
+ var attributes = links[i].attributes;
+ if (att = attributes.getNamedItem("href")) link.href = att.nodeValue;
+ if (att = attributes.getNamedItem("ref")) link.ref = att.nodeValue;
+ if (att = attributes.getNamedItem("type")) link.type = att.nodeValue;
+ if (att = attributes.getNamedItem("rel")) link.rel = att.nodeValue;
+ result.links.push(link);
+ }
+
+ // parse the aliases
+ var alias = xml.documentElement.getElementsByTagName("Alias");
+ for (i=0;i<alias.length;i++) {
+ result.alias.push(alias[i].nodeValue);
+ }
+
+ return result;
+}
+
exports.lookup = lookup;
-exports.xrd = xrd;
-exports.lrdd = lrdd;
+exports.hostXrd = hostXrd;
+exports.userXrd = userXrd;
exports.parseAcct = parseAcct;

0 comments on commit b7e3d96

Please sign in to comment.
Something went wrong with that request. Please try again.