@@ -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
- const authorizerOptions = { } ;
393
-
394
- // serverless 1.x will create default values, so we will to
395
- authorizerOptions . resultTtlInSeconds = '300' ;
396
- authorizerOptions . identitySource = 'method.request.header.Authorization' ;
397
-
398
- if ( typeof endpoint . authorizer === 'string' ) {
399
- authorizerOptions . name = authFunctionName ;
400
- }
401
- else {
402
- Object . assign ( 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,62 @@ 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
+ authorizerOptions . resultTtlInSeconds = '300' ;
751
+ authorizerOptions . identitySource = 'method.request.header.Authorization' ;
752
+
753
+ if ( typeof endpoint . authorizer === 'string' ) {
754
+ authorizerOptions . name = authFunctionName ;
755
+ }
756
+ else {
757
+ Object . assign ( authorizerOptions , endpoint . authorizer ) ;
758
+ }
759
+
760
+ // Create a unique scheme per endpoint
761
+ // This allows the methodArn on the event property to be set appropriately
762
+ const authKey = `${ funName } -${ authFunctionName } -${ method } -${ epath } ` ;
763
+ const authSchemeName = `scheme-${ authKey } ` ;
764
+ authStrategyName = `strategy-${ authKey } ` ; // set strategy name for the route config
765
+
766
+ debugLog ( `Creating Authorization scheme for ${ authKey } ` ) ;
767
+
768
+ // Create the Auth Scheme for the endpoint
769
+ const scheme = createAuthScheme (
770
+ authFunction ,
771
+ authorizerOptions ,
772
+ funName ,
773
+ epath ,
774
+ this . options ,
775
+ this . serverlessLog ,
776
+ servicePath
777
+ ) ;
778
+
779
+ // Set the auth scheme and strategy on the server
780
+ this . server . auth . scheme ( authSchemeName , scheme ) ;
781
+ this . server . auth . strategy ( authStrategyName , authSchemeName ) ;
782
+ }
783
+ return authStrategyName ;
784
+ }
785
+
784
786
// All done, we can listen to incomming requests
785
787
_listen ( ) {
786
788
return new Promise ( ( resolve , reject ) => {
0 commit comments