# @yingyeothon/aws-lambda-custom-authorizer

> Simple custom authorizer of AWS Lambda for API Gateway

Latest version **0.2.1** (published 2019-11-24) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @yingyeothon/aws-lambda-custom-authorizer
pnpm add @yingyeothon/aws-lambda-custom-authorizer
yarn add @yingyeothon/aws-lambda-custom-authorizer
bun add @yingyeothon/aws-lambda-custom-authorizer
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.2.1 |
| Published | 2019-11-24 |
| First published | 2019-07-31 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 10.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | jaeyoung.choi |
| Maintainers | lacti |
| Keywords | serverless, auth |

## Links

- npm: https://www.npmjs.com/package/@yingyeothon/aws-lambda-custom-authorizer
- Repository: https://github.com/yingyeothon/nodejs-toolkit
- Homepage: https://github.com/yingyeothon/nodejs-lambda-authorizer/tree/master/packages/aws-lambda-custom-authorizer
- Issues: https://github.com/yingyeothon/nodejs-toolkit/issues
- npm.io page: https://npm.io/package/@yingyeothon/aws-lambda-custom-authorizer

## Dependencies (1)

- [@yingyeothon/logger](https://npm.io/package/@yingyeothon/logger.md) ^0.2.0

## Alternatives

- [@pothos/plugin-authz](https://npm.io/package/@pothos/plugin-authz.md) — 12.4K weekly downloads
- [@bounded-sh/client](https://npm.io/package/@bounded-sh/client.md) — 3.2K weekly downloads
- [@luigi-project/plugin-auth-oauth2](https://npm.io/package/@luigi-project/plugin-auth-oauth2.md) — 2.3K weekly downloads
- [@nocobase/plugin-verification](https://npm.io/package/@nocobase/plugin-verification.md) — 2.0K weekly downloads
- [escher-request](https://npm.io/package/escher-request.md) — 545 weekly downloads

## Recent versions

- 0.2.1 (latest) — 2019-11-24
- 0.2.0 — 2019-11-15
- 0.1.0 — 2019-07-31

## README

# AWS Lambda custom authorizer simple builder

A simple function to build AWS Lambda custom authorizer easily.

## Usage

First, write a handler for authorization with an actual handler for HTTP event.

```typescript
import buildAuthorizer from "@yingyeothon/aws-lambda-custom-authorizer";

// Define the custom authorizer.
export const auth = buildAuthorizer({
  authorize: async (auth: Authorization) => {
    // "context" will be passed via `event.requestContext.authorizer` of APIGatewayProxy event.
    switch (auth.type) {
      case "Basic":
        // Authorization: Basic BASE64("id:password")
        // https://tools.ietf.org/html/rfc7617
        return auth.credential.id === "admin" &&
          auth.credential.password === "1234"
          ? { allow: true, context: { admin: true } }
          : { allow: false };
      case "Bearer":
        // Authorization: Bearer b64token
        // https://tools.ietf.org/html/rfc6750
        return auth.token === "verySecretToken"
          ? { allow: true, context: { token: auth.token } }
          : { allow: false };
    }
    // If "allow" is false, it would be cached a policy document of "Deny" access.
    return { allow: false };
  }
  /*
  onError: error => {
    console.error(error);
    throw new Error("Unauthorized");
  },
  logger: new ConsoleLogger("debug"),
  */
});

import { APIGatewayProxyHandler } from "aws-lambda";

// Define an actual HTTP handler.
export const hello: APIGatewayProxyHandler = async event => {
  // This handler is called only if "auth" Lambda generates a policy document to allow a request.
  // That is, if "auth" denies a request, this handler wouldn't be called.
  // And "context" will be passed to "event.requestContext.authorizer".
  return {
    statusCode: 200,
    body: JSON.stringify(event.requestContext.authorizer)
  };
};
```

And register that authorizer into `serverless.yml`.

```yaml
functions:
  # Register an authorizer Lambda.
  auth:
    handler: handler.auth
  hello:
    handler: handler.hello
    events:
      - http:
          method: get
          path: hello
          # And chain with that authorizer Lambda.
          authorizer: auth
```

---
_Source: https://npm.io/package/@yingyeothon/aws-lambda-custom-authorizer · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
