@@ -43,14 +43,20 @@ internals.Auth.prototype.strategy = function (name, scheme /*, mode, options */)
43
43
Hoek . assert ( scheme , 'Authentication strategy' , name , 'missing scheme' ) ;
44
44
Hoek . assert ( this . _schemes [ scheme ] , 'Authentication strategy' , name , 'uses unknown scheme:' , scheme ) ;
45
45
46
- var strategy = this . _schemes [ scheme ] ( this . connection . server . _clone ( [ this . connection ] ) , options ) ;
46
+ var server = this . connection . server . _clone ( [ this . connection ] , '' ) ;
47
+ var strategy = this . _schemes [ scheme ] ( server , options ) ;
48
+
47
49
Hoek . assert ( strategy . authenticate , 'Invalid scheme:' , name , 'missing authenticate() method' ) ;
48
50
Hoek . assert ( typeof strategy . authenticate === 'function' , 'Invalid scheme:' , name , 'invalid authenticate() method' ) ;
49
51
Hoek . assert ( ! strategy . payload || typeof strategy . payload === 'function' , 'Invalid scheme:' , name , 'invalid payload() method' ) ;
50
52
Hoek . assert ( ! strategy . response || typeof strategy . response === 'function' , 'Invalid scheme:' , name , 'invalid response() method' ) ;
51
53
strategy . options = strategy . options || { } ;
52
54
Hoek . assert ( strategy . payload || ! strategy . options . payload , 'Cannot require payload validation without a payload method' ) ;
53
- this . _strategies [ name ] = strategy ;
55
+
56
+ this . _strategies [ name ] = {
57
+ methods : strategy ,
58
+ realm : server . realm
59
+ } ;
54
60
55
61
if ( mode ) {
56
62
this . default ( { strategies : [ name ] , mode : mode === true ? 'required' : mode } ) ;
@@ -93,8 +99,8 @@ internals.Auth.prototype.test = function (name, request, next) {
93
99
return next ( response , data && data . credentials ) ;
94
100
} ;
95
101
96
- var reply = request . server . _replier . interface ( request , null , transfer ) ;
97
- strategy . authenticate ( request , reply ) ;
102
+ var reply = request . server . _replier . interface ( request , strategy . realm , transfer ) ;
103
+ strategy . methods . authenticate ( request , reply ) ;
98
104
} ;
99
105
100
106
@@ -131,9 +137,9 @@ internals.Auth.prototype._setupRoute = function (options, path) {
131
137
132
138
var strategy = self . _strategies [ name ] ;
133
139
Hoek . assert ( strategy , 'Unknown authentication strategy:' , name , 'in path:' , path ) ;
134
- Hoek . assert ( strategy . payload || options . payload !== 'required' , 'Payload validation can only be required when all strategies support it in path:' , path ) ;
135
- hasAuthenticatePayload = hasAuthenticatePayload || strategy . payload ;
136
- Hoek . assert ( ! strategy . options . payload || options . payload === undefined || options . payload === 'required' , 'Cannot set authentication payload to' , options . payload , 'when a strategy requires payload validation' , path ) ;
140
+ Hoek . assert ( strategy . methods . payload || options . payload !== 'required' , 'Payload validation can only be required when all strategies support it in path:' , path ) ;
141
+ hasAuthenticatePayload = hasAuthenticatePayload || strategy . methods . payload ;
142
+ Hoek . assert ( ! strategy . methods . options . payload || options . payload === undefined || options . payload === 'required' , 'Cannot set authentication payload to' , options . payload , 'when a strategy requires payload validation' , path ) ;
137
143
} ) ;
138
144
139
145
Hoek . assert ( ! options . payload || hasAuthenticatePayload , 'Payload authentication requires at least one strategy with payload support in path:' , path ) ;
@@ -193,24 +199,25 @@ internals.Auth.prototype._authenticate = function (request, next) {
193
199
return next ( err ) ;
194
200
}
195
201
196
- var strategy = config . strategies [ strategyPos ] ;
202
+ var name = config . strategies [ strategyPos ] ;
197
203
++ strategyPos ;
198
204
199
- request . _protect . run ( 'auth:request:' + strategy , validate , function ( exit ) {
205
+ request . _protect . run ( 'auth:request:' + name , validate , function ( exit ) {
200
206
201
207
var transfer = function ( response , data ) {
202
208
203
- exit ( response , strategy , data ) ;
209
+ exit ( response , name , data ) ;
204
210
} ;
205
211
206
- var reply = request . server . _replier . interface ( request , null , transfer ) ;
207
- self . _strategies [ strategy ] . authenticate ( request , reply ) ;
212
+ var strategy = self . _strategies [ name ] ;
213
+ var reply = request . server . _replier . interface ( request , strategy . realm , transfer ) ;
214
+ strategy . methods . authenticate ( request , reply ) ;
208
215
} ) ;
209
216
} ;
210
217
211
- var validate = function ( err , strategy , result ) { // err can be Boom, Error, or a valid response object
218
+ var validate = function ( err , name , result ) { // err can be Boom, Error, or a valid response object
212
219
213
- if ( ! strategy ) {
220
+ if ( ! name ) {
214
221
return next ( err ) ;
215
222
}
216
223
@@ -226,27 +233,27 @@ internals.Auth.prototype._authenticate = function (request, next) {
226
233
227
234
if ( err ) {
228
235
if ( err instanceof Error === false ) {
229
- request . _log ( [ 'auth' , 'unauthenticated' , 'response' , strategy ] , err . statusCode ) ;
236
+ request . _log ( [ 'auth' , 'unauthenticated' , 'response' , name ] , err . statusCode ) ;
230
237
return next ( err ) ;
231
238
}
232
239
233
- request . _log ( [ 'auth' , 'unauthenticated' , 'error' , strategy ] , err ) ;
240
+ request . _log ( [ 'auth' , 'unauthenticated' , 'error' , name ] , err ) ;
234
241
235
242
if ( err . isMissing ) {
236
243
237
- // Try next strategy
244
+ // Try next name
238
245
239
246
authErrors . push ( err . output . headers [ 'WWW-Authenticate' ] ) ;
240
247
return authenticate ( ) ;
241
248
}
242
249
243
250
if ( config . mode === 'try' ) {
244
251
request . auth . isAuthenticated = false ;
245
- request . auth . strategy = strategy ;
252
+ request . auth . strategy = name ;
246
253
request . auth . credentials = result . credentials ;
247
254
request . auth . artifacts = result . artifacts ;
248
255
request . auth . error = err ;
249
- request . _log ( [ 'auth' , 'unauthenticated' , 'try' , strategy ] , err ) ;
256
+ request . _log ( [ 'auth' , 'unauthenticated' , 'try' , name ] , err ) ;
250
257
return next ( ) ;
251
258
}
252
259
@@ -256,7 +263,7 @@ internals.Auth.prototype._authenticate = function (request, next) {
256
263
// Authenticated
257
264
258
265
var credentials = result . credentials ;
259
- request . auth . strategy = strategy ;
266
+ request . auth . strategy = name ;
260
267
request . auth . credentials = credentials ;
261
268
request . auth . artifacts = result . artifacts ;
262
269
@@ -268,7 +275,7 @@ internals.Auth.prototype._authenticate = function (request, next) {
268
275
( typeof credentials . scope === 'string' ? config . scope !== credentials . scope : credentials . scope . indexOf ( config . scope ) === - 1 ) :
269
276
( typeof credentials . scope === 'string' ? config . scope . indexOf ( credentials . scope ) === - 1 : ! Hoek . intersect ( config . scope , credentials . scope ) . length ) ) ) {
270
277
271
- request . _log ( [ 'auth' , 'scope' , 'error' , strategy ] , { got : credentials . scope , need : config . scope } ) ;
278
+ request . _log ( [ 'auth' , 'scope' , 'error' , name ] , { got : credentials . scope , need : config . scope } ) ;
272
279
return next ( Boom . forbidden ( 'Insufficient scope, expected any of: ' + config . scope ) ) ;
273
280
}
274
281
}
@@ -280,7 +287,7 @@ internals.Auth.prototype._authenticate = function (request, next) {
280
287
// Entity: 'any'
281
288
282
289
if ( entity === 'any' ) {
283
- request . _log ( [ 'auth' , strategy ] ) ;
290
+ request . _log ( [ 'auth' , name ] ) ;
284
291
request . auth . isAuthenticated = true ;
285
292
return next ( ) ;
286
293
}
@@ -289,23 +296,23 @@ internals.Auth.prototype._authenticate = function (request, next) {
289
296
290
297
if ( entity === 'user' ) {
291
298
if ( ! credentials . user ) {
292
- request . _log ( [ 'auth' , 'entity' , 'user' , 'error' , strategy ] ) ;
299
+ request . _log ( [ 'auth' , 'entity' , 'user' , 'error' , name ] ) ;
293
300
return next ( Boom . forbidden ( 'Application credentials cannot be used on a user endpoint' ) ) ;
294
301
}
295
302
296
- request . _log ( [ 'auth' , strategy ] ) ;
303
+ request . _log ( [ 'auth' , name ] ) ;
297
304
request . auth . isAuthenticated = true ;
298
305
return next ( ) ;
299
306
}
300
307
301
308
// Entity: 'app'
302
309
303
310
if ( credentials . user ) {
304
- request . _log ( [ 'auth' , 'entity' , 'app' , 'error' , strategy ] ) ;
311
+ request . _log ( [ 'auth' , 'entity' , 'app' , 'error' , name ] ) ;
305
312
return next ( Boom . forbidden ( 'User credentials cannot be used on an application endpoint' ) ) ;
306
313
}
307
314
308
- request . _log ( [ 'auth' , strategy ] ) ;
315
+ request . _log ( [ 'auth' , name ] ) ;
309
316
request . auth . isAuthenticated = true ;
310
317
return next ( ) ;
311
318
} ;
@@ -333,12 +340,12 @@ internals.Auth.payload = function (request, next) {
333
340
var auth = request . connection . auth ;
334
341
var strategy = auth . _strategies [ request . auth . strategy ] ;
335
342
336
- if ( ! strategy . payload ) {
343
+ if ( ! strategy . methods . payload ) {
337
344
return next ( ) ;
338
345
}
339
346
340
347
var config = auth . _routeConfig ( request ) ;
341
- var setting = config . payload || ( strategy . options . payload ? 'required' : false ) ;
348
+ var setting = config . payload || ( strategy . methods . options . payload ? 'required' : false ) ;
342
349
if ( ! setting ) {
343
350
return next ( ) ;
344
351
}
@@ -357,8 +364,8 @@ internals.Auth.payload = function (request, next) {
357
364
358
365
request . _protect . run ( 'auth:payload:' + request . auth . strategy , finalize , function ( exit ) {
359
366
360
- var reply = request . server . _replier . interface ( request , null , exit ) ;
361
- strategy . payload ( request , reply ) ;
367
+ var reply = request . server . _replier . interface ( request , strategy . realm , exit ) ;
368
+ strategy . methods . payload ( request , reply ) ;
362
369
} ) ;
363
370
} ;
364
371
@@ -375,13 +382,13 @@ internals.Auth.response = function (request, next) {
375
382
}
376
383
377
384
var strategy = auth . _strategies [ request . auth . strategy ] ;
378
- if ( ! strategy . response ) {
385
+ if ( ! strategy . methods . response ) {
379
386
return next ( ) ;
380
387
}
381
388
382
389
request . _protect . run ( 'auth:response:' + request . auth . strategy , next , function ( exit ) {
383
390
384
- var reply = request . server . _replier . interface ( request , null , exit ) ;
385
- strategy . response ( request , reply ) ;
391
+ var reply = request . server . _replier . interface ( request , strategy . realm , exit ) ;
392
+ strategy . methods . response ( request , reply ) ;
386
393
} ) ;
387
394
} ;
0 commit comments