New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

dcm-lambda-utils

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dcm-lambda-utils

Lambda utils by David CM

latest
Source
npmnpm
Version
1.1.4
Version published
Maintainers
1
Created
Source

AWS Lambda utils

Over the space of some time I've built quite a few lambdas for more or less complex applications. I've been extracting some of that into a package and will be using it on my own projects this way so I can share it with others.

Responses

There's two fundamental responses a Lambda will always send, things either went well or not. In the case they go well, you always want a 200 response. If it doesn't go as well then there's some different options.

Returning OK for an operation

const { ResponseUtil } = require('dcm-lambda-utils');

module.exports.doNothing = async (event, context) => {
    return ResponseUtil.OK({message: 'This doesnt do much!'});
}

Returning an Error for an operation

const { ResponseUtil } = require('dcm-lambda-utils');

module.exports.doNothing = async (event, context) => {
    return ResponseUtil.Error(401, 'I know this doesnt do much, but you dont even get that ;)');
}

Logging operations

In order to use the full benefits of structured logging, all that is required is to use the logger that comes bundled with the library.

By getting a logger, we will create an instance of WinstonJS and the returned object will be a standard Winston logger object. For more docs on how to get started with Winston you can use the official guide.

Using it is very simple:

const { BodyParser, getLogger } = require('dcm-lambda-utils');

module.exports.doNothing = async (event, context) => {
    const parsedBody = BodyParser(event);
    const logger = getLogger();

    if (parsedBody === null) {
        logger.error("There was an attempt to parse the body of the function and it returned null");
        return ResponseUtil.Error(400, 'Could not parse the body of the request');
    }
    
    return ResponseUtil.OK({message: 'Parsed!', body: parsedBody});
}

Parsing requests

Parsing a request is pretty simple:

const { ResponseUtil, BodyParser } = require('dcm-lambda-utils');

module.exports.doNothing = async (event, context) => {
    const parsedBody = BodyParser(event);

    if (parsedBody === null) {
        return ResponseUtil.Error(400, 'Could not parse the body of the request');
    }
    
    return ResponseUtil.OK({message: 'Parsed!', body: parsedBody});
}

Decoding JWT Tokens with Cognito

To check a jwt token issued by Cognito, you will need to know which Cognito pool your application is using and on which region is hosted. Once you have that you can check and decode the object very simply:

const { Security, ResponseUtil, BodyParser } = require('dcm-lambda-utils');

module.exports.doNothing = async (event, context) => {

    //First make sure you can get the token
    const parsedBody = BodyParser(event);

    if (parsedBody === null) {
        return ResponseUtil.Error(400, 'Could not parse the body of the request');
    }

    try {
        let validatedUser = await Security.decodeJWTToken(parsedBody.token, process.env.cognitoPoolId, process.env.AWS_REGION);

        if (validatedUser === null) {
            return ResponseUtil.Error(400, 'Could not validate your token');
        }

        const userName = validatedUser.username;

        if (userName === null) 
            userName = validatedUser['cognito:username'];
        
        return validatedUser.username;

    } catch(error) {
        console.log(error.Message)
        return null;
    }
    
    return ResponseUtil.OK({message: 'Parsed!', body: parsedBody});
}

Keywords

aws lambda

FAQs

Package last updated on 21 Aug 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts