SHARE
TWEET

Untitled

a guest Apr 18th, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const AWS = require("aws-sdk");
  2. const APIGATEWAY = new AWS.APIGateway();
  3.  
  4. exports.handler = async (event, context) => {
  5.   var apiKey = event.authorizationToken;
  6.  
  7.   //open arn for the whole API including all methods
  8.   let arn = event.methodArn.replace(/\/prod\/.*/,'/prod/*/*')
  9.   let keyname = 'unknown';
  10.  
  11.   //get all api key
  12.   var params = { includeValues: true, limit: 500};
  13.   let keys = await APIGATEWAY.getApiKeys(params).promise();
  14.   for(let idx=0;idx<keys.items.length;idx++){
  15.     let item = keys.items[idx];
  16.     if(item.value==apiKey){
  17.       keyname=item.name;
  18.       break;
  19.     }
  20.   }
  21.   //allow access
  22.   let policy ={
  23.     principalId: keyname,
  24.     policyDocument: {
  25.       Version: '2012-10-17',
  26.       Statement:[{
  27.         Effect: 'Allow',
  28.         Action: 'execute-api:Invoke',
  29.         Resource: arn
  30.       }]
  31.     },
  32.     context: {
  33.       apikey: keyname
  34.     }
  35.   }
  36.   console.log(JSON.stringify(policy));
  37.   return policy;
  38. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top