• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

chentsulin/koa-bearer-token: Bearer token parser middleware for koa

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

chentsulin/koa-bearer-token

开源软件地址:

https://github.com/chentsulin/koa-bearer-token

开源编程语言:

TypeScript 89.7%

开源软件介绍:

koa-bearer-token

npm version Build Status Coverage Status

Bearer token parser middleware for koa

Inspired by express-bearer-token

Installation

$ npm install koa-bearer-token

What?

Per RFC6750 this module will attempt to extract a bearer token from a request from these locations:

  • The key access_token in the request body.
  • The key access_token in the request query params.
  • The value from the header Authorization: Bearer <token>.
  • (Optional) Get a token from cookies header with key access_token.

If a token is found, it will be stored on ctx.request.token. If one has been provided in more than one location, this will abort the request immediately by sending code 400 (per [RFC6750]).

const Koa = require('koa');
const bodyParser = require('koa-bodyparser');
const { bearerToken } = require('koa-bearer-token');

const app = new Koa();

app.use(bodyParser());
app.use(bearerToken());

app.use((ctx) => {
  // ctx.request.token
});

app.listen(3000);

For APIs which are not compliant with [RFC6750], the key for the token in each location is customizable, as is the key the token is bound to on the request (default configuration shown):

app.use(
  bearerToken({
    bodyKey: 'access_token',
    queryKey: 'access_token',
    headerKey: 'Bearer',
    reqKey: 'token',
  }),
);

Get token from cookie key (it can be signed or not)

Warning: by NOT passing { signed: true } you are accepting a non signed cookie and an attacker might spoof the cookies. so keep in mind to use signed cookies

app.use(
  bearerToken({
    cookie: {
      signed: true, // if passed true you must pass secret otherwise will throw error
      secret: 'YOUR_APP_SECRET',
      key: 'access_token', // default value
    },
  }),
);

TypeScript

As of version 2.0.1 we've added initial support for TypeScript.

If you're using your custom reqKey, you must do module augmentation on your own:

declare module 'koa' {
  interface Request {
    myToken?: string;
  }
}

app.use(
  bearerToken({
    reqKey: 'myToken',
  }),
);

Compatibility table

koa version koa-bearer-token version
<2 0.x.x
2 >=1.x.x

License

MIT © C. T. Lin




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
cuixiaorui/teach-koa-setup: 实现一个基于 nodejs 的 cli 工具发布时间:2022-07-09
下一篇:
yosssi/koa-stylus: Stylus middleware for Koa发布时间:2022-07-09
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap