@@ -5,6 +5,7 @@ var http = require('http'),
5
5
lru = require ( 'lru-cache' ) ,
6
6
parseCacheControl = require ( 'parse-cache-control' ) ,
7
7
N3 = require ( 'n3' ) ,
8
+ N3Parser = N3 . Parser ,
8
9
N3Util = N3 . Util ,
9
10
Util = require ( '../Util' ) ;
10
11
@@ -29,24 +30,24 @@ WebIDControllerExtension.prototype._handleRequest = function (request, response,
29
30
certificate = request . connection . getPeerCertificate ( ) ;
30
31
31
32
if ( ! ( certificate . subject && certificate . subject . subjectAltName ) )
32
- return this . _handleNotAcceptable ( request , response , next ) ;
33
+ return this . _handleForbidden ( request , response ) ;
33
34
34
35
var webID = certificate . subject . subjectAltName . replace ( 'uniformResourceIdentifier:' , '' ) ;
35
36
this . _verifyWebID ( webID , certificate . modulus , parseInt ( certificate . exponent , 16 ) ,
36
37
function ( verified ) {
37
- console . log ( " WebID verified: " , verified ) ;
38
+ console . log ( ' WebID ' + webID + ' verified: ' , verified ) ;
38
39
39
40
if ( ! verified )
40
- return self . _handleNotAcceptable ( request , response , next ) ;
41
+ return self . _handleForbidden ( request , response , webID ) ;
41
42
42
43
next ( ) ;
43
44
} ) ;
44
45
} ;
45
46
46
47
// Verify webID
47
48
WebIDControllerExtension . prototype . _verifyWebID = function ( webID , modulus , exponent , callback ) {
48
- //request & parse
49
- var parser = N3 . Parser ( ) ,
49
+ // request & parse
50
+ var parser = n3parser ( ) ,
50
51
candidates = { } , verified = false ;
51
52
52
53
parser . parse ( processTriple ) ;
@@ -56,40 +57,43 @@ WebIDControllerExtension.prototype._verifyWebID = function (webID, modulus, expo
56
57
console . error ( 'Cannot parse WebID: ' + error ) ;
57
58
else if ( triple ) {
58
59
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 ;
83
86
}
84
- } else callback ( verified ) ;
87
+ }
88
+ else callback ( verified ) ;
85
89
}
86
90
87
91
// Try to get WebID from cache
88
92
var webIDFile = this . _cache . get ( webID ) ;
89
93
90
94
if ( webIDFile ) {
91
- parser . addChunk ( webIDFile ) ;
92
- parser . end ( ) ;
95
+ parser . addChunk ( webIDFile ) ;
96
+ parser . end ( ) ;
93
97
} else {
94
98
var req = http . request ( webID , function ( res ) {
95
99
res . setEncoding ( 'utf8' ) ;
@@ -116,9 +120,9 @@ WebIDControllerExtension.prototype._verifyWebID = function (webID, modulus, expo
116
120
}
117
121
} ;
118
122
119
- WebIDControllerExtension . prototype . _handleNotAcceptable = function ( request , response , next ) {
123
+ WebIDControllerExtension . prototype . _handleForbidden = function ( request , response , webID ) {
120
124
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.') ;
122
126
} ;
123
127
124
128
module . exports = WebIDControllerExtension ;
0 commit comments