diff --git a/api/federation.js b/api/federation.js new file mode 100644 index 0000000..a09e420 --- /dev/null +++ b/api/federation.js @@ -0,0 +1,38 @@ +var config = require('../config'); +var response = require('response'); +var libutils = require('../lib/utils'); + +exports.store; +var federation = function (req, res) { + if (!req.query.domain || req.query.domain !== config.federation.domain) { + response.json({"error":"invalidParams","result":"error","error_message":"Invalid domain"}).status(400).pipe(res); + return; + } + + if (!req.query.destination) { + if (req.query.user) { // Compatability + req.query.destination = req.query.user; + } else { + response.json({"error":"invalidParams","result":"error","error_message":"No destination provided"}).status(400).pipe(res); + return; + } + } + + var normalized_username = libutils.normalizeUsername(req.query.destination); + + exports.store.read({username:normalized_username,res:res},function(resp) { + if (resp.exists) { + // this is a 200 + response.json({ federation_json: { + type: "federation_record", + destination: req.query.destination, + domain: config.federation.domain, + destination_address: resp.address + }}).pipe(res); + } else { + response.json({"error":"noSuchUser","result":"error","error_message":"No such alias on that domain"}).status(404).pipe(res); + } + }); +}; + +exports.federation = federation; diff --git a/api/index.js b/api/index.js index 365794c..ad7da7b 100644 --- a/api/index.js +++ b/api/index.js @@ -1,8 +1,10 @@ exports.user = require('./user'); +exports.federation = require('./federation'); exports.blob = require('./blob'); exports.meta = require('./meta'); exports.setStore = function(store) { exports.user.store = store; + exports.federation.store = store; exports.blob.setStore(store); }; var error = require('../error'); diff --git a/config-example.js b/config-example.js index 8e4e144..f61aad7 100644 --- a/config-example.js +++ b/config-example.js @@ -8,6 +8,11 @@ exports.port = 8080; // Public URL for this blobvault (required for authinfo) exports.url = "https://blobvault.example.com"; +// Federation names provider settings +exports.federation = { + domain: "example.com" +}; + // SSL settings exports.ssl = false; diff --git a/server.js b/server.js index 4ff256e..a297ecb 100644 --- a/server.js +++ b/server.js @@ -35,6 +35,7 @@ app.post('/v1/user/:username', guard.locked, ecdsa.middleware, api.user.rename); app.delete('/v1/user', guard.locked, hmac.middleware, api.blob.delete); app.get('/v1/user/:username', api.user.get); app.get('/v1/user/:username/verify/:token', api.user.verify); +app.get('/v1/federation', api.federation.federation); // JSON handlers app.get('/v1/blob/:blob_id', api.blob.get);