@@ -12,153 +12,153 @@ var cleanFilename = require('../libs/helpers').cleanFilename;
12
12
var addSession = require ( '../libs/modifySessions' ) . add ;
13
13
14
14
// Unused but removing it breaks passport
15
- passport . serializeUser ( function ( user , done ) {
16
- done ( null , user . _id ) ;
15
+ passport . serializeUser ( function ( aUser , aDone ) {
16
+ aDone ( null , aUser . _id ) ;
17
17
} ) ;
18
18
19
19
// Setup all our auth strategies
20
20
var openIdStrategies = { } ;
21
- Strategy . find ( { } , function ( err , strategies ) {
21
+ Strategy . find ( { } , function ( aErr , aStrategies ) {
22
22
23
23
// Get OpenId strategies
24
24
for ( var name in allStrategies ) {
25
25
if ( ! allStrategies [ name ] . oauth ) {
26
26
openIdStrategies [ name ] = true ;
27
- strategies . push ( { 'name' : name , 'openid' : true } ) ;
27
+ aStrategies . push ( { 'name' : name , 'openid' : true } ) ;
28
28
}
29
29
}
30
30
31
31
// Load the passport module for each strategy
32
- strategies . forEach ( function ( strategy ) {
33
- loadPassport ( strategy ) ;
32
+ aStrategies . forEach ( function ( aStrategy ) {
33
+ loadPassport ( aStrategy ) ;
34
34
} ) ;
35
35
} ) ;
36
36
37
- exports . auth = function ( req , res , next ) {
38
- var user = req . session . user ;
39
- var strategy = req . body . auth || req . route . params . strategy ;
40
- var username = req . body . username || req . session . username ;
37
+ exports . auth = function ( aReq , aRes , aNext ) {
38
+ var user = aReq . session . user ;
39
+ var strategy = aReq . body . auth || aReq . route . params . strategy ;
40
+ var username = aReq . body . username || aReq . session . username ;
41
41
42
42
function auth ( ) {
43
43
var authenticate = passport . authenticate ( strategy ) ;
44
44
45
45
// Just in case some dumbass tries a bad /auth/* url
46
- if ( ! strategyInstances [ strategy ] ) { return next ( ) ; }
46
+ if ( ! strategyInstances [ strategy ] ) { return aNext ( ) ; }
47
47
48
- authenticate ( req , res ) ;
48
+ authenticate ( aReq , aRes ) ;
49
49
}
50
50
51
51
// Allow a logged in user to add a new strategy
52
52
if ( strategy && user ) {
53
- req . session . username = user . name ;
53
+ aReq . session . username = user . name ;
54
54
return auth ( ) ;
55
55
} else if ( user ) {
56
- return next ( ) ;
56
+ return aNext ( ) ;
57
57
}
58
58
59
- if ( ! username ) { return res . redirect ( '/register?noname' ) ; }
59
+ if ( ! username ) { return aRes . redirect ( '/register?noname' ) ; }
60
60
// Clean the username of leading and trailing whitespace,
61
61
// and other stuff that is unsafe in a url
62
62
username = cleanFilename ( username . replace ( / ^ \s + | \s + $ / g, '' ) ) ;
63
63
64
64
// The username could be empty after the replacements
65
- if ( ! username ) { return res . redirect ( '/register?noname' ) ; }
65
+ if ( ! username ) { return aRes . redirect ( '/register?noname' ) ; }
66
66
67
67
// Store the username in the session so we still have it when they
68
68
// get back from authentication
69
- if ( ! req . session . username ) {
70
- req . session . username = username ;
69
+ if ( ! aReq . session . username ) {
70
+ aReq . session . username = username ;
71
71
}
72
72
73
73
User . findOne ( { name : { $regex : new RegExp ( '^' + username + '$' , 'i' ) } } ,
74
- function ( err , user ) {
74
+ function ( aErr , aUser ) {
75
75
var strategies = null ;
76
76
var strat = null ;
77
77
78
- if ( user ) {
79
- strategies = user . strategies ;
78
+ if ( aUser ) {
79
+ strategies = aUser . strategies ;
80
80
strat = strategies . pop ( ) ;
81
81
82
- if ( req . session . newstrategy ) { // authenticate with a new strategy
83
- delete req . session . newstrategy ;
82
+ if ( aReq . session . newstrategy ) { // authenticate with a new strategy
83
+ delete aReq . session . newstrategy ;
84
84
} else if ( ! strategy ) { // use an existing strategy
85
85
strategy = strat ;
86
86
} else if ( strategies . indexOf ( strategy ) === - 1 ) {
87
87
// add a new strategy but first authenticate with existing strategy
88
- req . session . newstrategy = strategy ;
88
+ aReq . session . newstrategy = strategy ;
89
89
strategy = strat ;
90
90
} // else use the strategy that was given in the POST
91
91
}
92
92
93
93
if ( ! strategy ) {
94
- return res . redirect ( '/register' ) ;
94
+ return aRes . redirect ( '/register' ) ;
95
95
} else {
96
96
return auth ( ) ;
97
97
}
98
98
} ) ;
99
99
} ;
100
100
101
- exports . callback = function ( req , res , next ) {
102
- var strategy = req . route . params . strategy ;
103
- var username = req . session . username ;
104
- var newstrategy = req . session . newstrategy ;
101
+ exports . callback = function ( aReq , aRes , aNext ) {
102
+ var strategy = aReq . route . params . strategy ;
103
+ var username = aReq . session . username ;
104
+ var newstrategy = aReq . session . newstrategy ;
105
105
var strategyInstance = null ;
106
- var doneUrl = req . session . user ? '/user/edit' : '/' ;
106
+ var doneUrl = aReq . session . user ? '/user/edit' : '/' ;
107
107
108
108
// The callback was called improperly
109
- if ( ! strategy || ! username ) { return next ( ) ; }
109
+ if ( ! strategy || ! username ) { return aNext ( ) ; }
110
110
111
111
// Get the passport strategy instance so we can alter the _verfiy method
112
112
strategyInstance = strategyInstances [ strategy ] ;
113
113
114
114
// Hijak the private verify method so we can fuck shit up freely
115
115
// We use this library for things it was never intended to do
116
116
if ( openIdStrategies [ strategy ] ) {
117
- strategyInstance . _verify = function ( id , done ) {
118
- verifyPassport ( id , strategy , username , req . session . user , done ) ;
117
+ strategyInstance . _verify = function ( aId , aDone ) {
118
+ verifyPassport ( aId , strategy , username , aReq . session . user , aDone ) ;
119
119
}
120
120
} else {
121
121
strategyInstance . _verify =
122
- function ( token , refreshOrSecretToken , profile , done ) {
123
- req . session . profile = profile ;
124
- verifyPassport ( profile . id , strategy , username , req . session . user , done ) ;
122
+ function ( aToken , aRefreshOrSecretToken , aProfile , aDone ) {
123
+ aReq . session . profile = aProfile ;
124
+ verifyPassport ( aProfile . id , strategy , username , aReq . session . user , aDone ) ;
125
125
}
126
126
}
127
127
128
128
// This callback will happen after the verify routine
129
- var authenticate = passport . authenticate ( strategy , function ( err , user , info ) {
130
- if ( err ) { return next ( err ) ; }
131
- if ( ! user ) {
132
- return res . redirect ( doneUrl + ( doneUrl === '/' ? 'register' : '' )
129
+ var authenticate = passport . authenticate ( strategy , function ( aErr , aUser , aInfo ) {
130
+ if ( aErr ) { return aNext ( aErr ) ; }
131
+ if ( ! aUser ) {
132
+ return aRes . redirect ( doneUrl + ( doneUrl === '/' ? 'register' : '' )
133
133
+ '?authfail' ) ;
134
134
}
135
135
136
- req . logIn ( user , function ( err ) {
137
- if ( err ) { return next ( err ) ; }
136
+ aReq . logIn ( aUser , function ( aErr ) {
137
+ if ( aErr ) { return aNext ( aErr ) ; }
138
138
139
139
// Store the user info in the session
140
- req . session . user = user ;
140
+ aReq . session . user = aUser ;
141
141
142
142
// Save the session id on the user model
143
- user . sessionId = req . sessionID ;
143
+ aUser . sessionId = aReq . sessionID ;
144
144
145
145
// Save GitHub username.
146
- if ( req . session . profile && req . session . profile . provider === 'github' ) {
147
- user . ghUsername = req . session . profile . username ;
146
+ if ( aReq . session . profile && aReq . session . profile . provider === 'github' ) {
147
+ aUser . ghUsername = aReq . session . profile . username ;
148
148
}
149
149
150
- addSession ( req , user , function ( ) {
150
+ addSession ( aReq , aUser , function ( ) {
151
151
if ( newstrategy ) {
152
152
// Allow a user to link to another acount
153
- return res . redirect ( '/auth/' + newstrategy ) ;
153
+ return aRes . redirect ( '/auth/' + newstrategy ) ;
154
154
} else {
155
155
// Delete the username that was temporarily stored
156
- delete req . session . username ;
157
- return res . redirect ( doneUrl ) ;
156
+ delete aReq . session . username ;
157
+ return aRes . redirect ( doneUrl ) ;
158
158
}
159
159
} ) ;
160
160
} ) ;
161
161
} ) ;
162
162
163
- authenticate ( req , res , next ) ;
163
+ authenticate ( aReq , aRes , aNext ) ;
164
164
}
0 commit comments