Skip to content

Commit a02f73f

Browse files
Miel Vander SandeRubenVerborgh
authored andcommitted
Fixed forbidden handler
1 parent 39e539d commit a02f73f

File tree

1 file changed

+38
-34
lines changed

1 file changed

+38
-34
lines changed

lib/controllers/WebIDControllerExtension.js

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +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,
89
N3Util = N3.Util,
910
Util = require('../Util');
1011

@@ -29,24 +30,24 @@ WebIDControllerExtension.prototype._handleRequest = function (request, response,
2930
certificate = request.connection.getPeerCertificate();
3031

3132
if (!(certificate.subject && certificate.subject.subjectAltName))
32-
return this._handleNotAcceptable(request, response, next);
33+
return this._handleForbidden(request, response);
3334

3435
var webID = certificate.subject.subjectAltName.replace('uniformResourceIdentifier:', '');
3536
this._verifyWebID(webID, certificate.modulus, parseInt(certificate.exponent, 16),
3637
function (verified) {
37-
console.log("WebID verified: ", verified);
38+
console.log('WebID ' + webID + ' verified: ', verified);
3839

3940
if (!verified)
40-
return self._handleNotAcceptable(request, response, next);
41+
return self._handleForbidden(request, response, webID);
4142

4243
next();
4344
});
4445
};
4546

4647
// Verify webID
4748
WebIDControllerExtension.prototype._verifyWebID = function (webID, modulus, exponent, callback) {
48-
//request & parse
49-
var parser = N3.Parser(),
49+
// request & parse
50+
var parser = n3parser(),
5051
candidates = {}, verified = false;
5152

5253
parser.parse(processTriple);
@@ -56,40 +57,43 @@ WebIDControllerExtension.prototype._verifyWebID = function (webID, modulus, expo
5657
console.error('Cannot parse WebID: ' + error);
5758
else if (triple) {
5859
switch (triple.predicate) {
59-
case CERT_NS + 'modulus':
60-
var webidModulus = N3Util.getLiteralValue(triple.object);
61-
// Apply parsing method by nodejs
62-
webidModulus = webidModulus.slice(webidModulus.indexOf('00:') === 0 ? 3 : 0).replace(/:/g, '').toUpperCase();
63-
64-
if (modulus === webidModulus) {
65-
console.log('WebID modulus verified');
66-
if (candidates[triple.subject] && candidates[triple.subject] === exponent)
67-
verified = true;
68-
else
69-
candidates[triple.subject] = webidModulus;
70-
} else console.log('WebID modulus mismatch: %s (webid) <> %s (cert)', webidModulus, modulus);
71-
break;
72-
case CERT_NS + 'exponent':
73-
var webidExponent = parseInt(N3Util.getLiteralValue(triple.object));
74-
75-
if (webidExponent === exponent) {
76-
console.log('WebID exponent verified');
77-
if (candidates[triple.subject] && candidates[triple.subject] === modulus)
78-
verified = true;
79-
else
80-
candidates[triple.subject] = webidExponent;
81-
} else console.log('WebID exponent mismatch: %s (webid) <> %s (cert)', webidExponent, exponent);
82-
break;
60+
case CERT_NS + 'modulus':
61+
var webidModulus = N3Util.getLiteralValue(triple.object);
62+
// 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);
73+
break;
74+
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);
85+
break;
8386
}
84-
} else callback(verified);
87+
}
88+
else callback(verified);
8589
}
8690

8791
// Try to get WebID from cache
8892
var webIDFile = this._cache.get(webID);
8993

9094
if (webIDFile) {
91-
parser.addChunk(webIDFile);
92-
parser.end();
95+
parser.addChunk(webIDFile);
96+
parser.end();
9397
} else {
9498
var req = http.request(webID, function(res) {
9599
res.setEncoding('utf8');
@@ -116,9 +120,9 @@ WebIDControllerExtension.prototype._verifyWebID = function (webID, modulus, expo
116120
}
117121
};
118122

119-
WebIDControllerExtension.prototype._handleNotAcceptable = function (request, response, next) {
123+
WebIDControllerExtension.prototype._handleForbidden = function (request, response, webID) {
120124
response.writeHead(401, { 'Content-Type': Util.MIME_PLAINTEXT });
121-
response.end('Access to ' + request.url + ' is not allowed, WebID verification failed.');
125+
response.end('Access to ' + request.url + ' is not allowed, verification for WebID ' + (webID || '') + ' failed.');
122126
};
123127

124128
module.exports = WebIDControllerExtension;

0 commit comments

Comments
 (0)