Skip to content

Commit

Permalink
merged signing branch
Browse files Browse the repository at this point in the history
  • Loading branch information
piatra committed Aug 8, 2013
2 parents 2abf3be + a881865 commit 7cc2876
Show file tree
Hide file tree
Showing 44 changed files with 2,309 additions and 266 deletions.
51 changes: 49 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var express = require('express')
, assets = require('./routes/assets')
, auth = require('./routes/auth')
, http = require('http')
, user = require('./routes/user')
, path = require('path');

var app = express();
Expand All @@ -32,19 +33,65 @@ app.configure('development', function(){
app.use(express.errorHandler());
});

function requireLogin(req, res, next) {
if (req.session.email) {
next(); // allow the next route to run
} else {
// require the user to log in
res.redirect("/login"); // or render a form, etc.
}
}

app.get('/', routes.index);

app.get('/login', routes.login);

app.post('/user/set/username', user.setUsername);

app.post('/auth/verify', auth.verify);
app.post('/user/payswarm', auth.payswarmVerify);

app.get('/auth/createKeyPair', auth.createKeyPair);
app.post('/payswarm/register', auth.registerKey);
app.post('/payswarm/complete', auth.complatePayswarmRegistration);

app.get('/newasset', routes.newasset);
app.post('/newasset/process/', assets.processAsset);

/*
New asset
*/
app.get('/newasset', requireLogin, routes.newasset);
app.post('/newasset/process/', assets.createAssetAndListing);
app.post('/newasset/save', assets.saveAsset);

app.get('/assets/created', assets.getUserAssets);
app.get('/assets/:count', assets.getLatestAssets);

app.get('/resign/listing/:id', assets.resignListing);

app.get('/assets/asset/:id', assets.getAsset);

app.get('/assets/asset/:id/purchase', assets.purchase);
app.post('/assets/asset/:id/purchased', assets.purchased);
app.post('/assets/asset/:id/preview', assets.preview);
app.get('/assets/asset/:id/preview', assets.preview);

app.get('/listings/listing/:id', assets.getListing);

app.get('/assets/asset/:id/content', function (req, res){
res.end('The content!');
});

app.get('/decrypt/:type/:id', assets.decrypt);

app.get('/test', function (req, res){
res.render('test');
});

app.post('/upload', function (req, res) {
console.log(req.files);
res.json(req.files);
});

http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
73 changes: 68 additions & 5 deletions lib/asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ var _ = require('underscore'),
utils = require('../lib/utils')(),
payswarm = require('payswarm'),
mongoose = require('mongoose'),
db = require('../lib/db')
db = require(__dirname + '/db.js'),
host = 'webpayments.fwd.wf';
;

module.exports = function (options) {
Expand All @@ -24,9 +25,6 @@ module.exports = function (options) {
cb(err);
} else {
// set the asset to the listing
signedListing.asset = signedAsset.id;
signedListing.assetHash = assetHash;
signedListing.licenseHash = listingHash;
var assetAndListing = {
"@context": 'https://w3id.org/payswarm/v1',
"@graph": [signedAsset, signedListing]
Expand Down Expand Up @@ -136,6 +134,40 @@ module.exports = function (options) {
db.save.asset(graph, cb);
}

function getOne (query, cb) {
getOneAsset(query, function (asset) {
getOneListing({'assetId': query['_id']}, function (listing) {
var assetAndListing = {
"@context": 'https://w3id.org/payswarm/v1',
"@graph": [asset[0], listing[0]]
}
cb(assetAndListing);
});
});
}

function getOneAsset (query, cb) {
db.get.asset(query, function (err, doc) {
if (err) {
console.log('Error getting one asset' + err);
cb(err, {});
} else {
cb(null, doc);
}
});
}

function getOneListing (query, cb) {
db.get.listing(query, function (err, doc) {
if (err) {
console.log('Error getting one listing' + err);
cb({});
} else {
cb(doc);
}
});
}

function getUserAssets (query, cb) {
db.get.userAssets(query, function (err, assets) {
if (err) {
Expand All @@ -144,8 +176,10 @@ module.exports = function (options) {
} else {
var docs = [];
var l = assets.length;
console.log('found' + l + 'assets for query',query);
if(!l) cb(docs);
assets.forEach(function (asset) {
db.get.userListings(asset['_id'], function (err, doc) {
db.get.listing({'assetId':asset.listingId}, function (err, doc) {
if (err) {
console.log(err);
cb({'error':err});
Expand All @@ -164,10 +198,39 @@ module.exports = function (options) {
})
}

function getListing (query, cb) {
console.log('query', arguments);
var errMsg = {'error': 'Listing not found'};
db.get.listing(query, function (err, doc) {
if (err || !doc.length) {
console.log(err);
cb(errMsg);
} else {
cb(doc[0] || errMsg);
}
});
}

function getAsset (query, cb) {
var errMsg = {'error': 'Asset not found'};
db.get.asset(query, function (err, doc) {
if (err || !doc.length) {
console.log(err);
cb(errMsg);
} else {
cb(doc[0] || errMsg);
}
});
}

// sign is a facade for creating + signing
assetUtils.sign = sign;
assetUtils.save = saveAssetAndListing;
assetUtils.getUserAssets = getUserAssets;
assetUtils.getOne = getOne;
assetUtils.getListing = getListing;
assetUtils.getAsset = getAsset;
assetUtils.getOneListing = getOneListing;

return assetUtils;
};
7 changes: 4 additions & 3 deletions lib/asset.simple.cfg
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"@context": "https://w3id.org/payswarm/v1",
"id": "http: //listings.dev.payswarm.com/mozhacks/demo#asset",
"id": "http://listings.dev.payswarm.com/mozhacks/demo#asset",
"userId": "~",
"type": [
"Asset"
],
"creator": {
"fullName": "DeveloperJoe"
},
"title": "MozillaHacksDemoAsset",
"assetContent": "http: //listings.dev.payswarm.com/mozhacks/demo#asset",
"assetProvider": "https: //dev.payswarm.com/i/YOUR_IDENTITY_HERE"
"assetContent": "http://listings.dev.payswarm.com/mozhacks/demo#asset",
"assetProvider": "https://dev.payswarm.com/i/YOUR_IDENTITY_HERE"
}
65 changes: 38 additions & 27 deletions lib/db.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
var mongoose = require('mongoose');
var _ = require('underscore');

if (process.env.NODE_ENV)
var db = mongoose.connect('mongodb://nodejitsu:[email protected]:10074/nodejitsudb4747069906');
else
var db = mongoose.connect('mongodb://localhost:27017/webpayments');
else {
console.log('localhost');
var db = mongoose.connect('mongodb://localhost:27017/webpay');
}

var schemas = require('./schemas')(db);

function assert() {
argv = _.toArray(arguments);
if(!argv[0]) {
console.log(argv[2]);
throw new Error('Expected ' + argv[1] + ' to be defined');
}
}

var dbUtils = {
find: function (schema, query, fields, cb) {
if (typeof fields === 'function') {
Expand All @@ -26,21 +37,28 @@ var dbUtils = {
}
},
save : {
asset : function (graph, cb) {
var listing = new schemas.Listing(graph.listing);
console.log(graph.asset);
var asset = new schemas.Asset(graph.asset);
asset: function (assetAndListing, cb) {

console.log(arguments);

asset.save(function (err, d) {
if (err) cb(err);
else {
listing.assetId = d['_id'];
var asset = new schemas.Asset(assetAndListing['@graph'][0]);
var listing = new schemas.Listing(assetAndListing['@graph'][1]);

asset.save(function (err) {
console.log('asset err', err);
if (err) {
cb(err);
} else {
listing.save(function (err) {
if (err) cb(err);
else cb(null);
console.log('listing err', err);
if (err) {
cb(err);
} else {
cb(null);
}
})
}
});
})
}
},
findOne: {
Expand All @@ -67,21 +85,14 @@ var dbUtils = {
user: function (fields, user, cb) {
schemas.User.findOne(user, fields, dbUtils.next(cb));
},
asset: function (query, cb) {
schemas.Asset.find(query, dbUtils.next(cb));
},
listing: function (query, cb) {
schemas.Listing.find(query, dbUtils.next(cb));
},
userAssets: function (query, cb) {
dbUtils.get.user({
owner: 1
}, query, function (err, doc) {
if (err) {
console.log(err);
cb(err);
} else {
console.log(doc);
console.log(query);
schemas.Asset.find({
assetProvider: doc.owner
}, dbUtils.next(cb));
}
})
schemas.Asset.find(query, dbUtils.next(cb));
},
userListings: function (id, cb) {
dbUtils.find(schemas.Listing, {assetId: id}, dbUtils.next(cb));
Expand Down
2 changes: 1 addition & 1 deletion lib/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = function(opts) {
console.log('Error reading keys: ', err);
} else {
keyPair = JSON.parse(data);
payswarm.decrypt(encryptedMessage, {privateKey: keyPair.privateKeyPem}, cb);
payswarm.decrypt(encryptedMessage, {privateKey: keyPair.publicKey.privateKeyPem}, cb);
}
});
}
Expand Down
30 changes: 30 additions & 0 deletions lib/listing
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"@context": "https://w3id.org/payswarm/v1",
"id": "http://listings.dev.payswarm.com/mozhacks/html5-me-song#listing",
"userId": "~",
"type": ["Listing"],
"vendor": "https://dev.payswarm.com/i/the-webdevs",
"payee": [{
"type": "Payee",
"destination": "https://dev.payswarm.com/i/the-webdevs/accounts/royalties",
"payeeGroup": ["vendor"],
"payeeRateType": "FlatAmount",
"payeeRate": "1.00",
"currency": "USD",
"payeeApplyType": "ApplyExclusively",
"comment": "Payment to The Webdevs for creating the HTML5 Me, Baby song"
}],
"payeeRule": [{
"type": "PayeeRule",
"payeeGroupPrefix": ["authority"],
"payeeRateType": "Percentage",
"maximumPayeeRate": "5",
"payeeApplyType": "ApplyInclusively"
}],
"asset": "http://listings.dev.payswarm.com/mozhacks/html5-me-song#asset",
"assetHash": "assetHash",
"license": "https://w3id.org/payswarm/licenses/personal-use",
"licenseHash": "urn:sha256:d9dcfb7b3ba057df52b99f777747e8fe0fc598a3bb364e3d3eb529f90d58e1b9",
"validFrom": "date",
"validUntil": "date"
}
12 changes: 9 additions & 3 deletions lib/listing.simple.cfg
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"@context": "https://w3id.org/payswarm/v1",
"id": "http://listings.dev.payswarm.com/mozhacks/html5-me-song#listing",
"userId": "~",
"type": ["Listing"],
"vendor": "https://dev.payswarm.com/i/the-webdevs",
"vendor": "https://dev.payswarm.com/i/webpayments",
"payee": [{
"type": "Payee",
"destination": "https://dev.payswarm.com/i/the-webdevs/accounts/royalties",
"destination": "https://dev.payswarm.com/i/webpayments/accounts/primary",
"payeeGroup": ["vendor"],
"payeeRateType": "FlatAmount",
"payeeRate": "1.00",
Expand All @@ -22,8 +23,13 @@
}],
"asset": "http://listings.dev.payswarm.com/mozhacks/html5-me-song#asset",
"assetHash": "assetHash",
"listing": "listing",
"license": "https://w3id.org/payswarm/licenses/personal-use",
"licenseHash": "urn:sha256:d9dcfb7b3ba057df52b99f777747e8fe0fc598a3bb364e3d3eb529f90d58e1b9",
"listingHash": "whatev",
"nonce":"123123123",
"transactionId":"123789",
"validFrom": "date",
"validUntil": "date"
"validUntil": "date",
"identity":"1"
}
6 changes: 3 additions & 3 deletions lib/payswarm.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,8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XhKLaKK\n-----END RSA PRIVATE KEY-----\n",
"id": "https://dev.payswarm.com/i/webpayments/keys/3"
"id": "https://dev.payswarm.com/i/webpay/keys/1"
},
"owner": "https://dev.payswarm.com/i/webpayments",
"source": "https://dev.payswarm.com/i/webpayments/accounts/primary"
"owner": "https://dev.payswarm.com/i/webpay",
"source": "https://dev.payswarm.com/i/webpay/accounts/primary"
}
3 changes: 3 additions & 0 deletions lib/ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Public Key Owner : https://dev.payswarm.com/i/piatra
Financial Account: https://dev.payswarm.com/i/piatra/accounts/primary
Public Key URL : https://dev.payswarm.com/i/piatra/keys/30
Loading

0 comments on commit 7cc2876

Please sign in to comment.