Permalink
Browse files

Salmon enveloping

  • Loading branch information...
1 parent 731d580 commit 11b1b8c4a36b11ef6801e52a8a4e9caa51de1312 @astro committed Jun 20, 2011
Showing with 80 additions and 2 deletions.
  1. +39 −0 bin/send_salmon.coffee
  2. +37 −2 lib/ostatus/salmon.js
  3. +4 −0 tests/test_salmon.js
View
@@ -0,0 +1,39 @@
+#!/usr/bin/env coffee
+
+fs = require('fs')
+request = require('request')
+Salmon = require('../lib/ostatus').salmon;
+
+
+if process.argv.length isnt 5
+ console.error "Usage: #{process.argv[0]} #{process.argv[1]} <atom.xml> <private.key> <http://salmon/endpoint>"
+ process.exit 1
+
+# Load parameters
+atomPayload = fs.readFileSync process.argv[2]
+privKey = fs.readFileSync process.argv[3]
+salmonEndpoint = process.argv[4]
+
+# Sign & prepare envelope
+envelope = "<?xml version='1.0' encoding='UTF-8'?>\n" +
+ Salmon.signEnvelopeXML(atomPayload, privKey).toString()
+console.log 'Generated envelope: ' + envelope
+
+# Send envelope via HTTP
+opts =
+ method: 'POST'
+ uri: salmonEndpoint
+ body: envelope
+ headers:
+ 'Content-Type': 'application/magic-envelope+xml'
+
+request opts, (error, response, body) ->
+ if error
+ console.error error.message
+ return
+ if response.statusCode is 200
+ console.log body
+ else
+ console.error response.statusCode
+ console.log body
+
View
@@ -28,7 +28,7 @@ var Sys = require('sys'),
Util = require('util'),
Http = require('./http.js'),
Path = require('path'),
- Mu = require('mu'),
+ LTX = require('ltx'),
Buffer = require('buffer').Buffer;
/*
@@ -106,9 +106,42 @@ function _grabKey(jrd) {
}
}
+function signEnvelopeXML(data, privKey, data_type, key_id) {
+ var me = signEnvelopeJSON(data, privKey, data_type, key_id);
+ var meEl = new LTX.Element('me:env',
+ { 'xmlns:me': 'http://salmon-protocol.org/ns/magic-env' }).
+ c('me:data',
+ { type: me.data_type }).t(me.data).up().
+ c('me:encoding').t(me.encoding).up().
+ c('me:alg').t(me.alg).up();
+ me.sigs.forEach(function(sig) {
+ var sigEl = meEl.c('me:sig').t(sig.value);
+ if (sig.key_id)
+ sigEl.attrs.key_id = sig.key_id;
+ });
+ return meEl;
+}
+
+function signEnvelopeJSON(data, privKey, data_type, key_id) {
+ var me = {
+ data: base64url_encode(data),
+ data_type: data_type || 'application/atom+xml',
+ encoding: 'base64url',
+ alg: 'RSA-SHA256'
+ };
+
+ var sig = {};
+ sig.value = base64url_encode(generateSignature(me, privKey));
+ if (key_id)
+ sig.key_id = key_id;
+
+ me.sigs = [sig];
+ return me;
+}
+
// Assemble the signature base string
function baseString(data, data_type, encoding, alg) {
- return [base64url_encode(data),
+ return [data /* already transported base64url-encoded */,
base64url_encode(data_type),
base64url_encode(encoding),
base64url_encode(alg)
@@ -164,3 +197,5 @@ exports.base64url_encode = base64url_encode;
exports.generateKeys = generateKeys;
exports.generateSignature = generateSignature;
exports.verifySignature = verifySignature;
+exports.signEnvelopeJSON = signEnvelopeJSON;
+exports.signEnvelopeXML = signEnvelopeXML;
View
@@ -69,4 +69,8 @@ Vows.describe('Salmon').addBatch({
}
}
+ /* More test cases can be found at:
+ * http://code.google.com/p/salmon-protocol/source/browse/trunk/lib/python/magicsig/magicsig_test.py
+ */
+
}).run();

0 comments on commit 11b1b8c

Please sign in to comment.