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

koa-ts-identity

v0.0.31

Published

koa authentication and authorization

Downloads

6

Readme

Koa-ts-identity

Koa-ts-identity是基于Koa-ts-route实现身份认证和鉴权的库,使用TypeScript2编写。

特点 Features

  • 注解
  • 完全异步实现
  • Redis存储

安装 Install

$ npm install koa-ts-route

快速开始 Quick Start

初始化 Initialization

//app.ts

import Koa = require('koa');
import { Router, IRoute } from 'koa-ts-route';


import redis2 = require('redis');
import { Authenticator, Authorization, RedisUserStore } from 'koa-ts-identity';

export var koa = new Koa();
export var router = new Router();

var redisClient = redis2.createClient(config.redis);

redisClient.on('error', (err: any) => {
    ...
});

var userStore = new RedisUserStore(redisClient);

export var redis = userStore.redis;

export var authenticator = new Authenticator(userStore, config.authentication);
export var auth = new Authorization(config.url, null, null);

//other koa middleware....

koa.use(router.run());

koa.use(authenticator.run());
koa.use(auth.run());

//other koa middleware....

//last middleware:business code
koa.use(router.execute());

创建控制器 Create Controller

//order.controller.ts

import { router,auth } from './app';

@auth.authorize()
@router.route()
export class OrderController{
	
    get(){
    	return 'get';
    }
    
    post(){
    	return 'post';
    }
    
    put(){
    	return 'put';
    }
    
    delete(){
    	return 'delete';
    }
    
}

启动 Start

//index.ts

import { koa } from './app';

import 'order.controller';

koa.listen(3000);

GET http://localhost:3000/order -> http 401

POST http://localhost:3000/order -> http 401

....

登录 Login

//user.controller.ts

import { authenticator, router } from './app';

@router.route()
export class UserController{
	
    @router.post()
    async login(){
        var user;
    	...
    	await authenticator.signOut(this.context);
        await authenticator.signIn(this.context, user);
    	return user;
    }

}

部分认证 Part Authenticate

Use allowAnonymous

@auth.allowAnonymous()

Use @auth.authorize() on the Action

@router.route()
export class UserController{
	
    @auth.authorize()
    async get(){
    	...
    }

}

授权 Authorization

Use @auth.authorize()

@auth.authorize('admin')

Implements IRoleValidator

//app.ts

import { Authenticator, Authorization, RedisUserStore, IRoleValidator } from 'koa-ts-identity';

export class RoleValidator implements IRoleValidator {
	
    /**
     * user is login user, from authenticator.signIn(this.context, user)
     * roles from @auth.authorize() parameters
     */
    async validate(user: any, roles: string[]) {
    	let result : boolean;
        ...
        return result;
    }

}

export var auth = new Authorization(config.url, null, new RoleValidator());

用户活动日志 User Action Log

Implements IUserActionLogger

class UserActionLogger {
	
    /**
     * roles from @auth.authorize() parameters
     */
    async log(ctx: Koa.Context, roles: string[]) {
    	...
	}

}

export var auth = new Authorization(config.url, new UserActionLogger(), new RoleValidator());

License

MIT