/
requestMethods.js
160 lines (150 loc) · 5.29 KB
/
requestMethods.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*!
* Copyright(c) 2010 Ciaran Jessup <ciaranj@gmai.com>
* MIT Licensed
*/
/*
* This file contains the methods that will become 'mixed-in' with the connect request object, namely:
*
* authenticate( [strategy|options], callback(err, succcessFailOngoing) )
* getAuthDetails
* isAuthenticated( [scope] )
* isUnAuthenticated( [scope] )
* logout( [scope], [callback(err)])
*/
module.exports.authenticate= function(strategy, opts, callback, strategyExecutor, res, firstLoginHandler) {
var strategy, opts, callback;
var scope;
var trace= this.getAuthDetails().trace;
var req= this;
//ughhh pull this rubbish somewhere tidy...
if( strategy && opts && callback ) {
var type= typeof strategy;
if( strategy.constructor != Array ) {
strategy= [strategy]
}
scope= opts.scope;
}
else if( strategy && opts ) {
callback= opts;
var type= typeof strategy;
if( strategy.constructor == Array ) {
// do nothing
}
else if( type == 'string' ) {
strategy= [strategy];
}
else if( type == 'object') {
scope= strategy.scope
strategy= undefined;
}
}
else if( strategy ) {
callback= strategy;
strategy= undefined;
}
// Choose the first strategy defined if no strategy provided
if( !strategy && strategyExecutor.strategies ) {
for( var k in strategyExecutor.strategies ) {
strategy= [strategyExecutor.strategies[k].name];
break;
}
}
trace( "Authenticating ("+this.headers.host + this.url+")", scope, ">>>" );
if( req.isAuthenticated(scope) ) {
delete req.getAuthDetails().__performingAuthentication;
delete req.getAuthDetails().__originalUrl;
trace( "Authentication successful (Already Authenticated)", scope, "<<<" );
callback(null, true);
}
else {
var authContext= {scope:scope, request:req, response:res};
strategyExecutor.authenticate(strategy, authContext, function (error, executionResult) {
//TODO: This needs tidying up, the HTTP strategies have bled...
if( executionResult) {
req.getAuthDetails().errorResponse= executionResult.errorResponse;
if( req.getAuthDetails().__originalUrl ) {
executionResult.originalUrl= req.getAuthDetails().__originalUrl;
} else {
executionResult.originalUrl= req.url;
}
}
if(error) {
delete req.getAuthDetails().__performingAuthentication;
delete req.getAuthDetails().__originalUrl;
trace( "Authentication error: "+ error, scope, "<<<" );
callback(error);
}
else {
if( executionResult.authenticated === true ) {
trace( "Authentication successful", scope, "<<<" );
executionResult.originalUrl= req.getAuthDetails().__originalUrl;
delete req.getAuthDetails().__originalUrl;
if( scope === undefined) {
req.getAuthDetails().user= executionResult.user;
}
else {
if( req.getAuthDetails().scopedUsers[scope] === undefined ) {
req.getAuthDetails().scopedUsers[scope] = {};
}
req.getAuthDetails().scopedUsers[scope].user= executionResult.user;
}
if( req.getAuthDetails().__performingAuthentication ) {
try {
delete req.getAuthDetails().__performingAuthentication;
trace( "Firing 'FirstLogin' Handler", scope, "$$$" );
firstLoginHandler( authContext, executionResult, callback );
}
catch(err) {
trace( "error: With executing firstLoginHandler" + err.stack );
}
}
else {
callback(null, executionResult.authenticated)
}
}
else if( executionResult.authenticated === false ) {
delete req.getAuthDetails().__performingAuthentication;
delete req.getAuthDetails().__originalUrl;
trace( "Authentication failed", scope, "<<<" );
callback(null, executionResult.authenticated)
}
else {
req.getAuthDetails().__performingAuthentication= true;
req.getAuthDetails().__originalUrl= req.url;
trace( "Authentication ongoing (Requires browser interaction)", scope, "<<<" );
callback(null, executionResult.authenticated)
}
}
});
}
};
// mixins...
module.exports.getAuthDetails= function() {
return this._connect_auth
};
module.exports.isAuthenticated= function(scope) {
if( scope === undefined ) {
return (this.getAuthDetails().user) ? true : false;
}
else {
return (this.getAuthDetails().scopedUsers[scope] && this.getAuthDetails().scopedUsers[scope].user) ? true : false;
}
};
module.exports.isUnAuthenticated= function(scope) {
return !this.isAuthenticated( scope );
};
module.exports.logout= function( authContext, logoutHandler, middlewareCallback ) {
var ad= this.getAuthDetails();
ad.trace( "Logout", authContext.scope, "!!!" );
var user;
if( authContext.scope === undefined) {
user= ad.user;
delete ad.user;
ad.scopedUsers= {};
}
else {
user= ad.scopedUsers[authContext.scope].user;
delete ad.scopedUsers[authContext.scope].user;
}
logoutHandler( authContext, user, middlewareCallback );
};