-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.src.js
78 lines (56 loc) · 2.09 KB
/
index.src.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
var crypto = require('crypto');
function Auth ( secretHook ) {
this.secretHook = secretHook ;
this.check = function( request , body , callback) {
if( typeof this.secretHook != "function" ) {
return callback( "secret hook needs to be defined");
}
if( request.hasOwnProperty("headers") && request.headers.hasOwnProperty("authorization")) {
//we explicitly ask the client to send the body if they want to check it as parsing the body can interfere with body parsing further down the
var headers = request.headers;
if( headers.authorization.substring( 0 , 7 ) === "APIAuth" ) {
var authString =[
headers["content-type"] || ""
, headers["content-md5"] || ""
, request.url
, headers.date || ""
].join();
var id = headers.authorization.substring(8).split(":")[ 0 ];
//we explicitly ask the client to send the body if they want to check it as parsing the body can interfere with body parsing further down the
if( typeof body === "string" ) {
if( headers["content-md5"] !== crypto.createHash('md5').update(body).digest('base64') ) {
return callback( "body doesn't match" , false , id );
}
}
this.secretHook( id , function( secret ) {
var authorisationString = 'APIAuth ' + id + ':' + crypto.createHmac('sha1', secret ).update(authString).digest('base64');
if ( headers.authorization === authorisationString ) {
callback( null , true , id );
} else {
//Header Strings don't match
callback( null , false , id );
}
} );
} else {
callback( "authorization token does not conform to APIAuth spec");
}
} else {
callback( "no authorization token in header" );
}
}
}
function getBody( request , callback ) {
return callback( null , "" );
if (request.method !== 'POST' && request.method !== 'PUT') {
callback( null , "" );
} else {
var body = '';
request.on('data', function (data) {
body += data;
});
request.on('end', function () {
return callback( null , body );
});
}
}
module.exports = Auth;