npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@menbachi/koa-cognito

v0.0.7

Published

Koa cognito middleware for user authorization

Downloads

28

Readme

Koa-Cognito

Koa-cognito is simple middleware that will allow you to authenticate user with amazon cognito

Will search for JWT in header or cookie with default key = token

Installation

npm i @menbachi/koa-cognito

Usage

You can see example in example/index.ts file.

Simplest validation:

import Koa from 'koa';
import { getAuthorizer, isAuthenticated } from '@menbachi/koa-cognito';

const bootstrap = async () => {
  const app = new Koa();

  const authorizeRequest = await getAuthorizer({
    cognito: [
      {
        region: 'eu-central-1',
        userPoolId: 'eu-central-1_pool',
      },
    ],
  });
  
  // Gets token from header/cookie and parse it to ctx.state.user object
  app.use(authorizeRequest);  
  
  // Check if user is authenticated
  app.use(isAuthenticated);  
}

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

Tests

Feel free to test untested code

--------------------------------|---------|----------|---------|---------|-------------------
File                            | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
--------------------------------|---------|----------|---------|---------|-------------------
All files                       |   81.65 |    75.86 |   87.09 |   78.57 |                  
 cognito                        |     100 |       75 |     100 |     100 |                  
  cognito.ts                    |     100 |       75 |     100 |     100 | 22               
 errors                         |     100 |      100 |     100 |     100 |                  
  koa.cognito.http.exception.ts |     100 |      100 |     100 |     100 |                  
 token                          |   92.85 |       75 |     100 |    90.9 |                  
  get.token.from.cookie.ts      |     100 |      100 |     100 |     100 |                  
  get.token.from.header.ts      |   88.88 |    66.66 |     100 |    87.5 | 13               
 validator                      |   13.63 |        0 |       0 |   10.52 |                  
  token.validtor.ts             |   13.63 |        0 |       0 |   10.52 | 5-29             
 validator/group                |     100 |      100 |     100 |     100 |                  
  group.ts                      |     100 |      100 |     100 |     100 |                  
 validator/scope                |     100 |      100 |     100 |     100 |                  
  scope.ts                      |     100 |      100 |     100 |     100 |                  
 validator/user                 |     100 |      100 |     100 |     100 |                  
  is.authenticated.ts           |     100 |      100 |     100 |     100 |                  
--------------------------------|---------|----------|---------|---------|-------------------

Simple docs

AuthorizerConfig - authorizer config type

KoaCognitoHttpException - Exception thrown when unauthorized/forbidden

getAuthorizer(config: AuthorizerConfig) - gets request authorizer

hasScope(scope: string) - Middleware that check if user has selected scope

hasGroup(group: string) - Middleware that check if user has selected group

isAuthenticated() - Middleware that check if user is logged in

getGroups(ctx: Context) - gets group from request user

getScopes(ctx: Context) - gets scopes from request user

getUser(ctx: Context) - gets user from request

getTokenFromCookie(key: string) - look for jwt in cookie with default key = token

getTokenFromHeader(key: string) - look for jwt in headers with default key = token

Thanks to Eugene Lazutkin

It was highly based on koa-cognito-middleware by Eugener Lazutkin. Made some changes to codebase and added tests.

License

MIT