@@ -320,27 +320,52 @@ AuthPolicy.prototype = (function AuthPolicyClass() {
320320
321321
322322function processAuthRequest ( event , tokenIssuer , callback ) {
323+
324+ const apiOptions = { } ;
325+ const tmp = event . methodArn . split ( ':' ) ;
326+ const apiGatewayArnTmp = tmp [ 5 ] . split ( '/' ) ;
327+ const awsAccountId = tmp [ 4 ] ;
328+
329+ apiOptions . region = tmp [ 3 ] ;
330+ apiOptions . restApiId = apiGatewayArnTmp [ 0 ] ;
331+ apiOptions . stage = apiGatewayArnTmp [ 1 ] ;
332+
333+
334+
335+
336+
337+
323338 var token = event . authorizationToken ;
324339
325340 //Fail if the token is not jwt
326341 var decodedJwt = jwt . decode ( token , { complete : true } ) ;
327342 if ( ! decodedJwt ) {
328- console . log ( 'Not a valid JWT token' ) ;
329- callback ( 'Unauthorized' ) ;
343+ let policy = new AuthPolicy ( '' , awsAccountId , apiOptions ) ;
344+ logger . info ( "Not valid JWT token, returning deny all policy" ) ;
345+ policy . denyAllMethods ( ) ;
346+ let iamPolicy = policy . build ( ) ;
347+ callback ( null , iamPolicy ) ;
330348 return ;
331349 }
332350
333351 //Fail if token is not from your User Pool
334352 if ( decodedJwt . payload [ 'iss' ] != tokenIssuer ) {
335- console . log ( "invalid Issuer" ) ;
336- callback ( "Unauthorized" ) ;
353+ logger . info ( "Provided Token not from UserPool, returning deny all policy" ) ;
354+ let policy = new AuthPolicy ( '' , awsAccountId , apiOptions ) ;
355+ policy . denyAllMethods ( ) ;
356+ let iamPolicy = policy . build ( ) ;
357+ callback ( null , iamPolicy ) ;
337358 return ;
338359 }
339360
340- //Reject the jwt if it's not an 'Access Token'
361+ //Reject the jwt if it's not an 'Identity Token'
341362 if ( decodedJwt . payload [ 'token_use' ] != 'id' ) {
342363 console . log ( "Not an Identity token" ) ;
343- callback ( "Unauthorized" ) ;
364+ logger . info ( "Provided Token is not and identity token, returning deny all policy" ) ;
365+ let policy = new AuthPolicy ( '' , awsAccountId , apiOptions ) ;
366+ policy . denyAllMethods ( ) ;
367+ let iamPolicy = policy . build ( ) ;
368+ callback ( null , iamPolicy ) ;
344369 return ;
345370 }
346371
@@ -349,39 +374,39 @@ function processAuthRequest(event, tokenIssuer, callback) {
349374 var pem = PEMS [ kid ] ;
350375 if ( ! pem ) {
351376 console . log ( 'Invalid Identity token' ) ;
352- callback ( "Unauthorized" ) ;
377+ logger . info ( "Invalid Identity token, returning deny all policy" ) ;
378+ let policy = new AuthPolicy ( '' , awsAccountId , apiOptions ) ;
379+ policy . denyAllMethods ( ) ;
380+ let iamPolicy = policy . build ( ) ;
381+ callback ( null , iamPolicy ) ;
353382 return ;
354383 }
355384
356385 //Verify the signature of the JWT token to ensure it's really coming from your User Pool
357386
358387 jwt . verify ( token , pem , { issuer : tokenIssuer } , function ( err , payload ) {
359388 if ( err ) {
360- callback ( "Unauthorized" ) ;
389+ logger . info ( "Error while trying to verify the Token, returning deny-all policy" ) ;
390+ let policy = new AuthPolicy ( '' , awsAccountId , apiOptions ) ;
391+ policy . denyAllMethods ( ) ;
392+ let iamPolicy = policy . build ( ) ;
393+ callback ( null , iamPolicy ) ;
361394 } else {
362395 //Valid token. Generate the API Gateway policy for the user
363396 //Always generate the policy on value of 'sub' claim and not for
364397 // 'username' because username is reassignable
365398 //sub is UUID for a user which is never reassigned to another user.
366399
367- const apiOptions = { } ;
368- const tmp = event . methodArn . split ( ':' ) ;
369- const apiGatewayArnTmp = tmp [ 5 ] . split ( '/' ) ;
370- const principalId = payload . sub ;
371- const awsAccountId = tmp [ 4 ] ;
372-
373- apiOptions . region = tmp [ 3 ] ;
374- apiOptions . restApiId = apiGatewayArnTmp [ 0 ] ;
375- apiOptions . stage = apiGatewayArnTmp [ 1 ] ;
376-
377400 let admin = null ;
378- let policy = new AuthPolicy ( principalId , awsAccountId , apiOptions ) ;
401+ const pId = payload . sub ;
402+ logger . info ( pId ) ;
403+ let policy = new AuthPolicy ( pId , awsAccountId , apiOptions ) ;
379404 policy . allowAllMethods ( ) ;
380405
381-
382406 //Check the Cognito group entry for Admin.
383407 //Assuming here that the Admin group has always higher
384408 //precedence
409+ const principalId = payload . sub ;
385410
386411 if ( payload [ 'cognito:groups' ] &&
387412 payload [ 'cognito:groups' ] [ 0 ] === 'adminGroup' ) {
0 commit comments