Skip to content

thonatos/egg-oauth2-server

 
 

Repository files navigation

egg-oauth2-server

NPM version build status Test coverage David deps Known Vulnerabilities npm download

Chinese Example | 中文样例教程

时间紧张,Fork & Publish 只为个人使用,请支持原版。。

Install

$ npm i egg-oauth2-server --save

Usage

// {app_root}/config/plugin.js
exports.oAuth2Server = {
  enable: true,
  package: 'egg-oauth2-server',
};

// {app_root}/app/router.js
app.all('/user/token', app.oAuth2Server.token());
app.get('/user/authorize', app.oAuth2Server.authorize(), 'user.code');
app.get('/user/authenticate', app.oAuth2Server.authenticate(), 'user.authenticate');

Configuration

// {app_root}/config/config.default.js
module.exports = config => {
  const exports = {};
  exports.oAuth2Server = {
    debug: config.env === 'local',
    grants: [ 'password' ],
  };
  return exports;
};

See test/fixtures/apps/oauth2-server-test/config/config.unittest.js for reference.

Full description see https://www.npmjs.com/package/oauth2-server.

Implementation Example

A simple implementation of password mode OAuth 2.0 server, see test/fixtures/apps/oauth2-server-test/app/extend/oauth.js

// {app_root}/app/extend/oauth.js
'use strict';

module.exports = app => {  
  class Model {
    constructor(ctx) {}
    async getClient(clientId, clientSecret) {}
    async getUser(jobnumber, password) {}
    async getAccessToken(bearerToken) {}
    async saveToken(token, client, user) {}
    async revokeToken(token) {}
    async getAuthorizationCode(authorizationCode) {}
    async saveAuthorizationCode(code, client, user) {}
    async revokeAuthorizationCode(code) {}
  }  
  return Model;
};

Full description see https://www.npmjs.com/package/oauth2-server.

password mode app.oauth.token() lifecycle

getClient --> getUser --> saveToken

password mode app.oauth.authenticate() lifecycle

Only getAccessToken

authorization_code mode app.oauth.authorize() lifecycle

getClient --> getUser --> saveAuthorizationCode

authorization_code mode app.oauth.token() lifecycle

getClient --> getAuthorizationCode --> saveToken --> revokeAuthorizationCode

authorization_code mode app.oauth.authenticate() lifecycle

Only getAccessToken

Questions & Suggestions

Please open an issue here.

License

MIT

About

A koa-oauth-server plugin for egg.js based on node-oauth2-server

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%