@@ -370,61 +370,7 @@ class Offline {
370
370
this . serverlessLog ( `${ method } ${ fullPath } ` ) ;
371
371
372
372
// If the endpoint has an authorization function, create an authStrategy for the route
373
- let authStrategyName = null ;
374
-
375
- if ( endpoint . authorizer ) {
376
- let authFunctionName = endpoint . authorizer ;
377
- if ( typeof endpoint . authorizer === 'object' ) {
378
- if ( endpoint . authorizer . arn ) {
379
- this . serverlessLog ( `WARNING: Serverless Offline does not support non local authorizers: ${ endpoint . authorizer . arn } ` ) ;
380
-
381
- return ;
382
- }
383
- authFunctionName = endpoint . authorizer . name ;
384
- }
385
-
386
- this . serverlessLog ( `Configuring Authorization: ${ endpoint . path } ${ authFunctionName } ` ) ;
387
-
388
- const authFunction = this . service . getFunction ( authFunctionName ) ;
389
-
390
- if ( ! authFunction ) return this . serverlessLog ( `WARNING: Authorization function ${ authFunctionName } does not exist` ) ;
391
-
392
- let authorizerOptions = { } ;
393
- if ( typeof endpoint . authorizer === 'string' ) {
394
- // serverless 1.x will create default values, so we will to
395
- authorizerOptions . name = authFunctionName ;
396
- authorizerOptions . resultTtlInSeconds = '300' ;
397
- authorizerOptions . identitySource = 'method.request.header.Authorization' ;
398
- }
399
- else {
400
- authorizerOptions . identitySource = endpoint . authorizer . identitySource ||
401
- 'method.request.header.Authorization' ; // See #207
402
- authorizerOptions = endpoint . authorizer ;
403
- }
404
-
405
- // Create a unique scheme per endpoint
406
- // This allows the methodArn on the event property to be set appropriately
407
- const authKey = `${ funName } -${ authFunctionName } -${ method } -${ epath } ` ;
408
- const authSchemeName = `scheme-${ authKey } ` ;
409
- authStrategyName = `strategy-${ authKey } ` ; // set strategy name for the route config
410
-
411
- debugLog ( `Creating Authorization scheme for ${ authKey } ` ) ;
412
-
413
- // Create the Auth Scheme for the endpoint
414
- const scheme = createAuthScheme (
415
- authFunction ,
416
- authorizerOptions ,
417
- funName ,
418
- epath ,
419
- this . options ,
420
- this . serverlessLog ,
421
- servicePath
422
- ) ;
423
-
424
- // Set the auth scheme and strategy on the server
425
- this . server . auth . scheme ( authSchemeName , scheme ) ;
426
- this . server . auth . strategy ( authStrategyName , authSchemeName ) ;
427
- }
373
+ let authStrategyName = this . _configureAuthorization ( endpoint , funName , method , epath , servicePath ) ;
428
374
429
375
let cors = null ;
430
376
if ( endpoint . cors ) {
@@ -781,6 +727,64 @@ class Offline {
781
727
} ) ;
782
728
}
783
729
730
+ _configureAuthorization ( endpoint , funName , method , epath , servicePath ) {
731
+ let authStrategyName = null ;
732
+ if ( endpoint . authorizer ) {
733
+ let authFunctionName = endpoint . authorizer ;
734
+ if ( typeof endpoint . authorizer === 'object' ) {
735
+ if ( endpoint . authorizer . arn ) {
736
+ this . serverlessLog ( `WARNING: Serverless Offline does not support non local authorizers: ${ endpoint . authorizer . arn } ` ) ;
737
+
738
+ return authStrategyName ;
739
+ }
740
+ authFunctionName = endpoint . authorizer . name ;
741
+ }
742
+
743
+ this . serverlessLog ( `Configuring Authorization: ${ endpoint . path } ${ authFunctionName } ` ) ;
744
+
745
+ const authFunction = this . service . getFunction ( authFunctionName ) ;
746
+
747
+ if ( ! authFunction ) return this . serverlessLog ( `WARNING: Authorization function ${ authFunctionName } does not exist` ) ;
748
+
749
+ let authorizerOptions = { } ;
750
+ if ( typeof endpoint . authorizer === 'string' ) {
751
+ // serverless 1.x will create default values, so we will to
752
+ authorizerOptions . name = authFunctionName ;
753
+ authorizerOptions . resultTtlInSeconds = '300' ;
754
+ authorizerOptions . identitySource = 'method.request.header.Authorization' ;
755
+ }
756
+ else {
757
+ authorizerOptions . identitySource = endpoint . authorizer . identitySource ||
758
+ 'method.request.header.Authorization' ; // See #207
759
+ authorizerOptions = endpoint . authorizer ;
760
+ }
761
+
762
+ // Create a unique scheme per endpoint
763
+ // This allows the methodArn on the event property to be set appropriately
764
+ const authKey = `${ funName } -${ authFunctionName } -${ method } -${ epath } ` ;
765
+ const authSchemeName = `scheme-${ authKey } ` ;
766
+ authStrategyName = `strategy-${ authKey } ` ; // set strategy name for the route config
767
+
768
+ debugLog ( `Creating Authorization scheme for ${ authKey } ` ) ;
769
+
770
+ // Create the Auth Scheme for the endpoint
771
+ const scheme = createAuthScheme (
772
+ authFunction ,
773
+ authorizerOptions ,
774
+ funName ,
775
+ epath ,
776
+ this . options ,
777
+ this . serverlessLog ,
778
+ servicePath
779
+ ) ;
780
+
781
+ // Set the auth scheme and strategy on the server
782
+ this . server . auth . scheme ( authSchemeName , scheme ) ;
783
+ this . server . auth . strategy ( authStrategyName , authSchemeName ) ;
784
+ }
785
+ return authStrategyName ;
786
+ }
787
+
784
788
// All done, we can listen to incomming requests
785
789
_listen ( ) {
786
790
return new Promise ( ( resolve , reject ) => {
0 commit comments