Skip to content

Commit 364ef06

Browse files
author
Miel Vander Sande
committed
Better error handling. Fixed https options
1 parent a02f73f commit 364ef06

File tree

2 files changed

+37
-56
lines changed

2 files changed

+37
-56
lines changed

lib/LinkedDataFragmentsServer.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ function LinkedDataFragmentsServer(options) {
1515
server = require('http').createServer();
1616
break;
1717
case 'https':
18-
var ssl = options.ssl || {};
18+
var ssl = options.ssl || {}, authentication = options.authentication || {};
1919
// WebID authentication requires a client certificate
20-
if (ssl.webid)
20+
if (authentication.webid)
2121
ssl.requestCert = ssl.rejectUnauthorized = true;
22-
server = require('https').createServer(_.mapValues(ssl.keys, readHttpsOption));
22+
server = require('https').createServer(_.assign(ssl, _.mapValues(ssl.keys, readHttpsOption)));
2323
break;
2424
default:
2525
throw new Error('The configured protocol ' + options.protocol + ' is invalid.');

lib/controllers/WebIDControllerExtension.js

Lines changed: 34 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var http = require('http'),
55
lru = require('lru-cache'),
66
parseCacheControl = require('parse-cache-control'),
77
N3 = require('n3'),
8-
N3Parser = N3.Parser,
8+
n3parser = N3.Parser,
99
N3Util = N3.Util,
1010
Util = require('../Util');
1111

@@ -17,7 +17,7 @@ function WebIDControllerExtension(settings) {
1717
return new WebIDControllerExtension(settings);
1818

1919
this._cache = lru(50);
20-
this._protocol = settings.protocol || 'http';
20+
this._protocol = settings.protocol;
2121
}
2222

2323
// Add WebID Link headers
@@ -30,15 +30,13 @@ WebIDControllerExtension.prototype._handleRequest = function (request, response,
3030
certificate = request.connection.getPeerCertificate();
3131

3232
if (!(certificate.subject && certificate.subject.subjectAltName))
33-
return this._handleForbidden(request, response);
33+
return this._handleForbidden(request, response, { reason: 'No WebID found in client certificate.' });
3434

3535
var webID = certificate.subject.subjectAltName.replace('uniformResourceIdentifier:', '');
3636
this._verifyWebID(webID, certificate.modulus, parseInt(certificate.exponent, 16),
37-
function (verified) {
38-
console.log('WebID ' + webID + ' verified: ', verified);
39-
37+
function (error, verified, reason) {
4038
if (!verified)
41-
return self._handleForbidden(request, response, webID);
39+
return self._handleForbidden(request, response, { webID: webID, reason: reason });
4240

4341
next();
4442
});
@@ -48,81 +46,64 @@ WebIDControllerExtension.prototype._handleRequest = function (request, response,
4846
WebIDControllerExtension.prototype._verifyWebID = function (webID, modulus, exponent, callback) {
4947
// request & parse
5048
var parser = n3parser(),
51-
candidates = {}, verified = false;
52-
53-
parser.parse(processTriple);
49+
id = {};
5450

55-
function processTriple(error, triple, prefixes) {
51+
// parse webID
52+
function parseTriple(error, triple, prefixes) {
5653
if (error)
57-
console.error('Cannot parse WebID: ' + error);
54+
callback('Cannot parse WebID: ' + error);
5855
else if (triple) {
5956
switch (triple.predicate) {
6057
case CERT_NS + 'modulus':
61-
var webidModulus = N3Util.getLiteralValue(triple.object);
58+
// Add modulus
59+
var literalValue = N3Util.getLiteralValue(triple.object);
6260
// Apply parsing method by nodejs
63-
webidModulus = webidModulus.slice(webidModulus.indexOf('00:') === 0 ? 3 : 0).replace(/:/g, '').toUpperCase();
64-
65-
if (modulus === webidModulus) {
66-
console.log('WebID modulus verified');
67-
if (candidates[triple.subject] && candidates[triple.subject] === exponent)
68-
verified = true;
69-
else
70-
candidates[triple.subject] = webidModulus;
71-
}
72-
else console.log('WebID modulus mismatch: %s (webid) <> %s (cert)', webidModulus, modulus);
61+
id.modulus = literalValue.slice(literalValue.indexOf('00:') === 0 ? 3 : 0).replace(/:/g, '').toUpperCase();
7362
break;
7463
case CERT_NS + 'exponent':
75-
var webidExponent = parseInt(N3Util.getLiteralValue(triple.object), 16);
76-
77-
if (webidExponent === exponent) {
78-
console.log('WebID exponent verified');
79-
if (candidates[triple.subject] && candidates[triple.subject] === modulus)
80-
verified = true;
81-
else
82-
candidates[triple.subject] = webidExponent;
83-
}
84-
else console.log('WebID exponent mismatch: %s (webid) <> %s (cert)', webidExponent, exponent);
64+
// Add exponent
65+
id.exponent = parseInt(N3Util.getLiteralValue(triple.object), 10);
8566
break;
8667
}
8768
}
88-
else callback(verified);
69+
}
70+
71+
function verify(m, e) {
72+
if (m && m === modulus && e && e === exponent)
73+
callback(null, true);
74+
else
75+
callback(null, false, 'WebID does not match certificate: ' + m + ' - ' + e + ' (webid) <> ' + modulus + ' - ' + exponent + ' (cert)');
8976
}
9077

9178
// Try to get WebID from cache
92-
var webIDFile = this._cache.get(webID);
79+
var cachedId = this._cache.get(webID), self = this;
9380

94-
if (webIDFile) {
95-
parser.addChunk(webIDFile);
96-
parser.end();
97-
} else {
98-
var req = http.request(webID, function(res) {
81+
if (cachedId)
82+
verify(cachedId.modulus, cachedId.exponent);
83+
else {
84+
var req = http.request(webID, function (res) {
9985
res.setEncoding('utf8');
100-
var response = "";
10186

102-
res.on('data', function (data) {
103-
parser.addChunk(data);
104-
response += data;
105-
});
87+
parser.parse(res, parseTriple);
10688

10789
res.on('end', function () {
108-
parser.end();
109-
var cacheControl = parseCacheControl(res.getHeader("Cache-Control"));
110-
this._cache.set(webID, response, cacheControl['max-age']);
90+
var cacheControl = parseCacheControl(res.headers['Cache-Control'] || '');
91+
self._cache.set(webID, id, cacheControl['max-age'] || 0);
92+
verify(id.modulus, id.exponent);
11193
});
11294
});
11395

114-
req.on('error', function(e) {
115-
console.log('Problem with request: ' + e.message);
116-
callback(false);
96+
req.on('error', function (e) {
97+
callback(null, false, 'Unabled to download' + webID + '. Details: ' + e.message);
11798
});
11899

119100
req.end();
120101
}
121102
};
122103

123-
WebIDControllerExtension.prototype._handleForbidden = function (request, response, webID) {
104+
WebIDControllerExtension.prototype._handleForbidden = function (request, response, options) {
124105
response.writeHead(401, { 'Content-Type': Util.MIME_PLAINTEXT });
125-
response.end('Access to ' + request.url + ' is not allowed, verification for WebID ' + (webID || '') + ' failed.');
106+
response.end('Access to ' + request.url + ' is not allowed, verification for WebID ' + (options.webID || '') + ' failed. Reason: ' + (options.reason || ''));
126107
};
127108

128109
module.exports = WebIDControllerExtension;

0 commit comments

Comments
 (0)