@@ -5,7 +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
+ n3parser = N3 . Parser ,
9
9
N3Util = N3 . Util ,
10
10
Util = require ( '../Util' ) ;
11
11
@@ -17,7 +17,7 @@ function WebIDControllerExtension(settings) {
17
17
return new WebIDControllerExtension ( settings ) ;
18
18
19
19
this . _cache = lru ( 50 ) ;
20
- this . _protocol = settings . protocol || 'http' ;
20
+ this . _protocol = settings . protocol ;
21
21
}
22
22
23
23
// Add WebID Link headers
@@ -30,15 +30,13 @@ WebIDControllerExtension.prototype._handleRequest = function (request, response,
30
30
certificate = request . connection . getPeerCertificate ( ) ;
31
31
32
32
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.' } ) ;
34
34
35
35
var webID = certificate . subject . subjectAltName . replace ( 'uniformResourceIdentifier:' , '' ) ;
36
36
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 ) {
40
38
if ( ! verified )
41
- return self . _handleForbidden ( request , response , webID ) ;
39
+ return self . _handleForbidden ( request , response , { webID : webID , reason : reason } ) ;
42
40
43
41
next ( ) ;
44
42
} ) ;
@@ -48,81 +46,64 @@ WebIDControllerExtension.prototype._handleRequest = function (request, response,
48
46
WebIDControllerExtension . prototype . _verifyWebID = function ( webID , modulus , exponent , callback ) {
49
47
// request & parse
50
48
var parser = n3parser ( ) ,
51
- candidates = { } , verified = false ;
52
-
53
- parser . parse ( processTriple ) ;
49
+ id = { } ;
54
50
55
- function processTriple ( error , triple , prefixes ) {
51
+ // parse webID
52
+ function parseTriple ( error , triple , prefixes ) {
56
53
if ( error )
57
- console . error ( 'Cannot parse WebID: ' + error ) ;
54
+ callback ( 'Cannot parse WebID: ' + error ) ;
58
55
else if ( triple ) {
59
56
switch ( triple . predicate ) {
60
57
case CERT_NS + 'modulus' :
61
- var webidModulus = N3Util . getLiteralValue ( triple . object ) ;
58
+ // Add modulus
59
+ var literalValue = N3Util . getLiteralValue ( triple . object ) ;
62
60
// 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 ( ) ;
73
62
break ;
74
63
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 ) ;
85
66
break ;
86
67
}
87
68
}
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)' ) ;
89
76
}
90
77
91
78
// Try to get WebID from cache
92
- var webIDFile = this . _cache . get ( webID ) ;
79
+ var cachedId = this . _cache . get ( webID ) , self = this ;
93
80
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 ) {
99
85
res . setEncoding ( 'utf8' ) ;
100
- var response = "" ;
101
86
102
- res . on ( 'data' , function ( data ) {
103
- parser . addChunk ( data ) ;
104
- response += data ;
105
- } ) ;
87
+ parser . parse ( res , parseTriple ) ;
106
88
107
89
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 ) ;
111
93
} ) ;
112
94
} ) ;
113
95
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 ) ;
117
98
} ) ;
118
99
119
100
req . end ( ) ;
120
101
}
121
102
} ;
122
103
123
- WebIDControllerExtension . prototype . _handleForbidden = function ( request , response , webID ) {
104
+ WebIDControllerExtension . prototype . _handleForbidden = function ( request , response , options ) {
124
105
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 || '' ) ) ;
126
107
} ;
127
108
128
109
module . exports = WebIDControllerExtension ;
0 commit comments